/
var
/
www
/
html
/
restaurants
/
var
/
phpmyadmin
/
tests
/
unit
/
Plugins
/
Export
/
Upload File
HOME
<?php declare(strict_types=1); namespace PhpMyAdmin\Tests\Plugins\Export; use PhpMyAdmin\Column; use PhpMyAdmin\ColumnFull; use PhpMyAdmin\Config; use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\ConfigStorage\RelationParameters; use PhpMyAdmin\Current; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\ConnectionType; use PhpMyAdmin\Export\Export; use PhpMyAdmin\Identifiers\TableName; use PhpMyAdmin\Identifiers\TriggerName; use PhpMyAdmin\Plugins\Export\ExportTexytext; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup; use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem; use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem; use PhpMyAdmin\Properties\Options\Items\TextPropertyItem; use PhpMyAdmin\Properties\Plugins\ExportPluginProperties; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DbiDummy; use PhpMyAdmin\Transformations; use PhpMyAdmin\Triggers\Event; use PhpMyAdmin\Triggers\Timing; use PhpMyAdmin\Triggers\Trigger; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Medium; use ReflectionMethod; use ReflectionProperty; use function ob_get_clean; use function ob_start; #[CoversClass(ExportTexytext::class)] #[Medium] class ExportTexytextTest extends AbstractTestCase { protected DatabaseInterface $dbi; protected DbiDummy $dummyDbi; protected ExportTexytext $object; /** * Configures global environment. */ protected function setUp(): void { parent::setUp(); $this->dummyDbi = $this->createDbiDummy(); $this->dbi = $this->createDatabaseInterface($this->dummyDbi); DatabaseInterface::$instance = $this->dbi; $GLOBALS['output_kanji_conversion'] = false; $GLOBALS['buffer_needed'] = false; $GLOBALS['asfile'] = false; $GLOBALS['save_on_server'] = false; $GLOBALS['plugin_param'] = []; $GLOBALS['plugin_param']['export_type'] = 'table'; $GLOBALS['plugin_param']['single_table'] = false; Current::$database = ''; Current::$table = ''; $GLOBALS['lang'] = 'en'; Config::getInstance()->selectedServer['DisableIS'] = true; $this->object = new ExportTexytext( new Relation($this->dbi), new Export($this->dbi), new Transformations(), ); } /** * tearDown for test cases */ protected function tearDown(): void { parent::tearDown(); DatabaseInterface::$instance = null; unset($this->object); } public function testSetProperties(): void { $method = new ReflectionMethod(ExportTexytext::class, 'setProperties'); $method->invoke($this->object, null); $attrProperties = new ReflectionProperty(ExportTexytext::class, 'properties'); $properties = $attrProperties->getValue($this->object); self::assertInstanceOf(ExportPluginProperties::class, $properties); self::assertSame( 'Texy! text', $properties->getText(), ); self::assertSame( 'txt', $properties->getExtension(), ); self::assertSame( 'text/plain', $properties->getMimeType(), ); $options = $properties->getOptions(); self::assertInstanceOf(OptionsPropertyRootGroup::class, $options); self::assertSame( 'Format Specific Options', $options->getName(), ); $generalOptionsArray = $options->getProperties(); $generalOptions = $generalOptionsArray->current(); $generalOptionsArray->next(); self::assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions); self::assertSame( 'general_opts', $generalOptions->getName(), ); self::assertSame( 'Dump table', $generalOptions->getText(), ); $generalProperties = $generalOptions->getProperties(); $property = $generalProperties->current(); self::assertInstanceOf(RadioPropertyItem::class, $property); $generalOptions = $generalOptionsArray->current(); self::assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions); self::assertSame( 'data', $generalOptions->getName(), ); $generalProperties = $generalOptions->getProperties(); $property = $generalProperties->current(); $generalProperties->next(); self::assertInstanceOf(BoolPropertyItem::class, $property); self::assertSame( 'columns', $property->getName(), ); $property = $generalProperties->current(); self::assertInstanceOf(TextPropertyItem::class, $property); self::assertSame( 'null', $property->getName(), ); } public function testExportHeader(): void { self::assertTrue( $this->object->exportHeader(), ); } public function testExportFooter(): void { self::assertTrue( $this->object->exportFooter(), ); } public function testExportDBHeader(): void { $this->expectOutputString("===Database testDb\n\n"); self::assertTrue( $this->object->exportDBHeader('testDb'), ); } public function testExportDBFooter(): void { self::assertTrue( $this->object->exportDBFooter('testDB'), ); } public function testExportDBCreate(): void { self::assertTrue( $this->object->exportDBCreate('testDB', 'database'), ); } public function testExportData(): void { $GLOBALS['what'] = 'foo'; $GLOBALS['foo_columns'] = '&'; $GLOBALS['foo_null'] = '>'; ob_start(); self::assertTrue( $this->object->exportData( 'test_db', 'test_table', 'localhost', 'SELECT * FROM `test_db`.`test_table`;', ), ); $result = ob_get_clean(); self::assertIsString($result); self::assertSame( '== Dumping data for table test_table' . "\n\n" . '|------' . "\n" . '|id|name|datetimefield' . "\n" . '|------' . "\n" . '|1|abcd|2011-01-20 02:00:02' . "\n" . '|2|foo|2010-01-20 02:00:02' . "\n" . '|3|Abcd|2012-01-20 02:00:02' . "\n", $result, ); } public function testGetTableDefStandIn(): void { $this->dummyDbi->addSelectDb('test_db'); $result = $this->object->getTableDefStandIn('test_db', 'test_table'); $this->dummyDbi->assertAllSelectsConsumed(); self::assertSame( '|------' . "\n" . '|Column|Type|Null|Default' . "\n" . '|------' . "\n" . '|//**id**//|int(11)|No|NULL' . "\n" . '|name|varchar(20)|No|NULL' . "\n" . '|datetimefield|datetime|No|NULL' . "\n", $result, ); } public function testGetTableDef(): void { $this->object = $this->getMockBuilder(ExportTexytext::class) ->onlyMethods(['formatOneColumnDefinition']) ->setConstructorArgs([new Relation($this->dbi), new Export($this->dbi), new Transformations()]) ->getMock(); // case 1 $dbi = $this->getMockBuilder(DatabaseInterface::class) ->disableOriginalConstructor() ->getMock(); $keys = [['Non_unique' => 0, 'Column_name' => 'cname'], ['Non_unique' => 1, 'Column_name' => 'cname2']]; $dbi->expects(self::once()) ->method('getTableIndexes') ->with('db', 'table') ->willReturn($keys); $dbi->expects(self::exactly(2)) ->method('fetchResult') ->willReturn( ['fname' => ['foreign_table' => '<ftable', 'foreign_field' => 'ffield>']], ['fname' => ['values' => 'test-', 'transformation' => 'testfoo', 'mimetype' => 'test<']], ); $dbi->expects(self::once()) ->method('fetchValue') ->willReturn('SELECT a FROM b'); $column = new Column('fname', '', false, '', null, ''); $columnFull = new ColumnFull('fname', '', null, false, '', null, '', '', 'comm'); $dbi->expects(self::exactly(2)) ->method('getColumns') ->willReturnMap([ ['db', 'table', false, ConnectionType::User, [$column]], ['db', 'table', true, ConnectionType::User, [$columnFull]], ]); DatabaseInterface::$instance = $dbi; $this->object->relation = new Relation($dbi); $this->object->expects(self::exactly(1)) ->method('formatOneColumnDefinition') ->with($column, ['cname']) ->willReturn('1'); $relationParameters = RelationParameters::fromArray([ 'relwork' => true, 'commwork' => true, 'mimework' => true, 'db' => 'database', 'relation' => 'rel', 'column_info' => 'col', ]); (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters); $result = $this->object->getTableDef('db', 'table', true, true, true); self::assertStringContainsString('1|<ftable (ffield>)|comm|Test<', $result); } public function testGetTriggers(): void { $triggers = [ new Trigger( TriggerName::from('tna"me'), Timing::Before, Event::Delete, TableName::from('ta<ble'), 'def', 'test_user@localhost', ), ]; $result = $this->object->getTriggers($triggers); self::assertStringContainsString('|tna"me|BEFORE|DELETE|def', $result); self::assertStringContainsString('|Name|Time|Event|Definition', $result); } public function testExportStructure(): void { // case 1 ob_start(); $this->dummyDbi->addSelectDb('test_db'); self::assertTrue( $this->object->exportStructure( 'test_db', 'test_table', 'create_table', 'test', ), ); $this->dummyDbi->assertAllSelectsConsumed(); $result = ob_get_clean(); self::assertIsString($result); self::assertSame( '== Table structure for table test_table' . "\n\n" . '|------' . "\n" . '|Column|Type|Null|Default' . "\n" . '|------' . "\n" . '|//**id**//|int(11)|No|NULL' . "\n" . '|name|varchar(20)|No|NULL' . "\n" . '|datetimefield|datetime|No|NULL' . "\n", $result, ); // case 2 ob_start(); self::assertTrue( $this->object->exportStructure( 'test_db', 'test_table', 'triggers', 'test', ), ); $result = ob_get_clean(); self::assertSame( '== Triggers test_table' . "\n\n" . '|------' . "\n" . '|Name|Time|Event|Definition' . "\n" . '|------' . "\n" . '|test_trigger|AFTER|INSERT|BEGIN END' . "\n", $result, ); // case 3 ob_start(); $this->dummyDbi->addSelectDb('test_db'); self::assertTrue( $this->object->exportStructure( 'test_db', 'test_table', 'create_view', 'test', ), ); $this->dummyDbi->assertAllSelectsConsumed(); $result = ob_get_clean(); self::assertSame( '== Structure for view test_table' . "\n\n" . '|------' . "\n" . '|Column|Type|Null|Default' . "\n" . '|------' . "\n" . '|//**id**//|int(11)|No|NULL' . "\n" . '|name|varchar(20)|No|NULL' . "\n" . '|datetimefield|datetime|No|NULL' . "\n", $result, ); // case 4 ob_start(); $this->dummyDbi->addSelectDb('test_db'); self::assertTrue( $this->object->exportStructure( 'test_db', 'test_table', 'stand_in', 'test', ), ); $this->dummyDbi->assertAllSelectsConsumed(); $result = ob_get_clean(); self::assertSame( '== Stand-in structure for view test_table' . "\n\n" . '|------' . "\n" . '|Column|Type|Null|Default' . "\n" . '|------' . "\n" . '|//**id**//|int(11)|No|NULL' . "\n" . '|name|varchar(20)|No|NULL' . "\n" . '|datetimefield|datetime|No|NULL' . "\n", $result, ); } public function testFormatOneColumnDefinition(): void { $cols = new Column('field', 'set(abc)enum123', true, 'PRI', null, ''); $uniqueKeys = ['field']; self::assertSame( '|//**field**//|set(abc)|Yes|NULL', $this->object->formatOneColumnDefinition($cols, $uniqueKeys), ); $cols = new Column('fields', '', false, 'COMP', 'def', ''); $uniqueKeys = ['field']; self::assertSame( '|fields|&nbsp;|No|def', $this->object->formatOneColumnDefinition($cols, $uniqueKeys), ); } }