Skip to content

Commit e3f8168

Browse files
Merge pull request #18772 from MauricioFauth/ChangeTableInfoActionTest
Add test for Zoom Search's change table info action
2 parents 4a7f07b + dd79dd4 commit e3f8168

6 files changed

Lines changed: 95 additions & 41 deletions

File tree

phpstan-baseline.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7821,22 +7821,22 @@ parameters:
78217821
path: src/Controllers/Table/ZoomSearchController.php
78227822

78237823
-
7824-
message: "#^Only booleans are allowed in &&, array given on the left side\\.$#"
7824+
message: "#^Only booleans are allowed in &&, array\\|false given on the left side\\.$#"
78257825
count: 1
78267826
path: src/Controllers/Table/ZoomSearchController.php
78277827

78287828
-
7829-
message: "#^Only booleans are allowed in &&, array\\|false given on the right side\\.$#"
7829+
message: "#^Only booleans are allowed in a negated boolean, int\\|false given\\.$#"
78307830
count: 1
78317831
path: src/Controllers/Table/ZoomSearchController.php
78327832

78337833
-
7834-
message: "#^Only booleans are allowed in a negated boolean, int\\|false given\\.$#"
7834+
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#"
78357835
count: 1
78367836
path: src/Controllers/Table/ZoomSearchController.php
78377837

78387838
-
7839-
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#"
7839+
message: "#^Only booleans are allowed in an if condition, array given\\.$#"
78407840
count: 1
78417841
path: src/Controllers/Table/ZoomSearchController.php
78427842

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14095,6 +14095,7 @@
1409514095
<file src="test/classes/Controllers/Table/ZoomSearchControllerTest.php">
1409614096
<DeprecatedMethod>
1409714097
<code>Config::getInstance()</code>
14098+
<code>Config::getInstance()</code>
1409814099
</DeprecatedMethod>
1409914100
</file>
1410014101
<file src="test/classes/Controllers/ThemeSetControllerTest.php">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<select class="column-operator" id="ColumnOperator{{ search_index }}" name="criteriaColumnOperators[{{ search_index }}]">
2-
{{ type_operators|raw }}
2+
{{ type_operators|raw }}
33
</select>

resources/templates/table/search/input_box.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
{% endif %}
8989
{% else %}
9090
type="text"
91-
{% endif %}
91+
{%- endif %}
9292

9393
name="criteriaValues[{{ column_index }}]"
9494
data-type="{{ column_data_type }}"

src/Controllers/Table/ZoomSearchController.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function __invoke(ServerRequest $request): void
151151
* Handle AJAX request for changing field information
152152
* (value,collation,operators,field values) in input form
153153
*/
154-
if (isset($_POST['change_tbl_info']) && $_POST['change_tbl_info'] == true) {
154+
if ($request->hasBodyParam('change_tbl_info')) {
155155
$this->changeTableInfoAction();
156156

157157
return;
@@ -483,20 +483,22 @@ public function getColumnProperties(int $searchIndex, int $columnIndex): array
483483
$htmlAttributes .= ' onfocus="return '
484484
. 'verifyAfterSearchFieldChange(' . $searchIndex . ', \'#zoom_search_form\')"';
485485

486+
$searchColumnInForeigners = false;
486487
$foreignDropdown = '';
487-
488-
if (
489-
$this->foreigners
490-
&& $this->relation->searchColumnInForeigners($this->foreigners, $this->columnNames[$columnIndex])
491-
&& is_array($foreignData['disp_row'])
492-
) {
493-
$foreignDropdown = $this->relation->foreignDropdown(
494-
$foreignData['disp_row'],
495-
$foreignData['foreign_field'],
496-
$foreignData['foreign_display'],
497-
'',
498-
Config::getInstance()->settings['ForeignKeyMaxLimit'],
488+
if ($this->foreigners) {
489+
$searchColumnInForeigners = $this->relation->searchColumnInForeigners(
490+
$this->foreigners,
491+
$this->columnNames[$columnIndex],
499492
);
493+
if ($searchColumnInForeigners && is_array($foreignData['disp_row'])) {
494+
$foreignDropdown = $this->relation->foreignDropdown(
495+
$foreignData['disp_row'],
496+
$foreignData['foreign_field'],
497+
$foreignData['foreign_display'],
498+
'',
499+
Config::getInstance()->settings['ForeignKeyMaxLimit'],
500+
);
501+
}
500502
}
501503

502504
$value = $this->template->render('table/search/input_box', [
@@ -516,6 +518,7 @@ public function getColumnProperties(int $searchIndex, int $columnIndex): array
516518
'db' => $GLOBALS['db'],
517519
'in_fbs' => true,
518520
'foreign_dropdown' => $foreignDropdown,
521+
'search_column_in_foreigners' => $searchColumnInForeigners,
519522
'is_integer' => $isInteger,
520523
'is_float' => $isFloat,
521524
]);

test/classes/Controllers/Table/ZoomSearchControllerTest.php

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,17 @@
77
use PhpMyAdmin\Config;
88
use PhpMyAdmin\ConfigStorage\Relation;
99
use PhpMyAdmin\Controllers\Table\ZoomSearchController;
10-
use PhpMyAdmin\DatabaseInterface;
1110
use PhpMyAdmin\DbTableExists;
1211
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
1312
use PhpMyAdmin\Table\Search;
1413
use PhpMyAdmin\Template;
1514
use PhpMyAdmin\Tests\AbstractTestCase;
16-
use PhpMyAdmin\Tests\Stubs\DbiDummy;
1715
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
1816
use PHPUnit\Framework\Attributes\CoversClass;
1917

2018
#[CoversClass(ZoomSearchController::class)]
21-
class ZoomSearchControllerTest extends AbstractTestCase
19+
final class ZoomSearchControllerTest extends AbstractTestCase
2220
{
23-
protected DatabaseInterface $dbi;
24-
25-
protected DbiDummy $dummyDbi;
26-
27-
protected function setUp(): void
28-
{
29-
parent::setUp();
30-
31-
$this->dummyDbi = $this->createDbiDummy();
32-
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
33-
DatabaseInterface::$instance = $this->dbi;
34-
}
35-
3621
public function testZoomSearchController(): void
3722
{
3823
$GLOBALS['server'] = 2;
@@ -41,8 +26,11 @@ public function testZoomSearchController(): void
4126
$GLOBALS['text_dir'] = 'ltr';
4227
Config::getInstance()->selectedServer['DisableIS'] = true;
4328

44-
$this->dummyDbi->addSelectDb('test_db');
45-
$this->dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]);
29+
$dbiDummy = $this->createDbiDummy();
30+
$dbi = $this->createDatabaseInterface($dbiDummy);
31+
32+
$dbiDummy->addSelectDb('test_db');
33+
$dbiDummy->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]);
4634

4735
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/')
4836
->withQueryParams(['db' => 'test_db', 'table' => 'test_table']);
@@ -52,10 +40,10 @@ public function testZoomSearchController(): void
5240
$controller = new ZoomSearchController(
5341
$response,
5442
$template,
55-
new Search($this->dbi),
56-
new Relation($this->dbi),
57-
$this->dbi,
58-
new DbTableExists($this->dbi),
43+
new Search($dbi),
44+
new Relation($dbi),
45+
$dbi,
46+
new DbTableExists($dbi),
5947
);
6048
$controller($request);
6149

@@ -75,4 +63,66 @@ public function testZoomSearchController(): void
7563

7664
$this->assertSame($expected, $response->getHTMLResult());
7765
}
66+
67+
public function testChangeTableInfoAction(): void
68+
{
69+
$GLOBALS['db'] = 'test_db';
70+
$GLOBALS['table'] = 'test_table';
71+
Config::getInstance()->selectedServer['DisableIS'] = true;
72+
73+
$_POST['field'] = 'datetimefield';
74+
75+
$dbiDummy = $this->createDbiDummy();
76+
$dbi = $this->createDatabaseInterface($dbiDummy);
77+
78+
$dbiDummy->addSelectDb('test_db');
79+
$dbiDummy->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]);
80+
81+
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
82+
->withQueryParams(['db' => 'test_db', 'table' => 'test_table'])
83+
->withParsedBody(['change_tbl_info' => '1']);
84+
85+
$response = new ResponseRenderer();
86+
$template = new Template();
87+
$controller = new ZoomSearchController(
88+
$response,
89+
$template,
90+
new Search($dbi),
91+
new Relation($dbi),
92+
$dbi,
93+
new DbTableExists($dbi),
94+
);
95+
$controller($request);
96+
97+
// phpcs:disable Generic.Files.LineLength.TooLong
98+
$operators = <<<'HTML'
99+
<select class="column-operator" id="ColumnOperator0" name="criteriaColumnOperators[0]">
100+
<option value="=">=</option><option value="&gt;">&gt;</option><option value="&gt;=">&gt;=</option><option value="&lt;">&lt;</option><option value="&lt;=">&lt;=</option><option value="!=">!=</option><option value="LIKE">LIKE</option><option value="LIKE %...%">LIKE %...%</option><option value="NOT LIKE">NOT LIKE</option><option value="NOT LIKE %...%">NOT LIKE %...%</option><option value="IN (...)">IN (...)</option><option value="NOT IN (...)">NOT IN (...)</option><option value="BETWEEN">BETWEEN</option><option value="NOT BETWEEN">NOT BETWEEN</option>
101+
</select>
102+
103+
HTML;
104+
// phpcs:enable
105+
106+
$value = <<<'HTML'
107+
<input
108+
type="text"
109+
name="criteriaValues[0]"
110+
data-type="DATETIME"
111+
onfocus="return verifyAfterSearchFieldChange(0, '#zoom_search_form')"
112+
size="40"
113+
class="textfield datetimefield"
114+
id="fieldID_0"
115+
>
116+
117+
HTML;
118+
119+
$expected = [
120+
'field_type' => 'datetime',
121+
'field_collation' => '',
122+
'field_operators' => $operators,
123+
'field_value' => $value,
124+
];
125+
126+
$this->assertSame($expected, $response->getJSONResult());
127+
}
78128
}

0 commit comments

Comments
 (0)