Skip to content

Commit 8c90d4f

Browse files
authored
Merge pull request #56919 from nextcloud/jtr/fix-mime-detection-tests
fix(files/type): preserve numeric keys (follow-up)
2 parents f791d91 + b2bc785 commit 8c90d4f

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

lib/private/Files/Type/Detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function loadCustomDefinitions(string $fileName, array $definitions): ar
9797
if (file_exists($this->customConfigDir . '/' . $fileName)) {
9898
$custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true);
9999
if (json_last_error() === JSON_ERROR_NONE) {
100-
$definitions = array_merge($definitions, $custom);
100+
$definitions = array_replace($definitions, $custom);
101101
} else {
102102
$this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg());
103103
}

tests/lib/Files/Type/DetectionTest.php

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,72 @@ public static function dataMimeTypeCustom(): array {
117117
*/
118118
#[\PHPUnit\Framework\Attributes\DataProvider('dataMimeTypeCustom')]
119119
public function testDetectMimeTypeCustom(string $ext, string $mime): void {
120-
$confDir = sys_get_temp_dir();
121-
file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([]));
122-
123-
/** @var IURLGenerator $urlGenerator */
124-
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
125-
->disableOriginalConstructor()
126-
->getMock();
127-
128-
/** @var LoggerInterface $logger */
129-
$logger = $this->createMock(LoggerInterface::class);
130-
131-
// Create new mapping file
132-
file_put_contents($confDir . '/mimetypemapping.dist.json', json_encode([$ext => [$mime]]));
120+
$tmpDir = sys_get_temp_dir() . '/nc-detect-' . uniqid('', true);
121+
mkdir($tmpDir, 0700);
122+
123+
try {
124+
// Create a default / shipped mapping file (dist)
125+
file_put_contents($tmpDir . '/mimetypemapping.dist.json', json_encode([]));
126+
127+
// Create new custom mapping file that should be picked up by Detection
128+
file_put_contents($tmpDir . '/mimetypemapping.json', json_encode([$ext => [$mime]]));
129+
130+
/** @var IURLGenerator $urlGenerator */
131+
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
132+
->disableOriginalConstructor()
133+
->getMock();
134+
135+
/** @var LoggerInterface $logger */
136+
$logger = $this->createMock(LoggerInterface::class);
137+
138+
$detection = new Detection($urlGenerator, $logger, $tmpDir, $tmpDir);
139+
$mappings = $detection->getAllMappings();
140+
141+
$this->assertArrayHasKey($ext, $mappings);
142+
$this->assertEquals($mime, $detection->detectPath('foo.' . $ext));
143+
} finally {
144+
// cleanup
145+
@unlink($tmpDir . '/mimetypemapping.json');
146+
@unlink($tmpDir . '/mimetypemapping.dist.json');
147+
@rmdir($tmpDir);
148+
}
149+
}
133150

134-
$detection = new Detection($urlGenerator, $logger, $confDir, $confDir);
135-
$mappings = $detection->getAllMappings();
136-
$this->assertArrayHasKey($ext, $mappings);
137-
$this->assertEquals($mime, $detection->detectPath('foo.' . $ext));
151+
public function testDetectMimeTypePreservesLeadingZeroKeys(): void {
152+
$tmpDir = sys_get_temp_dir() . '/nc-detect-' . uniqid();
153+
mkdir($tmpDir, 0700);
154+
try {
155+
// Create a default / shipped mapping file (dist)
156+
file_put_contents($tmpDir . '/mimetypemapping.dist.json', json_encode([]));
157+
158+
// Create new custom mapping file with potentially problematic keys
159+
$mappings = [
160+
'001' => ['application/x-zeroone', null],
161+
'1' => ['application/x-one', null],
162+
];
163+
file_put_contents($tmpDir . '/mimetypemapping.json', json_encode($mappings));
164+
165+
/** @var IURLGenerator $urlGenerator */
166+
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)
167+
->disableOriginalConstructor()
168+
->getMock();
169+
170+
/** @var LoggerInterface $logger */
171+
$logger = $this->createMock(LoggerInterface::class);
172+
173+
$detection = new Detection($urlGenerator, $logger, $tmpDir, $tmpDir);
174+
$mappings = $detection->getAllMappings();
175+
176+
$this->assertArrayHasKey('001', $mappings, 'Expected mapping to contain key "001"');
177+
$this->assertArrayHasKey('1', $mappings, 'Expected mapping to contain key "1"');
178+
179+
$this->assertEquals('application/x-zeroone', $detection->detectPath('foo.001'));
180+
$this->assertEquals('application/x-one', $detection->detectPath('foo.1'));
181+
} finally {
182+
@unlink($tmpDir . '/mimetypemapping.json');
183+
@unlink($tmpDir . '/mimetypemapping.dist.json');
184+
@rmdir($tmpDir);
185+
}
138186
}
139187

140188
public static function dataGetSecureMimeType(): array {

0 commit comments

Comments
 (0)