PageRenderTime 203ms CodeModel.GetById 40ms app.highlight 76ms RepoModel.GetById 75ms app.codeStats 1ms

/cake/tests/cases/libs/view/helpers/cache.test.php

https://github.com/hardsshah/bookmarks
PHP | 268 lines | 110 code | 12 blank | 146 comment | 1 complexity | 19e1b651ff4f0abfa677d1eb08936d13 MD5 | raw file
  1<?php
  2/* SVN FILE: $Id$ */
  3/**
  4 * CacheHelperTest file
  5 *
  6 * Long description for file
  7 *
  8 * PHP versions 4 and 5
  9 *
 10 * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
 11 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 12 *
 13 *  Licensed under The Open Group Test Suite License
 14 *  Redistributions of files must retain the above copyright notice.
 15 *
 16 * @filesource
 17 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 18 * @link          https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
 19 * @package       cake
 20 * @subpackage    cake.tests.cases.libs.view.helpers
 21 * @since         CakePHP(tm) v 1.2.0.4206
 22 * @version       $Revision$
 23 * @modifiedby    $LastChangedBy$
 24 * @lastmodified  $Date$
 25 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 26 */
 27if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
 28	define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
 29}
 30App::import('Core', array('Controller', 'Model', 'View'));
 31App::import('Helper', 'Cache');
 32/**
 33 * TestCacheHelper class
 34 *
 35 * @package       cake
 36 * @subpackage    cake.tests.cases.libs.view.helpers
 37 */
 38class TestCacheHelper extends CacheHelper {
 39}
 40/**
 41 * CacheTestController class
 42 *
 43 * @package       cake
 44 * @subpackage    cake.tests.cases.libs.view.helpers
 45 */
 46class CacheTestController extends Controller {
 47/**
 48 * helpers property
 49 *
 50 * @var array
 51 * @access public
 52 */
 53	var $helpers = array('Html', 'Cache');
 54/**
 55 * cache_parsing method
 56 *
 57 * @access public
 58 * @return void
 59 */
 60	function cache_parsing() {
 61		$this->viewPath = 'posts';
 62		$this->layout = 'cache_layout';
 63		$this->set('variable', 'variableValue');
 64		$this->set('superman', 'clark kent');
 65	}
 66}
 67/**
 68 * CacheHelperTest class
 69 *
 70 * @package       cake
 71 * @subpackage    cake.tests.cases.libs.view.helpers
 72 */
 73class CacheHelperTest extends CakeTestCase {
 74/**
 75 * setUp method
 76 *
 77 * @access public
 78 * @return void
 79 */
 80	function setUp() {
 81		$this->Controller = new CacheTestController();
 82		$this->Cache = new TestCacheHelper();
 83		Configure::write('Cache.check', true);
 84		Configure::write('Cache.disable', false);
 85	}
 86/**
 87 * Start Case - switch view paths
 88 *
 89 * @access public
 90 * @return void
 91 */
 92	function startCase() {
 93		$this->_viewPaths = Configure::read('viewPaths');
 94		Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
 95	}
 96/**
 97 * End Case - restore view Paths
 98 *
 99 * @access public
100 * @return void
101 */
102	function endCase() {
103		Configure::write('viewPaths', $this->_viewPaths);
104	}
105/**
106 * tearDown method
107 *
108 * @access public
109 * @return void
110 */
111	function tearDown() {
112		unset($this->Cache);
113	}
114/**
115 * test cache parsing with no cake:nocache tags in view file.
116 *
117 * @access public
118 * @return void
119 */
120	function testLayoutCacheParsingNoTagsInView() {
121		$this->Controller->cache_parsing();
122		$this->Controller->cacheAction = 21600;
123		$this->Controller->here = '/cacheTest/cache_parsing';
124		$this->Controller->action = 'cache_parsing';
125
126		$View = new View($this->Controller);
127		$result = $View->render('index');
128		$this->assertNoPattern('/cake:nocache/', $result);
129		$this->assertNoPattern('/php echo/', $result);
130
131		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
132		$this->assertTrue(file_exists($filename));
133
134		$contents = file_get_contents($filename);
135		$this->assertPattern('/php echo \$variable/', $contents);
136		$this->assertPattern('/php echo microtime()/', $contents);
137		$this->assertPattern('/clark kent/', $result);
138
139		@unlink($filename);
140	}
141/**
142 * Test cache parsing with cake:nocache tags in view file.
143 *
144 * @access public
145 * @return void
146 */
147	function testLayoutCacheParsingWithTagsInView() {
148		$this->Controller->cache_parsing();
149		$this->Controller->cacheAction = 21600;
150		$this->Controller->here = '/cacheTest/cache_parsing';
151		$this->Controller->action = 'cache_parsing';
152
153		$View = new View($this->Controller);
154		$result = $View->render('test_nocache_tags');
155		$this->assertNoPattern('/cake:nocache/', $result);
156		$this->assertNoPattern('/php echo/', $result);
157
158		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
159		$this->assertTrue(file_exists($filename));
160
161		$contents = file_get_contents($filename);
162		$this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
163		$this->assertPattern('/php echo \$variable/', $contents);
164		$this->assertPattern('/php echo microtime()/', $contents);
165
166		@unlink($filename);
167	}
168/**
169 * testComplexNoCache method
170 *
171 * @return void
172 * @access public
173 */
174	function testComplexNoCache () {
175		$this->Controller->cache_parsing();
176		$this->Controller->cacheAction = array('cacheTest' => 21600);
177		$this->Controller->here = '/cacheTest/cache_complex';
178		$this->Controller->action = 'cache_complex';
179		$this->Controller->layout = 'multi_cache';
180		$this->Controller->viewPath = 'posts';
181
182		$View = new View($this->Controller);
183		$result = $View->render('sequencial_nocache');
184
185		$this->assertNoPattern('/cake:nocache/', $result);
186		$this->assertNoPattern('/php echo/', $result);
187		$this->assertPattern('/A\. Layout Before Content/', $result);
188		$this->assertPattern('/B\. In Plain Element/', $result);
189		$this->assertPattern('/C\. Layout After Test Element/', $result);
190		$this->assertPattern('/D\. In View File/', $result);
191		$this->assertPattern('/E\. Layout After Content/', $result);
192		//$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
193		$this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
194		$this->assertNoPattern('/1\. layout before content/', $result);
195		$this->assertNoPattern('/2\. in plain element/', $result);
196		$this->assertNoPattern('/3\. layout after test element/', $result);
197		$this->assertNoPattern('/4\. in view file/', $result);
198		$this->assertNoPattern('/5\. layout after content/', $result);
199		//$this->assertNoPattern('/6\. in element with no cache tags/', $result);
200		$this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
201
202		$filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
203		$this->assertTrue(file_exists($filename));
204		$contents = file_get_contents($filename);
205		@unlink($filename);
206
207		$this->assertPattern('/A\. Layout Before Content/', $contents);
208		$this->assertNoPattern('/B\. In Plain Element/', $contents);
209		$this->assertPattern('/C\. Layout After Test Element/', $contents);
210		$this->assertPattern('/D\. In View File/', $contents);
211		$this->assertPattern('/E\. Layout After Content/', $contents);
212		//$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
213		$this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
214		$this->assertPattern('/1\. layout before content/', $contents);
215		$this->assertNoPattern('/2\. in plain element/', $contents);
216		$this->assertPattern('/3\. layout after test element/', $contents);
217		$this->assertPattern('/4\. in view file/', $contents);
218		$this->assertPattern('/5\. layout after content/', $contents);
219		//$this->assertPattern('/6\. in element with no cache tags/', $contents);
220		$this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
221	}
222/**
223 * testCacheEmptySections method
224 *
225 * This test must be uncommented/fixed in next release (1.2+)
226 *
227 * @return void
228 * @access public
229 *
230	function testCacheEmptySections () {
231		$this->Controller->cache_parsing();
232		$this->Controller->cacheAction = array('cacheTest' => 21600);
233		$this->Controller->here = '/cacheTest/cache_empty_sections';
234		$this->Controller->action = 'cache_empty_sections';
235		$this->Controller->layout = 'cache_empty_sections';
236		$this->Controller->viewPath = 'posts';
237
238		$View = new View($this->Controller);
239		$result = $View->render('cache_empty_sections');
240		$this->assertNoPattern('/cake:nocache/', $result);
241		$this->assertNoPattern('/php echo/', $result);
242		$this->assertPattern(
243			'@</title>\s*</head>\s*' .
244			'<body>\s*' .
245			'View Content\s*' .
246			'cached count is: 3\s*' .
247			'</body>@', $result);
248
249		$filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
250		$this->assertTrue(file_exists($filename));
251		$contents = file_get_contents($filename);
252		$this->assertNoPattern('/cake:nocache/', $contents);
253		$this->assertPattern(
254			'@<head>\s*<title>Posts</title>\s*' .
255			"<\?php \$x = 1; \?>\s*" .
256			'</head>\s*' .
257			'<body>\s*' .
258			"<\?php \$x\+\+; \?>\s*" .
259			"<\?php \$x\+\+; \?>\s*" .
260			'View Content\s*' .
261			"<\?php \$y = 1; \?>\s*" .
262			"<\?php echo 'cached count is:' . \$x; \?>\s*" .
263			'@', $contents);
264		@unlink($filename);
265	}
266*/
267}
268?>