[TASK] Enable mock of ResourceFactory in ContentObjectRenderer 21/24821/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Tue, 15 Oct 2013 20:48:59 +0000 (22:48 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 15 Oct 2013 21:19:42 +0000 (23:19 +0200)
ext:frontend ContentObjectRenderer calls ResourceFactory::getInstance
at various places. This static call can not be mocked and is a hard
dependency to FAL.
The patch encapsulates the static call in a method and uses this
to mock instances of the ResourceFactory within some tests to have
controlled return values.
As a side effect one test does not fail anymore if only the
ext:frontend tests are executed and not the whole test suite. The
according hook-test was renamed and now tests the method that calls
the hook directly.

Change-Id: I0ee75462b7e7f25f70c6c8fb132045084b9ac537
Resolves: #52868
Resolves: #52643
Releases: 6.2
Reviewed-on: https://review.typo3.org/24821
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 3aedad2..01f49d1 100644 (file)
@@ -5301,19 +5301,19 @@ class ContentObjectRenderer {
 
                                        if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($file)) {
                                                if (!empty($fileArray['treatIdAsReference'])) {
-                                                       $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($file)->getOriginalFile();
+                                                       $fileObject = $this->getResourceFactory()->getFileReferenceObject($file)->getOriginalFile();
                                                } else {
-                                                       $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileObject($file);
+                                                       $fileObject = $this->getResourceFactory()->getFileObject($file);
                                                }
                                        } elseif (preg_match('/^(0|[1-9][0-9]*):/', $file)) { // combined identifier
-                                               $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($file);
+                                               $fileObject = $this->getResourceFactory()->retrieveFileOrFolderObject($file);
                                        } else {
                                                if (isset($importedFile) && !empty($importedFile) && !empty($fileArray['import'])) {
                                                        $file = $fileArray['import'] . $file;
                                                }
                                                // clean ../ sections of the path and resolve to proper string. This is necessary for the Tx_File_BackwardsCompatibility_TslibContentAdapter to work.
                                                $file = GeneralUtility::resolveBackPath($file);
-                                               $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($file);
+                                               $fileObject = $this->getResourceFactory()->retrieveFileOrFolderObject($file);
                                        }
                                } catch(\TYPO3\CMS\Core\Resource\Exception $exception) {
                                        /** @var \TYPO3\CMS\Core\Log\Logger $logger */
@@ -5804,7 +5804,7 @@ class ContentObjectRenderer {
                        // Resolve FAL-api "file:UID-of-sys_file-record" and "file:combined-identifier"
                        if ($linkHandlerKeyword === 'file') {
                                try {
-                                       $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($linkHandlerValue);
+                                       $fileOrFolderObject = $this->getResourceFactory()->retrieveFileOrFolderObject($linkHandlerValue);
                                        // Link to a folder or file
                                        if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\ResourceInterface) {
                                                $link_paramA[0] = $fileOrFolderObject->getPublicUrl();
@@ -8072,4 +8072,12 @@ class ContentObjectRenderer {
                }
        }
 
-}
+       /**
+        * Get instance of FAL resource factory
+        *
+        * @return \TYPO3\CMS\Core\Resource\ResourceFactory
+        */
+       protected function getResourceFactory() {
+               return \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
+       }
+}
\ No newline at end of file
index 5ccb23b..8bdd823 100644 (file)
@@ -62,7 +62,10 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $GLOBALS['TSFE']->csConvObj = new \TYPO3\CMS\Core\Charset\CharsetConverter();
                $GLOBALS['TSFE']->renderCharset = 'utf-8';
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['TYPO3\\CMS\\Core\\Charset\\CharsetConverter_utils'] = 'mbstring';
-               $this->cObj = $this->getAccessibleMock('\\TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer', array('dummy'));
+               $this->cObj = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer',
+                       array('getResourceFactory')
+               );
                $this->cObj->start(array(), 'tt_content');
        }
 
@@ -88,14 +91,16 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
-       public function getImgResourceHookGetsCalled() {
+       public function getImgResourceCallsGetImgResourcePostProcessHook() {
                $this->template
                        ->expects($this->atLeastOnce())
                        ->method('getFileName')
                        ->with('typo3/clear.gif')
                        ->will($this->returnValue('typo3/clear.gif'));
-               // Reset some global variable to not trigger unrelated method code parts
-               $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] = '';
+
+               $resourceFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array(), array(), '', FALSE);
+               $this->cObj->expects($this->any())->method('getResourceFactory')->will($this->returnValue($resourceFactory));
+
                $className = uniqid('tx_coretest');
                $getImgResourceHookMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectGetImageResourceHookInterface', array('getImgResourcePostProcess'), array(), $className);
                $getImgResourceHookMock
@@ -104,7 +109,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ->will($this->returnCallback(array($this, 'isGetImgResourceHookCalledCallback')));
                $getImgResourceHookObjects = array($getImgResourceHookMock);
                $this->cObj->_setRef('getImgResourceHookObjects', $getImgResourceHookObjects);
-               $this->cObj->IMAGE(array('file' => 'typo3/clear.gif'));
+               $this->cObj->getImgResource('typo3/clear.gif', array());
        }
 
        /**
@@ -2356,6 +2361,9 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ->method('getImgResource')
                        ->will($this->returnValue(array(100, 100, NULL, 'bar-file.jpg')));
 
+               $resourceFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array(), array(), '', FALSE);
+               $this->cObj->expects($this->any())->method('getResourceFactory')->will($this->returnValue($resourceFactory));
+
                $className = uniqid('tx_coretest_getImageSourceCollectionHookCalled');
                $getImageSourceCollectionHookMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectOneSourceCollectionHookInterface', array('getOneSourceCollection'), array(), $className);
                $GLOBALS['T3_VAR']['getUserObj'][$className] = $getImageSourceCollectionHookMock;