Skip to content

Commit eabaf49

Browse files
Merge pull request #18877 from MauricioFauth/changelog-controller
Add unit test for ChangeLogController
2 parents 244094d + e41f311 commit eabaf49

9 files changed

Lines changed: 145 additions & 35 deletions

File tree

app/services_controllers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
],
5959
ChangeLogController::class => [
6060
'class' => ChangeLogController::class,
61-
'arguments' => ['$response' => '@response', '$template' => '@template'],
61+
'arguments' => ['$response' => '@response', '$template' => '@template', '$config' => '@config'],
6262
],
6363
CheckRelationsController::class => [
6464
'class' => CheckRelationsController::class,

phpstan-baseline.neon

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ parameters:
390390
count: 7
391391
path: src/Config.php
392392

393+
-
394+
message: "#^Method PhpMyAdmin\\\\Config\\:\\:getChangeLogFilePath\\(\\) should return string but returns mixed\\.$#"
395+
count: 1
396+
path: src/Config.php
397+
393398
-
394399
message: "#^Method PhpMyAdmin\\\\Config\\:\\:getUploadTempDir\\(\\) should return string\\|null but returns string\\|false\\.$#"
395400
count: 1
@@ -1465,31 +1470,6 @@ parameters:
14651470
count: 1
14661471
path: src/Controllers/AbstractController.php
14671472

1468-
-
1469-
message: "#^Parameter \\#1 \\$filename of function file_get_contents expects string, mixed given\\.$#"
1470-
count: 1
1471-
path: src/Controllers/ChangeLogController.php
1472-
1473-
-
1474-
message: "#^Parameter \\#1 \\$filename of function is_readable expects string, mixed given\\.$#"
1475-
count: 1
1476-
path: src/Controllers/ChangeLogController.php
1477-
1478-
-
1479-
message: "#^Parameter \\#1 \\$filename of function readgzfile expects string, mixed given\\.$#"
1480-
count: 1
1481-
path: src/Controllers/ChangeLogController.php
1482-
1483-
-
1484-
message: "#^Parameter \\#1 \\$haystack of function str_ends_with expects string, mixed given\\.$#"
1485-
count: 1
1486-
path: src/Controllers/ChangeLogController.php
1487-
1488-
-
1489-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function printf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
1490-
count: 1
1491-
path: src/Controllers/ChangeLogController.php
1492-
14931473
-
14941474
message: "#^Parameter \\#2 \\$table of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:getColumnsNotInCentralList\\(\\) expects string, mixed given\\.$#"
14951475
count: 1

psalm-baseline.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,12 @@
851851
</MixedArgumentTypeCoercion>
852852
</file>
853853
<file src="src/Controllers/ChangeLogController.php">
854-
<PossiblyUnusedParam>
855-
<code>$request</code>
856-
</PossiblyUnusedParam>
857854
<UnusedFunctionCall>
858855
<code>readgzfile</code>
859856
</UnusedFunctionCall>
857+
<UnusedParam>
858+
<code>$request</code>
859+
</UnusedParam>
860860
</file>
861861
<file src="src/Controllers/CheckRelationsController.php">
862862
<DeprecatedMethod>

src/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use function time;
6262
use function trim;
6363

64+
use const CHANGELOG_FILE;
6465
use const DIRECTORY_SEPARATOR;
6566
use const PHP_OS;
6667
use const PHP_URL_PATH;
@@ -1208,4 +1209,9 @@ public function hasSelectedServer(): bool
12081209
{
12091210
return $this->hasSelectedServer;
12101211
}
1212+
1213+
public function getChangeLogFilePath(): string
1214+
{
1215+
return CHANGELOG_FILE;
1216+
}
12111217
}

src/Controllers/ChangeLogController.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
2-
/**
3-
* Simple script to set correct charset for changelog
4-
*/
52

63
declare(strict_types=1);
74

85
namespace PhpMyAdmin\Controllers;
96

7+
use PhpMyAdmin\Config;
108
use PhpMyAdmin\Http\ServerRequest;
9+
use PhpMyAdmin\ResponseRenderer;
10+
use PhpMyAdmin\Template;
1111
use PhpMyAdmin\Url;
1212

1313
use function __;
@@ -22,14 +22,19 @@
2222
use function readgzfile;
2323
use function str_ends_with;
2424

25-
class ChangeLogController extends AbstractController
25+
final class ChangeLogController extends AbstractController
2626
{
27+
public function __construct(ResponseRenderer $response, Template $template, private readonly Config $config)
28+
{
29+
parent::__construct($response, $template);
30+
}
31+
2732
public function __invoke(ServerRequest $request): void
2833
{
2934
$this->response->disable();
3035
$this->response->getHeader()->sendHttpHeaders();
3136

32-
$filename = CHANGELOG_FILE;
37+
$filename = $this->config->getChangeLogFilePath();
3338

3439
/**
3540
* Read changelog.
@@ -47,7 +52,7 @@ public function __invoke(ServerRequest $request): void
4752
return;
4853
}
4954

50-
// Test if the if is in a compressed format
55+
// Test if the file is in a compressed format
5156
if (str_ends_with($filename, '.gz')) {
5257
ob_start();
5358
readgzfile($filename);

tests/classes/ConfigTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use function tempnam;
3737
use function unlink;
3838

39+
use const CHANGELOG_FILE;
3940
use const CONFIG_FILE;
4041
use const DIRECTORY_SEPARATOR;
4142
use const INFO_MODULES;
@@ -926,4 +927,9 @@ public function testVendorConfigFile(): void
926927
self::assertDirectoryExists($vendorConfig['cacheDir']);
927928
self::assertIsString($vendorConfig['versionSuffix']);
928929
}
930+
931+
public function testGetChangeLogFilePath(): void
932+
{
933+
self::assertSame(CHANGELOG_FILE, (new Config())->getChangeLogFilePath());
934+
}
929935
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Tests\Controllers;
6+
7+
use PhpMyAdmin\Config;
8+
use PhpMyAdmin\Controllers\ChangeLogController;
9+
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
10+
use PhpMyAdmin\Template;
11+
use PhpMyAdmin\Tests\AbstractTestCase;
12+
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
15+
16+
use const TEST_PATH;
17+
18+
#[CoversClass(ChangeLogController::class)]
19+
final class ChangeLogControllerTest extends AbstractTestCase
20+
{
21+
public function testWithValidFile(): void
22+
{
23+
$config = self::createStub(Config::class);
24+
$config->method('getChangeLogFilePath')->willReturn(TEST_PATH . 'tests/test_data/changelog/ChangeLog');
25+
26+
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/');
27+
28+
$responseRenderer = new ResponseRenderer();
29+
$template = new Template();
30+
$controller = new ChangeLogController($responseRenderer, $template, $config);
31+
$controller($request);
32+
33+
self::assertTrue($responseRenderer->isDisabled());
34+
$response = $responseRenderer->getResponse();
35+
self::assertSame(['text/html; charset=utf-8'], $response->getHeader('Content-Type'));
36+
37+
// phpcs:disable Generic.Files.LineLength.TooLong
38+
$changelog = <<<'HTML'
39+
phpMyAdmin - ChangeLog
40+
======================
41+
42+
5.2.2 (not yet released)
43+
- <a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://github.com/phpmyadmin/phpmyadmin/issues/17522">issue #17522</a> Fix case where the routes cache file is invalid
44+
- issue Upgrade slim/psr7 to 1.4.1 for <a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://www.cve.org/CVERecord?id=CVE-2023-30536">CVE-2023-30536</a> - GHSA-q2qj-628g-vhfw
45+
46+
5.2.1 (2023-02-07)
47+
- <a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://github.com/phpmyadmin/phpmyadmin/issues/16418">issue #16418</a> Fix <a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://docs.phpmyadmin.net/en/latest/faq.html#faq1-44">FAQ 1.44</a> about manually removing vendor folders
48+
- issue [security] Fix an XSS attack through the drag-and-drop upload feature (<a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://www.phpmyadmin.net/security/PMASA-2023-01/">PMASA-2023-01</a>)
49+
50+
--- Older ChangeLogs can be found on our project website ---
51+
<a target="_blank" rel="noopener noreferrer" href="index.php?route=/url&lang=en&url=https://www.phpmyadmin.net/old-stuff/ChangeLogs/">https://www.phpmyadmin.net/old-stuff/ChangeLogs/</a>
52+
53+
HTML;
54+
// phpcs:enable
55+
$expected = $template->render('changelog', ['changelog' => $changelog]);
56+
57+
self::assertSame($expected, $responseRenderer->getHTMLResult());
58+
}
59+
60+
#[RequiresPhpExtension('zlib')]
61+
public function testWithCompressedFile(): void
62+
{
63+
$config = self::createStub(Config::class);
64+
$config->method('getChangeLogFilePath')->willReturn(TEST_PATH . 'tests/test_data/changelog/ChangeLog.gz');
65+
66+
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/');
67+
68+
$responseRenderer = new ResponseRenderer();
69+
$controller = new ChangeLogController($responseRenderer, new Template(), $config);
70+
$controller($request);
71+
72+
self::assertStringContainsString(
73+
'- <a target="_blank" rel="noopener noreferrer"'
74+
. ' href="index.php?route=/url&lang=en&url=https://github.com/phpmyadmin/phpmyadmin/issues/16418">'
75+
. 'issue #16418</a> Fix <a target="_blank" rel="noopener noreferrer"'
76+
. ' href="index.php?route=/url&lang=en&url=https://docs.phpmyadmin.net/en/latest/faq.html#faq1-44">'
77+
. 'FAQ 1.44</a> about manually removing vendor folders',
78+
$responseRenderer->getHTMLResult(),
79+
);
80+
}
81+
82+
public function testWithInvalidFile(): void
83+
{
84+
$config = self::createStub(Config::class);
85+
$config->method('getChangeLogFilePath')->willReturn(TEST_PATH . 'tests/test_data/changelog/InvalidChangeLog');
86+
87+
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/');
88+
89+
$responseRenderer = new ResponseRenderer();
90+
$controller = new ChangeLogController($responseRenderer, new Template(), $config);
91+
$controller($request);
92+
93+
self::assertSame('', $responseRenderer->getHTMLResult());
94+
self::assertSame(
95+
'The ' . TEST_PATH . 'tests/test_data/changelog/InvalidChangeLog file is not available on this system,'
96+
. ' please visit <a href="https://www.phpmyadmin.net/">phpmyadmin.net</a> for more information.',
97+
self::getActualOutputForAssertion(),
98+
);
99+
}
100+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
phpMyAdmin - ChangeLog
2+
======================
3+
4+
5.2.2 (not yet released)
5+
- issue #17522 Fix case where the routes cache file is invalid
6+
- issue Upgrade slim/psr7 to 1.4.1 for CVE-2023-30536 - GHSA-q2qj-628g-vhfw
7+
8+
5.2.1 (2023-02-07)
9+
- issue #16418 Fix FAQ 1.44 about manually removing vendor folders
10+
- issue [security] Fix an XSS attack through the drag-and-drop upload feature (PMASA-2023-01)
11+
12+
--- Older ChangeLogs can be found on our project website ---
13+
https://www.phpmyadmin.net/old-stuff/ChangeLogs/
382 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)