[FEATURE] Add stdWrap feature: addPageCacheTags 94/9294/9
authorFabrizio Branca <mail@fabrizio-branca.de>
Tue, 28 Feb 2012 23:42:31 +0000 (00:42 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 30 Mar 2013 11:16:30 +0000 (12:16 +0100)
This feature comes after feature freeze for 4.7 but accidently it was
already mentioned in the release notes while not being implemented yet.

Release notes mention that the new stdWrap.cache feature (#34299)
stores cache tags that can be flushed by tag from TCEmain.clearCacheCmd
with feature #34352.

As stdWrap.cache stores tags into cache_hash and not into cache_pages
the feature mentioned in the release notes does not work that way.

But nevertheless that would be a handy feature and so I suggest to
add stdWrap.addPageCacheTags = pagetag1,pagetag2,pagetag3

To distinguish these tags being written to cache_pages instead of
cache_hash I'd suggest to not have this feature inside "cache." but
as a separate configuration option "addPageCacheTags".

See http://forge.typo3.org/issues/34403 for some more details.

Including the other cache (tag) related features this completes a set
of handy tools to handly cache tags in a very flexible way.

Change-Id: Ida42937356644f8ea1c27cecdf1e8252182b06ee
Resolves: #34403
Releases: 6.1
Reviewed-on: https://review.typo3.org/9294
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 39e3dc3..7d5d7fe 100644 (file)
@@ -61,6 +61,8 @@ class ContentObjectRenderer {
                // this is a placeholder for checking if the content is available in cache
                'setContentToCurrent' => 'boolean',
                'setContentToCurrent.' => 'array',
+               'addPageCacheTags' => 'string',
+               'addPageCacheTags.' => 'array',
                'setCurrent' => 'string',
                'setCurrent.' => 'array',
                'lang.' => 'array',
@@ -2058,6 +2060,24 @@ class ContentObjectRenderer {
        }
 
        /**
+        * Add tags to page cache (comma-separated list)
+        *
+        * @param string $content Input value undergoing processing in these functions.
+        * @param array $conf All stdWrap properties, not just the ones for a particular function.
+        * @return string The processed input value
+        */
+       public function stdWrap_addPageCacheTags($content = '', $conf = array()) {
+               $tags = isset($conf['addPageCacheTags.'])
+                       ? $this->stdWrap($conf['addPageCacheTags'], $conf['addPageCacheTags.'])
+                       : $conf['addPageCacheTags'];
+               if (!empty($tags)) {
+                       $cacheTags = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $tags, TRUE);
+                       $GLOBALS['TSFE']->addCacheTags($cacheTags);
+               }
+               return $content;
+       }
+
+       /**
         * setContentToCurrent
         * actually it just does the contrary: Sets the value of 'current' based on current content
         *
index f1ed4d8..4769cb3 100644 (file)
@@ -37,7 +37,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        protected $cObj = NULL;
 
        /**
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
+        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
         */
        protected $tsfe = NULL;
 
@@ -51,7 +51,7 @@ 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->getAccessibleMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array('dummy'), array(), '', FALSE);
                $this->tsfe->tmpl = $this->template;
                $this->tsfe->config = array();
                $this->tsfe->page = array();
@@ -1177,7 +1177,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function stdWrap_strftimeReturnsFormattedString($content, $conf) {
                        // Set exec_time to a hard timestamp
                $GLOBALS['EXEC_TIME'] = 1346500800;
-                       // Save current timezone and set to UTC to make the system unter test behave
+                       // Save current timezone and set to UTC to make the system under test behave
                        // the same in all server timezone settings
                $timezoneBackup = date_default_timezone_get();
                date_default_timezone_set('UTC');
@@ -1298,6 +1298,40 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ),
                );
        }
+
+       /**
+        * @param array $expectedTags
+        * @param array $configuration
+        * @test
+        * @dataProvider stdWrap_addPageCacheTagsAddsPageTagsDataProvider
+        */
+       public function stdWrap_addPageCacheTagsAddsPageTags(array $expectedTags, array $configuration) {
+               $this->cObj->stdWrap_addPageCacheTags('', $configuration);
+               $this->assertEquals($expectedTags, $this->tsfe->_get('pageCacheTags'));
+       }
+
+       /**
+        * @return array
+        */
+       public function stdWrap_addPageCacheTagsAddsPageTagsDataProvider() {
+               return array(
+                       'No Tag' => array(
+                               array(),
+                               array('addPageCacheTags' => ''),
+                       ),
+                       'Two expectedTags' => array(
+                               array('tag1', 'tag2'),
+                               array('addPageCacheTags' => 'tag1,tag2'),
+                       ),
+                       'Two expectedTags plus one with stdWrap' => array(
+                               array('tag1', 'tag2', 'tag3'),
+                               array(
+                                       'addPageCacheTags' => 'tag1,tag2',
+                                       'addPageCacheTags.' => array('wrap' => '|,tag3')
+                               ),
+                       ),
+               );
+       }
 }
 
 ?>