3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests\Database
;
7 use PhpMyAdmin\Database\Search
;
8 use PhpMyAdmin\DatabaseInterface
;
9 use PhpMyAdmin\Template
;
10 use PhpMyAdmin\Tests\AbstractTestCase
;
13 * Tests for database search.
15 class SearchTest
extends AbstractTestCase
21 * Sets up the fixture, for example, opens a network connection.
22 * This method is called before a test is executed.
26 protected function setUp(): void
29 parent
::loadDefaultConfig();
31 $GLOBALS['server'] = 0;
32 $GLOBALS['db'] = 'pma';
33 $GLOBALS['_POST'] = [];
36 $dbi = $this->getMockBuilder(DatabaseInterface
::class)
37 ->disableOriginalConstructor()
40 $dbi->expects($this->any())
41 ->method('getColumns')
42 ->with('pma', 'table1')
43 ->will($this->returnValue([
44 ['Field' => 'column1'],
45 ['Field' => 'column2'],
48 $dbi->expects($this->any())
49 ->method('escapeString')
50 ->will($this->returnArgument(0));
52 $GLOBALS['dbi'] = $dbi;
53 $this->object = new Search($dbi, 'pma_test', new Template());
57 * Tears down the fixture, for example, closes a network connection.
58 * This method is called after a test is executed.
62 protected function tearDown(): void
69 * Test for generating where clause for different search types
71 * @param string $type type
72 * @param string $expected expected result
74 * @dataProvider searchTypes
76 public function testGetWhereClause(string $type, string $expected): void
78 $_POST['criteriaSearchType'] = $type;
79 $_POST['criteriaSearchString'] = 'search string';
81 $this->object = new Search($GLOBALS['dbi'], 'pma_test', new Template());
94 * Data provider for testGetWhereClause
98 public function searchTypes(): array
103 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search%'"
104 . " OR CONVERT(`column2` USING utf8) LIKE '%search%') "
105 . " OR (CONVERT(`column1` USING utf8) LIKE '%string%'"
106 . " OR CONVERT(`column2` USING utf8) LIKE '%string%')",
110 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search%'"
111 . " OR CONVERT(`column2` USING utf8) LIKE '%search%') "
112 . " AND (CONVERT(`column1` USING utf8) LIKE '%string%'"
113 . " OR CONVERT(`column2` USING utf8) LIKE '%string%')",
117 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search string%'"
118 . " OR CONVERT(`column2` USING utf8) LIKE '%search string%')",
122 " WHERE (CONVERT(`column1` USING utf8) LIKE 'search string'"
123 . " OR CONVERT(`column2` USING utf8) LIKE 'search string')",
127 " WHERE (CONVERT(`column1` USING utf8) REGEXP 'search string'"
128 . " OR CONVERT(`column2` USING utf8) REGEXP 'search string')",
134 * Test for getSearchSqls
136 public function testGetSearchSqls(): void
140 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
141 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
143 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE',
155 * Test for getSearchResults
157 public function testGetSearchResults(): void
159 $this->assertStringContainsString(
160 'Search results for "<em></em>" :',
161 $this->object->getSearchResults()
166 * Test for getSelectionForm
168 public function testGetMainHtml(): void
170 $main = $this->object->getMainHtml();
172 // test selection form
173 $this->assertStringContainsString('<form', $main);
174 $this->assertStringContainsString('<a id="togglesearchformlink">', $main);
175 $this->assertStringContainsString('criteriaSearchType', $main);
178 $this->assertStringContainsString(
179 '<div id="table-info"',
182 $this->assertStringContainsString(
183 '<a id="table-link"',
186 $this->assertStringContainsString(
187 '<div id="browse-results"',
190 $this->assertStringContainsString(
191 '<div id="sqlqueryform"',
194 $this->assertStringContainsString(
195 '<button class="btn btn-secondary" id="togglequerybox"',