Fix Display\ResultsTest test on Windows
[phpmyadmin.git] / test / classes / Database / SearchTest.php
blobfed45f33d2524c1432801de6712f51c3b9492436
1 <?php
2 /**
3 * Tests for PhpMyAdmin\Database\Search
5 * @package PhpMyAdmin-test
6 */
7 declare(strict_types=1);
9 namespace PhpMyAdmin\Tests\Database;
11 use PhpMyAdmin\Database\Search;
12 use PhpMyAdmin\Template;
13 use PhpMyAdmin\Tests\PmaTestCase;
14 use PhpMyAdmin\Theme;
15 use ReflectionClass;
17 /**
18 * Tests for database search.
20 * @package PhpMyAdmin-test
22 class SearchTest extends PmaTestCase
24 /**
25 * @access protected
27 protected $object;
29 /**
30 * Sets up the fixture, for example, opens a network connection.
31 * This method is called before a test is executed.
33 * @access protected
34 * @return void
36 protected function setUp(): void
38 $GLOBALS['server'] = 0;
39 $GLOBALS['db'] = 'pma';
41 //mock DBI
42 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
43 ->disableOriginalConstructor()
44 ->getMock();
46 $dbi->expects($this->any())
47 ->method('getColumns')
48 ->with('pma', 'table1')
49 ->will($this->returnValue([
50 ['Field' => 'column1'],
51 ['Field' => 'column2'],
52 ]));
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());
62 /**
63 * Tears down the fixture, for example, closes a network connection.
64 * This method is called after a test is executed.
66 * @access protected
67 * @return void
69 protected function tearDown(): void
71 unset($this->object);
74 /**
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);
90 /**
91 * Test for generating where clause for different search types
93 * @param string $type type
94 * @param string $expected expected result
96 * @return void
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());
106 $this->assertEquals(
107 $expected,
108 $this->callProtectedFunction(
109 'getWhereClause',
110 ['table1']
116 * Data provider for testGetWhereClause
118 * @return array
120 public function searchTypes()
122 return [
124 '1',
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%')",
128 '2',
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%')",
132 '3',
133 " WHERE (CONVERT(`column1` USING utf8) LIKE '%search string%' OR CONVERT(`column2` USING utf8) LIKE '%search string%')",
136 '4',
137 " WHERE (CONVERT(`column1` USING utf8) LIKE 'search string' OR CONVERT(`column2` USING utf8) LIKE 'search string')",
140 '5',
141 " WHERE (CONVERT(`column1` USING utf8) REGEXP 'search string' OR CONVERT(`column2` USING utf8) REGEXP 'search string')",
147 * Test for _getSearchSqls
149 * @return void
151 public function testGetSearchSqls()
153 $this->assertEquals(
155 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
156 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
157 'WHERE FALSE',
158 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE',
160 $this->callProtectedFunction(
161 'getSearchSqls',
162 ['table1']
168 * Test for getSearchResults
170 * @return void
172 public function testGetSearchResults()
174 $this->assertStringContainsString(
175 'Search results for "<em></em>" :',
176 $this->object->getSearchResults()
181 * Test for getSelectionForm
183 * @return void
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);
194 // test result divs
195 $this->assertStringContainsString(
196 '<div id="table-info"',
197 $main
199 $this->assertStringContainsString(
200 '<a id="table-link"',
201 $main
203 $this->assertStringContainsString(
204 '<div id="browse-results"',
205 $main
207 $this->assertStringContainsString(
208 '<div id="sqlqueryform"',
209 $main
211 $this->assertStringContainsString(
212 '<a id="togglequerybox"',
213 $main