PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/Plugins/Export/ExportPhparrayTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 215 lines | 165 code | 38 blank | 12 comment | 0 complexity | 072da850d8cca8f66c2919d361423bba 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\Plugins\Export\ExportPhparray;
  5. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  6. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  7. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  8. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  9. use PhpMyAdmin\Tests\AbstractTestCase;
  10. use ReflectionMethod;
  11. use ReflectionProperty;
  12. use function array_shift;
  13. use function ob_get_clean;
  14. use function ob_start;
  15. /**
  16. * @covers \PhpMyAdmin\Plugins\Export\ExportPhparray
  17. * @group medium
  18. */
  19. class ExportPhparrayTest extends AbstractTestCase
  20. {
  21. /** @var ExportPhparray */
  22. protected $object;
  23. /**
  24. * Configures global environment.
  25. */
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. $GLOBALS['server'] = 0;
  30. $GLOBALS['output_kanji_conversion'] = false;
  31. $GLOBALS['output_charset_conversion'] = false;
  32. $GLOBALS['buffer_needed'] = false;
  33. $GLOBALS['asfile'] = true;
  34. $GLOBALS['save_on_server'] = false;
  35. $GLOBALS['db'] = '';
  36. $GLOBALS['table'] = '';
  37. $GLOBALS['lang'] = 'en';
  38. $GLOBALS['text_dir'] = 'ltr';
  39. $GLOBALS['PMA_PHP_SELF'] = '';
  40. $this->object = new ExportPhparray();
  41. }
  42. /**
  43. * tearDown for test cases
  44. */
  45. protected function tearDown(): void
  46. {
  47. parent::tearDown();
  48. unset($this->object);
  49. }
  50. public function testSetProperties(): void
  51. {
  52. $method = new ReflectionMethod(ExportPhparray::class, 'setProperties');
  53. $method->setAccessible(true);
  54. $method->invoke($this->object, null);
  55. $attrProperties = new ReflectionProperty(ExportPhparray::class, 'properties');
  56. $attrProperties->setAccessible(true);
  57. $properties = $attrProperties->getValue($this->object);
  58. $this->assertInstanceOf(ExportPluginProperties::class, $properties);
  59. $this->assertEquals(
  60. 'PHP array',
  61. $properties->getText()
  62. );
  63. $this->assertEquals(
  64. 'php',
  65. $properties->getExtension()
  66. );
  67. $this->assertEquals(
  68. 'text/plain',
  69. $properties->getMimeType()
  70. );
  71. $this->assertEquals(
  72. 'Options',
  73. $properties->getOptionsText()
  74. );
  75. $options = $properties->getOptions();
  76. $this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
  77. $this->assertEquals(
  78. 'Format Specific Options',
  79. $options->getName()
  80. );
  81. $generalOptionsArray = $options->getProperties();
  82. $generalOptions = $generalOptionsArray[0];
  83. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  84. $this->assertEquals(
  85. 'general_opts',
  86. $generalOptions->getName()
  87. );
  88. $generalProperties = $generalOptions->getProperties();
  89. $property = array_shift($generalProperties);
  90. $this->assertInstanceOf(HiddenPropertyItem::class, $property);
  91. }
  92. public function testExportHeader(): void
  93. {
  94. $GLOBALS['crlf'] = ' ';
  95. ob_start();
  96. $this->assertTrue(
  97. $this->object->exportHeader()
  98. );
  99. $result = ob_get_clean();
  100. $this->assertIsString($result);
  101. $this->assertStringContainsString('<?php ', $result);
  102. }
  103. public function testExportFooter(): void
  104. {
  105. $this->assertTrue(
  106. $this->object->exportFooter()
  107. );
  108. }
  109. public function testExportDBHeader(): void
  110. {
  111. $GLOBALS['crlf'] = "\n";
  112. ob_start();
  113. $this->assertTrue(
  114. $this->object->exportDBHeader('db')
  115. );
  116. $result = ob_get_clean();
  117. $this->assertIsString($result);
  118. $this->assertStringContainsString("/**\n * Database `db`\n */", $result);
  119. }
  120. public function testExportDBFooter(): void
  121. {
  122. $this->assertTrue(
  123. $this->object->exportDBFooter('testDB')
  124. );
  125. }
  126. public function testExportDBCreate(): void
  127. {
  128. $this->assertTrue(
  129. $this->object->exportDBCreate('testDB', 'database')
  130. );
  131. }
  132. public function testExportData(): void
  133. {
  134. ob_start();
  135. $this->assertTrue(
  136. $this->object->exportData(
  137. 'test_db',
  138. 'test_table',
  139. "\n",
  140. 'phpmyadmin.net/err',
  141. 'SELECT * FROM `test_db`.`test_table`;'
  142. )
  143. );
  144. $result = ob_get_clean();
  145. $this->assertEquals(
  146. "\n" . '/* `test_db`.`test_table` */' . "\n" .
  147. '$test_table = array(' . "\n" .
  148. ' array(\'id\' => \'1\',\'name\' => \'abcd\',\'datetimefield\' => \'2011-01-20 02:00:02\'),' . "\n" .
  149. ' array(\'id\' => \'2\',\'name\' => \'foo\',\'datetimefield\' => \'2010-01-20 02:00:02\'),' . "\n" .
  150. ' array(\'id\' => \'3\',\'name\' => \'Abcd\',\'datetimefield\' => \'2012-01-20 02:00:02\')' . "\n" .
  151. ');' . "\n",
  152. $result
  153. );
  154. // case 2: test invalid variable name fix
  155. ob_start();
  156. $this->assertTrue(
  157. $this->object->exportData(
  158. 'test_db',
  159. '0`932table',
  160. "\n",
  161. 'phpmyadmin.net/err',
  162. 'SELECT * FROM `test_db`.`test_table`;'
  163. )
  164. );
  165. $result = ob_get_clean();
  166. $this->assertIsString($result);
  167. $this->assertEquals(
  168. "\n" . '/* `test_db`.`0``932table` */' . "\n" .
  169. '$_0_932table = array(' . "\n" .
  170. ' array(\'id\' => \'1\',\'name\' => \'abcd\',\'datetimefield\' => \'2011-01-20 02:00:02\'),' . "\n" .
  171. ' array(\'id\' => \'2\',\'name\' => \'foo\',\'datetimefield\' => \'2010-01-20 02:00:02\'),' . "\n" .
  172. ' array(\'id\' => \'3\',\'name\' => \'Abcd\',\'datetimefield\' => \'2012-01-20 02:00:02\')' . "\n" .
  173. ');' . "\n",
  174. $result
  175. );
  176. }
  177. }