[TASK] Cover getContentObject related methods by unit tests. 67/52767/2
authorElmar Hinz <t3elmar@gmail.com>
Thu, 6 Apr 2017 18:11:52 +0000 (20:11 +0200)
committerTymoteusz Motylewski <t.motylewski@gmail.com>
Thu, 11 May 2017 12:40:05 +0000 (14:40 +0200)
Cover getContentObject related methods of ContentObjectRenderer
by unit tests.

Resolves: #80737
Releases: master, 8.7, 7.6
Change-Id: Ida957cd18c35d10cfc41f954ffdc15e0a04c03a8
Reviewed-on: https://review.typo3.org/52767
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index f49683f..1671dee 100644 (file)
@@ -36,6 +36,7 @@ use TYPO3\CMS\Frontend\ContentObject\ContentObjectOneSourceCollectionHookInterfa
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectStdWrapHookInterface;
 use TYPO3\CMS\Frontend\ContentObject\EditPanelContentObject;
+use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
 use TYPO3\CMS\Frontend\ContentObject\FileContentObject;
 use TYPO3\CMS\Frontend\ContentObject\FilesContentObject;
 use TYPO3\CMS\Frontend\ContentObject\FluidTemplateContentObject;
@@ -239,10 +240,94 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     }
 
     //////////////////////////////////////
-    // Tests concerning getContentObject
+    // Tests related to getContentObject
     //////////////////////////////////////
 
-    public function getContentObjectValidContentObjectsDataProvider()
+    /**
+     * Show registration of a class for a TypoScript object name and getting
+     * the registered content object is working.
+     *
+     * Prove is done by successfully creating an object based on the mapping.
+     * Note two conditions in contrast to other tests, where the creation
+     * fails.
+     *
+     * 1. The type must be of AbstractContentObject.
+     * 2. Registration can only be done by public methods.
+     *
+     * @test
+     */
+    public function canRegisterAContentObjectClassForATypoScriptName()
+    {
+        $className = TextContentObject::class;
+        $contentObjectName = 'TEST_TEXT';
+        $this->subject->registerContentObjectClass($className,
+            $contentObjectName);
+        $object = $this->subject->getContentObject($contentObjectName);
+        $this->assertInstanceOf($className, $object);
+    }
+
+    /**
+     * Show that setting of the class map and getting a registered content
+     * object is working.
+     *
+     * @see ContentObjectRendererTest::canRegisterAContentObjectClassForATypoScriptName
+     * @test
+     */
+    public function canSetTheContentObjectClassMapAndGetARegisteredContentObject()
+    {
+        $className = TextContentObject::class;
+        $contentObjectName = 'TEST_TEXT';
+        $classMap = [$contentObjectName => $className];
+        $this->subject->setContentObjectClassMap($classMap);
+        $object = $this->subject->getContentObject($contentObjectName);
+        $this->assertInstanceOf($className, $object);
+    }
+
+    /**
+     * Show that the map is not set as an externally accessible reference.
+     *
+     * Prove is done by missing success when trying to use it this way.
+     *
+     * @see ContentObjectRendererTest::canRegisterAContentObjectClassForATypoScriptName
+     * @test
+     */
+    public function canNotAccessInternalContentObjectMapByReference()
+    {
+        $className = TextContentObject::class;
+        $contentObjectName = 'TEST_TEXT';
+        $classMap = [];
+        $this->subject->setContentObjectClassMap($classMap);
+        $classMap[$contentObjectName] = $className;
+        $object = $this->subject->getContentObject($contentObjectName);
+        $this->assertNull($object);
+    }
+
+    /**
+     * @see ContentObjectRendererTest::canRegisterAContentObjectClassForATypoScriptName
+     * @test
+     */
+    public function willReturnNullForUnregisteredObject()
+    {
+        $object = $this->subject->getContentObject('FOO');
+        $this->assertNull($object);
+    }
+
+    /**
+     * @see ContentObjectRendererTest::canRegisterAContentObjectClassForATypoScriptName
+     * @test
+     */
+    public function willThrowAnExceptionForARegisteredNonContentObject()
+    {
+        $this->expectException(ContentRenderingException::class);
+        $this->subject->registerContentObjectClass(\stdClass::class,
+            'STDCLASS');
+        $this->subject->getContentObject('STDCLASS');
+    }
+
+    /**
+     * @return string[][] [[$name, $fullClassName],]
+     */
+    public function registersAllDefaultContentObjectsDataProvider(): array
     {
         $dataProvider = [];
         foreach ($this->contentObjectMap as $name => $className) {
@@ -252,16 +337,22 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     }
 
     /**
+     * Prove that all content objects are registered and a class is available
+     * for each of them.
+     *
      * @test
-     * @dataProvider getContentObjectValidContentObjectsDataProvider
-     * @param string $name TypoScript name of content object
-     * @param string $fullClassName Expected class name
+     * @dataProvider registersAllDefaultContentObjectsDataProvider
+     * @param string $objectName TypoScript name of content object
+     * @param string $className Expected class name
      */
-    public function getContentObjectCallsMakeInstanceForNewContentObjectInstance($name, $fullClassName)
-    {
-        $contentObjectInstance = $this->createMock($fullClassName);
-        GeneralUtility::addInstance($fullClassName, $contentObjectInstance);
-        $this->assertSame($contentObjectInstance, $this->subject->getContentObject($name));
+    public function registersAllDefaultContentObjects(
+        string $objectName,
+        string $className
+    ) {
+        $this->assertTrue(
+            is_subclass_of($className, AbstractContentObject::class));
+        $object = $this->subject->getContentObject($objectName);
+        $this->assertInstanceOf($className, $object);
     }
 
     /////////////////////////////////////////