From 915ea19a0b30355891b2ed25b96b9e78ea64fede Mon Sep 17 00:00:00 2001 From: Elmar Hinz <t3elmar@gmail.com> Date: Thu, 6 Apr 2017 20:11:52 +0200 Subject: [PATCH] [TASK] Cover getContentObject related methods by unit tests. 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/52378 Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com> Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com> --- .../ContentObjectRendererTest.php | 111 ++++++++++++++++-- 1 file changed, 101 insertions(+), 10 deletions(-) diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php index d9652213decf..b8f41d78fd3c 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php @@ -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); } ///////////////////////////////////////// -- GitLab