PageRenderTime 125ms CodeModel.GetById 41ms app.highlight 34ms RepoModel.GetById 42ms app.codeStats 1ms

/lib/Cake/Test/Case/View/Helper/CacheHelperTest.php

https://bitbucket.org/LeThanhDat/firstdummyapp
PHP | 651 lines | 427 code | 85 blank | 139 comment | 1 complexity | 8008705d9316681d57afad2f6f4ab26d MD5 | raw file
  1<?php
  2/**
  3 * CacheHelperTest file
  4 *
  5 * PHP 5
  6 *
  7 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9 *
 10 * Licensed under The MIT License
 11 * For full copyright and license information, please see the LICENSE.txt
 12 * Redistributions of files must retain the above copyright notice
 13 *
 14 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
 16 * @package       Cake.Test.Case.View.Helper
 17 * @since         CakePHP(tm) v 1.2.0.4206
 18 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19 */
 20
 21App::uses('Controller', 'Controller');
 22App::uses('Model', 'Model');
 23App::uses('View', 'View');
 24App::uses('CacheHelper', 'View/Helper');
 25
 26/**
 27 * CacheTestController class
 28 *
 29 * @package       Cake.Test.Case.View.Helper
 30 */
 31class CacheTestController extends Controller {
 32
 33/**
 34 * helpers property
 35 *
 36 * @var array
 37 */
 38	public $helpers = array('Html', 'Cache');
 39
 40/**
 41 * cache_parsing method
 42 *
 43 * @return void
 44 */
 45	public 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/**
 57 * CacheHelperTest class
 58 *
 59 * @package       Cake.Test.Case.View.Helper
 60 */
 61class 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	public function skip() {
 69		if (!is_writable(TMP . 'cache' . DS . 'views' . DS)) {
 70			$this->markTestSkipped('TMP/views is not writable %s');
 71		}
 72	}
 73
 74/**
 75 * setUp method
 76 *
 77 * @return void
 78 */
 79	public function setUp() {
 80		parent::setUp();
 81		$_GET = array();
 82		$request = new CakeRequest();
 83		$this->Controller = new CacheTestController($request);
 84		$View = new View($this->Controller);
 85		$this->Cache = new CacheHelper($View);
 86		Configure::write('Cache.check', true);
 87		Configure::write('Cache.disable', false);
 88		App::build(array(
 89			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
 90		), App::RESET);
 91	}
 92
 93/**
 94 * tearDown method
 95 *
 96 * @return void
 97 */
 98	public function tearDown() {
 99		clearCache();
100		unset($this->Cache);
101		parent::tearDown();
102	}
103
104/**
105 * test cache parsing with no cake:nocache tags in view file.
106 *
107 * @return void
108 */
109	public function testLayoutCacheParsingNoTagsInView() {
110		$this->Controller->cache_parsing();
111		$this->Controller->request->addParams(array(
112			'controller' => 'cache_test',
113			'action' => 'cache_parsing',
114			'pass' => array(),
115			'named' => array()
116		));
117		$this->Controller->cacheAction = 21600;
118		$this->Controller->request->here = '/cacheTest/cache_parsing';
119		$this->Controller->request->action = 'cache_parsing';
120
121		$View = new View($this->Controller);
122		$result = $View->render('index');
123		$this->assertNotRegExp('/cake:nocache/', $result);
124		$this->assertNotRegExp('/php echo/', $result);
125
126		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
127		$this->assertTrue(file_exists($filename));
128
129		$contents = file_get_contents($filename);
130		$this->assertRegExp('/php echo \$variable/', $contents);
131		$this->assertRegExp('/php echo microtime()/', $contents);
132		$this->assertRegExp('/clark kent/', $result);
133
134		unlink($filename);
135	}
136
137/**
138 * test cache parsing with non-latin characters in current route
139 *
140 * @return void
141 */
142	public function testCacheNonLatinCharactersInRoute() {
143		$this->Controller->cache_parsing();
144		$this->Controller->request->addParams(array(
145			'controller' => 'cache_test',
146			'action' => 'cache_parsing',
147			'pass' => array('風街ろまん'),
148			'named' => array()
149		));
150		$this->Controller->cacheAction = 21600;
151		$this->Controller->request->here = '/posts/view/風街ろまん';
152		$this->Controller->action = 'view';
153
154		$View = new View($this->Controller);
155		$View->render('index');
156
157		$filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
158		$this->assertTrue(file_exists($filename));
159
160		unlink($filename);
161	}
162
163/**
164 * Test cache parsing with cake:nocache tags in view file.
165 *
166 * @return void
167 */
168	public function testLayoutCacheParsingWithTagsInView() {
169		$this->Controller->cache_parsing();
170		$this->Controller->request->addParams(array(
171			'controller' => 'cache_test',
172			'action' => 'cache_parsing',
173			'pass' => array(),
174			'named' => array()
175		));
176		$this->Controller->cacheAction = 21600;
177		$this->Controller->request->here = '/cacheTest/cache_parsing';
178		$this->Controller->action = 'cache_parsing';
179
180		$View = new View($this->Controller);
181		$result = $View->render('test_nocache_tags');
182		$this->assertNotRegExp('/cake:nocache/', $result);
183		$this->assertNotRegExp('/php echo/', $result);
184
185		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
186		$this->assertTrue(file_exists($filename));
187
188		$contents = file_get_contents($filename);
189		$this->assertRegExp('/if \(is_writable\(TMP\)\)\:/', $contents);
190		$this->assertRegExp('/php echo \$variable/', $contents);
191		$this->assertRegExp('/php echo microtime()/', $contents);
192		$this->assertNotRegExp('/cake:nocache/', $contents);
193
194		unlink($filename);
195	}
196
197/**
198 * test that multiple <!--nocache--> tags function with multiple nocache tags in the layout.
199 *
200 * @return void
201 */
202	public function testMultipleNoCacheTagsInViewfile() {
203		$this->Controller->cache_parsing();
204		$this->Controller->request->addParams(array(
205			'controller' => 'cache_test',
206			'action' => 'cache_parsing',
207			'pass' => array(),
208			'named' => array()
209		));
210		$this->Controller->cacheAction = 21600;
211		$this->Controller->request->here = '/cacheTest/cache_parsing';
212		$this->Controller->action = 'cache_parsing';
213
214		$View = new View($this->Controller);
215		$result = $View->render('multiple_nocache');
216
217		$this->assertNotRegExp('/cake:nocache/', $result);
218		$this->assertNotRegExp('/php echo/', $result);
219
220		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
221		$this->assertTrue(file_exists($filename));
222
223		$contents = file_get_contents($filename);
224		$this->assertNotRegExp('/cake:nocache/', $contents);
225		unlink($filename);
226	}
227
228/**
229 * testComplexNoCache method
230 *
231 * @return void
232 */
233	public function testComplexNoCache() {
234		$this->Controller->cache_parsing();
235		$this->Controller->request->addParams(array(
236			'controller' => 'cache_test',
237			'action' => 'cache_complex',
238			'pass' => array(),
239			'named' => array()
240		));
241		$this->Controller->cacheAction = array('cache_complex' => 21600);
242		$this->Controller->request->here = '/cacheTest/cache_complex';
243		$this->Controller->action = 'cache_complex';
244		$this->Controller->layout = 'multi_cache';
245		$this->Controller->viewPath = 'Posts';
246
247		$View = new View($this->Controller);
248		$result = $View->render('sequencial_nocache');
249
250		$this->assertNotRegExp('/cake:nocache/', $result);
251		$this->assertNotRegExp('/php echo/', $result);
252		$this->assertRegExp('/A\. Layout Before Content/', $result);
253		$this->assertRegExp('/B\. In Plain Element/', $result);
254		$this->assertRegExp('/C\. Layout After Test Element/', $result);
255		$this->assertRegExp('/D\. In View File/', $result);
256		$this->assertRegExp('/E\. Layout After Content/', $result);
257		$this->assertRegExp('/F\. In Element With No Cache Tags/', $result);
258		$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $result);
259		$this->assertNotRegExp('/1\. layout before content/', $result);
260		$this->assertNotRegExp('/2\. in plain element/', $result);
261		$this->assertNotRegExp('/3\. layout after test element/', $result);
262		$this->assertNotRegExp('/4\. in view file/', $result);
263		$this->assertNotRegExp('/5\. layout after content/', $result);
264		$this->assertNotRegExp('/6\. in element with no cache tags/', $result);
265		$this->assertNotRegExp('/7\. layout after content and after element with no cache tags/', $result);
266
267		$filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
268		$this->assertTrue(file_exists($filename));
269		$contents = file_get_contents($filename);
270		unlink($filename);
271
272		$this->assertRegExp('/A\. Layout Before Content/', $contents);
273		$this->assertNotRegExp('/B\. In Plain Element/', $contents);
274		$this->assertRegExp('/C\. Layout After Test Element/', $contents);
275		$this->assertRegExp('/D\. In View File/', $contents);
276		$this->assertRegExp('/E\. Layout After Content/', $contents);
277		$this->assertRegExp('/F\. In Element With No Cache Tags/', $contents);
278		$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
279		$this->assertRegExp('/1\. layout before content/', $contents);
280		$this->assertNotRegExp('/2\. in plain element/', $contents);
281		$this->assertRegExp('/3\. layout after test element/', $contents);
282		$this->assertRegExp('/4\. in view file/', $contents);
283		$this->assertRegExp('/5\. layout after content/', $contents);
284		$this->assertRegExp('/6\. in element with no cache tags/', $contents);
285		$this->assertRegExp('/7\. layout after content and after element with no cache tags/', $contents);
286	}
287
288/**
289 * test cache of view vars
290 *
291 * @return void
292 */
293	public function testCacheViewVars() {
294		$this->Controller->cache_parsing();
295		$this->Controller->request->addParams(array(
296			'controller' => 'cache_test',
297			'action' => 'cache_parsing',
298			'pass' => array(),
299			'named' => array()
300		));
301		$this->Controller->request->here = '/cacheTest/cache_parsing';
302		$this->Controller->cacheAction = 21600;
303
304		$View = new View($this->Controller);
305		$result = $View->render('index');
306		$this->assertNotRegExp('/cake:nocache/', $result);
307		$this->assertNotRegExp('/php echo/', $result);
308
309		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
310		$this->assertTrue(file_exists($filename));
311
312		$contents = file_get_contents($filename);
313		$this->assertRegExp('/\$this\-\>viewVars/', $contents);
314		$this->assertRegExp('/extract\(\$this\-\>viewVars, EXTR_SKIP\);/', $contents);
315		$this->assertRegExp('/php echo \$variable/', $contents);
316
317		unlink($filename);
318	}
319
320/**
321 * Test that callback code is generated correctly.
322 *
323 * @return void
324 */
325	public function testCacheCallbacks() {
326		$this->Controller->request->addParams(array(
327			'controller' => 'cache_test',
328			'action' => 'cache_parsing',
329			'pass' => array(),
330			'named' => array()
331		));
332		$this->Controller->cacheAction = array(
333			'cache_parsing' => array(
334				'duration' => 21600,
335				'callbacks' => true
336			)
337		);
338		$this->Controller->request->here = '/cacheTest/cache_parsing';
339		$this->Controller->cache_parsing();
340
341		$View = new View($this->Controller);
342		$View->render('index');
343
344		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
345		$this->assertTrue(file_exists($filename));
346
347		$contents = file_get_contents($filename);
348
349		$this->assertRegExp('/\$controller->startupProcess\(\);/', $contents);
350
351		unlink($filename);
352	}
353
354/**
355 * test cacheAction set to a boolean
356 *
357 * @return void
358 */
359	public function testCacheActionArray() {
360		$this->Controller->request->addParams(array(
361			'controller' => 'cache_test',
362			'action' => 'cache_parsing',
363			'pass' => array(),
364			'named' => array()
365		));
366		$this->Controller->request->here = '/cache_test/cache_parsing';
367		$this->Controller->cacheAction = array(
368			'cache_parsing' => 21600
369		);
370
371		$this->Controller->cache_parsing();
372
373		$View = new View($this->Controller);
374		$result = $View->render('index');
375
376		$this->assertNotRegExp('/cake:nocache/', $result);
377		$this->assertNotRegExp('/php echo/', $result);
378
379		$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
380		$this->assertTrue(file_exists($filename));
381		unlink($filename);
382	}
383
384/**
385 * Test that cacheAction works with camelcased controller names.
386 *
387 * @return void
388 */
389	public function testCacheActionArrayCamelCase() {
390		$this->Controller->request->addParams(array(
391			'controller' => 'cache_test',
392			'action' => 'cache_parsing',
393			'pass' => array(),
394			'named' => array()
395		));
396		$this->Controller->cacheAction = array(
397			'cache_parsing' => 21600
398		);
399		$this->Controller->request->here = '/cacheTest/cache_parsing';
400		$this->Controller->cache_parsing();
401
402		$View = new View($this->Controller);
403		$result = $View->render('index');
404
405		$this->assertNotRegExp('/cake:nocache/', $result);
406		$this->assertNotRegExp('/php echo/', $result);
407
408		$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
409		$this->assertTrue(file_exists($filename));
410		unlink($filename);
411	}
412
413/**
414 * test with named and pass args.
415 *
416 * @return void
417 */
418	public function testCacheWithNamedAndPassedArgs() {
419		Router::reload();
420
421		$this->Controller->cache_parsing();
422		$this->Controller->request->addParams(array(
423			'controller' => 'cache_test',
424			'action' => 'cache_parsing',
425			'pass' => array(1, 2),
426			'named' => array(
427				'name' => 'mark',
428				'ice' => 'cream'
429			)
430		));
431		$this->Controller->cacheAction = array(
432			'cache_parsing' => 21600
433		);
434		$this->Controller->request->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
435
436		$View = new View($this->Controller);
437		$result = $View->render('index');
438
439		$this->assertNotRegExp('/cake:nocache/', $result);
440		$this->assertNotRegExp('/php echo/', $result);
441
442		$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
443		$this->assertTrue(file_exists($filename));
444		unlink($filename);
445	}
446
447/**
448 * Test that query string parameters are included in the cache filename.
449 *
450 * @return void
451 */
452	public function testCacheWithQueryStringParams() {
453		Router::reload();
454
455		$this->Controller->cache_parsing();
456		$this->Controller->request->addParams(array(
457			'controller' => 'cache_test',
458			'action' => 'cache_parsing',
459			'pass' => array(),
460			'named' => array()
461		));
462		$this->Controller->request->query = array('q' => 'cakephp');
463		$this->Controller->cacheAction = array(
464			'cache_parsing' => 21600
465		);
466		$this->Controller->request->here = '/cache_test/cache_parsing';
467
468		$View = new View($this->Controller);
469		$result = $View->render('index');
470
471		$this->assertNotRegExp('/cake:nocache/', $result);
472		$this->assertNotRegExp('/php echo/', $result);
473
474		$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_q_cakephp.php';
475		$this->assertTrue(file_exists($filename), 'Missing cache file ' . $filename);
476		unlink($filename);
477	}
478
479/**
480 * test that custom routes are respected when generating cache files.
481 *
482 * @return void
483 */
484	public function testCacheWithCustomRoutes() {
485		Router::reload();
486		Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
487
488		$this->Controller->cache_parsing();
489		$this->Controller->request->addParams(array(
490			'lang' => 'en',
491			'controller' => 'cache_test',
492			'action' => 'cache_parsing',
493			'pass' => array(),
494			'named' => array()
495		));
496		$this->Controller->cacheAction = array(
497			'cache_parsing' => 21600
498		);
499		$this->Controller->request->here = '/en/cache_test/cache_parsing';
500		$this->Controller->action = 'cache_parsing';
501
502		$View = new View($this->Controller);
503		$result = $View->render('index');
504
505		$this->assertNotRegExp('/cake:nocache/', $result);
506		$this->assertNotRegExp('/php echo/', $result);
507
508		$filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
509		$this->assertTrue(file_exists($filename));
510		unlink($filename);
511	}
512
513/**
514 * test ControllerName contains AppName
515 *
516 * This test verifies view cache is created correctly when the app name is contained in part of the controller name.
517 * (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
518 * apps url would look something like http://localhost/cache/cacheTest/cache_name
519 *
520 * @return void
521 */
522	public function testCacheBaseNameControllerName() {
523		$this->Controller->cache_parsing();
524		$this->Controller->cacheAction = array(
525			'cache_name' => 21600
526		);
527		$this->Controller->params = array(
528			'controller' => 'cacheTest',
529			'action' => 'cache_name',
530			'pass' => array(),
531			'named' => array()
532		);
533		$this->Controller->here = '/cache/cacheTest/cache_name';
534		$this->Controller->action = 'cache_name';
535		$this->Controller->base = '/cache';
536
537		$View = new View($this->Controller);
538		$result = $View->render('index');
539
540		$this->assertNotRegExp('/cake:nocache/', $result);
541		$this->assertNotRegExp('/php echo/', $result);
542
543		$filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
544		$this->assertTrue(file_exists($filename));
545		unlink($filename);
546	}
547
548/**
549 * test that afterRender checks the conditions correctly.
550 *
551 * @return void
552 */
553	public function testAfterRenderConditions() {
554		Configure::write('Cache.check', true);
555		$View = new View($this->Controller);
556		$View->cacheAction = '+1 day';
557		$View->output = 'test';
558
559		$Cache = $this->getMock('CacheHelper', array('_parseContent'), array($View));
560		$Cache->expects($this->once())
561			->method('_parseContent')
562			->with('posts/index', 'content')
563			->will($this->returnValue(''));
564
565		$Cache->afterRenderFile('posts/index', 'content');
566
567		Configure::write('Cache.check', false);
568		$Cache->afterRender('posts/index');
569
570		Configure::write('Cache.check', true);
571		$View->cacheAction = false;
572		$Cache->afterRender('posts/index');
573	}
574
575/**
576 * test that afterRender checks the conditions correctly.
577 *
578 * @return void
579 */
580	public function testAfterLayoutConditions() {
581		Configure::write('Cache.check', true);
582		$View = new View($this->Controller);
583		$View->cacheAction = '+1 day';
584		$View->output = 'test';
585
586		$Cache = $this->getMock('CacheHelper', array('cache'), array($View));
587		$Cache->expects($this->once())
588			->method('cache')
589			->with('posts/index', $View->output)
590			->will($this->returnValue(''));
591
592		$Cache->afterLayout('posts/index');
593
594		Configure::write('Cache.check', false);
595		$Cache->afterLayout('posts/index');
596
597		Configure::write('Cache.check', true);
598		$View->cacheAction = false;
599		$Cache->afterLayout('posts/index');
600	}
601
602/**
603 * testCacheEmptySections method
604 *
605 * This test must be uncommented/fixed in next release (1.2+)
606 *
607 * @return void
608 */
609	public function testCacheEmptySections() {
610		$this->Controller->cache_parsing();
611		$this->Controller->params = array(
612			'controller' => 'cacheTest',
613			'action' => 'cache_empty_sections',
614			'pass' => array(),
615			'named' => array()
616		);
617		$this->Controller->cacheAction = array('cache_empty_sections' => 21600);
618		$this->Controller->here = '/cacheTest/cache_empty_sections';
619		$this->Controller->action = 'cache_empty_sections';
620		$this->Controller->layout = 'cache_empty_sections';
621		$this->Controller->viewPath = 'Posts';
622
623		$View = new View($this->Controller);
624		$result = $View->render('cache_empty_sections');
625		$this->assertNotRegExp('/nocache/', $result);
626		$this->assertNotRegExp('/php echo/', $result);
627		$this->assertRegExp(
628			'@</title>\s*</head>\s*' .
629			'<body>\s*' .
630			'View Content\s*' .
631			'cached count is: 3\s*' .
632			'</body>@', $result);
633
634		$filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
635		$this->assertTrue(file_exists($filename));
636		$contents = file_get_contents($filename);
637		$this->assertNotRegExp('/nocache/', $contents);
638		$this->assertRegExp(
639			'@<head>\s*<title>Posts</title>\s*' .
640			'<\?php \$x \= 1; \?>\s*' .
641			'</head>\s*' .
642			'<body>\s*' .
643			'<\?php \$x\+\+; \?>\s*' .
644			'<\?php \$x\+\+; \?>\s*' .
645			'View Content\s*' .
646			'<\?php \$y = 1; \?>\s*' .
647			'<\?php echo \'cached count is: \' . \$x; \?>\s*' .
648			'@', $contents);
649		unlink($filename);
650	}
651}