test(files/type): leading zero extension preservation

- adds a focused DetectionTest that ensures numeric-looking keys (e.g. "001") are preserved
- refactors filesystem tests to use an isolated temp dir with proper cleanup

Signed-off-by: Josh <josh.t.richards@gmail.com>
This commit is contained in:
Josh 2025-12-08 10:09:05 -05:00
parent f967134f58
commit 50a94a35ee

View file

@ -117,24 +117,72 @@ class DetectionTest extends \Test\TestCase {
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataMimeTypeCustom')]
public function testDetectMimeTypeCustom(string $ext, string $mime): void {
$confDir = sys_get_temp_dir();
file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([]));
$tmpDir = sys_get_temp_dir() . '/nc-detect-' . uniqid('', true);
mkdir($tmpDir, 0700);
/** @var IURLGenerator $urlGenerator */
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
->disableOriginalConstructor()
->getMock();
try {
// Create a default / shipped mapping file (dist)
file_put_contents($tmpDir . '/mimetypemapping.dist.json', json_encode([]));
// Create new custom mapping file that should be picked up by Detection
file_put_contents($tmpDir . '/mimetypemapping.json', json_encode([$ext => [$mime]]));
/** @var LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);
/** @var IURLGenerator $urlGenerator */
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
->disableOriginalConstructor()
->getMock();
// Create new mapping file
file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([$ext => [$mime]]));
/** @var LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);
$detection = new Detection($urlGenerator, $logger, $confDir, $confDir);
$mappings = $detection->getAllMappings();
$this->assertArrayHasKey($ext, $mappings);
$this->assertEquals($mime, $detection->detectPath('foo.' . $ext));
$detection = new Detection($urlGenerator, $logger, $tmpDir, $tmpDir);
$mappings = $detection->getAllMappings();
$this->assertArrayHasKey($ext, $mappings);
$this->assertEquals($mime, $detection->detectPath('foo.' . $ext));
} finally {
// cleanup
@unlink($tmpDir . '/mimetypemapping.json');
@unlink($tmpDir . '/mimetypemapping.dist.json');
@rmdir($tmpDir);
}
}
public function testDetectMimeTypePreservesLeadingZeroKeys(): void {
$tmpDir = sys_get_temp_dir() . '/nc-detect-' . uniqid();
mkdir($tmpDir, 0700);
try {
// Create a default / shipped mapping file (dist)
file_put_contents($tmpDir . '/mimetypemapping.dist.json', json_encode([]));
// Create new custom mapping file with potentially problematic keys
$mappings = [
'001' => ['application/x-zeroone', null],
'1' => ['application/x-one', null],
];
file_put_contents($tmpDir . '/mimetypemapping.json', json_encode($mappings));
/** @var IURLGenerator $urlGenerator */
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
->disableOriginalConstructor()
->getMock();
/** @var LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);
$detection = new Detection($urlGenerator, $logger, $tmpDir, $tmpDir);
$mappings = $detection->getAllMappings();
$this->assertArrayHasKey('001', $mappings, 'Expected mapping to contain key "001"');
$this->assertArrayHasKey('1', $mappings, 'Expected mapping to contain key "1"');
$this->assertEquals('application/x-zeroone', $detection->detectPath('foo.001'));
$this->assertEquals('application/x-one', $detection->detectPath('foo.1'));
} finally {
@unlink($tmpDir . '/mimetypemapping.json');
@unlink($tmpDir . '/mimetypemapping.dist.json');
@rmdir($tmpDir);
}
}
public static function dataGetSecureMimeType(): array {