object = new ExportHtmlword();
$GLOBALS['output_kanji_conversion'] = false;
$GLOBALS['output_charset_conversion'] = false;
$GLOBALS['buffer_needed'] = false;
$GLOBALS['asfile'] = true;
$GLOBALS['save_on_server'] = false;
$GLOBALS['db'] = '';
$GLOBALS['table'] = '';
$GLOBALS['lang'] = '';
$GLOBALS['text_dir'] = '';
$GLOBALS['PMA_PHP_SELF'] = '';
$GLOBALS['cfg']['Server']['DisableIS'] = true;
}
/**
* tearDown for test cases
*/
protected function tearDown(): void
{
parent::tearDown();
unset($this->object);
}
public function testSetProperties(): void
{
$method = new ReflectionMethod(ExportHtmlword::class, 'setProperties');
$method->setAccessible(true);
$method->invoke($this->object, null);
$attrProperties = new ReflectionProperty(ExportHtmlword::class, 'properties');
$attrProperties->setAccessible(true);
$properties = $attrProperties->getValue($this->object);
$this->assertInstanceOf(ExportPluginProperties::class, $properties);
$this->assertEquals(
'Microsoft Word 2000',
$properties->getText()
);
$this->assertEquals(
'doc',
$properties->getExtension()
);
$this->assertEquals(
'application/vnd.ms-word',
$properties->getMimeType()
);
$this->assertEquals(
'Options',
$properties->getOptionsText()
);
$this->assertTrue(
$properties->getForceFile()
);
$options = $properties->getOptions();
$this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
$this->assertEquals(
'Format Specific Options',
$options->getName()
);
$generalOptionsArray = $options->getProperties();
$generalOptions = $generalOptionsArray[0];
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
$this->assertEquals(
'dump_what',
$generalOptions->getName()
);
$this->assertEquals(
'Dump table',
$generalOptions->getText()
);
$generalProperties = $generalOptions->getProperties();
$property = array_shift($generalProperties);
$this->assertInstanceOf(RadioPropertyItem::class, $property);
$this->assertEquals(
'structure_or_data',
$property->getName()
);
$this->assertEquals(
[
'structure' => __('structure'),
'data' => __('data'),
'structure_and_data' => __('structure and data'),
],
$property->getValues()
);
$generalOptions = $generalOptionsArray[1];
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
$this->assertEquals(
'dump_what',
$generalOptions->getName()
);
$this->assertEquals(
'Data dump options',
$generalOptions->getText()
);
$this->assertEquals(
'structure',
$generalOptions->getForce()
);
$generalProperties = $generalOptions->getProperties();
$property = array_shift($generalProperties);
$this->assertInstanceOf(TextPropertyItem::class, $property);
$this->assertEquals(
'null',
$property->getName()
);
$this->assertEquals(
'Replace NULL with:',
$property->getText()
);
$property = array_shift($generalProperties);
$this->assertInstanceOf(BoolPropertyItem::class, $property);
$this->assertEquals(
'columns',
$property->getName()
);
$this->assertEquals(
'Put columns names in the first row',
$property->getText()
);
}
public function testExportHeader(): void
{
ob_start();
$this->object->exportHeader();
$result = ob_get_clean();
$expected = '
';
$this->assertEquals($expected, $result);
// case 2
$GLOBALS['charset'] = 'ISO-8859-1';
ob_start();
$this->object->exportHeader();
$result = ob_get_clean();
$expected = '
';
$this->assertEquals($expected, $result);
}
public function testExportFooter(): void
{
ob_start();
$this->assertTrue(
$this->object->exportFooter()
);
$result = ob_get_clean();
$this->assertEquals('', $result);
}
public function testExportDBHeader(): void
{
ob_start();
$this->assertTrue(
$this->object->exportDBHeader('d"b')
);
$result = ob_get_clean();
$this->assertEquals('Database d"b
', $result);
}
public function testExportDBFooter(): void
{
$this->assertTrue(
$this->object->exportDBFooter('testDB')
);
}
public function testExportDBCreate(): void
{
$this->assertTrue(
$this->object->exportDBCreate('testDB', 'database')
);
}
public function testExportData(): void
{
// case 1
$GLOBALS['htmlword_columns'] = true;
$GLOBALS['what'] = 'UT';
$GLOBALS['UT_null'] = 'customNull';
$GLOBALS['output_kanji_conversion'] = false;
$GLOBALS['output_charset_conversion'] = false;
$GLOBALS['buffer_needed'] = false;
$GLOBALS['asfile'] = true;
$GLOBALS['save_on_server'] = false;
ob_start();
$this->assertTrue($this->object->exportData(
'test_db',
'test_table',
"\n",
'localhost',
'SELECT * FROM `test_db`.`test_table`;'
));
$result = ob_get_clean();
$this->assertEquals(
'Dumping data for table test_table
'
. ''
. '| id | '
. 'name | '
. 'datetimefield | '
. '
'
. '| 1 | abcd | 2011-01-20 02:00:02 | '
. '
'
. '| 2 | foo | 2010-01-20 02:00:02 | '
. '
'
. '| 3 | Abcd | 2012-01-20 02:00:02 | '
. '
',
$result
);
}
public function testGetTableDefStandIn(): void
{
$this->object = $this->getMockBuilder(ExportHtmlword::class)
->onlyMethods(['formatOneColumnDefinition'])
->getMock();
// case 1
$keys = [
[
'Non_unique' => 0,
'Column_name' => 'name1',
],
[
'Non_unique' => 1,
'Column_name' => 'name2',
],
];
$dbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$dbi->expects($this->once())
->method('getTableIndexes')
->with('database', 'view')
->will($this->returnValue($keys));
$dbi->expects($this->once())
->method('getColumns')
->with('database', 'view')
->will($this->returnValue([['Field' => 'column']]));
$GLOBALS['dbi'] = $dbi;
$this->object->expects($this->once())
->method('formatOneColumnDefinition')
->with(['Field' => 'column'], ['name1'], 'column')
->will($this->returnValue(1));
$this->assertEquals(
'' .
'| Column | ' .
'Type | ' .
'Null | ' .
'Default |
' .
'1
',
$this->object->getTableDefStandIn('database', 'view', "\n")
);
}
public function testGetTableDef(): void
{
$this->object = $this->getMockBuilder(ExportHtmlword::class)
->onlyMethods(['formatOneColumnDefinition'])
->getMock();
$keys = [
[
'Non_unique' => 0,
'Column_name' => 'name1',
],
[
'Non_unique' => 1,
'Column_name' => 'name2',
],
];
// case 1
$resultStub = $this->createMock(DummyResult::class);
$dbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$dbi->expects($this->exactly(2))
->method('fetchResult')
->willReturnOnConsecutiveCalls(
[],
[
'fieldname' => [
'values' => 'test-',
'transformation' => 'testfoo',
'mimetype' => 'test<',
],
]
);
$dbi->expects($this->once())
->method('getTableIndexes')
->with('database', '')
->will($this->returnValue($keys));
$columns = ['Field' => 'fieldname'];
$dbi->expects($this->once())
->method('getColumns')
->with('database', '')
->will($this->returnValue([$columns]));
$dbi->expects($this->once())
->method('tryQueryAsControlUser')
->will($this->returnValue($resultStub));
$resultStub->expects($this->once())
->method('numRows')
->will($this->returnValue(1));
$resultStub->expects($this->once())
->method('fetchAssoc')
->will($this->returnValue(['comment' => 'testComment']));
$dbi->expects($this->any())->method('escapeString')
->will($this->returnArgument(0));
$GLOBALS['dbi'] = $dbi;
$this->object->relation = new Relation($dbi);
$this->object->expects($this->exactly(3))
->method('formatOneColumnDefinition')
->with($columns, ['name1'])
->will($this->returnValue(1));
$_SESSION['relation'] = [];
$_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
'relwork' => true,
'commwork' => true,
'mimework' => true,
'db' => 'database',
'relation' => 'rel',
'column_info' => 'col',
])->toArray();
$result = $this->object->getTableDef('database', '', true, true, true);
$this->assertEquals(
'' .
'| Column | ' .
'Type | ' .
'Null | ' .
'Default | ' .
'Comments | ' .
'Media type |
' .
'1 | Test< |
',
$result
);
// case 2
$resultStub = $this->createMock(DummyResult::class);
$dbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$dbi->expects($this->exactly(2))
->method('fetchResult')
->willReturnOnConsecutiveCalls(
[
'fieldname' => [
'foreign_table' => 'ftable',
'foreign_field' => 'ffield',
],
],
[
'field' => [
'values' => 'test-',
'transformation' => 'testfoo',
'mimetype' => 'test<',
],
]
);
$dbi->expects($this->once())
->method('getTableIndexes')
->with('database', '')
->will($this->returnValue($keys));
$columns = ['Field' => 'fieldname'];
$dbi->expects($this->once())
->method('getColumns')
->with('database', '')
->will($this->returnValue([$columns]));
$dbi->expects($this->once())
->method('tryQueryAsControlUser')
->will($this->returnValue($resultStub));
$resultStub->expects($this->once())
->method('numRows')
->will($this->returnValue(1));
$resultStub->expects($this->once())
->method('fetchAssoc')
->will($this->returnValue(['comment' => 'testComment']));
$dbi->expects($this->any())->method('escapeString')
->will($this->returnArgument(0));
$GLOBALS['dbi'] = $dbi;
$this->object->relation = new Relation($dbi);
$_SESSION['relation'] = [];
$_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
'relwork' => true,
'commwork' => true,
'mimework' => true,
'db' => 'database',
'relation' => 'rel',
'column_info' => 'col',
])->toArray();
$result = $this->object->getTableDef('database', '', true, true, true);
$this->assertStringContainsString('ftable (ffield) | ', $result);
$this->assertStringContainsString(' | | ', $result);
// case 3
$dbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$dbi->expects($this->once())
->method('getTableIndexes')
->with('database', '')
->will($this->returnValue($keys));
$columns = ['Field' => 'fieldname'];
$dbi->expects($this->once())
->method('getColumns')
->with('database', '')
->will($this->returnValue([$columns]));
$dbi->expects($this->never())
->method('tryQuery');
$dbi->expects($this->any())->method('escapeString')
->will($this->returnArgument(0));
$GLOBALS['dbi'] = $dbi;
$_SESSION['relation'] = [];
$_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
'db' => 'database',
'relation' => 'rel',
'column_info' => 'col',
])->toArray();
$result = $this->object->getTableDef('database', '', false, false, false);
$this->assertEquals(
'' .
'| Column | ' .
'Type | ' .
'Null | ' .
'Default |
1
',
$result
);
}
public function testGetTriggers(): void
{
$dbi = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
->getMock();
$triggers = [
[
'name' => 'tna"me',
'action_timing' => 'ac>t',
'event_manipulation' => 'manip&',
'definition' => 'def',
],
];
$dbi->expects($this->once())
->method('getTriggers')
->with('database', 'table')
->will($this->returnValue($triggers));
$GLOBALS['dbi'] = $dbi;
$method = new ReflectionMethod(ExportHtmlword::class, 'getTriggers');
$method->setAccessible(true);
$result = $method->invoke($this->object, 'database', 'table');
$this->assertStringContainsString(
'tna"me | ' .
'ac>t | ' .
'manip& | ' .
'def | ',
$result
);
}
public function testExportStructure(): void
{
ob_start();
$this->dummyDbi->addSelectDb('test_db');
$this->assertTrue(
$this->object->exportStructure(
'test_db',
'test_table',
"\n",
'localhost',
'create_table',
'test'
)
);
$this->assertAllSelectsConsumed();
$result = ob_get_clean();
$this->assertEquals(
'Table structure for table test_table
'
. ''
. '| Column | Type | '
. 'Null | Default |
'
. '| id | '
. 'int(11) | No | NULL |
'
. '| name | varchar(20) | '
. 'No | NULL |
'
. '| datetimefield | datetime | '
. 'No | NULL |
',
$result
);
ob_start();
$this->assertTrue(
$this->object->exportStructure(
'test_db',
'test_table',
"\n",
'localhost',
'triggers',
'test'
)
);
$result = ob_get_clean();
$this->assertEquals(
'Triggers test_table
'
. '| Name | '
. 'Time | Event | '
. 'Definition |
|---|
'
. '| test_trigger | AFTER | '
. 'INSERT | BEGIN END |
',
$result
);
ob_start();
$this->dummyDbi->addSelectDb('test_db');
$this->assertTrue(
$this->object->exportStructure(
'test_db',
'test_table',
"\n",
'localhost',
'create_view',
'test'
)
);
$this->assertAllSelectsConsumed();
$result = ob_get_clean();
$this->assertEquals(
'Structure for view test_table
'
. ''
. '| Column | Type | '
. 'Null | Default'
. ' |
| id | '
. 'int(11) | No | NULL |
'
. '| name | varchar(20) | '
. 'No | NULL |
'
. '| datetimefield | datetime | '
. 'No | NULL |
',
$result
);
ob_start();
$this->assertTrue(
$this->object->exportStructure(
'test_db',
'test_table',
"\n",
'localhost',
'stand_in',
'test'
)
);
$result = ob_get_clean();
$this->assertEquals(
'Stand-in structure for view test_table
'
. ''
. '| Column | Type | '
. 'Null | Default | '
. '
'
. '| id | int(11) | '
. 'No | NULL |
'
. '| name | varchar(20) | No | '
. 'NULL |
'
. '| datetimefield | datetime | '
. 'No | NULL |
',
$result
);
}
public function testFormatOneColumnDefinition(): void
{
$method = new ReflectionMethod(ExportHtmlword::class, 'formatOneColumnDefinition');
$method->setAccessible(true);
$cols = [
'Null' => 'Yes',
'Field' => 'field',
'Key' => 'PRI',
'Type' => 'set(abc)enum123',
];
$unique_keys = ['field'];
$this->assertEquals(
'| ' .
'field | set(abc) | ' .
'Yes | NULL | ',
$method->invoke($this->object, $cols, $unique_keys)
);
$cols = [
'Null' => 'NO',
'Field' => 'fields',
'Key' => 'COMP',
'Type' => '',
'Default' => 'def',
];
$unique_keys = ['field'];
$this->assertEquals(
'
| fields | ' .
' | No | ' .
'def | ',
$method->invoke($this->object, $cols, $unique_keys)
);
}
}