Browse files

Fixing $cacheAction requiring the inclusion of the controller name fo…

…r view cache files to be generated. This behavior makes the cache helper behave as documented.

Test cases added.  Fixes #232
  • Loading branch information...
1 parent 873760a commit a87d31cc7f570a1ac03abc2c04b04f71e4f6c955 @markstory markstory committed Nov 2, 2009
Showing with 92 additions and 14 deletions.
  1. +12 −4 cake/libs/view/helpers/cache.php
  2. +80 −10 cake/tests/cases/libs/view/helpers/cache.test.php
View
16 cake/libs/view/helpers/cache.php
@@ -75,24 +75,32 @@ function cache($file, $out, $cache = false) {
$useCallbacks = false;
if (is_array($this->cacheAction)) {
$controller = Inflector::underscore($this->controllerName);
+ $controllerAlternate = Inflector::variable($this->controllerName);
+
$check = str_replace('/', '_', $this->here);
- $replace = str_replace('/', '_', $this->base);
+ $basePath = str_replace('/', '_', $this->base);
+
$match = str_replace($this->base, '', $this->here);
$match = str_replace('//', '/', $match);
$match = str_replace('/' . $controller . '/', '', $match);
+ $match = str_replace('/' . $controllerAlternate . '/', '', $match);
$match = str_replace('/' . $this->controllerName . '/', '', $match);
- $check = str_replace($replace, '', $check);
+
+ $check = str_replace($basePath, '', $check);
$check = str_replace('_' . $controller . '_', '', $check);
$check = str_replace('_' . $this->controllerName . '_', '', $check);
+ $check = str_replace('_' . $controllerAlternate . '_', '', $match);
+
$check = Inflector::slug($check);
- $check = preg_replace('/^_+/', '', $check);
+ $check = trim($check, '_');
+
$keys = str_replace('/', '_', array_keys($this->cacheAction));
$found = array_keys($this->cacheAction);
$index = null;
$count = 0;
foreach ($keys as $key => $value) {
- if (strpos($check, $value) === 0) {
+ if (strpos($check, rtrim($value, '_')) === 0) {
$index = $found[$count];
break;
}
View
90 cake/tests/cases/libs/view/helpers/cache.test.php
@@ -29,14 +29,7 @@
}
App::import('Core', array('Controller', 'Model', 'View'));
App::import('Helper', 'Cache');
-/**
- * TestCacheHelper class
- *
- * @package cake
- * @subpackage cake.tests.cases.libs.view.helpers
- */
-class TestCacheHelper extends CacheHelper {
-}
+
/**
* CacheTestController class
*
@@ -81,7 +74,8 @@ class CacheHelperTest extends CakeTestCase {
*/
function setUp() {
$this->Controller = new CacheTestController();
- $this->Cache = new TestCacheHelper();
+ $this->Cache = new CacheHelper();
+ $this->_cacheSettings = Configure::read('Cache');
Configure::write('Cache.check', true);
Configure::write('Cache.disable', false);
}
@@ -112,6 +106,7 @@ function endCase() {
*/
function tearDown() {
unset($this->Cache);
+ Configure::write('Cache', $this->_cacheSettings);
}
/**
* test cache parsing with no cake:nocache tags in view file.
@@ -201,7 +196,7 @@ function testMultipleNoCacheTagsInViewfile() {
*/
function testComplexNoCache () {
$this->Controller->cache_parsing();
- $this->Controller->cacheAction = array('cacheTest' => 21600);
+ $this->Controller->cacheAction = array('cache_complex' => 21600);
$this->Controller->here = '/cacheTest/cache_complex';
$this->Controller->action = 'cache_complex';
$this->Controller->layout = 'multi_cache';
@@ -248,6 +243,81 @@ function testComplexNoCache () {
$this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
}
/**
+ * test cacheAction set to a boolean
+ *
+ * @return void
+ **/
+ function testCacheActionArray() {
+ $this->Controller->cache_parsing();
+ $this->Controller->cacheAction = array(
+ 'cache_parsing' => 21600
+ );
+ $this->Controller->here = '/cache_test/cache_parsing';
+ $this->Controller->action = 'cache_parsing';
+
+ $View = new View($this->Controller);
+ $result = $View->render('index');
+
+ $this->assertNoPattern('/cake:nocache/', $result);
+ $this->assertNoPattern('/php echo/', $result);
+
+ $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
+ $this->assertTrue(file_exists($filename));
+ @unlink($filename);
+
+
+ $this->Controller->cache_parsing();
+ $this->Controller->cacheAction = array(
+ 'cache_parsing/' => 21600
+ );
+ $this->Controller->here = '/cacheTest/cache_parsing';
+ $this->Controller->action = 'cache_parsing';
+
+ $View = new View($this->Controller);
+ $result = $View->render('index');
+
+ $this->assertNoPattern('/cake:nocache/', $result);
+ $this->assertNoPattern('/php echo/', $result);
+
+ $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
+ $this->assertTrue(file_exists($filename));
+ @unlink($filename);
+
+
+ $this->Controller->cache_parsing();
+ $this->Controller->cacheAction = array(
+ 'cache_parsing/33' => 21600
+ );
+ $this->Controller->here = '/cacheTest/cache_parsing/33';
+ $this->Controller->action = 'cache_parsing';
+
+ $View = new View($this->Controller);
+ $result = $View->render('index');
+
+ $this->assertNoPattern('/cake:nocache/', $result);
+ $this->assertNoPattern('/php echo/', $result);
+
+ $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing_33.php';
+ $this->assertTrue(file_exists($filename));
+ @unlink($filename);
+
+ $this->Controller->cache_parsing();
+ $this->Controller->cacheAction = array(
+ 'cache_parsing/33' => 21600
+ );
+ $this->Controller->here = '/cacheTest/cache_parsing';
+ $this->Controller->action = 'cache_parsing';
+
+ $View = new View($this->Controller);
+ $result = $View->render('index');
+
+ $this->assertNoPattern('/cake:nocache/', $result);
+ $this->assertNoPattern('/php echo/', $result);
+
+ $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
+ $this->assertFalse(file_exists($filename));
+ }
+/**
* testCacheEmptySections method
*
* This test must be uncommented/fixed in next release (1.2+)

0 comments on commit a87d31c

Please sign in to comment.