413 lines
13 KiB
PHP
413 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpMyAdmin\Tests\Plugins\Export;
|
|
|
|
use PhpMyAdmin\Config;
|
|
use PhpMyAdmin\ConfigStorage\Relation;
|
|
use PhpMyAdmin\DatabaseInterface;
|
|
use PhpMyAdmin\Export\Export;
|
|
use PhpMyAdmin\Plugins\Export\ExportXml;
|
|
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
|
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
|
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
|
|
use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
|
|
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
|
|
use PhpMyAdmin\Table\Table;
|
|
use PhpMyAdmin\Tests\AbstractTestCase;
|
|
use PhpMyAdmin\Transformations;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use ReflectionMethod;
|
|
use ReflectionProperty;
|
|
|
|
use function ob_get_clean;
|
|
use function ob_start;
|
|
|
|
#[CoversClass(ExportXml::class)]
|
|
#[Group('medium')]
|
|
class ExportXmlTest extends AbstractTestCase
|
|
{
|
|
protected ExportXml $object;
|
|
|
|
/**
|
|
* Configures global environment.
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$dbi = $this->createDatabaseInterface();
|
|
DatabaseInterface::$instance = $dbi;
|
|
$GLOBALS['server'] = 0;
|
|
$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;
|
|
$GLOBALS['db'] = 'db';
|
|
Config::getInstance()->selectedServer['DisableIS'] = true;
|
|
$this->object = new ExportXml(
|
|
new Relation($dbi),
|
|
new Export($dbi),
|
|
new Transformations(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* tearDown for test cases
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
DatabaseInterface::$instance = null;
|
|
unset($this->object);
|
|
}
|
|
|
|
#[Group('medium')]
|
|
public function testSetProperties(): void
|
|
{
|
|
$method = new ReflectionMethod(ExportXml::class, 'setProperties');
|
|
$method->invoke($this->object, null);
|
|
|
|
$attrProperties = new ReflectionProperty(ExportXml::class, 'properties');
|
|
$properties = $attrProperties->getValue($this->object);
|
|
|
|
$this->assertInstanceOf(ExportPluginProperties::class, $properties);
|
|
|
|
$this->assertEquals(
|
|
'XML',
|
|
$properties->getText(),
|
|
);
|
|
|
|
$this->assertEquals(
|
|
'xml',
|
|
$properties->getExtension(),
|
|
);
|
|
|
|
$this->assertEquals(
|
|
'text/xml',
|
|
$properties->getMimeType(),
|
|
);
|
|
|
|
$options = $properties->getOptions();
|
|
|
|
$this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
|
|
|
|
$this->assertEquals(
|
|
'Format Specific Options',
|
|
$options->getName(),
|
|
);
|
|
|
|
$generalOptionsArray = $options->getProperties();
|
|
|
|
$generalOptions = $generalOptionsArray->current();
|
|
$generalOptionsArray->next();
|
|
|
|
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
|
|
|
|
$this->assertEquals(
|
|
'general_opts',
|
|
$generalOptions->getName(),
|
|
);
|
|
|
|
$generalProperties = $generalOptions->getProperties();
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(HiddenPropertyItem::class, $property);
|
|
|
|
$generalOptions = $generalOptionsArray->current();
|
|
$generalOptionsArray->next();
|
|
|
|
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
|
|
|
|
$this->assertEquals(
|
|
'structure',
|
|
$generalOptions->getName(),
|
|
);
|
|
|
|
$generalProperties = $generalOptions->getProperties();
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
$generalOptions = $generalOptionsArray->current();
|
|
|
|
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
|
|
|
|
$this->assertEquals(
|
|
'data',
|
|
$generalOptions->getName(),
|
|
);
|
|
|
|
$generalProperties = $generalOptions->getProperties();
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
$this->assertInstanceOf(BoolPropertyItem::class, $property);
|
|
}
|
|
|
|
#[Group('medium')]
|
|
public function testExportHeader(): void
|
|
{
|
|
$GLOBALS['xml_export_functions'] = 1;
|
|
$GLOBALS['xml_export_contents'] = 1;
|
|
$GLOBALS['output_charset_conversion'] = 1;
|
|
$GLOBALS['charset'] = 'iso-8859-1';
|
|
$config = Config::getInstance();
|
|
$config->selectedServer['port'] = 80;
|
|
$config->selectedServer['host'] = 'localhost';
|
|
$config->selectedServer['DisableIS'] = false;
|
|
$GLOBALS['xml_export_tables'] = 1;
|
|
$GLOBALS['xml_export_triggers'] = 1;
|
|
$GLOBALS['xml_export_procedures'] = 1;
|
|
$GLOBALS['xml_export_functions'] = 1;
|
|
$GLOBALS['db'] = 'd<"b';
|
|
|
|
$result = [
|
|
0 => ['DEFAULT_COLLATION_NAME' => 'utf8_general_ci', 'DEFAULT_CHARACTER_SET_NAME' => 'utf-8'],
|
|
'table' => [null, '"tbl"'],
|
|
];
|
|
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$triggers = [
|
|
[
|
|
'TRIGGER_SCHEMA' => 'd<"b',
|
|
'TRIGGER_NAME' => 'trname',
|
|
'EVENT_MANIPULATION' => 'INSERT',
|
|
'EVENT_OBJECT_TABLE' => 'table',
|
|
'ACTION_TIMING' => 'AFTER',
|
|
'ACTION_STATEMENT' => 'BEGIN END',
|
|
'EVENT_OBJECT_SCHEMA' => 'd<"b',
|
|
'DEFINER' => 'test_user@localhost',
|
|
],
|
|
];
|
|
$functions = [['Db' => 'd<"b', 'Name' => 'fn', 'Type' => 'FUNCTION']];
|
|
$procedures = [['Db' => 'd<"b', 'Name' => 'pr', 'Type' => 'PROCEDURE']];
|
|
|
|
$dbi->expects($this->exactly(5))
|
|
->method('fetchResult')
|
|
->willReturn($result, $result, $triggers, $functions, $procedures);
|
|
|
|
$dbi->expects($this->exactly(3))
|
|
->method('fetchValue')
|
|
->willReturn(false, 'fndef', 'prdef');
|
|
|
|
$dbi->expects($this->once())
|
|
->method('getTable')
|
|
->willReturn(new Table('table', 'd<"b', $dbi));
|
|
|
|
DatabaseInterface::$instance = $dbi;
|
|
|
|
$GLOBALS['tables'] = [];
|
|
$GLOBALS['table'] = 'table';
|
|
|
|
ob_start();
|
|
$this->assertTrue(
|
|
$this->object->exportHeader(),
|
|
);
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertIsString($result);
|
|
|
|
$this->assertStringContainsString(
|
|
'<pma_xml_export version="1.0" xmlns:pma="' .
|
|
'https://www.phpmyadmin.net/some_doc_url/">',
|
|
$result,
|
|
);
|
|
|
|
$this->assertStringContainsString(
|
|
'<pma:structure_schemas>' . "\n" .
|
|
' <pma:database name="d&lt;&quot;b" collat' .
|
|
'ion="utf8_general_ci" charset="utf-8">' . "\n" .
|
|
' <pma:table name="table">' . "\n" .
|
|
' &quot;tbl&quot;;' . "\n" .
|
|
' </pma:table>' . "\n" .
|
|
' <pma:trigger name="trname">' . "\n" .
|
|
' CREATE TRIGGER `trname` AFTER INSERT ON `table`' . "\n" .
|
|
' FOR EACH ROW BEGIN END' . "\n" .
|
|
' </pma:trigger>' . "\n" .
|
|
' <pma:function name="fn">' . "\n" .
|
|
' fndef' . "\n" .
|
|
' </pma:function>' . "\n" .
|
|
' <pma:procedure name="pr">' . "\n" .
|
|
' prdef' . "\n" .
|
|
' </pma:procedure>' . "\n" .
|
|
' </pma:database>' . "\n" .
|
|
' </pma:structure_schemas>',
|
|
$result,
|
|
);
|
|
|
|
// case 2 with isView as true and false
|
|
|
|
unset($GLOBALS['xml_export_contents']);
|
|
unset($GLOBALS['xml_export_views']);
|
|
unset($GLOBALS['xml_export_tables']);
|
|
unset($GLOBALS['xml_export_functions']);
|
|
unset($GLOBALS['xml_export_procedures']);
|
|
$GLOBALS['output_charset_conversion'] = 0;
|
|
|
|
$dbi = $this->getMockBuilder(DatabaseInterface::class)
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$result1 = [['DEFAULT_COLLATION_NAME' => 'utf8_general_ci', 'DEFAULT_CHARACTER_SET_NAME' => 'utf-8']];
|
|
$result2 = ['t1' => [null, '"tbl"']];
|
|
|
|
$result3 = ['t2' => [null, '"tbl"']];
|
|
|
|
$dbi->expects($this->exactly(3))
|
|
->method('fetchResult')
|
|
->willReturn($result1, $result2, $result3);
|
|
|
|
$dbi->expects($this->exactly(2))
|
|
->method('fetchValue')
|
|
->willReturn('table', false);
|
|
|
|
$dbi->expects($this->any())
|
|
->method('getTable')
|
|
->willReturn(new Table('table', 'd<"b', $dbi));
|
|
|
|
DatabaseInterface::$instance = $dbi;
|
|
|
|
$GLOBALS['tables'] = ['t1', 't2'];
|
|
|
|
ob_start();
|
|
$this->assertTrue(
|
|
$this->object->exportHeader(),
|
|
);
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertIsString($result);
|
|
|
|
$this->assertStringContainsString(
|
|
'<pma:structure_schemas>' . "\n" .
|
|
' <pma:database name="d&lt;&quot;b" collat' .
|
|
'ion="utf8_general_ci" charset="utf-8">' . "\n" .
|
|
' </pma:database>' . "\n" .
|
|
' </pma:structure_schemas>',
|
|
$result,
|
|
);
|
|
}
|
|
|
|
public function testExportFooter(): void
|
|
{
|
|
$this->expectOutputString('</pma_xml_export>');
|
|
$this->assertTrue(
|
|
$this->object->exportFooter(),
|
|
);
|
|
}
|
|
|
|
public function testExportDBHeader(): void
|
|
{
|
|
$GLOBALS['xml_export_contents'] = true;
|
|
|
|
ob_start();
|
|
$this->assertTrue(
|
|
$this->object->exportDBHeader('&db'),
|
|
);
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertIsString($result);
|
|
|
|
$this->assertStringContainsString('<database name="&amp;db">', $result);
|
|
|
|
$GLOBALS['xml_export_contents'] = false;
|
|
|
|
$this->assertTrue(
|
|
$this->object->exportDBHeader('&db'),
|
|
);
|
|
}
|
|
|
|
public function testExportDBFooter(): void
|
|
{
|
|
$GLOBALS['xml_export_contents'] = true;
|
|
|
|
ob_start();
|
|
$this->assertTrue(
|
|
$this->object->exportDBFooter('&db'),
|
|
);
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertIsString($result);
|
|
|
|
$this->assertStringContainsString('</database>', $result);
|
|
|
|
$GLOBALS['xml_export_contents'] = false;
|
|
|
|
$this->assertTrue(
|
|
$this->object->exportDBFooter('&db'),
|
|
);
|
|
}
|
|
|
|
public function testExportDBCreate(): void
|
|
{
|
|
$this->assertTrue(
|
|
$this->object->exportDBCreate('testDB', 'database'),
|
|
);
|
|
}
|
|
|
|
public function testExportData(): void
|
|
{
|
|
$GLOBALS['xml_export_contents'] = true;
|
|
$GLOBALS['asfile'] = true;
|
|
$GLOBALS['output_charset_conversion'] = false;
|
|
|
|
ob_start();
|
|
$this->assertTrue(
|
|
$this->object->exportData(
|
|
'test_db',
|
|
'test_table',
|
|
'localhost',
|
|
'SELECT * FROM `test_db`.`test_table`;',
|
|
),
|
|
);
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertIsString($result);
|
|
$this->assertEquals(
|
|
' <!-- Table test_table -->' . "\n"
|
|
. ' <table name="test_table">' . "\n"
|
|
. ' <column name="id">1</column>' . "\n"
|
|
. ' <column name="name">abcd</column>' . "\n"
|
|
. ' <column name="datetimefield">2011-01-20 02:00:02</column>' . "\n"
|
|
. ' </table>' . "\n"
|
|
. ' <table name="test_table">' . "\n"
|
|
. ' <column name="id">2</column>' . "\n"
|
|
. ' <column name="name">foo</column>' . "\n"
|
|
. ' <column name="datetimefield">2010-01-20 02:00:02</column>' . "\n"
|
|
. ' </table>' . "\n"
|
|
. ' <table name="test_table">' . "\n"
|
|
. ' <column name="id">3</column>' . "\n"
|
|
. ' <column name="name">Abcd</column>' . "\n"
|
|
. ' <column name="datetimefield">2012-01-20 02:00:02</column>' . "\n"
|
|
. ' </table>' . "\n",
|
|
$result,
|
|
);
|
|
}
|
|
}
|