PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/libraries/PMA_relation_cleanup_test.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 511 lines | 325 code | 47 blank | 139 comment | 8 complexity | d6d4740ab2f2ed8b1182b355268adb9d MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * tests for relation_cleanup.lib.php
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. /*
  9. * Include to test.
  10. */
  11. require_once 'libraries/database_interface.inc.php';
  12. require_once 'libraries/relation.lib.php';
  13. require_once 'libraries/relation_cleanup.lib.php';
  14. use PMA\libraries\DatabaseInterface;
  15. /**
  16. * PMA_Relation_Cleanup_Test class
  17. *
  18. * this class is for testing relation_cleanup.lib.php functions
  19. *
  20. * @package PhpMyAdmin-test
  21. */
  22. class PMA_Relation_Cleanup_Test extends PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * Prepares environment for the test.
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. $_SESSION['relation'] = array();
  32. $GLOBALS['server'] = 1;
  33. $GLOBALS['cfg']['Server']['user'] = "user";
  34. $GLOBALS['cfg']['Server']['pmadb'] = "pmadb";
  35. $GLOBALS['cfg']['Server']['bookmarktable'] = 'bookmark';
  36. $GLOBALS['cfg']['Server']['relation'] = 'relation';
  37. $GLOBALS['cfg']['Server']['table_info'] = 'table_info';
  38. $GLOBALS['cfg']['Server']['table_coords'] = 'table_coords';
  39. $GLOBALS['cfg']['Server']['column_info'] = 'column_info';
  40. $GLOBALS['cfg']['Server']['pdf_pages'] = 'pdf_pages';
  41. $GLOBALS['cfg']['Server']['history'] = 'history';
  42. $GLOBALS['cfg']['Server']['recent'] = 'recent';
  43. $GLOBALS['cfg']['Server']['favorite'] = 'favorite';
  44. $GLOBALS['cfg']['Server']['table_uiprefs'] = 'table_uiprefs';
  45. $GLOBALS['cfg']['Server']['tracking'] = 'tracking';
  46. $GLOBALS['cfg']['Server']['userconfig'] = 'userconfig';
  47. $GLOBALS['cfg']['Server']['users'] = 'users';
  48. $GLOBALS['cfg']['Server']['usergroups'] = 'usergroups';
  49. $GLOBALS['cfg']['Server']['navigationhiding'] = 'navigationhiding';
  50. $GLOBALS['cfg']['Server']['savedsearches'] = 'savedsearches';
  51. $GLOBALS['cfg']['Server']['central_columns'] = 'central_columns';
  52. $GLOBALS['cfg']['Server']['designer_settings'] = 'designer_settings';
  53. $GLOBALS['cfg']['Server']['export_templates'] = 'pma__export_templates';
  54. $this->redefineRelation();
  55. }
  56. /**
  57. * functions for redefine DBI_PMA_Relation_Cleanup
  58. *
  59. * @return void
  60. */
  61. public function redefineRelation()
  62. {
  63. $GLOBALS['dbi'] = new DBI_PMA_Relation_Cleanup();
  64. unset($_SESSION['relation'][$GLOBALS['server']]);
  65. }
  66. /**
  67. * Test for PMA_relationsCleanupColumn
  68. *
  69. * @return void
  70. * @group medium
  71. */
  72. public function testPMARelationsCleanupColumn()
  73. {
  74. $db = "PMA";
  75. $table = "PMA_bookmark";
  76. $column = "name";
  77. $this->redefineRelation();
  78. //the $cfgRelation value before cleanup column
  79. $cfgRelation = PMA_checkRelationsParam();
  80. $this->assertEquals(
  81. true,
  82. $cfgRelation['commwork']
  83. );
  84. //validate PMA_getDbComments when commwork = true
  85. $db_comments = PMA_getDbComments();
  86. $this->assertEquals(
  87. array('db_name0' => 'comment0','db_name1' => 'comment1'),
  88. $db_comments
  89. );
  90. $this->assertEquals(
  91. true,
  92. $cfgRelation['displaywork']
  93. );
  94. //validate PMA_getDisplayField when displaywork = true
  95. $display_field = PMA_getDisplayField($db, $table);
  96. $this->assertEquals(
  97. 'PMA_display_field',
  98. $display_field
  99. );
  100. $this->assertEquals(
  101. true,
  102. $cfgRelation['relwork']
  103. );
  104. $this->assertEquals(
  105. 'column_info',
  106. $cfgRelation['column_info']
  107. );
  108. $this->assertEquals(
  109. 'table_info',
  110. $cfgRelation['table_info']
  111. );
  112. $this->assertEquals(
  113. 'relation',
  114. $cfgRelation['relation']
  115. );
  116. //cleanup
  117. PMA_relationsCleanupColumn($db, $table, $column);
  118. //the $cfgRelation value after cleanup column
  119. $cfgRelation = PMA_checkRelationsParam();
  120. $is_defined_column_info
  121. = isset($cfgRelation['column_info'])? $cfgRelation['column_info'] : null;
  122. $is_defined_table_info
  123. = isset($cfgRelation['table_info'])? $cfgRelation['table_info'] : null;
  124. $is_defined_relation
  125. = isset($cfgRelation['relation'])? $cfgRelation['relation'] : null;
  126. $this->assertEquals(
  127. null,
  128. $is_defined_column_info
  129. );
  130. $this->assertEquals(
  131. null,
  132. $is_defined_table_info
  133. );
  134. $this->assertEquals(
  135. null,
  136. $is_defined_relation
  137. );
  138. }
  139. /**
  140. * Test for PMA_relationsCleanupTable
  141. *
  142. * @return void
  143. */
  144. public function testPMARelationsCleanupTable()
  145. {
  146. $db = "PMA";
  147. $table = "PMA_bookmark";
  148. $this->redefineRelation();
  149. //the $cfgRelation value before cleanup column
  150. $cfgRelation = PMA_checkRelationsParam();
  151. $this->assertEquals(
  152. 'column_info',
  153. $cfgRelation['column_info']
  154. );
  155. $this->assertEquals(
  156. 'table_info',
  157. $cfgRelation['table_info']
  158. );
  159. $this->assertEquals(
  160. 'table_coords',
  161. $cfgRelation['table_coords']
  162. );
  163. $this->assertEquals(
  164. 'relation',
  165. $cfgRelation['relation']
  166. );
  167. //PMA_relationsCleanupTable
  168. PMA_relationsCleanupTable($db, $table);
  169. //the $cfgRelation value after cleanup column
  170. $cfgRelation = PMA_checkRelationsParam();
  171. $is_defined_column_info
  172. = isset($cfgRelation['column_info'])? $cfgRelation['column_info'] : null;
  173. $is_defined_table_info
  174. = isset($cfgRelation['table_info'])? $cfgRelation['table_info'] : null;
  175. $is_defined_relation
  176. = isset($cfgRelation['relation'])? $cfgRelation['relation'] : null;
  177. $is_defined_table_coords
  178. = isset($cfgRelation['table_coords'])
  179. ? $cfgRelation['table_coords']
  180. : null;
  181. $this->assertEquals(
  182. null,
  183. $is_defined_column_info
  184. );
  185. $this->assertEquals(
  186. null,
  187. $is_defined_table_info
  188. );
  189. $this->assertEquals(
  190. null,
  191. $is_defined_relation
  192. );
  193. $this->assertEquals(
  194. null,
  195. $is_defined_table_coords
  196. );
  197. }
  198. /**
  199. * Test for PMA_relationsCleanupDatabase
  200. *
  201. * @return void
  202. */
  203. public function testPMARelationsCleanupDatabase()
  204. {
  205. $db = "PMA";
  206. $this->redefineRelation();
  207. //the $cfgRelation value before cleanup column
  208. $cfgRelation = PMA_checkRelationsParam();
  209. $this->assertEquals(
  210. 'column_info',
  211. $cfgRelation['column_info']
  212. );
  213. $this->assertEquals(
  214. 'bookmark',
  215. $cfgRelation['bookmark']
  216. );
  217. $this->assertEquals(
  218. 'table_info',
  219. $cfgRelation['table_info']
  220. );
  221. $this->assertEquals(
  222. 'pdf_pages',
  223. $cfgRelation['pdf_pages']
  224. );
  225. $this->assertEquals(
  226. 'table_coords',
  227. $cfgRelation['table_coords']
  228. );
  229. $this->assertEquals(
  230. 'relation',
  231. $cfgRelation['relation']
  232. );
  233. //cleanup
  234. PMA_relationsCleanupDatabase($db);
  235. //the value after cleanup column
  236. $cfgRelation = PMA_checkRelationsParam();
  237. $is_defined_column_info
  238. = isset($cfgRelation['column_info'])? $cfgRelation['column_info'] : null;
  239. $is_defined_table_info
  240. = isset($cfgRelation['table_info'])? $cfgRelation['table_info'] : null;
  241. $is_defined_relation
  242. = isset($cfgRelation['relation'])? $cfgRelation['relation'] : null;
  243. $is_defined_table_coords
  244. = isset($cfgRelation['table_coords'])
  245. ? $cfgRelation['table_coords']
  246. : null;
  247. $this->assertEquals(
  248. null,
  249. $is_defined_column_info
  250. );
  251. $this->assertEquals(
  252. null,
  253. $is_defined_table_info
  254. );
  255. $this->assertEquals(
  256. null,
  257. $is_defined_relation
  258. );
  259. $this->assertEquals(
  260. null,
  261. $is_defined_table_coords
  262. );
  263. }
  264. }
  265. /**
  266. * DBI_PMA_Relation_Cleanup for Mock DBI class
  267. *
  268. * this class is for Mock DBI
  269. *
  270. * @package PhpMyAdmin-test
  271. */
  272. class DBI_PMA_Relation_Cleanup extends DatabaseInterface
  273. {
  274. var $index;
  275. var $assocIndex;
  276. var $totalNum;
  277. var $values = array();
  278. var $indexs = array();
  279. /**
  280. * Constructor
  281. *
  282. */
  283. public function __construct()
  284. {
  285. $this->index = 0;
  286. $this->assocIndex = 0;
  287. $this->totalNum = 2;
  288. $this->values = array(
  289. 'bookmark',
  290. 'relation',
  291. 'table_info',
  292. 'table_coords',
  293. 'column_info',
  294. 'pdf_pages',
  295. 'history',
  296. 'recent',
  297. 'table_uiprefs',
  298. 'tracking',
  299. 'userconfig',
  300. 'users',
  301. 'usergroups',
  302. 'navigationhiding',
  303. );
  304. $this->indexs = array(
  305. 'bookmark' => 0,
  306. 'relation' => 1,
  307. 'table_info' => 2,
  308. 'table_coords' => 3,
  309. 'column_info' => 4,
  310. 'pdf_pages' => 5,
  311. 'history' => 6,
  312. 'recent' => 7,
  313. 'table_uiprefs' => 8,
  314. 'tracking' => 9,
  315. 'userconfig' => 10,
  316. 'users' => 11,
  317. 'usergroups' => 12,
  318. 'navigationhiding' => 13,
  319. );
  320. }
  321. /**
  322. * returns array of rows with numeric keys from $result
  323. *
  324. * @param object $result result set identifier
  325. *
  326. * @return array
  327. */
  328. function fetchRow($result)
  329. {
  330. $curr_table = array();
  331. if ($this->index < count($this->values)) {
  332. $curr_table[0] = $this->values[$this->index];
  333. $this->index++;
  334. return $curr_table;
  335. }
  336. $this->index = 0;
  337. return false;
  338. }
  339. /**
  340. * runs a query
  341. *
  342. * @param string $sql SQL query to execute
  343. * @param mixed $link optional database link to use
  344. * @param int $options optional query options
  345. * @param bool $cache_affected_rows whether to cache affected rows
  346. *
  347. * @return mixed
  348. */
  349. function query($sql, $link = null, $options = 0, $cache_affected_rows = true)
  350. {
  351. if (mb_stripos($sql, "column_info") !== false) {
  352. unset($this->values[$this->indexs['column_info']]);
  353. }
  354. if (mb_stripos($sql, "table_info") !== false) {
  355. unset($this->values[$this->indexs['table_info']]);
  356. }
  357. if (mb_stripos($sql, "table_coords") !== false) {
  358. unset($this->values[$this->indexs['table_coords']]);
  359. }
  360. if (mb_stripos($sql, "relation") !== false) {
  361. unset($this->values[$this->indexs['relation']]);
  362. }
  363. if (mb_stripos($sql, "pdf_pages") !== false) {
  364. unset($GLOBALS [$this->indexs['pdf_pages']]);
  365. }
  366. if (mb_stripos($sql, "bookmark") !== false) {
  367. unset($GLOBALS [$this->indexs['bookmark']]);
  368. }
  369. return true;
  370. }
  371. /**
  372. * runs a query and returns the result
  373. *
  374. * @param string $query query to run
  375. * @param resource $link mysql link resource
  376. * @param integer $options query options
  377. * @param bool $cache_affected_rows whether to cache affected row
  378. *
  379. * @return mixed
  380. */
  381. public function tryQuery(
  382. $query, $link = null, $options = 0, $cache_affected_rows = true
  383. ) {
  384. return true;
  385. }
  386. /**
  387. * selects given database
  388. *
  389. * @param string $dbname database name to select
  390. * @param object $link connection object
  391. *
  392. * @return boolean
  393. */
  394. public function selectDb($dbname, $link = null)
  395. {
  396. return true;
  397. }
  398. /**
  399. * Frees memory associated with the result
  400. *
  401. * @param object $result database result
  402. *
  403. * @return bool
  404. */
  405. public function freeResult($result)
  406. {
  407. return true;
  408. }
  409. /**
  410. * returns only the first row from the result
  411. *
  412. * <code>
  413. * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
  414. * $user = $GLOBALS['dbi']->fetchSingleRow($sql);
  415. * // produces
  416. * // $user = array('id' => 123, 'name' => 'John Doe')
  417. * </code>
  418. *
  419. * @param string|mysql_result $result query or mysql result
  420. * @param string $type NUM|ASSOC|BOTH
  421. * returned array should either numeric
  422. * associative or booth
  423. * @param resource $link mysql link
  424. *
  425. * @return array|boolean first row from result
  426. * or false if result is empty
  427. */
  428. public function fetchSingleRow($result, $type = 'ASSOC', $link = null)
  429. {
  430. return array(
  431. 'display_field' => "PMA_display_field"
  432. );
  433. }
  434. /**
  435. * returns array of rows with associative keys from $result
  436. *
  437. * @param object $result result set identifier
  438. *
  439. * @return array
  440. */
  441. public function fetchAssoc($result)
  442. {
  443. $assocResult = array();
  444. if ($this->assocIndex < $this->totalNum) {
  445. $assocResult['db_name'] = "db_name" . $this->assocIndex;
  446. $assocResult['comment'] = "comment" . $this->assocIndex;
  447. $this->assocIndex++;
  448. return $assocResult;
  449. }
  450. $this->assocIndex = 0;
  451. return false;
  452. }
  453. /**
  454. * returns the number of rows returned by last query
  455. *
  456. * @param object $result result set identifier
  457. *
  458. * @return string|int
  459. */
  460. public function numRows($result)
  461. {
  462. return $this->totalNum;
  463. }
  464. }