[TASK] Add tests for ContentObjectRenderer::getData() 41/22641/3
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 28 Jul 2013 09:18:49 +0000 (11:18 +0200)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 28 Jul 2013 16:09:44 +0000 (18:09 +0200)
* Adds unittests

Change-Id: I2b4ff89c828384c852e6fbf9f82c55e6ebbac281
Resolves: #50258
Releases: 6.2, 6.1, 6.0
Reviewed-on: https://review.typo3.org/22641
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 574a002..a54f1a5 100644 (file)
@@ -5195,12 +5195,12 @@ class ContentObjectRenderer {
         * Implements the TypoScript data type "getText". This takes a string with parameters and based on those a value from somewhere in the system is returned.
         *
         * @param string $string The parameter string, eg. "field : title" or "field : navtitle // field : title" (in the latter case and example of how the value is FIRST splitted by "//" is shown)
-        * @param mixed $fieldArray Alternative field array; If you set this to an array this variable will be used to look up values for the "field" key. Otherwise the current page record in $GLOBALS['TSFE']->page is used.
+        * @param NULL|array $fieldArray Alternative field array; If you set this to an array this variable will be used to look up values for the "field" key. Otherwise the current page record in $GLOBALS['TSFE']->page is used.
         * @return string The value fetched
         * @see getFieldVal()
         * @todo Define visibility
         */
-       public function getData($string, $fieldArray) {
+       public function getData($string, $fieldArray = NULL) {
                global $TYPO3_CONF_VARS;
                if (!is_array($fieldArray)) {
                        $fieldArray = $GLOBALS['TSFE']->page;
index 7220205..c5965a2 100644 (file)
@@ -51,11 +51,11 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function setUp() {
                $this->template = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\TemplateService', array('getFileName', 'linkData'));
-               $this->tsfe = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
+               $this->tsfe = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array('dummy'), array(), '', FALSE);
                $this->tsfe->tmpl = $this->template;
                $this->tsfe->config = array();
                $this->tsfe->page = array();
-               $sysPageMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
+               $sysPageMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('getRawRecord'));
                $this->tsfe->sys_page = $sysPageMock;
                $GLOBALS['TSFE'] = $this->tsfe;
                $GLOBALS['TSFE']->csConvObj = new \TYPO3\CMS\Core\Charset\CharsetConverter();
@@ -1175,6 +1175,398 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ),
                );
        }
+
+
+       /////////////////////////////
+       // Tests concerning getData()
+       /////////////////////////////
+
+       /**
+        * @return array
+        */
+       public function getDataWithTypeGpDataProvider() {
+               return array(
+                       'Value in get-data' => array('onlyInGet', 'GetValue'),
+                       'Value in post-data' => array('onlyInPost', 'PostValue'),
+                       'Value in post-data overriding get-data' => array('inGetAndPost', 'ValueInPost'),
+               );
+       }
+
+       /**
+        * Checks if getData() works with type "gp"
+        *
+        * @test
+        * @dataProvider getDataWithTypeGpDataProvider
+        */
+       public function getDataWithTypeGp($key, $expectedValue) {
+               $_GET = array(
+                       'onlyInGet' => 'GetValue',
+                       'inGetAndPost' => 'ValueInGet',
+               );
+               $_POST = array(
+                       'onlyInPost' => 'PostValue',
+                       'inGetAndPost' => 'ValueInPost',
+               );
+               $this->assertEquals($expectedValue, $this->cObj->getData('gp:' . $key));
+       }
+
+       /**
+        * Checks if getData() works with type "tsfe"
+        *
+        * @test
+        */
+       public function getDataWithTypeTsfe() {
+               $this->assertEquals($GLOBALS['TSFE']->renderCharset, $this->cObj->getData('tsfe:renderCharset'));
+       }
+
+       /**
+        * Checks if getData() works with type "getenv"
+        *
+        * @test
+        */
+       public function getDataWithTypeGetenv() {
+               $envName = uniqid('frontendtest');
+               $value = uniqid('someValue');
+               putenv($envName . '=' . $value);
+               $this->assertEquals($value, $this->cObj->getData('getenv:' . $envName));
+       }
+
+       /**
+        * Checks if getData() works with type "getindpenv"
+        *
+        * @test
+        */
+       public function getDataWithTypeGetindpenv() {
+               $this->assertEquals(PATH_thisScript, $this->cObj->getData('getindpenv:SCRIPT_FILENAME'));
+       }
+
+       /**
+        * Checks if getData() works with type "getindpenv"
+        *
+        * @test
+        */
+       public function getDataWithTypeField() {
+               $key = 'someKey';
+               $value = 'someValue';
+               $field = array($key => $value);
+
+               $this->assertEquals($value, $this->cObj->getData('field:' . $key, $field));
+       }
+
+       /**
+        * Checks if getData() works with type "file"
+        *
+        * @test
+        */
+       public function getDataWithTypeFile() {
+               $uid = rand();
+               $properties = array(
+                       uniqid() => uniqid(),
+                       uniqid() => uniqid(),
+                       'uid' => $uid
+               );
+               $file = new \TYPO3\CMS\Core\Resource\File($properties);
+               $this->cObj->setCurrentFile($file);
+
+               $this->assertEquals($uid, $this->cObj->getData('file:current:uid'));
+       }
+
+       /**
+        * Checks if getData() works with type "parameters"
+        *
+        * @test
+        */
+       public function getDataWithTypeParameters() {
+               $key = uniqid('someKey');
+               $value = uniqid('someValue');
+               $this->cObj->parameters[$key] = $value;
+
+               $this->assertEquals($value, $this->cObj->getData('parameters:' . $key));
+       }
+
+       /**
+        * Checks if getData() works with type "register"
+        *
+        * @test
+        */
+       public function getDataWithTypeRegister() {
+               $key = uniqid('someKey');
+               $value = uniqid('someValue');
+               $GLOBALS['TSFE']->register[$key] = $value;
+
+               $this->assertEquals($value, $this->cObj->getData('register:' . $key));
+       }
+
+       /**
+        * Checks if getData() works with type "level"
+        *
+        * @test
+        */
+       public function getDataWithTypeLevel() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1'),
+                       1 => array('uid' => 2, 'title' => 'title2'),
+                       2 => array('uid' => 3, 'title' => 'title3'),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+               $this->assertEquals(2, $this->cObj->getData('level:dummy'));
+       }
+
+       /**
+        * Checks if getData() works with type "global"
+        *
+        * @test
+        */
+       public function getDataWithTypeGlobal() {
+               $this->assertEquals($GLOBALS['TSFE']->renderCharset, $this->cObj->getData('global:TSFE|renderCharset'));
+       }
+
+       /**
+        * Checks if getData() works with type "leveltitle"
+        *
+        * @test
+        */
+       public function getDataWithTypeLeveltitle() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1'),
+                       1 => array('uid' => 2, 'title' => 'title2'),
+                       2 => array('uid' => 3, 'title' => ''),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+               $this->assertEquals('', $this->cObj->getData('leveltitle:-1'));
+               // since "title3" is not set, it will slide to "title2"
+               $this->assertEquals('title2', $this->cObj->getData('leveltitle:-1,slide'));
+       }
+
+       /**
+        * Checks if getData() works with type "levelmedia"
+        *
+        * @test
+        */
+       public function getDataWithTypeLevelmedia() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1', 'media' => 'media1'),
+                       1 => array('uid' => 2, 'title' => 'title2', 'media' => 'media2'),
+                       2 => array('uid' => 3, 'title' => 'title3', 'media' => ''),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+               $this->assertEquals('', $this->cObj->getData('levelmedia:-1'));
+               // since "title3" is not set, it will slide to "title2"
+               $this->assertEquals('media2', $this->cObj->getData('levelmedia:-1,slide'));
+       }
+
+       /**
+        * Checks if getData() works with type "leveluid"
+        *
+        * @test
+        */
+       public function getDataWithTypeLeveluid() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1'),
+                       1 => array('uid' => 2, 'title' => 'title2'),
+                       2 => array('uid' => 3, 'title' => 'title3'),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+               $this->assertEquals(3, $this->cObj->getData('leveluid:-1'));
+               // every element will have a uid - so adding slide doesn't really make sense, just for completeness
+               $this->assertEquals(3, $this->cObj->getData('leveluid:-1,slide'));
+       }
+
+       /**
+        * Checks if getData() works with type "levelfield"
+        *
+        * @test
+        */
+       public function getDataWithTypeLevelfield() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1', 'testfield' => 'field1'),
+                       1 => array('uid' => 2, 'title' => 'title2', 'testfield' => 'field2'),
+                       2 => array('uid' => 3, 'title' => 'title3', 'testfield' => ''),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+               $this->assertEquals('', $this->cObj->getData('levelfield:-1,testfield'));
+               $this->assertEquals('field2', $this->cObj->getData('levelfield:-1,testfield,slide'));
+       }
+
+       /**
+        * Checks if getData() works with type "fullrootline"
+        *
+        * @test
+        */
+       public function getDataWithTypeFullrootline() {
+               $rootline1 = array(
+                       0 => array('uid' => 1, 'title' => 'title1', 'testfield' => 'field1'),
+               );
+               $rootline2 = array(
+                       0 => array('uid' => 1, 'title' => 'title1', 'testfield' => 'field1'),
+                       1 => array('uid' => 2, 'title' => 'title2', 'testfield' => 'field2'),
+                       2 => array('uid' => 3, 'title' => 'title3', 'testfield' => 'field3'),
+               );
+
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline1;
+               $GLOBALS['TSFE']->rootLine = $rootline2;
+               $this->assertEquals('field2', $this->cObj->getData('fullrootline:-1,testfield'));
+       }
+
+       /**
+        * Checks if getData() works with type "date"
+        *
+        * @test
+        */
+       public function getDataWithTypeDate() {
+               $format = 'Y-M-D';
+
+               $this->assertEquals(date($format, $GLOBALS['EXEC_TIME']), $this->cObj->getData('date:' . $format));
+       }
+
+       /**
+        * Checks if getData() works with type "page"
+        *
+        * @test
+        */
+       public function getDataWithTypePage() {
+               $uid = rand();
+               $GLOBALS['TSFE']->page['uid'] = $uid;
+               $this->assertEquals($uid, $this->cObj->getData('page:uid'));
+       }
+
+       /**
+        * Checks if getData() works with type "current"
+        *
+        * @test
+        */
+       public function getDataWithTypeCurrent() {
+               $key = uniqid('someKey');
+               $value = uniqid('someValue');
+               $this->cObj->data[$key] = $value;
+               $this->cObj->currentValKey = $key;
+               $this->assertEquals($value, $this->cObj->getData('current:dummy'));
+       }
+
+       /**
+        * Checks if getData() works with type "db"
+        *
+        * @test
+        */
+       public function getDataWithTypeDb() {
+               $dummyRecord = array('uid' => 5, 'title' => 'someTitle');
+
+               $GLOBALS['TSFE']->sys_page->expects($this->atLeastOnce())->method('getRawRecord')->with('tt_content', '106')->will($this->returnValue($dummyRecord));
+               $this->assertEquals($dummyRecord['title'], $this->cObj->getData('db:tt_content:106:title'));
+       }
+
+       /**
+        * Checks if getData() works with type "lll"
+        *
+        * @test
+        */
+       public function getDataWithTypeLll() {
+               $key = uniqid('someKey');
+               $value = uniqid('someValue');
+               $language = uniqid('someLanguage');
+               $GLOBALS['TSFE']->LL_labels_cache[$language]['LLL:' . $key] = $value;
+               $GLOBALS['TSFE']->lang = $language;
+
+               $this->assertEquals($value, $this->cObj->getData('lll:' . $key));
+       }
+
+       /**
+        * Checks if getData() works with type "path"
+        *
+        * @test
+        */
+       public function getDataWithTypePath() {
+               $filenameIn = uniqid('someValue');
+               $filenameOut = uniqid('someValue');
+               $this->template->expects($this->atLeastOnce())->method('getFileName')->with($filenameIn)->will($this->returnValue($filenameOut));
+               $this->assertEquals($filenameOut, $this->cObj->getData('path:' . $filenameIn));
+       }
+
+       /**
+        * Checks if getData() works with type "parentRecordNumber"
+        *
+        * @test
+        */
+       public function getDataWithTypeParentRecordNumber() {
+               $recordNumber = rand();
+               $this->cObj->parentRecordNumber = $recordNumber;
+               $this->assertEquals($recordNumber, $this->cObj->getData('cobj:parentRecordNumber'));
+       }
+
+       /**
+        * Checks if getData() works with type "debug:rootLine"
+        *
+        * @test
+        */
+       public function getDataWithTypeDebugRootline() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1'),
+                       1 => array('uid' => 2, 'title' => 'title2'),
+                       2 => array('uid' => 3, 'title' => ''),
+               );
+               $expectedResult = '0uid1titletitle11uid2titletitle22uid3title';
+               $GLOBALS['TSFE']->tmpl->rootLine = $rootline;
+
+               $result = $this->cObj->getData('debug:rootLine');
+               $cleanedResult = strip_tags($result);
+               $cleanedResult = str_replace("\r", '', $cleanedResult);
+               $cleanedResult = str_replace("\n", '', $cleanedResult);
+               $cleanedResult = str_replace("\t", '', $cleanedResult);
+               $cleanedResult = str_replace(' ', '', $cleanedResult);
+
+               $this->assertEquals($expectedResult, $cleanedResult);
+       }
+
+       /**
+        * Checks if getData() works with type "debug:fullRootLine"
+        *
+        * @test
+        */
+       public function getDataWithTypeDebugFullRootline() {
+               $rootline = array(
+                       0 => array('uid' => 1, 'title' => 'title1'),
+                       1 => array('uid' => 2, 'title' => 'title2'),
+                       2 => array('uid' => 3, 'title' => ''),
+               );
+               $expectedResult = '0uid1titletitle11uid2titletitle22uid3title';
+               $GLOBALS['TSFE']->rootLine = $rootline;
+
+               $result = $this->cObj->getData('debug:fullRootLine');
+               $cleanedResult = strip_tags($result);
+               $cleanedResult = str_replace("\r", '', $cleanedResult);
+               $cleanedResult = str_replace("\n", '', $cleanedResult);
+               $cleanedResult = str_replace("\t", '', $cleanedResult);
+               $cleanedResult = str_replace(' ', '', $cleanedResult);
+
+               $this->assertEquals($expectedResult, $cleanedResult);
+       }
+
+       /**
+        * Checks if getData() works with type "debug:data"
+        *
+        * @test
+        */
+       public function getDataWithTypeDebugData() {
+               $key = uniqid('someKey');
+               $value = uniqid('someValue');
+               $this->cObj->data = array($key => $value);
+
+               $expectedResult = $key . $value;
+
+               $result = $this->cObj->getData('debug:data');
+               $cleanedResult = strip_tags($result);
+               $cleanedResult = str_replace("\r", '', $cleanedResult);
+               $cleanedResult = str_replace("\n", '', $cleanedResult);
+               $cleanedResult = str_replace("\t", '', $cleanedResult);
+               $cleanedResult = str_replace(' ', '', $cleanedResult);
+
+               $this->assertEquals($expectedResult, $cleanedResult);
+       }
 }
 
 ?>