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

/test/classes/DbSearchTest.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 270 lines | 176 code | 18 blank | 76 comment | 0 complexity | dbe47fb2ea03dc1b2ec6945cb8ed5395 MD5 | raw file
  1. <?php
  2. /**
  3. * Tests for DbSearch.php
  4. *
  5. * @package PhpMyAdmin-test
  6. */
  7. /*
  8. * Include to test.
  9. */
  10. require_once 'libraries/database_interface.inc.php';
  11. require_once 'test/PMATestCase.php';
  12. use PMA\libraries\DbSearch;
  13. use PMA\libraries\Theme;
  14. /**
  15. * Tests for database search.
  16. *
  17. * @package PhpMyAdmin-test
  18. */
  19. class DbSearchTest extends PMATestCase
  20. {
  21. /**
  22. * @access protected
  23. */
  24. protected $object;
  25. /**
  26. * Sets up the fixture, for example, opens a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @access protected
  30. * @return void
  31. */
  32. protected function setUp()
  33. {
  34. $this->object = new DbSearch('pma_test');
  35. $GLOBALS['server'] = 0;
  36. $GLOBALS['db'] = 'pma';
  37. }
  38. /**
  39. * Tears down the fixture, for example, closes a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @access protected
  43. * @return void
  44. */
  45. protected function tearDown()
  46. {
  47. unset($this->object);
  48. }
  49. /**
  50. * Call protected functions by setting visibility to public.
  51. *
  52. * @param string $name method name
  53. * @param array $params parameters for the invocation
  54. *
  55. * @return the output from the protected method.
  56. */
  57. private function _callProtectedFunction($name, $params)
  58. {
  59. $class = new ReflectionClass('PMA\libraries\DbSearch');
  60. $method = $class->getMethod($name);
  61. $method->setAccessible(true);
  62. return $method->invokeArgs($this->object, $params);
  63. }
  64. /**
  65. * Test for _getSearchSqls
  66. *
  67. * @return void
  68. */
  69. public function testGetSearchSqls()
  70. {
  71. //mock DBI
  72. $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $dbi->expects($this->any())
  76. ->method('getColumns')
  77. ->with('pma', 'table1')
  78. ->will($this->returnValue(array()));
  79. $GLOBALS['dbi'] = $dbi;
  80. $this->assertEquals(
  81. array (
  82. 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
  83. 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
  84. 'WHERE FALSE',
  85. 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE'
  86. ),
  87. $this->_callProtectedFunction(
  88. '_getSearchSqls',
  89. array('table1')
  90. )
  91. );
  92. }
  93. /**
  94. * Test for getSearchResults
  95. *
  96. * @return void
  97. */
  98. public function testGetSearchResults()
  99. {
  100. $this->assertEquals(
  101. '<br /><table class="data"><caption class="tblHeaders">Search results '
  102. . 'for "<i></i>" :</caption></table>',
  103. $this->object->getSearchResults()
  104. );
  105. }
  106. /**
  107. * Test for _getResultsRow
  108. *
  109. * @param string $each_table Tables on which search is to be performed
  110. * @param array $newsearchsqls Contains SQL queries
  111. * @param bool $odd_row For displaying contrasting table rows
  112. * @param string $output Expected HTML output
  113. *
  114. * @return void
  115. *
  116. * @dataProvider providerForTestGetResultsRow
  117. */
  118. public function testGetResultsRow(
  119. $each_table, $newsearchsqls, $odd_row, $output
  120. ) {
  121. $this->assertEquals(
  122. $output,
  123. $this->_callProtectedFunction(
  124. '_getResultsRow',
  125. array($each_table, $newsearchsqls, $odd_row, 2)
  126. )
  127. );
  128. }
  129. /**
  130. * Data provider for testGetResultRow
  131. *
  132. * @return array provider for testGetResultsRow
  133. */
  134. public function providerForTestGetResultsRow()
  135. {
  136. return array(
  137. array(
  138. 'table1',
  139. array(
  140. 'SELECT * FROM `pma`.`table1` WHERE FALSE',
  141. 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` WHERE FALSE',
  142. 'select_count' => 2,
  143. 'select_columns' => 'column1',
  144. 'delete' => 'column2'
  145. ),
  146. true,
  147. '<tr class="noclick odd"><td>2 matches in <strong>table1</strong>'
  148. . '</td><td><a name="browse_search" class="ajax" '
  149. . 'href="sql.php?db=pma&amp;table'
  150. . '=table1&amp;goto=db_sql.php&amp;pos=0&amp;is_js_confirmed=0&amp;'
  151. . 'sql_query=column1&amp;server=0&amp;lang=en&amp;'
  152. . 'collation_connection=utf-8&amp;token=token" '
  153. . 'onclick="loadResult(\'sql.php?db=pma&amp;table=table1&amp;goto='
  154. . 'db_sql.php&amp;pos=0&amp;is_js_confirmed=0&amp;sql_query=column1'
  155. . '&amp;server=0&amp;lang=en&amp;collation_connection=utf-8'
  156. . '&amp;token=token\',\'table1\',\'?db=pma'
  157. . '&amp;table=table1&amp;server=0&amp;lang=en'
  158. . '&amp;collation_connection=utf-8&amp;token=token\');'
  159. . 'return false;" >Browse</a></td><td>'
  160. . '<a name="delete_search" class="ajax" href'
  161. . '="sql.php?db=pma&amp;table=table1&amp;goto=db_sql.php&amp;pos=0'
  162. . '&amp;is_js_confirmed=0&amp;sql_query=column2&amp;server=0&amp;'
  163. . 'lang=en&amp;collation_connection=utf-8&amp;token=token"'
  164. . ' onclick="deleteResult(\'sql.php?db=pma'
  165. . '&amp;table=table1&amp;goto=db_sql.php&amp;pos=0&amp;is_js_'
  166. . 'confirmed=0&amp;sql_query=column2&amp;server=0&amp;lang=en'
  167. . '&amp;collation_connection=utf-8&amp;'
  168. . 'token=token\' , \'Delete the matches for the table1 table?\');'
  169. . 'return false;">Delete</a></td></tr>'
  170. )
  171. );
  172. }
  173. /**
  174. * Test for getSelectionForm
  175. *
  176. * @return void
  177. */
  178. public function testGetSelectionForm()
  179. {
  180. $this->assertEquals(
  181. '<a id="db_search"></a><form id="db_search_form" class="ajax lock-page" '
  182. . 'method="post" action="db_search.php" name="db_search">'
  183. . '<input type="hidden" name="db" value="pma" />'
  184. . '<input type="hidden" name="lang" value="en" />'
  185. . '<input type="hidden" name="collation_connection" value="utf-8" />'
  186. . '<input type="hidden" name="token" value="token" />'
  187. . '<fieldset><legend>Search in database</legend><table class='
  188. . '"formlayout"><tr><td>Words or values to search for (wildcard: "%"):'
  189. . '</td><td><input type="text" name="criteriaSearchString" size="60" '
  190. . 'value="" /></td></tr><tr><td class="right vtop">Find:</td><td><input '
  191. . 'type="radio" name="criteriaSearchType" id="criteriaSearchType_1" '
  192. . 'value="1" checked="checked" />' . "\n"
  193. . '<label for="criteriaSearchType_1">at least one of the words<span '
  194. . 'class="pma_hint"><img src="themes/dot.gif" title="" alt="" class="icon ic_b_help" '
  195. . '/><span class="hide">Words are separated by a space character (" ").'
  196. . '</span></span></label><br />' . "\n"
  197. . '<input type="radio" name="criteriaSearchType" id="criteriaSearchType'
  198. . '_2" value="2" />' . "\n"
  199. . '<label for="criteriaSearchType_2">all words<span class="pma_hint">'
  200. . '<img src="themes/dot.gif" title="" alt="" class="icon ic_b_help" /><span class'
  201. . '="hide">Words are separated by a space character (" ").</span></span>'
  202. . '</label><br />' . "\n"
  203. . '<input type="radio" name="criteriaSearchType" id="criteriaSearchType'
  204. . '_3" value="3" />' . "\n"
  205. . '<label for="criteriaSearchType_3">the exact phrase</label><br />'
  206. . "\n" . '<input type="radio" name="criteriaSearchType" id="criteria'
  207. . 'SearchType_4" value="4" />' . "\n"
  208. . '<label for="criteriaSearchType_4">as regular expression <a href='
  209. . '"./url.php?url=https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen'
  210. . '%2Fregexp.html" target='
  211. . '"mysql_doc"><img src="themes/dot.gif" title="Documentation"'
  212. . ' alt="Documentation" class="icon ic_b_help" /></a></label><br />' . "\n"
  213. . '</td></tr><tr><td class="right vtop">Inside tables:</td>'
  214. . '<td rowspan="2"><select name="criteriaTables[]" size="6" '
  215. . 'multiple="multiple"><option value="table1">table1</option>'
  216. . '<option value="table2">table2</option></select></td></tr><tr>'
  217. . '<td class="right vbottom"><a href="#" onclick="setSelectOptions'
  218. . '(\'db_search\', \'criteriaTables[]\', true); return false;">Select '
  219. . 'all</a> &nbsp;/&nbsp;<a href="#" onclick="setSelectOptions'
  220. . '(\'db_search\', \'criteriaTables[]\', false); return false;">Unselect'
  221. . ' all</a></td></tr><tr><td class="right">Inside column:</td><td>'
  222. . '<input type="text" name="criteriaColumnName" size="60"value="" />'
  223. . '</td></tr></table></fieldset><fieldset class="tblFooters"><input '
  224. . 'type="submit" name="submit_search" value="Go" id="buttonGo" />'
  225. . '</fieldset></form><div id="togglesearchformdiv">'
  226. . '<a id="togglesearchformlink"></a></div>',
  227. $this->object->getSelectionForm()
  228. );
  229. }
  230. /**
  231. * Test for getResultDivs
  232. *
  233. * @return void
  234. */
  235. public function testGetResultDivs()
  236. {
  237. $this->assertEquals(
  238. '<!-- These two table-image and table-link elements display the '
  239. . 'table name in browse search results --><div id="table-info">'
  240. . '<a class="item" id="table-link" ></a></div><div id="browse-results">'
  241. . '<!-- this browse-results div is used to load the browse and delete '
  242. . 'results in the db search --></div><br class="clearfloat" />'
  243. . '<div id="sqlqueryform"><!-- this sqlqueryform div is used to load the'
  244. . ' delete form in the db search --></div><!-- toggle query box link-->'
  245. . '<a id="togglequerybox"></a>',
  246. $this->_callProtectedFunction(
  247. 'getResultDivs',
  248. array()
  249. )
  250. );
  251. }
  252. }