Skip to content

Commit 3bf17af

Browse files
committed
Merge branch 'QA_5_2'
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
2 parents dc14087 + b1cec4e commit 3bf17af

4 files changed

Lines changed: 62 additions & 22 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14496,24 +14496,12 @@ parameters:
1449614496
count: 1
1449714497
path: tests/unit/Config/UserPreferencesTest.php
1449814498

14499-
-
14500-
message: '#^Cannot access offset ''secret'' on mixed\.$#'
14501-
identifier: offsetAccess.nonOffsetAccessible
14502-
count: 1
14503-
path: tests/unit/Config/UserPreferencesTest.php
14504-
1450514499
-
1450614500
message: '#^Cannot access offset ''server_2'' on mixed\.$#'
1450714501
identifier: offsetAccess.nonOffsetAccessible
1450814502
count: 1
1450914503
path: tests/unit/Config/UserPreferencesTest.php
1451014504

14511-
-
14512-
message: '#^Cannot access offset ''settings'' on mixed\.$#'
14513-
identifier: offsetAccess.nonOffsetAccessible
14514-
count: 1
14515-
path: tests/unit/Config/UserPreferencesTest.php
14516-
1451714505
-
1451814506
message: '#^Cannot access offset ''ts'' on mixed\.$#'
1451914507
identifier: offsetAccess.nonOffsetAccessible

psalm-baseline.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@
397397
<code><![CDATA[$excludeList[$path]]]></code>
398398
</MixedArrayTypeCoercion>
399399
<MixedAssignment>
400+
<code><![CDATA[$configArray['2fa']]]></code>
400401
<code><![CDATA[$configData]]></code>
401402
<code><![CDATA[$configData]]></code>
402403
<code><![CDATA[$prefs['config_data'][$path]]]></code>
@@ -9166,8 +9167,6 @@
91669167
<MixedArrayAccess>
91679168
<code><![CDATA[$_SESSION['userconfig']['db']]]></code>
91689169
<code><![CDATA[$_SESSION['userconfig']['ts']]]></code>
9169-
<code><![CDATA[$resultConfig['2fa']['settings']]]></code>
9170-
<code><![CDATA[$resultConfig['2fa']['settings']['secret']]]></code>
91719170
</MixedArrayAccess>
91729171
</file>
91739172
<file src="tests/unit/ConfigStorage/RelationTest.php">

src/Config/UserPreferences.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function save(array $configArray): true|Message
127127
$existingPrefs = $this->load();
128128
if (isset($existingPrefs['config_data']['2fa'])) {
129129
// This is likely a partial save from page settings - merge to preserve 2fa
130-
$configArray = array_merge($existingPrefs['config_data'], $configArray);
130+
$configArray['2fa'] = $existingPrefs['config_data']['2fa'];
131131
}
132132
}
133133

tests/unit/Config/UserPreferencesTest.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,10 @@ public function testSave(): void
272272
public function testSaveAndKeep2FA(): void
273273
{
274274
$initialConfig = [
275+
'CharEditing' => 'textarea',
275276
'2fa' => ['backend' => 'application', 'settings' => ['secret' => 'thisisasecret']],
276-
'theme' => 'dark',
277+
'RowActionLinks' => 'both',
278+
'TableNavigationLinksMode' => 'both',
277279
];
278280
$dummyDbi = $this->createDbiDummy();
279281

@@ -371,17 +373,27 @@ public function testSaveAndKeep2FA(): void
371373
// 2FA is combined with the previous config
372374
$dummyDbi->addResult(
373375
<<<'SQL'
374-
UPDATE `pma-db`.`pma__userconfig` SET `timevalue` = NOW(), `config_data` = '{\"2fa\":{\"backend\":\"application\",\"settings\":{\"secret\":\"thisisasecret\"}},\"theme\":\"dark\",\"Console\\/Mode\":\"collapse\"}' WHERE `username` = 'root'
376+
UPDATE `pma-db`.`pma__userconfig` SET `timevalue` = NOW(), `config_data` = '{\"CharEditing\":\"textarea\",\"TableNavigationLinksMode\":\"text\",\"Console\\/Mode\":\"collapse\",\"2fa\":{\"backend\":\"application\",\"settings\":{\"secret\":\"thisisasecret\"}}}' WHERE `username` = 'root'
375377
SQL,
376378
[['root']],
377379
['username'],
378380
);
379381

380382
// Partial save without 2fa
381-
$partialConfig = ['Console/Mode' => 'collapse'];
383+
$partialConfig = [
384+
'CharEditing' => 'textarea',
385+
'TableNavigationLinksMode' => 'text',
386+
'Console/Mode' => 'collapse',
387+
];
382388
$userPreferences->save($partialConfig);
383389

384-
$encodedConfig = json_encode([...$initialConfig, 'Console/Mode' => 'collapse']);
390+
$expected = [
391+
'CharEditing' => 'textarea',
392+
'TableNavigationLinksMode' => 'text',
393+
'Console/Mode' => 'collapse',
394+
'2fa' => ['backend' => 'application', 'settings' => ['secret' => 'thisisasecret']],
395+
];
396+
$encodedConfig = json_encode($expected);
385397

386398
$dummyDbi->addResult(
387399
<<<'SQL'
@@ -390,13 +402,54 @@ public function testSaveAndKeep2FA(): void
390402
[[$encodedConfig, 1767029179]],
391403
['config_data', 'ts'],
392404
);
405+
// phpcs:enable Generic.Files.LineLength.TooLong
393406

394407
// Check that 2fa is still present
395408
$resultConfig = $userPreferences->load()['config_data'];
396-
self::assertSame('thisisasecret', $resultConfig['2fa']['settings']['secret']);
397-
self::assertSame('collapse', $resultConfig['Console/Mode']);
409+
self::assertSame($expected, $resultConfig);
410+
398411
$dummyDbi->assertAllSelectsConsumed();
399-
// phpcs:enable Generic.Files.LineLength.TooLong
412+
}
413+
414+
public function testSaveAndKeep2FAWithSession(): void
415+
{
416+
$dbi = $this->createDatabaseInterface();
417+
$config = new Config();
418+
(new ReflectionProperty(Relation::class, 'cache'))->setValue(null, null);
419+
$relation = new Relation($dbi, $config);
420+
$clock = MockClock::from('2015-10-21T05:28:00-02:00');
421+
$userPreferences = new UserPreferences($dbi, $relation, new Template($config), $config, $clock);
422+
423+
unset($_SESSION['userconfig']);
424+
$initialConfig = [
425+
'CharEditing' => 'textarea',
426+
'2fa' => ['backend' => 'application', 'settings' => ['secret' => 'thisisasecret']],
427+
'RowActionLinks' => 'both',
428+
'TableNavigationLinksMode' => 'both',
429+
];
430+
$userPreferences->save($initialConfig);
431+
432+
/** @phpstan-ignore offsetAccess.notFound */
433+
self::assertSame(['db' => $initialConfig, 'ts' => 1445412480], $_SESSION['userconfig']);
434+
435+
$partialConfig = [
436+
'CharEditing' => 'textarea',
437+
'TableNavigationLinksMode' => 'text',
438+
'Console/Mode' => 'collapse',
439+
];
440+
$userPreferences->save($partialConfig);
441+
442+
$expected = [
443+
'db' => [
444+
'CharEditing' => 'textarea',
445+
'TableNavigationLinksMode' => 'text',
446+
'Console/Mode' => 'collapse',
447+
'2fa' => ['backend' => 'application', 'settings' => ['secret' => 'thisisasecret']],
448+
],
449+
'ts' => 1445412480,
450+
];
451+
/** @psalm-suppress DocblockTypeContradiction */
452+
self::assertSame($expected, $_SESSION['userconfig']);
400453
}
401454

402455
/**

0 commit comments

Comments
 (0)