diff --git a/cake/libs/view/helpers/cache.php b/cake/libs/view/helpers/cache.php index 60c1a9eae16..59baef79333 100644 --- a/cake/libs/view/helpers/cache.php +++ b/cake/libs/view/helpers/cache.php @@ -70,19 +70,15 @@ function cache($file, $out, $cache = false) { $check = Inflector::slug(Router::reverse($this->params)); $base = trim(str_replace('/', '_', $this->base), '_'); $check = trim(str_replace($base, '', $check), '_'); - $match = $check; - $keys = str_replace('/', '_', array_keys($this->cacheAction)); - $found = array_keys($this->cacheAction); + $keys = array_keys($this->cacheAction); $index = null; - $count = 0; - foreach ($keys as $key => $value) { - if (strpos($check, rtrim($value, '_')) !== false) { - $index = $found[$count]; + foreach ($keys as $action) { + if ($action == $this->params['action']) { + $index = $action; break; } - $count++; } if (!isset($index) && $this->action == 'index') { @@ -92,19 +88,17 @@ function cache($file, $out, $cache = false) { $options = $this->cacheAction; if (isset($this->cacheAction[$index])) { if (is_array($this->cacheAction[$index])) { - $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]); + $options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]); } else { $cacheTime = $this->cacheAction[$index]; } } - - if (array_key_exists('duration', $options)) { + if (isset($options['duration'])) { $cacheTime = $options['duration']; } - if (array_key_exists('callbacks', $options)) { + if (isset($options['callbacks'])) { $useCallbacks = $options['callbacks']; } - } else { $cacheTime = $this->cacheAction; } diff --git a/cake/tests/cases/libs/view/helpers/cache.test.php b/cake/tests/cases/libs/view/helpers/cache.test.php index 3cc5966c6e0..d804e2f1dfd 100644 --- a/cake/tests/cases/libs/view/helpers/cache.test.php +++ b/cake/tests/cases/libs/view/helpers/cache.test.php @@ -17,9 +17,6 @@ * @since CakePHP(tm) v 1.2.0.4206 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ -if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { - define('CAKEPHP_UNIT_TEST_EXECUTION', 1); -} App::import('Core', array('Controller', 'Model', 'View')); App::import('Helper', 'Cache'); @@ -344,7 +341,7 @@ function testCacheActionArray() { $this->Controller->cache_parsing(); $this->Controller->cacheAction = array( - 'cache_parsing/' => 21600 + 'cache_parsing' => 21600 ); $this->Controller->here = '/cacheTest/cache_parsing'; $this->Controller->action = 'cache_parsing'; @@ -365,13 +362,13 @@ function testCacheActionArray() { 'controller' => 'cache_test', 'action' => 'cache_parsing', 'url' => array(), - 'pass' => array(33), + 'pass' => array(), 'named' => array() ); $this->Controller->cacheAction = array( - 'cache_parsing/33' => 21600 + 'some_other_action' => 21600 ); - $this->Controller->here = '/cacheTest/cache_parsing/33'; + $this->Controller->here = '/cacheTest/cache_parsing'; $this->Controller->action = 'cache_parsing'; $View = new View($this->Controller); @@ -380,12 +377,22 @@ function testCacheActionArray() { $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); + $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php'; + $this->assertFalse(file_exists($filename)); + } +/** + * test that custom routes are respected when generating cache files. + * + * @return void + */ + function testCacheWithCustomRoutes() { + Router::reload(); + Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}')); + $this->Controller->cache_parsing(); $this->Controller->params = array( + 'lang' => 'en', 'controller' => 'cache_test', 'action' => 'cache_parsing', 'url' => array(), @@ -393,9 +400,9 @@ function testCacheActionArray() { 'named' => array() ); $this->Controller->cacheAction = array( - 'cache_parsing/33' => 21600 + 'cache_parsing' => 21600 ); - $this->Controller->here = '/cacheTest/cache_parsing'; + $this->Controller->here = '/en/cache_test/cache_parsing'; $this->Controller->action = 'cache_parsing'; $View = new View($this->Controller); @@ -404,19 +411,9 @@ function testCacheActionArray() { $this->assertNoPattern('/cake:nocache/', $result); $this->assertNoPattern('/php echo/', $result); - $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php'; - $this->assertFalse(file_exists($filename)); - } - -/** - * test that custom routes are respected when generating cache files. - * - * @return void - */ - function testCacheWithCustomRoutes() { - Router::reload(); - Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}')); - + $filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php'; + $this->assertTrue(file_exists($filename)); + @unlink($filename); } /** * testCacheEmptySections method