From f12cbdba380479b96bb842c11e6e8f250e584728 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 1 Oct 2009 01:03:47 -0400 Subject: [PATCH] Fixing CacheHelper and multiple cake:nocache tags in a view file, breaking cake:nocache following $content_for_layout. Fixes #136 --- cake/libs/view/helpers/cache.php | 27 ++++++++++-------- .../cases/libs/view/helpers/cache.test.php | 28 +++++++++++++++++++ .../test_app/views/posts/multiple_nocache.ctp | 15 ++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 cake/tests/test_app/views/posts/multiple_nocache.ctp diff --git a/cake/libs/view/helpers/cache.php b/cake/libs/view/helpers/cache.php index 009286fe6..afbf0ce57 100644 --- a/cake/libs/view/helpers/cache.php +++ b/cake/libs/view/helpers/cache.php @@ -156,27 +156,30 @@ class CacheHelper extends AppHelper { } elseif ($file = fileExistsInPath($file)) { $file = file_get_contents($file); } - - preg_match_all('/((?<=)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER); - preg_match_all('/(?<=)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER); + preg_match_all('/((?<=)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER); + preg_match_all('/(?<=)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER); + $fileResult = $fileResult[0]; + $outputResult = $outputResult[0]; if (!empty($this->__replace)) { - foreach ($oresult['0'] as $k => $element) { + foreach ($outputResult as $i => $element) { $index = array_search($element, $this->__match); if ($index !== false) { - array_splice($oresult[0], $k, 1); + unset($outputResult[$i]); } } + $outputResult = array_values($outputResult); } - if (!empty($result['0'])) { - $count = 0; - foreach ($result['0'] as $block) { - if (isset($oresult['0'][$count])) { - $this->__replace[] = $block; - $this->__match[] = $oresult['0'][$count]; + + if (!empty($fileResult)) { + $i = 0; + foreach ($fileResult as $cacheBlock) { + if (isset($outputResult[$i])) { + $this->__replace[] = $cacheBlock; + $this->__match[] = $outputResult[$i]; } - $count++; + $i++; } } } diff --git a/cake/tests/cases/libs/view/helpers/cache.test.php b/cake/tests/cases/libs/view/helpers/cache.test.php index f6e9b081e..fc450ecc9 100644 --- a/cake/tests/cases/libs/view/helpers/cache.test.php +++ b/cake/tests/cases/libs/view/helpers/cache.test.php @@ -62,6 +62,8 @@ class CacheTestController extends Controller { $this->layout = 'cache_layout'; $this->set('variable', 'variableValue'); $this->set('superman', 'clark kent'); + $this->set('batman', 'bruce wayne'); + $this->set('spiderman', 'peter parker'); } } /** @@ -162,9 +164,35 @@ class CacheHelperTest extends CakeTestCase { $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents); $this->assertPattern('/php echo \$variable/', $contents); $this->assertPattern('/php echo microtime()/', $contents); + $this->assertNoPattern('/cake:nocache/', $contents); @unlink($filename); } + +/** + * test that multiple tags function with multiple nocache tags in the layout. + * + * @return void + **/ + function testMultipleNoCacheTagsInViewfile() { + $this->Controller->cache_parsing(); + $this->Controller->cacheAction = 21600; + $this->Controller->here = '/cacheTest/cache_parsing'; + $this->Controller->action = 'cache_parsing'; + + $View = new View($this->Controller); + $result = $View->render('multiple_nocache'); + + $this->assertNoPattern('/cake:nocache/', $result); + $this->assertNoPattern('/php echo/', $result); + + $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php'; + $this->assertTrue(file_exists($filename)); + + $contents = file_get_contents($filename); + $this->assertNoPattern('/cake:nocache/', $contents); + @unlink($filename); + } /** * testComplexNoCache method * diff --git a/cake/tests/test_app/views/posts/multiple_nocache.ctp b/cake/tests/test_app/views/posts/multiple_nocache.ctp new file mode 100644 index 000000000..eb397e492 --- /dev/null +++ b/cake/tests/test_app/views/posts/multiple_nocache.ctp @@ -0,0 +1,15 @@ +--view start-- + + + + +this view has 3 nocache blocks + + + + + + + + +--view end-- \ No newline at end of file