[BUGFIX] File must use MIME type from index record 14/37214/2
authorAndreas Wolf <aw@foundata.net>
Wed, 25 Feb 2015 12:52:22 +0000 (13:52 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Sat, 28 Feb 2015 16:39:40 +0000 (17:39 +0100)
The MIME type is stored in a field "mimetype" in the drivers, while the
field is called "mime_type" in the database. As the file object deals
with both when retrieving the type, it must respect this mismatch.

Change-Id: I06882c4d77e38284a48f7f7d7527bfc1c535edf3
Resolves: #65335
Releases: master, 6.2
Reviewed-on: http://review.typo3.org/37214
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Classes/Resource/AbstractFile.php
typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php

index 453a038..38fc36b 100644 (file)
@@ -253,7 +253,7 @@ abstract class AbstractFile implements FileInterface {
         * @return array file information
         */
        public function getMimeType() {
-               return $this->properties['mimetype'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), array('mimetype')));
+               return $this->properties['mime_type'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), array('mimetype')));
        }
 
        /**
index b79e010..ea5c5de 100644 (file)
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Core\Tests\Unit\Resource;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Resource\AbstractFile;
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Resource\ResourceStorage;
+
 /**
  * Testcase for the abstract file class of the TYPO3 FAL
  *
@@ -28,21 +32,40 @@ class AbstractFileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $parentIdentifier = '/parent/';
                $currentIdentifier = '/parent/current/';
 
-               $mockedStorageForParent = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array(), array(), '', FALSE);
+               /** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $mockedStorageForParent */
+               $mockedStorageForParent = $this->getMock(ResourceStorage::class, array(), array(), '', FALSE);
 
-               /** @var \TYPO3\CMS\Core\Resource\AbstractFile $parentFolderFixture */
-               $parentFolderFixture = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\AbstractFile::class);
+               /** @var AbstractFile $parentFolderFixture */
+               $parentFolderFixture = $this->getMockForAbstractClass(AbstractFile::class);
                $parentFolderFixture->setIdentifier($parentIdentifier)->setStorage($mockedStorageForParent);
 
-               $mockedStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, array('getFolderIdentifierFromFileIdentifier', 'getFolder'), array(), '', FALSE);
+               /** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $mockedStorage */
+               $mockedStorage = $this->getMock(ResourceStorage::class, array('getFolderIdentifierFromFileIdentifier', 'getFolder'), array(), '', FALSE);
                $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
                $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
 
-               /** @var \TYPO3\CMS\Core\Resource\AbstractFile $currentFolderFixture */
-               $currentFolderFixture = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\AbstractFile::class);
+               /** @var AbstractFile $currentFolderFixture */
+               $currentFolderFixture = $this->getMockForAbstractClass(AbstractFile::class);
                $currentFolderFixture->setIdentifier($currentIdentifier)->setStorage($mockedStorage);
 
                $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
        }
 
+       /**
+        * This test accounts for an inconsistency in the Storage–Driver interface of FAL: The driver returns the MIME
+        * type in a field "mimetype", while the file object and the database table use mime_type.
+        * The test is placed in the test case for AbstractFile because the broken functionality resides there, though
+        * it is only triggered when constructing a File instance with an index record.
+        *
+        * @test
+        */
+       public function storageIsNotAskedForMimeTypeForPersistedRecord() {
+               /** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $mockedStorage */
+               $mockedStorage = $this->getMockBuilder(ResourceStorage::class)->disableOriginalConstructor()->getMock();
+               $mockedStorage->expects($this->never())->method('getFileInfoByIdentifier')->with('/foo', 'mimetype');
+               $subject = new File(array('identifier' => '/foo', 'mime_type' => 'my/mime-type'), $mockedStorage);
+
+               $this->assertEquals('my/mime-type', $subject->getMimeType());
+       }
+
 }