Remove some useless comments
[phpmyadmin.git] / test / classes / Database / SearchTest.php
blob4a4b25306c774ed29f64be227341047ddcb3a78a
1 <?php
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;
12 /**
13 * Tests for database search.
15 class SearchTest extends AbstractTestCase
17 /** @var Search */
18 protected $object;
20 /**
21 * Sets up the fixture, for example, opens a network connection.
22 * This method is called before a test is executed.
24 * @access protected
26 protected function setUp(): void
28 parent::setUp();
29 parent::loadDefaultConfig();
31 $GLOBALS['server'] = 0;
32 $GLOBALS['db'] = 'pma';
33 $GLOBALS['_POST'] = [];
35 //mock DBI
36 $dbi = $this->getMockBuilder(DatabaseInterface::class)
37 ->disableOriginalConstructor()
38 ->getMock();
40 $dbi->expects($this->any())
41 ->method('getColumns')
42 ->with('pma', 'table1')
43 ->will($this->returnValue([
44 ['Field' => 'column1'],
45 ['Field' => 'column2'],
46 ]));
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());
56 /**
57 * Tears down the fixture, for example, closes a network connection.
58 * This method is called after a test is executed.
60 * @access protected
62 protected function tearDown(): void
64 parent::tearDown();
65 unset($this->object);
68 /**
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());
82 $this->assertEquals(
83 $expected,
84 $this->callFunction(
85 $this->object,
86 Search::class,
87 'getWhereClause',
88 ['table1']
93 /**
94 * Data provider for testGetWhereClause
96 * @return array
98 public function searchTypes(): array
100 return [
102 '1',
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%')",
109 '2',
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%')",
116 '3',
117 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search string%'"
118 . " OR CONVERT(`column2` USING utf8) LIKE '%search string%')",
121 '4',
122 " WHERE (CONVERT(`column1` USING utf8) LIKE 'search string'"
123 . " OR CONVERT(`column2` USING utf8) LIKE 'search string')",
126 '5',
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
138 $this->assertEquals(
140 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
141 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
142 'WHERE FALSE',
143 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE',
145 $this->callFunction(
146 $this->object,
147 Search::class,
148 'getSearchSqls',
149 ['table1']
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);
177 // test result divs
178 $this->assertStringContainsString(
179 '<div id="table-info"',
180 $main
182 $this->assertStringContainsString(
183 '<a id="table-link"',
184 $main
186 $this->assertStringContainsString(
187 '<div id="browse-results"',
188 $main
190 $this->assertStringContainsString(
191 '<div id="sqlqueryform"',
192 $main
194 $this->assertStringContainsString(
195 '<button class="btn btn-secondary" id="togglequerybox"',
196 $main