Skip to content

Commit

Permalink
Passing viewVars to cache views, avoiding cake:nocache problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 18, 2010
1 parent 7416e58 commit a7c7436
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cake/libs/view/helpers/cache.php
Expand Up @@ -233,6 +233,7 @@ function __writeFile($content, $timestamp, $useCallbacks = false) {
$controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
$controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
$controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
$controller->viewVars = $this->viewVars = unserialize(stripslashes(\'' . addslashes(serialize($this->viewVars)) . '\'));
$controller->theme = $this->theme = \'' . $this->theme . '\';
Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));';

Expand All @@ -253,6 +254,7 @@ function __writeFile($content, $timestamp, $useCallbacks = false) {
$this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
$this->{$helper} =& $loadedHelpers[$helper];
}
extract($this->viewVars, EXTR_SKIP);
?>';
$content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
$file .= $content;
Expand Down
1 change: 1 addition & 0 deletions cake/libs/view/view.php
Expand Up @@ -751,6 +751,7 @@ function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = fal
$cache->controllerName = $this->name;
$cache->layout = $this->layout;
$cache->cacheAction = $this->cacheAction;
$cache->viewVars = $this->viewVars;
$cache->cache($___viewFn, $out, $cached);
}
}
Expand Down
36 changes: 36 additions & 0 deletions cake/tests/cases/libs/view/helpers/cache.test.php
Expand Up @@ -308,6 +308,42 @@ function testComplexNoCache () {
$this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
}

/**
* test cache of view vars
*
* @access public
* @return void
*/
function testCacheViewVars() {
$this->Controller->cache_parsing();
$this->Controller->params = array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = 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));

$contents = file_get_contents($filename);
$this->assertPattern('/\$this\-\>viewVars.*variable/', $contents);
$this->assertPattern('/extract\(\$this\-\>viewVars, EXTR_SKIP\);/', $contents);
$this->assertPattern('/php echo \$variable/', $contents);
$this->assertPattern('/variableValue/', $contents);

@unlink($filename);
}

/**
* test cacheAction set to a boolean
*
Expand Down

0 comments on commit a7c7436

Please sign in to comment.