PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/test/classes/plugin/export/PMA_ExportTexytext_test.php

https://github.com/cz-Michael/phpmyadmin
PHP | 663 lines | 470 code | 106 blank | 87 comment | 1 complexity | f7620545759a9cc699557e471f2e6578 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * tests for ExportTexytext class
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. require_once 'libraries/plugins/export/ExportTexytext.class.php';
  9. require_once 'libraries/export.lib.php';
  10. require_once 'libraries/Util.class.php';
  11. require_once 'libraries/Theme.class.php';
  12. require_once 'libraries/Config.class.php';
  13. require_once 'libraries/php-gettext/gettext.inc';
  14. require_once 'libraries/config.default.php';
  15. require_once 'libraries/DatabaseInterface.class.php';
  16. require_once 'libraries/relation.lib.php';
  17. require_once 'libraries/sqlparser.lib.php';
  18. require_once 'libraries/transformations.lib.php';
  19. require_once 'export.php';
  20. /**
  21. * tests for ExportTexytext class
  22. *
  23. * @package PhpMyAdmin-test
  24. * @group medium
  25. */
  26. class PMA_ExportTexytext_Test extends PHPUnit_Framework_TestCase
  27. {
  28. protected $object;
  29. /**
  30. * Configures global environment.
  31. *
  32. * @return void
  33. */
  34. function setup()
  35. {
  36. if (!defined("PMA_DRIZZLE")) {
  37. define("PMA_DRIZZLE", false);
  38. }
  39. $GLOBALS['server'] = 0;
  40. $GLOBALS['output_kanji_conversion'] = false;
  41. $GLOBALS['buffer_needed'] = false;
  42. $GLOBALS['asfile'] = false;
  43. $GLOBALS['save_on_server'] = false;
  44. $GLOBALS['plugin_param'] = array();
  45. $GLOBALS['plugin_param']['export_type'] = 'table';
  46. $GLOBALS['plugin_param']['single_table'] = false;
  47. $GLOBALS['cfgRelation']['relation'] = true;
  48. $this->object = new ExportTexytext();
  49. }
  50. /**
  51. * tearDown for test cases
  52. *
  53. * @return void
  54. */
  55. public function tearDown()
  56. {
  57. unset($this->object);
  58. }
  59. /**
  60. * Test for ExportTexytext::setProperties
  61. *
  62. * @return void
  63. */
  64. public function testSetProperties()
  65. {
  66. $method = new ReflectionMethod('ExportTexytext', 'setProperties');
  67. $method->setAccessible(true);
  68. $method->invoke($this->object, null);
  69. $attrProperties = new ReflectionProperty('ExportTexytext', 'properties');
  70. $attrProperties->setAccessible(true);
  71. $properties = $attrProperties->getValue($this->object);
  72. $this->assertInstanceOf(
  73. 'ExportPluginProperties',
  74. $properties
  75. );
  76. $this->assertEquals(
  77. 'Texy! text',
  78. $properties->getText()
  79. );
  80. $this->assertEquals(
  81. 'txt',
  82. $properties->getExtension()
  83. );
  84. $this->assertEquals(
  85. 'text/plain',
  86. $properties->getMimeType()
  87. );
  88. $options = $properties->getOptions();
  89. $this->assertInstanceOf(
  90. 'OptionsPropertyRootGroup',
  91. $options
  92. );
  93. $this->assertEquals(
  94. 'Format Specific Options',
  95. $options->getName()
  96. );
  97. $generalOptionsArray = $options->getProperties();
  98. $generalOptions = array_shift($generalOptionsArray);
  99. $this->assertInstanceOf(
  100. 'OptionsPropertyMainGroup',
  101. $generalOptions
  102. );
  103. $this->assertEquals(
  104. 'general_opts',
  105. $generalOptions->getName()
  106. );
  107. $this->assertEquals(
  108. "Dump table",
  109. $generalOptions->getText()
  110. );
  111. $generalProperties = $generalOptions->getProperties();
  112. $property = array_shift($generalProperties);
  113. $this->assertInstanceOf(
  114. 'RadioPropertyItem',
  115. $property
  116. );
  117. $generalOptions = array_shift($generalOptionsArray);
  118. $this->assertInstanceOf(
  119. 'OptionsPropertyMainGroup',
  120. $generalOptions
  121. );
  122. $this->assertEquals(
  123. 'data',
  124. $generalOptions->getName()
  125. );
  126. $generalProperties = $generalOptions->getProperties();
  127. $property = array_shift($generalProperties);
  128. $this->assertInstanceOf(
  129. 'BoolPropertyItem',
  130. $property
  131. );
  132. $this->assertEquals(
  133. 'columns',
  134. $property->getName()
  135. );
  136. $property = array_shift($generalProperties);
  137. $this->assertInstanceOf(
  138. 'TextPropertyItem',
  139. $property
  140. );
  141. $this->assertEquals(
  142. 'null',
  143. $property->getName()
  144. );
  145. }
  146. /**
  147. * Test for ExportTexytext::exportHeader
  148. *
  149. * @return void
  150. */
  151. public function testExportHeader()
  152. {
  153. $this->assertTrue(
  154. $this->object->exportHeader()
  155. );
  156. }
  157. /**
  158. * Test for ExportTexytext::exportFooter
  159. *
  160. * @return void
  161. */
  162. public function testExportFooter()
  163. {
  164. $this->assertTrue(
  165. $this->object->exportFooter()
  166. );
  167. }
  168. /**
  169. * Test for ExportTexytext::exportDBHeader
  170. *
  171. * @return void
  172. */
  173. public function testExportDBHeader()
  174. {
  175. $this->expectOutputString(
  176. "===Database testDb\n\n"
  177. );
  178. $this->assertTrue(
  179. $this->object->exportDBHeader('testDb')
  180. );
  181. }
  182. /**
  183. * Test for ExportTexytext::exportDBFooter
  184. *
  185. * @return void
  186. */
  187. public function testExportDBFooter()
  188. {
  189. $this->assertTrue(
  190. $this->object->exportDBFooter('testDB')
  191. );
  192. }
  193. /**
  194. * Test for ExportTexytext::exportDBCreate
  195. *
  196. * @return void
  197. */
  198. public function testExportDBCreate()
  199. {
  200. $this->assertTrue(
  201. $this->object->exportDBCreate('testDB')
  202. );
  203. }
  204. /**
  205. * Test for ExportTexytext::exportData
  206. *
  207. * @return void
  208. */
  209. public function testExportData()
  210. {
  211. $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. $dbi->expects($this->once())
  215. ->method('query')
  216. ->with('SELECT', null, PMA_DatabaseInterface::QUERY_UNBUFFERED)
  217. ->will($this->returnValue(true));
  218. $dbi->expects($this->once())
  219. ->method('numFields')
  220. ->with(true)
  221. ->will($this->returnValue(3));
  222. $dbi->expects($this->at(2))
  223. ->method('fieldName')
  224. ->will($this->returnValue('fName1'));
  225. $dbi->expects($this->at(3))
  226. ->method('fieldName')
  227. ->will($this->returnValue('fNa"me2'));
  228. $dbi->expects($this->at(4))
  229. ->method('fieldName')
  230. ->will($this->returnValue('fName3'));
  231. $dbi->expects($this->at(5))
  232. ->method('fetchRow')
  233. ->with(true)
  234. ->will($this->returnValue(array(null, '0', 'test')));
  235. $GLOBALS['dbi'] = $dbi;
  236. $GLOBALS['what'] = 'foo';
  237. $GLOBALS['foo_columns'] = "&";
  238. $GLOBALS['foo_null'] = ">";
  239. ob_start();
  240. $this->assertTrue(
  241. $this->object->exportData(
  242. 'db', 'ta<ble', "\n", "example.com", "SELECT"
  243. )
  244. );
  245. $result = ob_get_clean();
  246. $this->assertContains(
  247. "|fName1|fNa&amp;quot;me2|fName3",
  248. $result
  249. );
  250. $this->assertContains(
  251. "|&amp;gt;|0|test",
  252. $result
  253. );
  254. }
  255. /**
  256. * Test for ExportTexytext::getTableDefStandIn
  257. *
  258. * @return void
  259. */
  260. public function testGetTableDefStandIn()
  261. {
  262. $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
  263. ->disableOriginalConstructor()
  264. ->getMock();
  265. $dbi->expects($this->once())
  266. ->method('getColumns')
  267. ->with('db', 'view')
  268. ->will($this->returnValue(array(1, 2)));
  269. $keys = array(
  270. array(
  271. 'Non_unique' => 0,
  272. 'Column_name' => 'cname'
  273. ),
  274. array(
  275. 'Non_unique' => 1,
  276. 'Column_name' => 'cname2'
  277. )
  278. );
  279. $dbi->expects($this->once())
  280. ->method('getTableIndexes')
  281. ->with('db', 'view')
  282. ->will($this->returnValue($keys));
  283. $dbi->expects($this->once())
  284. ->method('selectDb')
  285. ->with('db');
  286. $GLOBALS['dbi'] = $dbi;
  287. $this->object = $this->getMockBuilder('ExportTexytext')
  288. ->disableOriginalConstructor()
  289. ->setMethods(array('formatOneColumnDefinition'))
  290. ->getMock();
  291. $this->object->expects($this->at(0))
  292. ->method('formatOneColumnDefinition')
  293. ->with(1, array('cname'))
  294. ->will($this->returnValue('c1'));
  295. $this->object->expects($this->at(1))
  296. ->method('formatOneColumnDefinition')
  297. ->with(2, array('cname'))
  298. ->will($this->returnValue('c2'));
  299. $result = $this->object->getTableDefStandIn('db', 'view', '#');
  300. $this->assertContains(
  301. "c1\nc2",
  302. $result
  303. );
  304. }
  305. /**
  306. * Test for ExportTexytext::getTableDef
  307. *
  308. * @return void
  309. */
  310. public function testGetTableDef()
  311. {
  312. $this->object = $this->getMockBuilder('ExportTexytext')
  313. ->setMethods(array('formatOneColumnDefinition'))
  314. ->getMock();
  315. // case 1
  316. $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
  317. ->disableOriginalConstructor()
  318. ->getMock();
  319. $keys = array(
  320. array(
  321. 'Non_unique' => 0,
  322. 'Column_name' => 'cname'
  323. ),
  324. array(
  325. 'Non_unique' => 1,
  326. 'Column_name' => 'cname2'
  327. )
  328. );
  329. $dbi->expects($this->once())
  330. ->method('getTableIndexes')
  331. ->with('db', 'table')
  332. ->will($this->returnValue($keys));
  333. $dbi->expects($this->at(2))
  334. ->method('fetchResult')
  335. ->will(
  336. $this->returnValue(
  337. array(
  338. 'fname' => array(
  339. 'foreign_table' => '<ftable',
  340. 'foreign_field' => 'ffield>'
  341. )
  342. )
  343. )
  344. );
  345. $dbi->expects($this->at(3))
  346. ->method('fetchValue')
  347. ->will(
  348. $this->returnValue(
  349. 'SELECT a FROM b'
  350. )
  351. );
  352. $dbi->expects($this->at(5))
  353. ->method('fetchResult')
  354. ->will(
  355. $this->returnValue(
  356. array(
  357. 'fname' => array(
  358. 'values' => 'test-',
  359. 'transformation' => 'testfoo',
  360. 'mimetype' => 'test<'
  361. )
  362. )
  363. )
  364. );
  365. $columns = array(
  366. 'Field' => 'fname',
  367. 'Comment' => 'comm'
  368. );
  369. $dbi->expects($this->exactly(2))
  370. ->method('getColumns')
  371. ->with('db', 'table')
  372. ->will($this->returnValue(array($columns)));
  373. $GLOBALS['dbi'] = $dbi;
  374. $this->object->expects($this->exactly(1))
  375. ->method('formatOneColumnDefinition')
  376. ->with(array('Field' => 'fname', 'Comment' => 'comm'), array('cname'))
  377. ->will($this->returnValue(1));
  378. $GLOBALS['cfgRelation']['relation'] = true;
  379. $_SESSION['relation'][0] = array(
  380. 'relwork' => true,
  381. 'commwork' => true,
  382. 'mimework' => true,
  383. 'db' => 'db',
  384. 'relation' => 'rel',
  385. 'column_info' => 'col'
  386. );
  387. $result = $this->object->getTableDef(
  388. 'db',
  389. 'table',
  390. "\n",
  391. "example.com",
  392. true,
  393. true,
  394. true
  395. );
  396. $this->assertContains(
  397. '1|&lt;ftable (ffield&gt;)|comm|Test&lt;',
  398. $result
  399. );
  400. }
  401. /**
  402. * Test for ExportTexytext::getTriggers
  403. *
  404. * @return void
  405. */
  406. public function testGetTriggers()
  407. {
  408. $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
  409. ->disableOriginalConstructor()
  410. ->getMock();
  411. $triggers = array(
  412. array(
  413. 'name' => 'tna"me',
  414. 'action_timing' => 'ac>t',
  415. 'event_manipulation' => 'manip&',
  416. 'definition' => 'def'
  417. )
  418. );
  419. $dbi->expects($this->once())
  420. ->method('getTriggers')
  421. ->with('database', 'ta<ble')
  422. ->will($this->returnValue($triggers));
  423. $GLOBALS['dbi'] = $dbi;
  424. $result = $this->object->getTriggers('database', 'ta<ble');
  425. $this->assertContains(
  426. '|tna"me|ac>t|manip&|def',
  427. $result
  428. );
  429. $this->assertContains(
  430. '|Name|Time|Event|Definition',
  431. $result
  432. );
  433. }
  434. /**
  435. * Test for ExportTexytext::exportStructure
  436. *
  437. * @return void
  438. */
  439. public function testExportStructure()
  440. {
  441. $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
  442. ->disableOriginalConstructor()
  443. ->getMock();
  444. $dbi->expects($this->once())
  445. ->method('getTriggers')
  446. ->with('db', 't&bl')
  447. ->will($this->returnValue(1));
  448. $this->object = $this->getMockBuilder('ExportTexytext')
  449. ->setMethods(array('getTableDef', 'getTriggers', 'getTableDefStandIn'))
  450. ->getMock();
  451. $this->object->expects($this->at(0))
  452. ->method('getTableDef')
  453. ->with('db', 't&bl', "\n", "example.com", false, false, false, false)
  454. ->will($this->returnValue('dumpText1'));
  455. $this->object->expects($this->once())
  456. ->method('getTriggers')
  457. ->with('db', 't&bl')
  458. ->will($this->returnValue('dumpText2'));
  459. $this->object->expects($this->at(2))
  460. ->method('getTableDef')
  461. ->with(
  462. 'db', 't&bl', "\n", "example.com",
  463. false, false, false, false, true, true
  464. )
  465. ->will($this->returnValue('dumpText3'));
  466. $this->object->expects($this->once())
  467. ->method('getTableDefStandIn')
  468. ->with('db', 't&bl', "\n")
  469. ->will($this->returnValue('dumpText4'));
  470. $GLOBALS['dbi'] = $dbi;
  471. // case 1
  472. ob_start();
  473. $this->assertTrue(
  474. $this->object->exportStructure(
  475. 'db', 't&bl', "\n", "example.com", "create_table", "test"
  476. )
  477. );
  478. $result = ob_get_clean();
  479. $this->assertContains(
  480. '== Table structure for table t&amp;bl' . "\n\ndumpText1",
  481. $result
  482. );
  483. // case 2
  484. ob_start();
  485. $this->assertTrue(
  486. $this->object->exportStructure(
  487. 'db', 't&bl', "\n", "example.com", "triggers", "test"
  488. )
  489. );
  490. $result = ob_get_clean();
  491. $this->assertEquals(
  492. '== Triggers t&amp;bl' . "\n\ndumpText2",
  493. $result
  494. );
  495. // case 3
  496. ob_start();
  497. $this->assertTrue(
  498. $this->object->exportStructure(
  499. 'db', 't&bl', "\n", "example.com", "create_view", "test"
  500. )
  501. );
  502. $result = ob_get_clean();
  503. $this->assertEquals(
  504. '== Structure for view t&amp;bl' . "\n\ndumpText3",
  505. $result
  506. );
  507. // case 4
  508. ob_start();
  509. $this->assertTrue(
  510. $this->object->exportStructure(
  511. 'db', 't&bl', "\n", "example.com", "stand_in", "test"
  512. )
  513. );
  514. $result = ob_get_clean();
  515. $this->assertEquals(
  516. '== Stand-in structure for view t&amp;bl' . "\n\ndumpText4",
  517. $result
  518. );
  519. }
  520. /**
  521. * Test for ExportTexytext::formatOneColumnDefinition
  522. *
  523. * @return void
  524. */
  525. public function testFormatOneColumnDefinition()
  526. {
  527. $GLOBALS['cfg']['LimitChars'] = 40;
  528. $cols = array(
  529. 'Null' => 'Yes',
  530. 'Field' => 'field',
  531. 'Key' => 'PRI',
  532. 'Type' => 'set(abc)enum123'
  533. );
  534. $unique_keys = array(
  535. 'field'
  536. );
  537. $this->assertEquals(
  538. '|//**field**//|set(abc)|Yes|NULL',
  539. $this->object->formatOneColumnDefinition($cols, $unique_keys)
  540. );
  541. $cols = array(
  542. 'Null' => 'NO',
  543. 'Field' => 'fields',
  544. 'Key' => 'COMP',
  545. 'Type' => '',
  546. 'Default' => 'def'
  547. );
  548. $unique_keys = array(
  549. 'field'
  550. );
  551. $this->assertEquals(
  552. '|fields|&amp;nbsp;|No|def',
  553. $this->object->formatOneColumnDefinition($cols, $unique_keys)
  554. );
  555. }
  556. }
  557. ?>