From 0e5797d69d75b2c54c710d278db3fba0cde779e4 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 17 Sep 2011 13:19:43 -0400 Subject: [PATCH] Make full page caching + Themes work. Dispatcher now uses ThemeView for rendering cache files. This enables nocache blocks to reference theme elements. Fixes #1858 --- lib/Cake/Routing/Dispatcher.php | 4 +- lib/Cake/Test/Case/Routing/DispatcherTest.php | 48 +++++++++++++++++++ .../View/Themed/TestTheme/Layouts/default.ctp | 3 +- .../View/Themed/TestTheme/Posts/themed.ctp | 4 ++ lib/Cake/View/ThemeView.php | 4 +- 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 lib/Cake/Test/test_app/View/Themed/TestTheme/Posts/themed.ctp diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index f526a2f91..3acfc71bf 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -218,8 +218,10 @@ class Dispatcher { } if (file_exists($filename)) { + App::uses('ThemeView', 'View'); + $controller = null; - $view = new View($controller); + $view = new ThemeView($controller); return $view->renderCache($filename, microtime(true)); } } diff --git a/lib/Cake/Test/Case/Routing/DispatcherTest.php b/lib/Cake/Test/Case/Routing/DispatcherTest.php index 24197dadb..d5f4f1d5b 100644 --- a/lib/Cake/Test/Case/Routing/DispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/DispatcherTest.php @@ -442,6 +442,15 @@ class TestCachedPagesController extends Controller { $this->cacheAction = 10; $this->helpers[] = 'Form'; } + +/** + * Test cached views with themes. + */ + public function themed() { + $this->cacheAction = 10; + $this->viewClass = 'Theme'; + $this->theme = 'TestTheme'; + } } /** @@ -1424,6 +1433,45 @@ class DispatcherTest extends CakeTestCase { unlink($filename); } +/** + * Test full page caching with themes. + * + * @return void + */ + public function testFullPageCachingWithThemes() { + Configure::write('Cache.disable', false); + Configure::write('Cache.check', true); + Configure::write('debug', 2); + + Router::reload(); + Router::connect('/:controller/:action/*'); + + App::build(array( + 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS), + ), true); + + $dispatcher = new TestDispatcher(); + $request = new CakeRequest('/test_cached_pages/themed'); + $response = new CakeResponse(); + + ob_start(); + $dispatcher->dispatch($request, $response); + $out = ob_get_clean(); + + ob_start(); + $dispatcher->cached($request->here); + $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($expected, $result); + + $filename = $this->__cachePath($request->here); + unlink($filename); + } + /** * testHttpMethodOverrides method * diff --git a/lib/Cake/Test/test_app/View/Themed/TestTheme/Layouts/default.ctp b/lib/Cake/Test/test_app/View/Themed/TestTheme/Layouts/default.ctp index 89107d393..f055297e2 100644 --- a/lib/Cake/Test/test_app/View/Themed/TestTheme/Layouts/default.ctp +++ b/lib/Cake/Test/test_app/View/Themed/TestTheme/Layouts/default.ctp @@ -1 +1,2 @@ -default test_theme layout \ No newline at end of file +default test_theme layout + diff --git a/lib/Cake/Test/test_app/View/Themed/TestTheme/Posts/themed.ctp b/lib/Cake/Test/test_app/View/Themed/TestTheme/Posts/themed.ctp new file mode 100644 index 000000000..b8492b1bc --- /dev/null +++ b/lib/Cake/Test/test_app/View/Themed/TestTheme/Posts/themed.ctp @@ -0,0 +1,4 @@ +posts themed themed file. + +element('test_element'); ?> + diff --git a/lib/Cake/View/ThemeView.php b/lib/Cake/View/ThemeView.php index bd4cb885d..ed5a0e73b 100644 --- a/lib/Cake/View/ThemeView.php +++ b/lib/Cake/View/ThemeView.php @@ -38,7 +38,9 @@ class ThemeView extends View { */ public function __construct($controller) { parent::__construct($controller); - $this->theme = $controller->theme; + if ($controller) { + $this->theme = $controller->theme; + } } /**