Skip to content

Commit

Permalink
Adding test case for cached view files, and fatal errors caused by th…
Browse files Browse the repository at this point in the history
…e view instance not being registered. View instances are now registered when rendering view caches, and unregistered if the cached view fails. This fixes issues rendering flash messages with custom layouts and fixes FormHelper methods inside nocache blocks.

Fixes #60
  • Loading branch information
markstory committed Dec 13, 2009
1 parent c3bf6bc commit 47a9401
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cake/dispatcher.php
Expand Up @@ -678,8 +678,12 @@ function cached($url) {
App::import('Core', 'View');
}
$controller = null;
$view =& new View($controller, false);
return $view->renderCache($filename, getMicrotime());
$view =& new View($controller);
$return = $view->renderCache($filename, getMicrotime());
if (!$return) {
ClassRegistry::removeObject('view');
}
return $return;
}
}
return false;
Expand Down
51 changes: 51 additions & 0 deletions cake/tests/cases/dispatcher.test.php
Expand Up @@ -448,6 +448,15 @@ function test_nocache_tags() {
function view($id = null) {
$this->render('index');
}
/**
* test cached forms / tests view object being registered
*
* @return void
*/
function cache_form() {
$this->cacheAction = 10;
$this->helpers[] = 'Form';
}
}
/**
* TimesheetsController class
Expand Down Expand Up @@ -1899,6 +1908,48 @@ function testFullPageCachingDispatch() {
$filename = $this->__cachePath($dispatcher->here);
$this->assertTrue(file_exists($filename));
unlink($filename);

$url = 'TestCachedPages/test_nocache_tags';
}
/**
* test that cached() registers a view and un-registers it. Tests
* that helpers using ClassRegistry::getObject('view'); don't fail
*
* @return void
*/
function testCachedRegisteringViewObject() {
Configure::write('Cache.disable', false);
Configure::write('Cache.check', true);
Configure::write('debug', 2);

$_POST = array();
$_SERVER['PHP_SELF'] = '/';

Router::reload();
Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));

$dispatcher =& new Dispatcher();
$dispatcher->base = false;

$url = 'test_cached_pages/cache_form';
ob_start();
$dispatcher->dispatch($url);
$out = ob_get_clean();

ClassRegistry::flush();

ob_start();
$dispatcher->cached($url);
$cached = ob_get_clean();

$result = str_replace(array("\t", "\r\n", "\n"), "", $out);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);

$this->assertEqual($result, $expected);
$filename = $this->__cachePath($dispatcher->here);
unlink($filename);
ClassRegistry::flush();
}
/**
* testHttpMethodOverrides method
Expand Down
14 changes: 14 additions & 0 deletions cake/tests/test_app/views/posts/cache_form.ctp
@@ -0,0 +1,14 @@
<div class="users form">
<cake:nocache>
<?php echo $form->create('User');?>
<fieldset>
<legend><?php __('Add User');?></legend>
<?php
echo $form->input('username');
echo $form->input('email');
echo $form->input('password');
?>
</fieldset>
<?php echo $form->end('Submit');?>
</cake:nocache>
</div>

0 comments on commit 47a9401

Please sign in to comment.