@@ -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