PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/classes/Plugins/Export/ExportTexytextTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 495 lines | 400 code | 79 blank | 16 comment | 0 complexity | aed48f25fa5bbdab7f25a5e981dbed21 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\ExportTexytext;
  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\RadioPropertyItem;
  10. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  11. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  12. use PhpMyAdmin\Relation;
  13. use PhpMyAdmin\Tests\AbstractTestCase;
  14. use PhpMyAdmin\Version;
  15. use ReflectionMethod;
  16. use ReflectionProperty;
  17. use function array_shift;
  18. use function ob_get_clean;
  19. use function ob_start;
  20. /**
  21. * @covers \PhpMyAdmin\Plugins\Export\ExportTexytext
  22. * @group medium
  23. */
  24. class ExportTexytextTest extends AbstractTestCase
  25. {
  26. /** @var ExportTexytext */
  27. protected $object;
  28. /**
  29. * Configures global environment.
  30. */
  31. protected function setUp(): void
  32. {
  33. parent::setUp();
  34. $GLOBALS['server'] = 0;
  35. $GLOBALS['output_kanji_conversion'] = false;
  36. $GLOBALS['buffer_needed'] = false;
  37. $GLOBALS['asfile'] = false;
  38. $GLOBALS['save_on_server'] = false;
  39. $GLOBALS['plugin_param'] = [];
  40. $GLOBALS['plugin_param']['export_type'] = 'table';
  41. $GLOBALS['plugin_param']['single_table'] = false;
  42. $GLOBALS['cfgRelation']['relation'] = true;
  43. $GLOBALS['db'] = '';
  44. $GLOBALS['table'] = '';
  45. $GLOBALS['lang'] = 'en';
  46. $GLOBALS['text_dir'] = 'ltr';
  47. $GLOBALS['PMA_PHP_SELF'] = '';
  48. $GLOBALS['cfg']['Server']['DisableIS'] = true;
  49. $this->object = new ExportTexytext();
  50. }
  51. /**
  52. * tearDown for test cases
  53. */
  54. protected function tearDown(): void
  55. {
  56. parent::tearDown();
  57. unset($this->object);
  58. }
  59. public function testSetProperties(): void
  60. {
  61. $method = new ReflectionMethod(ExportTexytext::class, 'setProperties');
  62. $method->setAccessible(true);
  63. $method->invoke($this->object, null);
  64. $attrProperties = new ReflectionProperty(ExportTexytext::class, 'properties');
  65. $attrProperties->setAccessible(true);
  66. $properties = $attrProperties->getValue($this->object);
  67. $this->assertInstanceOf(ExportPluginProperties::class, $properties);
  68. $this->assertEquals(
  69. 'Texy! text',
  70. $properties->getText()
  71. );
  72. $this->assertEquals(
  73. 'txt',
  74. $properties->getExtension()
  75. );
  76. $this->assertEquals(
  77. 'text/plain',
  78. $properties->getMimeType()
  79. );
  80. $options = $properties->getOptions();
  81. $this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
  82. $this->assertEquals(
  83. 'Format Specific Options',
  84. $options->getName()
  85. );
  86. $generalOptionsArray = $options->getProperties();
  87. $generalOptions = array_shift($generalOptionsArray);
  88. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  89. $this->assertEquals(
  90. 'general_opts',
  91. $generalOptions->getName()
  92. );
  93. $this->assertEquals(
  94. 'Dump table',
  95. $generalOptions->getText()
  96. );
  97. $generalProperties = $generalOptions->getProperties();
  98. $property = array_shift($generalProperties);
  99. $this->assertInstanceOf(RadioPropertyItem::class, $property);
  100. $generalOptions = array_shift($generalOptionsArray);
  101. $this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
  102. $this->assertEquals(
  103. 'data',
  104. $generalOptions->getName()
  105. );
  106. $generalProperties = $generalOptions->getProperties();
  107. $property = array_shift($generalProperties);
  108. $this->assertInstanceOf(BoolPropertyItem::class, $property);
  109. $this->assertEquals(
  110. 'columns',
  111. $property->getName()
  112. );
  113. $property = array_shift($generalProperties);
  114. $this->assertInstanceOf(TextPropertyItem::class, $property);
  115. $this->assertEquals(
  116. 'null',
  117. $property->getName()
  118. );
  119. }
  120. public function testExportHeader(): void
  121. {
  122. $this->assertTrue(
  123. $this->object->exportHeader()
  124. );
  125. }
  126. public function testExportFooter(): void
  127. {
  128. $this->assertTrue(
  129. $this->object->exportFooter()
  130. );
  131. }
  132. public function testExportDBHeader(): void
  133. {
  134. $this->expectOutputString("===Database testDb\n\n");
  135. $this->assertTrue(
  136. $this->object->exportDBHeader('testDb')
  137. );
  138. }
  139. public function testExportDBFooter(): void
  140. {
  141. $this->assertTrue(
  142. $this->object->exportDBFooter('testDB')
  143. );
  144. }
  145. public function testExportDBCreate(): void
  146. {
  147. $this->assertTrue(
  148. $this->object->exportDBCreate('testDB', 'database')
  149. );
  150. }
  151. public function testExportData(): void
  152. {
  153. $GLOBALS['what'] = 'foo';
  154. $GLOBALS['foo_columns'] = '&';
  155. $GLOBALS['foo_null'] = '>';
  156. ob_start();
  157. $this->assertTrue(
  158. $this->object->exportData(
  159. 'test_db',
  160. 'test_table',
  161. "\n",
  162. 'localhost',
  163. 'SELECT * FROM `test_db`.`test_table`;'
  164. )
  165. );
  166. $result = ob_get_clean();
  167. $this->assertIsString($result);
  168. $this->assertEquals(
  169. '== Dumping data for table test_table' . "\n\n"
  170. . '|------' . "\n"
  171. . '|id|name|datetimefield' . "\n"
  172. . '|------' . "\n"
  173. . '|1|abcd|2011-01-20 02:00:02' . "\n"
  174. . '|2|foo|2010-01-20 02:00:02' . "\n"
  175. . '|3|Abcd|2012-01-20 02:00:02' . "\n",
  176. $result
  177. );
  178. }
  179. public function testGetTableDefStandIn(): void
  180. {
  181. $this->dummyDbi->addSelectDb('test_db');
  182. $result = $this->object->getTableDefStandIn('test_db', 'test_table', "\n");
  183. $this->assertAllSelectsConsumed();
  184. $this->assertEquals(
  185. '|------' . "\n"
  186. . '|Column|Type|Null|Default' . "\n"
  187. . '|------' . "\n"
  188. . '|//**id**//|int(11)|No|NULL' . "\n"
  189. . '|name|varchar(20)|No|NULL' . "\n"
  190. . '|datetimefield|datetime|No|NULL' . "\n",
  191. $result
  192. );
  193. }
  194. public function testGetTableDef(): void
  195. {
  196. $this->object = $this->getMockBuilder(ExportTexytext::class)
  197. ->onlyMethods(['formatOneColumnDefinition'])
  198. ->getMock();
  199. // case 1
  200. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $keys = [
  204. [
  205. 'Non_unique' => 0,
  206. 'Column_name' => 'cname',
  207. ],
  208. [
  209. 'Non_unique' => 1,
  210. 'Column_name' => 'cname2',
  211. ],
  212. ];
  213. $dbi->expects($this->once())
  214. ->method('getTableIndexes')
  215. ->with('db', 'table')
  216. ->will($this->returnValue($keys));
  217. $dbi->expects($this->exactly(2))
  218. ->method('fetchResult')
  219. ->willReturnOnConsecutiveCalls(
  220. [
  221. 'fname' => [
  222. 'foreign_table' => '<ftable',
  223. 'foreign_field' => 'ffield>',
  224. ],
  225. ],
  226. [
  227. 'fname' => [
  228. 'values' => 'test-',
  229. 'transformation' => 'testfoo',
  230. 'mimetype' => 'test<',
  231. ],
  232. ]
  233. );
  234. $dbi->expects($this->once())
  235. ->method('fetchValue')
  236. ->will(
  237. $this->returnValue(
  238. 'SELECT a FROM b'
  239. )
  240. );
  241. $columns = [
  242. 'Field' => 'fname',
  243. 'Comment' => 'comm',
  244. ];
  245. $dbi->expects($this->exactly(2))
  246. ->method('getColumns')
  247. ->with('db', 'table')
  248. ->will($this->returnValue([$columns]));
  249. $GLOBALS['dbi'] = $dbi;
  250. $this->object->relation = new Relation($dbi);
  251. $this->object->expects($this->exactly(1))
  252. ->method('formatOneColumnDefinition')
  253. ->with(['Field' => 'fname', 'Comment' => 'comm'], ['cname'])
  254. ->will($this->returnValue(1));
  255. $GLOBALS['cfgRelation']['relation'] = true;
  256. $_SESSION['relation'][0] = [
  257. 'version' => Version::VERSION,
  258. 'relwork' => true,
  259. 'commwork' => true,
  260. 'mimework' => true,
  261. 'db' => 'db',
  262. 'relation' => 'rel',
  263. 'column_info' => 'col',
  264. ];
  265. $result = $this->object->getTableDef('db', 'table', "\n", 'example.com', true, true, true);
  266. $this->assertStringContainsString('1|&lt;ftable (ffield&gt;)|comm|Test&lt;', $result);
  267. }
  268. public function testGetTriggers(): void
  269. {
  270. $dbi = $this->getMockBuilder(DatabaseInterface::class)
  271. ->disableOriginalConstructor()
  272. ->getMock();
  273. $triggers = [
  274. [
  275. 'name' => 'tna"me',
  276. 'action_timing' => 'ac>t',
  277. 'event_manipulation' => 'manip&',
  278. 'definition' => 'def',
  279. ],
  280. ];
  281. $dbi->expects($this->once())
  282. ->method('getTriggers')
  283. ->with('database', 'ta<ble')
  284. ->will($this->returnValue($triggers));
  285. $GLOBALS['dbi'] = $dbi;
  286. $result = $this->object->getTriggers('database', 'ta<ble');
  287. $this->assertStringContainsString('|tna"me|ac>t|manip&|def', $result);
  288. $this->assertStringContainsString('|Name|Time|Event|Definition', $result);
  289. }
  290. public function testExportStructure(): void
  291. {
  292. // case 1
  293. ob_start();
  294. $this->dummyDbi->addSelectDb('test_db');
  295. $this->assertTrue(
  296. $this->object->exportStructure(
  297. 'test_db',
  298. 'test_table',
  299. "\n",
  300. 'localhost',
  301. 'create_table',
  302. 'test'
  303. )
  304. );
  305. $this->assertAllSelectsConsumed();
  306. $result = ob_get_clean();
  307. $this->assertIsString($result);
  308. $this->assertEquals(
  309. '== Table structure for table test_table' . "\n\n"
  310. . '|------' . "\n"
  311. . '|Column|Type|Null|Default' . "\n"
  312. . '|------' . "\n"
  313. . '|//**id**//|int(11)|No|NULL' . "\n"
  314. . '|name|varchar(20)|No|NULL' . "\n"
  315. . '|datetimefield|datetime|No|NULL' . "\n",
  316. $result
  317. );
  318. // case 2
  319. ob_start();
  320. $this->assertTrue(
  321. $this->object->exportStructure(
  322. 'test_db',
  323. 'test_table',
  324. "\n",
  325. 'localhost',
  326. 'triggers',
  327. 'test'
  328. )
  329. );
  330. $result = ob_get_clean();
  331. $this->assertEquals(
  332. '== Triggers test_table' . "\n\n"
  333. . '|------' . "\n"
  334. . '|Name|Time|Event|Definition' . "\n"
  335. . '|------' . "\n"
  336. . '|test_trigger|AFTER|INSERT|BEGIN END' . "\n",
  337. $result
  338. );
  339. // case 3
  340. ob_start();
  341. $this->dummyDbi->addSelectDb('test_db');
  342. $this->assertTrue(
  343. $this->object->exportStructure(
  344. 'test_db',
  345. 'test_table',
  346. "\n",
  347. 'localhost',
  348. 'create_view',
  349. 'test'
  350. )
  351. );
  352. $this->assertAllSelectsConsumed();
  353. $result = ob_get_clean();
  354. $this->assertEquals(
  355. '== Structure for view test_table' . "\n\n"
  356. . '|------' . "\n"
  357. . '|Column|Type|Null|Default' . "\n"
  358. . '|------' . "\n"
  359. . '|//**id**//|int(11)|No|NULL' . "\n"
  360. . '|name|varchar(20)|No|NULL' . "\n"
  361. . '|datetimefield|datetime|No|NULL' . "\n",
  362. $result
  363. );
  364. // case 4
  365. ob_start();
  366. $this->dummyDbi->addSelectDb('test_db');
  367. $this->assertTrue(
  368. $this->object->exportStructure(
  369. 'test_db',
  370. 'test_table',
  371. "\n",
  372. 'localhost',
  373. 'stand_in',
  374. 'test'
  375. )
  376. );
  377. $this->assertAllSelectsConsumed();
  378. $result = ob_get_clean();
  379. $this->assertEquals(
  380. '== Stand-in structure for view test_table' . "\n\n"
  381. . '|------' . "\n"
  382. . '|Column|Type|Null|Default' . "\n"
  383. . '|------' . "\n"
  384. . '|//**id**//|int(11)|No|NULL' . "\n"
  385. . '|name|varchar(20)|No|NULL' . "\n"
  386. . '|datetimefield|datetime|No|NULL' . "\n",
  387. $result
  388. );
  389. }
  390. public function testFormatOneColumnDefinition(): void
  391. {
  392. $cols = [
  393. 'Null' => 'Yes',
  394. 'Field' => 'field',
  395. 'Key' => 'PRI',
  396. 'Type' => 'set(abc)enum123',
  397. ];
  398. $unique_keys = ['field'];
  399. $this->assertEquals(
  400. '|//**field**//|set(abc)|Yes|NULL',
  401. $this->object->formatOneColumnDefinition($cols, $unique_keys)
  402. );
  403. $cols = [
  404. 'Null' => 'NO',
  405. 'Field' => 'fields',
  406. 'Key' => 'COMP',
  407. 'Type' => '',
  408. 'Default' => 'def',
  409. ];
  410. $unique_keys = ['field'];
  411. $this->assertEquals(
  412. '|fields|&amp;nbsp;|No|def',
  413. $this->object->formatOneColumnDefinition($cols, $unique_keys)
  414. );
  415. }
  416. }