comparison cake/tests/cases/libs/view/helpers/cache.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * CacheHelperTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs.view.helpers
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', array('Controller', 'Model', 'View'));
21 App::import('Helper', 'Cache');
22
23 /**
24 * CacheTestController class
25 *
26 * @package cake
27 * @subpackage cake.tests.cases.libs.view.helpers
28 */
29 class CacheTestController extends Controller {
30
31 /**
32 * helpers property
33 *
34 * @var array
35 * @access public
36 */
37 var $helpers = array('Html', 'Cache');
38
39 /**
40 * cache_parsing method
41 *
42 * @access public
43 * @return void
44 */
45 function cache_parsing() {
46 $this->viewPath = 'posts';
47 $this->layout = 'cache_layout';
48 $this->set('variable', 'variableValue');
49 $this->set('superman', 'clark kent');
50 $this->set('batman', 'bruce wayne');
51 $this->set('spiderman', 'peter parker');
52 }
53 }
54
55 /**
56 * CacheHelperTest class
57 *
58 * @package cake
59 * @subpackage cake.tests.cases.libs.view.helpers
60 */
61 class CacheHelperTest extends CakeTestCase {
62
63 /**
64 * Checks if TMP/views is writable, and skips the case if it is not.
65 *
66 * @return void
67 */
68 function skip() {
69 $this->skipUnless(is_writable(TMP . 'cache' . DS . 'views' . DS), 'TMP/views is not writable %s');
70 }
71 /**
72 * setUp method
73 *
74 * @access public
75 * @return void
76 */
77 function setUp() {
78 $this->Controller = new CacheTestController();
79 $this->Cache = new CacheHelper();
80 $this->_cacheSettings = Configure::read('Cache');
81 Configure::write('Cache.check', true);
82 Configure::write('Cache.disable', false);
83 }
84
85 /**
86 * Start Case - switch view paths
87 *
88 * @access public
89 * @return void
90 */
91 function startCase() {
92 App::build(array(
93 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
94 ), true);
95 }
96
97 /**
98 * End Case - restore view Paths
99 *
100 * @access public
101 * @return void
102 */
103 function endCase() {
104 App::build();
105 }
106
107 /**
108 * tearDown method
109 *
110 * @access public
111 * @return void
112 */
113 function tearDown() {
114 clearCache();
115 unset($this->Cache);
116 Configure::write('Cache', $this->_cacheSettings);
117 }
118
119 /**
120 * test cache parsing with no cake:nocache tags in view file.
121 *
122 * @access public
123 * @return void
124 */
125 function testLayoutCacheParsingNoTagsInView() {
126 $this->Controller->cache_parsing();
127 $this->Controller->params = array(
128 'controller' => 'cache_test',
129 'action' => 'cache_parsing',
130 'url' => array(),
131 'pass' => array(),
132 'named' => array()
133 );
134 $this->Controller->cacheAction = 21600;
135 $this->Controller->here = '/cacheTest/cache_parsing';
136 $this->Controller->action = 'cache_parsing';
137
138 $View = new View($this->Controller);
139 $result = $View->render('index');
140 $this->assertNoPattern('/cake:nocache/', $result);
141 $this->assertNoPattern('/php echo/', $result);
142
143 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
144 $this->assertTrue(file_exists($filename));
145
146 $contents = file_get_contents($filename);
147 $this->assertPattern('/php echo \$variable/', $contents);
148 $this->assertPattern('/php echo microtime()/', $contents);
149 $this->assertPattern('/clark kent/', $result);
150
151 @unlink($filename);
152 }
153
154 /**
155 * test cache parsing with non-latin characters in current route
156 *
157 * @access public
158 * @return void
159 */
160 function testCacheNonLatinCharactersInRoute() {
161 $this->Controller->cache_parsing();
162 $this->Controller->params = array(
163 'controller' => 'cache_test',
164 'action' => 'cache_parsing',
165 'url' => array(),
166 'pass' => array('風街ろまん'),
167 'named' => array()
168 );
169 $this->Controller->cacheAction = 21600;
170 $this->Controller->here = '/posts/view/風街ろまん';
171 $this->Controller->action = 'view';
172
173 $View = new View($this->Controller);
174 $result = $View->render('index');
175
176 $filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
177 $this->assertTrue(file_exists($filename));
178
179 @unlink($filename);
180 }
181 /**
182 * Test cache parsing with cake:nocache tags in view file.
183 *
184 * @access public
185 * @return void
186 */
187 function testLayoutCacheParsingWithTagsInView() {
188 $this->Controller->cache_parsing();
189 $this->Controller->params = array(
190 'controller' => 'cache_test',
191 'action' => 'cache_parsing',
192 'url' => array(),
193 'pass' => array(),
194 'named' => array()
195 );
196 $this->Controller->cacheAction = 21600;
197 $this->Controller->here = '/cacheTest/cache_parsing';
198 $this->Controller->action = 'cache_parsing';
199
200 $View = new View($this->Controller);
201 $result = $View->render('test_nocache_tags');
202 $this->assertNoPattern('/cake:nocache/', $result);
203 $this->assertNoPattern('/php echo/', $result);
204
205 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
206 $this->assertTrue(file_exists($filename));
207
208 $contents = file_get_contents($filename);
209 $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
210 $this->assertPattern('/php echo \$variable/', $contents);
211 $this->assertPattern('/php echo microtime()/', $contents);
212 $this->assertNoPattern('/cake:nocache/', $contents);
213
214 @unlink($filename);
215 }
216
217 /**
218 * test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
219 *
220 * @return void
221 */
222 function testMultipleNoCacheTagsInViewfile() {
223 $this->Controller->cache_parsing();
224 $this->Controller->params = array(
225 'controller' => 'cache_test',
226 'action' => 'cache_parsing',
227 'url' => array(),
228 'pass' => array(),
229 'named' => array()
230 );
231 $this->Controller->cacheAction = 21600;
232 $this->Controller->here = '/cacheTest/cache_parsing';
233 $this->Controller->action = 'cache_parsing';
234
235 $View = new View($this->Controller);
236 $result = $View->render('multiple_nocache');
237
238 $this->assertNoPattern('/cake:nocache/', $result);
239 $this->assertNoPattern('/php echo/', $result);
240
241 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
242 $this->assertTrue(file_exists($filename));
243
244 $contents = file_get_contents($filename);
245 $this->assertNoPattern('/cake:nocache/', $contents);
246 @unlink($filename);
247 }
248
249 /**
250 * testComplexNoCache method
251 *
252 * @return void
253 * @access public
254 */
255 function testComplexNoCache () {
256 $this->Controller->cache_parsing();
257 $this->Controller->params = array(
258 'controller' => 'cache_test',
259 'action' => 'cache_complex',
260 'url' => array(),
261 'pass' => array(),
262 'named' => array()
263 );
264 $this->Controller->cacheAction = array('cache_complex' => 21600);
265 $this->Controller->here = '/cacheTest/cache_complex';
266 $this->Controller->action = 'cache_complex';
267 $this->Controller->layout = 'multi_cache';
268 $this->Controller->viewPath = 'posts';
269
270 $View = new View($this->Controller);
271 $result = $View->render('sequencial_nocache');
272
273 $this->assertNoPattern('/cake:nocache/', $result);
274 $this->assertNoPattern('/php echo/', $result);
275 $this->assertPattern('/A\. Layout Before Content/', $result);
276 $this->assertPattern('/B\. In Plain Element/', $result);
277 $this->assertPattern('/C\. Layout After Test Element/', $result);
278 $this->assertPattern('/D\. In View File/', $result);
279 $this->assertPattern('/E\. Layout After Content/', $result);
280 //$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
281 $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
282 $this->assertNoPattern('/1\. layout before content/', $result);
283 $this->assertNoPattern('/2\. in plain element/', $result);
284 $this->assertNoPattern('/3\. layout after test element/', $result);
285 $this->assertNoPattern('/4\. in view file/', $result);
286 $this->assertNoPattern('/5\. layout after content/', $result);
287 //$this->assertNoPattern('/6\. in element with no cache tags/', $result);
288 $this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
289
290 $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
291 $this->assertTrue(file_exists($filename));
292 $contents = file_get_contents($filename);
293 @unlink($filename);
294
295 $this->assertPattern('/A\. Layout Before Content/', $contents);
296 $this->assertNoPattern('/B\. In Plain Element/', $contents);
297 $this->assertPattern('/C\. Layout After Test Element/', $contents);
298 $this->assertPattern('/D\. In View File/', $contents);
299 $this->assertPattern('/E\. Layout After Content/', $contents);
300 //$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
301 $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
302 $this->assertPattern('/1\. layout before content/', $contents);
303 $this->assertNoPattern('/2\. in plain element/', $contents);
304 $this->assertPattern('/3\. layout after test element/', $contents);
305 $this->assertPattern('/4\. in view file/', $contents);
306 $this->assertPattern('/5\. layout after content/', $contents);
307 //$this->assertPattern('/6\. in element with no cache tags/', $contents);
308 $this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
309 }
310
311 /**
312 * test cache of view vars
313 *
314 * @access public
315 * @return void
316 */
317 function testCacheViewVars() {
318 $this->Controller->cache_parsing();
319 $this->Controller->params = array(
320 'controller' => 'cache_test',
321 'action' => 'cache_parsing',
322 'url' => array(),
323 'pass' => array(),
324 'named' => array()
325 );
326 $this->Controller->cacheAction = 21600;
327 $this->Controller->here = '/cacheTest/cache_parsing';
328 $this->Controller->action = 'cache_parsing';
329
330 $View = new View($this->Controller);
331 $result = $View->render('index');
332 $this->assertNoPattern('/cake:nocache/', $result);
333 $this->assertNoPattern('/php echo/', $result);
334
335 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
336 $this->assertTrue(file_exists($filename));
337
338 $contents = file_get_contents($filename);
339 $this->assertPattern('/\$this\-\>viewVars/', $contents);
340 $this->assertPattern('/extract\(\$this\-\>viewVars, EXTR_SKIP\);/', $contents);
341 $this->assertPattern('/php echo \$variable/', $contents);
342
343 @unlink($filename);
344 }
345
346 /**
347 * test cacheAction set to a boolean
348 *
349 * @return void
350 */
351 function testCacheActionArray() {
352 $this->Controller->cache_parsing();
353 $this->Controller->params = array(
354 'controller' => 'cache_test',
355 'action' => 'cache_parsing',
356 'url' => array(),
357 'pass' => array(),
358 'named' => array()
359 );
360 $this->Controller->cacheAction = array(
361 'cache_parsing' => 21600
362 );
363 $this->Controller->here = '/cache_test/cache_parsing';
364 $this->Controller->action = 'cache_parsing';
365
366 $View = new View($this->Controller);
367 $result = $View->render('index');
368
369 $this->assertNoPattern('/cake:nocache/', $result);
370 $this->assertNoPattern('/php echo/', $result);
371
372 $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
373 $this->assertTrue(file_exists($filename));
374 @unlink($filename);
375
376
377 $this->Controller->cache_parsing();
378 $this->Controller->cacheAction = array(
379 'cache_parsing' => 21600
380 );
381 $this->Controller->here = '/cacheTest/cache_parsing';
382 $this->Controller->action = 'cache_parsing';
383
384 $View = new View($this->Controller);
385 $result = $View->render('index');
386
387 $this->assertNoPattern('/cake:nocache/', $result);
388 $this->assertNoPattern('/php echo/', $result);
389
390 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
391 $this->assertTrue(file_exists($filename));
392 @unlink($filename);
393
394
395 $this->Controller->cache_parsing();
396 $this->Controller->params = array(
397 'controller' => 'cache_test',
398 'action' => 'cache_parsing',
399 'url' => array(),
400 'pass' => array(),
401 'named' => array()
402 );
403 $this->Controller->cacheAction = array(
404 'some_other_action' => 21600
405 );
406 $this->Controller->here = '/cacheTest/cache_parsing';
407 $this->Controller->action = 'cache_parsing';
408
409 $View = new View($this->Controller);
410 $result = $View->render('index');
411
412 $this->assertNoPattern('/cake:nocache/', $result);
413 $this->assertNoPattern('/php echo/', $result);
414
415 $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
416 $this->assertFalse(file_exists($filename));
417 }
418
419 /**
420 * test with named and pass args.
421 *
422 * @return void
423 */
424 function testCacheWithNamedAndPassedArgs() {
425 Router::reload();
426
427 $this->Controller->cache_parsing();
428 $this->Controller->params = array(
429 'controller' => 'cache_test',
430 'action' => 'cache_parsing',
431 'url' => array(),
432 'pass' => array(1, 2),
433 'named' => array(
434 'name' => 'mark',
435 'ice' => 'cream'
436 )
437 );
438 $this->Controller->cacheAction = array(
439 'cache_parsing' => 21600
440 );
441 $this->Controller->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
442 $this->Controller->action = 'cache_parsing';
443
444 $View = new View($this->Controller);
445 $result = $View->render('index');
446
447 $this->assertNoPattern('/cake:nocache/', $result);
448 $this->assertNoPattern('/php echo/', $result);
449
450 $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
451 $this->assertTrue(file_exists($filename));
452 @unlink($filename);
453 }
454
455 /**
456 * test that custom routes are respected when generating cache files.
457 *
458 * @return void
459 */
460 function testCacheWithCustomRoutes() {
461 Router::reload();
462 Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
463
464 $this->Controller->cache_parsing();
465 $this->Controller->params = array(
466 'lang' => 'en',
467 'controller' => 'cache_test',
468 'action' => 'cache_parsing',
469 'url' => array(),
470 'pass' => array(),
471 'named' => array()
472 );
473 $this->Controller->cacheAction = array(
474 'cache_parsing' => 21600
475 );
476 $this->Controller->here = '/en/cache_test/cache_parsing';
477 $this->Controller->action = 'cache_parsing';
478
479 $View = new View($this->Controller);
480 $result = $View->render('index');
481
482 $this->assertNoPattern('/cake:nocache/', $result);
483 $this->assertNoPattern('/php echo/', $result);
484
485 $filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
486 $this->assertTrue(file_exists($filename));
487 @unlink($filename);
488 }
489
490 /**
491 * test ControllerName contains AppName
492 *
493 * This test verifys view cache is created correctly when the app name is contained in part of the controller name.
494 * (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
495 * apps url would look somehing like http://localhost/cache/cacheTest/cache_name
496 *
497 * @return void
498 **/
499 function testCacheBaseNameControllerName() {
500 $this->Controller->cache_parsing();
501 $this->Controller->cacheAction = array(
502 'cache_name' => 21600
503 );
504 $this->Controller->params = array(
505 'controller' => 'cacheTest',
506 'action' => 'cache_name',
507 'url' => array(),
508 'pass' => array(),
509 'named' => array()
510 );
511 $this->Controller->here = '/cache/cacheTest/cache_name';
512 $this->Controller->action = 'cache_name';
513 $this->Controller->base = '/cache';
514
515 $View = new View($this->Controller);
516 $result = $View->render('index');
517
518 $this->assertNoPattern('/cake:nocache/', $result);
519 $this->assertNoPattern('/php echo/', $result);
520
521 $filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
522 $this->assertTrue(file_exists($filename));
523 @unlink($filename);
524 }
525
526 /**
527 * testCacheEmptySections method
528 *
529 * This test must be uncommented/fixed in next release (1.2+)
530 *
531 * @return void
532 * @access public
533 *
534 function testCacheEmptySections () {
535 $this->Controller->cache_parsing();
536 $this->Controller->cacheAction = array('cacheTest' => 21600);
537 $this->Controller->here = '/cacheTest/cache_empty_sections';
538 $this->Controller->action = 'cache_empty_sections';
539 $this->Controller->layout = 'cache_empty_sections';
540 $this->Controller->viewPath = 'posts';
541
542 $View = new View($this->Controller);
543 $result = $View->render('cache_empty_sections');
544 $this->assertNoPattern('/cake:nocache/', $result);
545 $this->assertNoPattern('/php echo/', $result);
546 $this->assertPattern(
547 '@</title>\s*</head>\s*' .
548 '<body>\s*' .
549 'View Content\s*' .
550 'cached count is: 3\s*' .
551 '</body>@', $result);
552
553 $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
554 $this->assertTrue(file_exists($filename));
555 $contents = file_get_contents($filename);
556 $this->assertNoPattern('/cake:nocache/', $contents);
557 $this->assertPattern(
558 '@<head>\s*<title>Posts</title>\s*' .
559 "<\?php \$x = 1; \?>\s*" .
560 '</head>\s*' .
561 '<body>\s*' .
562 "<\?php \$x\+\+; \?>\s*" .
563 "<\?php \$x\+\+; \?>\s*" .
564 'View Content\s*' .
565 "<\?php \$y = 1; \?>\s*" .
566 "<\?php echo 'cached count is:' . \$x; \?>\s*" .
567 '@', $contents);
568 @unlink($filename);
569 }
570 */
571 }