Visitar URL original
Merge branch 'QA_5_2' · phpmyadmin/phpmyadmin@8a6f2ea · GitHub
Skip to content

Commit 8a6f2ea

Browse files
committed
Merge branch 'QA_5_2'
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
2 parents 72d933a + 298797c commit 8a6f2ea

2 files changed

Lines changed: 48 additions & 53 deletions

File tree

‎src/Core.php‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,19 @@ public static function getNoCacheHeaders(): array
250250
}
251251

252252
/**
253-
* Sends header indicating file download.
254-
*
255-
* @param string $filename Filename to include in headers if empty,
256-
* none Content-Disposition header will be sent.
253+
* @param string $filename Filename to include in headers if empty, none Content-Disposition header will be sent.
257254
* @param string $mimetype MIME type to include in headers.
258255
* @param int $length Length of content (optional)
259256
* @param bool $noCache Whether to include no-caching headers.
257+
*
258+
* @return array<string, string>
260259
*/
261-
public static function downloadHeader(
260+
public static function getDownloadHeaders(
262261
string $filename,
263262
string $mimetype,
264263
int $length = 0,
265264
bool $noCache = true,
266-
): void {
265+
): array {
267266
$headers = [];
268267

269268
if ($noCache) {
@@ -279,17 +278,36 @@ public static function downloadHeader(
279278

280279
$headers['Content-Type'] = $mimetype;
281280

282-
// The default output in PMA uses gzip,
283-
// so if we want to output uncompressed file, we should reset the encoding.
284-
// See PHP bug https://github.com/php/php-src/issues/8218
285-
header_remove('Content-Encoding');
286-
287281
$headers['Content-Transfer-Encoding'] = 'binary';
288282

289283
if ($length > 0) {
290284
$headers['Content-Length'] = (string) $length;
291285
}
292286

287+
return $headers;
288+
}
289+
290+
/**
291+
* Sends header indicating file download.
292+
*
293+
* @param string $filename Filename to include in headers if empty, none Content-Disposition header will be sent.
294+
* @param string $mimetype MIME type to include in headers.
295+
* @param int $length Length of content (optional)
296+
* @param bool $noCache Whether to include no-caching headers.
297+
*/
298+
public static function downloadHeader(
299+
string $filename,
300+
string $mimetype,
301+
int $length = 0,
302+
bool $noCache = true,
303+
): void {
304+
$headers = self::getDownloadHeaders($filename, $mimetype, $length, $noCache);
305+
306+
// The default output in PMA uses gzip,
307+
// so if we want to output uncompressed file, we should reset the encoding.
308+
// See PHP bug https://github.com/php/php-src/issues/8218
309+
header_remove('Content-Encoding');
310+
293311
foreach ($headers as $name => $value) {
294312
header(sprintf('%s: %s', $name, $value));
295313
}

‎tests/unit/CoreTest.php‎

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@
1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\Attributes\Group;
17-
use PHPUnit\Framework\Attributes\PreserveGlobalState;
18-
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
19-
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
2017
use stdClass;
2118

2219
use function _pgettext;
2320
use function hash;
24-
use function header;
2521
use function putenv;
2622
use function serialize;
2723
use function str_repeat;
@@ -660,50 +656,31 @@ public static function providerForTestPopulateRequestWithEncryptedQueryParamsWit
660656
return [[[], []], [['eq' => []], []], [['eq' => ''], []], [['eq' => 'invalid'], []]];
661657
}
662658

663-
#[PreserveGlobalState(false)]
664-
#[Group('ext-xdebug')]
665-
#[RequiresPhpExtension('xdebug')]
666-
#[RunInSeparateProcess]
667-
public function testDownloadHeader(): void
659+
public function testGetDownloadHeaders(): void
668660
{
669-
header('Cache-Control: private, max-age=10800');
661+
$headersList = Core::getDownloadHeaders('test.sql', 'text/x-sql', 100, false);
670662

671-
Core::downloadHeader('test.sql', 'text/x-sql', 100, false);
672-
673-
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
674-
$headersList = \xdebug_get_headers();
675-
// phpcs:enable
676-
677-
self::assertContains('Cache-Control: private, max-age=10800', $headersList);
678-
self::assertContains('Content-Description: File Transfer', $headersList);
679-
self::assertContains('Content-Disposition: attachment; filename="test.sql"', $headersList);
680-
self::assertContains('Content-type: text/x-sql;charset=UTF-8', $headersList);
681-
self::assertContains('Content-Transfer-Encoding: binary', $headersList);
682-
self::assertContains('Content-Length: 100', $headersList);
683-
self::assertNotContains('Content-Encoding: gzip', $headersList);
663+
$expected = [
664+
'Content-Description' => 'File Transfer',
665+
'Content-Disposition' => 'attachment; filename="test.sql"',
666+
'Content-Type' => 'text/x-sql',
667+
'Content-Transfer-Encoding' => 'binary',
668+
'Content-Length' => '100',
669+
];
670+
self::assertSame($expected, $headersList);
684671
}
685672

686-
#[PreserveGlobalState(false)]
687-
#[Group('ext-xdebug')]
688-
#[RequiresPhpExtension('xdebug')]
689-
#[RunInSeparateProcess]
690-
public function testDownloadHeader2(): void
673+
public function testGetDownloadHeaders2(): void
691674
{
692-
header('Cache-Control: private, max-age=10800');
675+
$headersList = Core::getDownloadHeaders('test.sql.gz', 'application/x-gzip', 0, false);
693676

694-
Core::downloadHeader('test.sql.gz', 'application/x-gzip', 0, false);
695-
696-
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
697-
$headersList = \xdebug_get_headers();
698-
// phpcs:enable
699-
700-
self::assertContains('Cache-Control: private, max-age=10800', $headersList);
701-
self::assertContains('Content-Description: File Transfer', $headersList);
702-
self::assertContains('Content-Disposition: attachment; filename="test.sql.gz"', $headersList);
703-
self::assertContains('Content-Type: application/x-gzip', $headersList);
704-
self::assertNotContains('Content-Encoding: gzip', $headersList);
705-
self::assertContains('Content-Transfer-Encoding: binary', $headersList);
706-
self::assertNotContains('Content-Length: 0', $headersList);
677+
$expected = [
678+
'Content-Description' => 'File Transfer',
679+
'Content-Disposition' => 'attachment; filename="test.sql.gz"',
680+
'Content-Type' => 'application/x-gzip',
681+
'Content-Transfer-Encoding' => 'binary',
682+
];
683+
self::assertSame($expected, $headersList);
707684
}
708685

709686
public function testGetEnv(): void

0 commit comments

Comments
 (0)