From a14a54efd11a7423abcfaecb793b48ed268b8908 Mon Sep 17 00:00:00 2001
From: Andreas Wolf <aw@foundata.net>
Date: Wed, 25 Feb 2015 13:52:22 +0100
Subject: [PATCH] [BUGFIX] File must use MIME type from index record

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>
---
 .../core/Classes/Resource/AbstractFile.php    |  2 +-
 .../Tests/Unit/Resource/AbstractFileTest.php  | 35 +++++++++++++++----
 2 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/core/Classes/Resource/AbstractFile.php b/typo3/sysext/core/Classes/Resource/AbstractFile.php
index 453a03854760..38fc36b0d471 100644
--- a/typo3/sysext/core/Classes/Resource/AbstractFile.php
+++ b/typo3/sysext/core/Classes/Resource/AbstractFile.php
@@ -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')));
 	}
 
 	/**
diff --git a/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php b/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php
index b79e01003c4c..ea5c5de9ee56 100644
--- a/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php
+++ b/typo3/sysext/core/Tests/Unit/Resource/AbstractFileTest.php
@@ -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());
+	}
+
 }
-- 
GitLab