PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/Plugins/Export/ExportXmlTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 436 lines | 331 code | 87 blank | 18 comment | 0 complexity | 5348479e60df138ceef76615ea8053c8 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests\Plugins\Export;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Plugins\Export\ExportXml;
  6. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  7. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  8. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  9. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  10. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  11. use PhpMyAdmin\Table;
  12. use PhpMyAdmin\Tests\AbstractTestCase;
  13. use ReflectionMethod;
  14. use ReflectionProperty;
  15. use function array_shift;
  16. use function ob_get_clean;
  17. use function ob_start;
  18. /**
  19. * @covers \PhpMyAdmin\Plugins\Export\ExportXml
  20. * @group medium
  21. */
  22. class ExportXmlTest extends AbstractTestCase
  23. {
  24. /** @var ExportXml */
  25. protected $object;
  26. /**
  27. * Configures global environment.
  28. */
  29. protected function setUp(): void
  30. {
  31. parent::setUp();
  32. $GLOBALS['server'] = 0;
  33. $GLOBALS['output_kanji_conversion'] = false;
  34. $GLOBALS['buffer_needed'] = false;
  35. $GLOBALS['asfile'] = false;
  36. $GLOBALS['save_on_server'] = false;
  37. $GLOBALS['plugin_param'] = [];
  38. $GLOBALS['plugin_param']['export_type'] = 'table';
  39. $GLOBALS['plugin_param']['single_table'] = false;
  40. $GLOBALS['cfgRelation']['relation'] = true;
  41. $GLOBALS['db'] = 'db';
  42. $GLOBALS['cfg']['Server']['DisableIS'] = true;
  43. $this->object = new ExportXml();
  44. }
  45. /**
  46. * tearDown for test cases
  47. */
  48. protected function tearDown(): void
  49. {
  50. parent::tearDown();
  51. unset($this->object);
  52. }
  53. /**
  54. * @group medium
  55. */
  56. public function testSetProperties(): void
  57. {
  58. $method = new ReflectionMethod(ExportXml::class, 'setProperties');
  59. $method->setAccessible(true);
  60. $method->invoke($this->object, null);
  61. $attrProperties = new ReflectionProperty(ExportXml::class, 'properties');
  62. $attrProperties->setAccessible(true);
  63. $properties = $attrProperties->getValue($this->object);
  64. $this->assertInstanceOf(ExportPluginProperties::class, $properties);
  65. $this->assertEquals(
  66. 'XML',
  67. $properties->getText()
  68. );
  69. $this->assertEquals(
  70. 'xml',
  71. $properties->getExtension()
  72. );
  73. $this->assertEquals(
  74. 'text/xml',
  75. $properties->getMimeType()
  76. );
  77. $options = $properties->getOptions();
  78. $this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
  79. $this->assertEquals(
  80. 'Format Specific Options',
  81. $options->getName()
  82. );
  83. $generalOptionsArray = $options->getProperties();
  84. $generalOptions = array_shift($generalOptionsArray);
  85. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  86. $this->assertEquals(
  87. 'general_opts',
  88. $generalOptions->getName()
  89. );
  90. $generalProperties = $generalOptions->getProperties();
  91. $property = array_shift($generalProperties);
  92. $this->assertInstanceOf(HiddenPropertyItem::class, $property);
  93. $generalOptions = array_shift($generalOptionsArray);
  94. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  95. $this->assertEquals(
  96. 'structure',
  97. $generalOptions->getName()
  98. );
  99. $generalProperties = $generalOptions->getProperties();
  100. $property = array_shift($generalProperties);
  101. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  102. $property = array_shift($generalProperties);
  103. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  104. $property = array_shift($generalProperties);
  105. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  106. $property = array_shift($generalProperties);
  107. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  108. $property = array_shift($generalProperties);
  109. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  110. $generalOptions = array_shift($generalOptionsArray);
  111. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  112. $this->assertEquals(
  113. 'data',
  114. $generalOptions->getName()
  115. );
  116. $generalProperties = $generalOptions->getProperties();
  117. $property = array_shift($generalProperties);
  118. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  119. }
  120. /**
  121. * @group medium
  122. */
  123. public function testExportHeader(): void
  124. {
  125. $GLOBALS['xml_export_functions'] = 1;
  126. $GLOBALS['xml_export_contents'] = 1;
  127. $GLOBALS['output_charset_conversion'] = 1;
  128. $GLOBALS['charset'] = 'iso-8859-1';
  129. $GLOBALS['cfg']['Server']['port'] = 80;
  130. $GLOBALS['cfg']['Server']['host'] = 'localhost';
  131. $GLOBALS['cfg']['Server']['DisableIS'] = false;
  132. $GLOBALS['xml_export_tables'] = 1;
  133. $GLOBALS['xml_export_triggers'] = 1;
  134. $GLOBALS['xml_export_procedures'] = 1;
  135. $GLOBALS['xml_export_functions'] = 1;
  136. $GLOBALS['crlf'] = "\n";
  137. $GLOBALS['db'] = 'd<"b';
  138. $result = [
  139. 0 => [
  140. 'DEFAULT_COLLATION_NAME' => 'utf8_general_ci',
  141. 'DEFAULT_CHARACTER_SET_NAME' => 'utf-8',
  142. ],
  143. 'table' => [
  144. null,
  145. '"tbl"',
  146. ],
  147. ];
  148. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $dbi->expects($this->exactly(3))
  152. ->method('fetchResult')
  153. ->willReturnOnConsecutiveCalls($result, $result, false);
  154. $dbi->expects($this->once())
  155. ->method('getTriggers')
  156. ->with('d<"b', 'table')
  157. ->will(
  158. $this->returnValue(
  159. [
  160. [
  161. 'create' => 'crt',
  162. 'name' => 'trname',
  163. ],
  164. ]
  165. )
  166. );
  167. $dbi->expects($this->exactly(2))
  168. ->method('getProceduresOrFunctions')
  169. ->willReturnOnConsecutiveCalls(
  170. ['fn'],
  171. ['pr']
  172. );
  173. $dbi->expects($this->exactly(2))
  174. ->method('getDefinition')
  175. ->willReturnOnConsecutiveCalls('fndef', 'prdef');
  176. $dbi->expects($this->once())
  177. ->method('getTable')
  178. ->will($this->returnValue(new Table('table', 'd<"b', $dbi)));
  179. $dbi->expects($this->any())->method('escapeString')
  180. ->will($this->returnArgument(0));
  181. $GLOBALS['dbi'] = $dbi;
  182. $GLOBALS['tables'] = [];
  183. $GLOBALS['table'] = 'table';
  184. ob_start();
  185. $this->assertTrue(
  186. $this->object->exportHeader()
  187. );
  188. $result = ob_get_clean();
  189. $this->assertIsString($result);
  190. $this->assertStringContainsString(
  191. '&lt;pma_xml_export version=&quot;1.0&quot; xmlns:pma=&quot;' .
  192. 'https://www.phpmyadmin.net/some_doc_url/&quot;&gt;',
  193. $result
  194. );
  195. $this->assertStringContainsString(
  196. '&lt;pma:structure_schemas&gt;' . "\n" .
  197. ' &lt;pma:database name=&quot;d&amp;lt;&amp;quot;b&quot; collat' .
  198. 'ion=&quot;utf8_general_ci&quot; charset=&quot;utf-8&quot;&gt;' . "\n" .
  199. ' &lt;pma:table name=&quot;table&quot;&gt;' . "\n" .
  200. ' &amp;quot;tbl&amp;quot;;' . "\n" .
  201. ' &lt;/pma:table&gt;' . "\n" .
  202. ' &lt;pma:trigger name=&quot;trname&quot;&gt;' . "\n" .
  203. ' ' . "\n" .
  204. ' &lt;/pma:trigger&gt;' . "\n" .
  205. ' &lt;pma:function name=&quot;fn&quot;&gt;' . "\n" .
  206. ' fndef' . "\n" .
  207. ' &lt;/pma:function&gt;' . "\n" .
  208. ' &lt;pma:procedure name=&quot;pr&quot;&gt;' . "\n" .
  209. ' prdef' . "\n" .
  210. ' &lt;/pma:procedure&gt;' . "\n" .
  211. ' &lt;/pma:database&gt;' . "\n" .
  212. ' &lt;/pma:structure_schemas&gt;',
  213. $result
  214. );
  215. // case 2 with isView as true and false
  216. unset($GLOBALS['xml_export_contents']);
  217. unset($GLOBALS['xml_export_views']);
  218. unset($GLOBALS['xml_export_tables']);
  219. unset($GLOBALS['xml_export_functions']);
  220. unset($GLOBALS['xml_export_procedures']);
  221. $GLOBALS['output_charset_conversion'] = 0;
  222. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $result_1 = [
  226. [
  227. 'DEFAULT_COLLATION_NAME' => 'utf8_general_ci',
  228. 'DEFAULT_CHARACTER_SET_NAME' => 'utf-8',
  229. ],
  230. ];
  231. $result_2 = [
  232. 't1' => [
  233. null,
  234. '"tbl"',
  235. ],
  236. ];
  237. $result_3 = [
  238. 't2' => [
  239. null,
  240. '"tbl"',
  241. ],
  242. ];
  243. $dbi->expects($this->exactly(5))
  244. ->method('fetchResult')
  245. ->willReturnOnConsecutiveCalls($result_1, $result_2, true, $result_3, false);
  246. $dbi->expects($this->any())
  247. ->method('getTable')
  248. ->will($this->returnValue(new Table('table', 'd<"b', $dbi)));
  249. $GLOBALS['dbi'] = $dbi;
  250. $GLOBALS['tables'] = [
  251. 't1',
  252. 't2',
  253. ];
  254. ob_start();
  255. $this->assertTrue(
  256. $this->object->exportHeader()
  257. );
  258. $result = ob_get_clean();
  259. $this->assertIsString($result);
  260. $this->assertStringContainsString(
  261. '&lt;pma:structure_schemas&gt;' . "\n" .
  262. ' &lt;pma:database name=&quot;d&amp;lt;&amp;quot;b&quot; collat' .
  263. 'ion=&quot;utf8_general_ci&quot; charset=&quot;utf-8&quot;&gt;' . "\n" .
  264. ' &lt;/pma:database&gt;' . "\n" .
  265. ' &lt;/pma:structure_schemas&gt;',
  266. $result
  267. );
  268. }
  269. public function testExportFooter(): void
  270. {
  271. $this->expectOutputString('&lt;/pma_xml_export&gt;');
  272. $this->assertTrue(
  273. $this->object->exportFooter()
  274. );
  275. }
  276. public function testExportDBHeader(): void
  277. {
  278. $GLOBALS['xml_export_contents'] = true;
  279. ob_start();
  280. $this->assertTrue(
  281. $this->object->exportDBHeader('&db')
  282. );
  283. $result = ob_get_clean();
  284. $this->assertIsString($result);
  285. $this->assertStringContainsString('&lt;database name=&quot;&amp;amp;db&quot;&gt;', $result);
  286. $GLOBALS['xml_export_contents'] = false;
  287. $this->assertTrue(
  288. $this->object->exportDBHeader('&db')
  289. );
  290. }
  291. public function testExportDBFooter(): void
  292. {
  293. $GLOBALS['xml_export_contents'] = true;
  294. ob_start();
  295. $this->assertTrue(
  296. $this->object->exportDBFooter('&db')
  297. );
  298. $result = ob_get_clean();
  299. $this->assertIsString($result);
  300. $this->assertStringContainsString('&lt;/database&gt;', $result);
  301. $GLOBALS['xml_export_contents'] = false;
  302. $this->assertTrue(
  303. $this->object->exportDBFooter('&db')
  304. );
  305. }
  306. public function testExportDBCreate(): void
  307. {
  308. $this->assertTrue(
  309. $this->object->exportDBCreate('testDB', 'database')
  310. );
  311. }
  312. public function testExportData(): void
  313. {
  314. $GLOBALS['xml_export_contents'] = true;
  315. $GLOBALS['asfile'] = true;
  316. $GLOBALS['output_charset_conversion'] = false;
  317. ob_start();
  318. $this->assertTrue(
  319. $this->object->exportData(
  320. 'test_db',
  321. 'test_table',
  322. "\n",
  323. 'localhost',
  324. 'SELECT * FROM `test_db`.`test_table`;'
  325. )
  326. );
  327. $result = ob_get_clean();
  328. $this->assertIsString($result);
  329. $this->assertEquals(
  330. ' <!-- Table test_table -->' . "\n"
  331. . ' <table name="test_table">' . "\n"
  332. . ' <column name="id">1</column>' . "\n"
  333. . ' <column name="name">abcd</column>' . "\n"
  334. . ' <column name="datetimefield">2011-01-20 02:00:02</column>' . "\n"
  335. . ' </table>' . "\n"
  336. . ' <table name="test_table">' . "\n"
  337. . ' <column name="id">2</column>' . "\n"
  338. . ' <column name="name">foo</column>' . "\n"
  339. . ' <column name="datetimefield">2010-01-20 02:00:02</column>' . "\n"
  340. . ' </table>' . "\n"
  341. . ' <table name="test_table">' . "\n"
  342. . ' <column name="id">3</column>' . "\n"
  343. . ' <column name="name">Abcd</column>' . "\n"
  344. . ' <column name="datetimefield">2012-01-20 02:00:02</column>' . "\n"
  345. . ' </table>' . "\n",
  346. $result
  347. );
  348. }
  349. }