3 * Tests for PhpMyAdmin\Database\Search
5 * @package PhpMyAdmin-test
7 declare(strict_types
=1);
9 namespace PhpMyAdmin\Tests\Database
;
11 use PhpMyAdmin\Database\Search
;
12 use PhpMyAdmin\Template
;
13 use PhpMyAdmin\Tests\PmaTestCase
;
18 * Tests for database search.
20 * @package PhpMyAdmin-test
22 class SearchTest
extends PmaTestCase
30 * Sets up the fixture, for example, opens a network connection.
31 * This method is called before a test is executed.
36 protected function setUp(): void
38 $GLOBALS['server'] = 0;
39 $GLOBALS['db'] = 'pma';
42 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
43 ->disableOriginalConstructor()
46 $dbi->expects($this->any())
47 ->method('getColumns')
48 ->with('pma', 'table1')
49 ->will($this->returnValue([
50 ['Field' => 'column1'],
51 ['Field' => 'column2'],
54 $dbi->expects($this->any())
55 ->method('escapeString')
56 ->will($this->returnArgument(0));
58 $GLOBALS['dbi'] = $dbi;
59 $this->object = new Search($dbi, 'pma_test', new Template());
63 * Tears down the fixture, for example, closes a network connection.
64 * This method is called after a test is executed.
69 protected function tearDown(): void
75 * Call protected functions by setting visibility to public.
77 * @param string $name method name
78 * @param array $params parameters for the invocation
80 * @return mixed the output from the protected method.
82 private function callProtectedFunction($name, $params)
84 $class = new ReflectionClass(Search
::class);
85 $method = $class->getMethod($name);
86 $method->setAccessible(true);
87 return $method->invokeArgs($this->object, $params);
91 * Test for generating where clause for different search types
93 * @param string $type type
94 * @param string $expected expected result
98 * @dataProvider searchTypes
100 public function testGetWhereClause($type, $expected): void
102 $_POST['criteriaSearchType'] = $type;
103 $_POST['criteriaSearchString'] = 'search string';
105 $this->object = new Search($GLOBALS['dbi'], 'pma_test', new Template());
108 $this->callProtectedFunction(
116 * Data provider for testGetWhereClause
120 public function searchTypes()
125 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search%' OR CONVERT(`column2` USING utf8) LIKE '%search%') OR (CONVERT(`column1` USING utf8) LIKE '%string%' OR CONVERT(`column2` USING utf8) LIKE '%string%')",
129 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search%' OR CONVERT(`column2` USING utf8) LIKE '%search%') AND (CONVERT(`column1` USING utf8) LIKE '%string%' OR CONVERT(`column2` USING utf8) LIKE '%string%')",
133 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search string%' OR CONVERT(`column2` USING utf8) LIKE '%search string%')",
137 " WHERE (CONVERT(`column1` USING utf8) LIKE 'search string' OR CONVERT(`column2` USING utf8) LIKE 'search string')",
141 " WHERE (CONVERT(`column1` USING utf8) REGEXP 'search string' OR CONVERT(`column2` USING utf8) REGEXP 'search string')",
147 * Test for _getSearchSqls
151 public function testGetSearchSqls()
155 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
156 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
158 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE',
160 $this->callProtectedFunction(
168 * Test for getSearchResults
172 public function testGetSearchResults()
174 $this->assertStringContainsString(
175 'Search results for "<em></em>" :',
176 $this->object->getSearchResults()
181 * Test for getSelectionForm
185 public function testGetMainHtml()
187 $main = $this->object->getMainHtml();
189 // test selection form
190 $this->assertStringContainsString('<form', $main);
191 $this->assertStringContainsString('<a id="togglesearchformlink">', $main);
192 $this->assertStringContainsString('criteriaSearchType', $main);
195 $this->assertStringContainsString(
196 '<div id="table-info"',
199 $this->assertStringContainsString(
200 '<a id="table-link"',
203 $this->assertStringContainsString(
204 '<div id="browse-results"',
207 $this->assertStringContainsString(
208 '<div id="sqlqueryform"',
211 $this->assertStringContainsString(
212 '<a id="togglequerybox"',