From 47a9401d4343c36c5518f104177066dceaf9ce27 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 13 Dec 2009 12:39:39 -0500 Subject: [PATCH] Adding test case for cached view files, and fatal errors caused by the 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 --- cake/dispatcher.php | 8 ++- cake/tests/cases/dispatcher.test.php | 51 +++++++++++++++++++ .../tests/test_app/views/posts/cache_form.ctp | 14 +++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 cake/tests/test_app/views/posts/cache_form.ctp diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 76db8f292..69e6a3f31 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -678,8 +678,12 @@ class Dispatcher extends Object { 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; diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index 4ea07969b..bc6515f01 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -448,6 +448,15 @@ class TestCachedPagesController extends AppController { 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 @@ -1899,6 +1908,48 @@ class DispatcherTest extends CakeTestCase { $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 diff --git a/cake/tests/test_app/views/posts/cache_form.ctp b/cake/tests/test_app/views/posts/cache_form.ctp new file mode 100644 index 000000000..87ec9cf5b --- /dev/null +++ b/cake/tests/test_app/views/posts/cache_form.ctp @@ -0,0 +1,14 @@ +
+ + create('User');?> +
+ + input('username'); + echo $form->input('email'); + echo $form->input('password'); + ?> +
+ end('Submit');?> +
+
\ No newline at end of file