[TASK] Cover getContentObject related methods by unit tests. 43/53943/3
authorTymoteusz Motylewski <t.motylewski@gmail.com>
Thu, 7 Sep 2017 11:17:43 +0000 (13:17 +0200)
committerTymoteusz Motylewski <t.motylewski@gmail.com>
Thu, 7 Sep 2017 12:54:16 +0000 (14:54 +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/53943
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 1f760f3..4d13e31 100644 (file)
@@ -24,6 +24,8 @@ use TYPO3\CMS\Core\Utility\DebugUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
+use TYPO3\CMS\Frontend\ContentObject\TextContentObject;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Fixtures\PageRepositoryFixture;
 
@@ -203,10 +205,94 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     }
 
     //////////////////////////////////////
-    // 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
+     * @expectedException \TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
+     * @test
+     */
+    public function willThrowAnExceptionForARegisteredNonContentObject()
+    {
+        $this->subject->registerContentObjectClass(\stdClass::class,
+            'STDCLASS');
+        $this->subject->getContentObject('STDCLASS');
+    }
+
+    /**
+     * @return string[][] [[$name, $fullClassName],]
+     */
+    public function registersAllDefaultContentObjectsDataProvider()
     {
         $dataProvider = [];
         foreach ($this->contentObjectMap as $name => $className) {
@@ -216,16 +302,22 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     }
 
     /**
+     * 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
-     */
-    public function getContentObjectCallsMakeInstanceForNewContentObjectInstance($name, $fullClassName)
-    {
-        $contentObjectInstance = $this->getMock($fullClassName, [], [], '', false);
-        \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($fullClassName, $contentObjectInstance);
-        $this->assertSame($contentObjectInstance, $this->subject->getContentObject($name));
+     * @dataProvider registersAllDefaultContentObjectsDataProvider
+     * @param string $objectName TypoScript name of content object
+     * @param string $className Expected class name
+     */
+    public function registersAllDefaultContentObjects(
+        $objectName,
+        $className
+    ) {
+        $this->assertTrue(
+            is_subclass_of($className, AbstractContentObject::class));
+        $object = $this->subject->getContentObject($objectName);
+        $this->assertInstanceOf($className, $object);
     }
 
     /////////////////////////////////////////