7
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The Open Group Test Suite License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
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
20
App::import('Core', array('Controller', 'Model', 'View'));
21
App::import('Helper', 'Cache');
24
* CacheTestController class
27
* @subpackage cake.tests.cases.libs.view.helpers
29
class CacheTestController extends Controller {
37
var $helpers = array('Html', 'Cache');
40
* cache_parsing method
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');
56
* CacheHelperTest class
59
* @subpackage cake.tests.cases.libs.view.helpers
61
class CacheHelperTest extends CakeTestCase {
64
* Checks if TMP/views is writable, and skips the case if it is not.
69
$this->skipUnless(is_writable(TMP . 'cache' . DS . 'views' . DS), 'TMP/views is not writable %s');
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);
86
* Start Case - switch view paths
91
function startCase() {
93
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
98
* End Case - restore view Paths
113
function tearDown() {
116
Configure::write('Cache', $this->_cacheSettings);
120
* test cache parsing with no cake:nocache tags in view file.
125
function testLayoutCacheParsingNoTagsInView() {
126
$this->Controller->cache_parsing();
127
$this->Controller->params = array(
128
'controller' => 'cache_test',
129
'action' => 'cache_parsing',
134
$this->Controller->cacheAction = 21600;
135
$this->Controller->here = '/cacheTest/cache_parsing';
136
$this->Controller->action = 'cache_parsing';
138
$View = new View($this->Controller);
139
$result = $View->render('index');
140
$this->assertNoPattern('/cake:nocache/', $result);
141
$this->assertNoPattern('/php echo/', $result);
143
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
144
$this->assertTrue(file_exists($filename));
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);
155
* test cache parsing with non-latin characters in current route
160
function testCacheNonLatinCharactersInRoute() {
161
$this->Controller->cache_parsing();
162
$this->Controller->params = array(
163
'controller' => 'cache_test',
164
'action' => 'cache_parsing',
166
'pass' => array('風街ろまん'),
169
$this->Controller->cacheAction = 21600;
170
$this->Controller->here = '/posts/view/風街ろまん';
171
$this->Controller->action = 'view';
173
$View = new View($this->Controller);
174
$result = $View->render('index');
176
$filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
177
$this->assertTrue(file_exists($filename));
182
* Test cache parsing with cake:nocache tags in view file.
187
function testLayoutCacheParsingWithTagsInView() {
188
$this->Controller->cache_parsing();
189
$this->Controller->params = array(
190
'controller' => 'cache_test',
191
'action' => 'cache_parsing',
196
$this->Controller->cacheAction = 21600;
197
$this->Controller->here = '/cacheTest/cache_parsing';
198
$this->Controller->action = 'cache_parsing';
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);
205
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
206
$this->assertTrue(file_exists($filename));
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);
218
* test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
222
function testMultipleNoCacheTagsInViewfile() {
223
$this->Controller->cache_parsing();
224
$this->Controller->params = array(
225
'controller' => 'cache_test',
226
'action' => 'cache_parsing',
231
$this->Controller->cacheAction = 21600;
232
$this->Controller->here = '/cacheTest/cache_parsing';
233
$this->Controller->action = 'cache_parsing';
235
$View = new View($this->Controller);
236
$result = $View->render('multiple_nocache');
238
$this->assertNoPattern('/cake:nocache/', $result);
239
$this->assertNoPattern('/php echo/', $result);
241
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
242
$this->assertTrue(file_exists($filename));
244
$contents = file_get_contents($filename);
245
$this->assertNoPattern('/cake:nocache/', $contents);
250
* testComplexNoCache method
255
function testComplexNoCache () {
256
$this->Controller->cache_parsing();
257
$this->Controller->params = array(
258
'controller' => 'cache_test',
259
'action' => 'cache_complex',
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';
270
$View = new View($this->Controller);
271
$result = $View->render('sequencial_nocache');
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);
290
$filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
291
$this->assertTrue(file_exists($filename));
292
$contents = file_get_contents($filename);
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);
312
* test cacheAction set to a boolean
316
function testCacheActionArray() {
317
$this->Controller->cache_parsing();
318
$this->Controller->params = array(
319
'controller' => 'cache_test',
320
'action' => 'cache_parsing',
325
$this->Controller->cacheAction = array(
326
'cache_parsing' => 21600
328
$this->Controller->here = '/cache_test/cache_parsing';
329
$this->Controller->action = 'cache_parsing';
331
$View = new View($this->Controller);
332
$result = $View->render('index');
334
$this->assertNoPattern('/cake:nocache/', $result);
335
$this->assertNoPattern('/php echo/', $result);
337
$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
338
$this->assertTrue(file_exists($filename));
342
$this->Controller->cache_parsing();
343
$this->Controller->cacheAction = array(
344
'cache_parsing' => 21600
346
$this->Controller->here = '/cacheTest/cache_parsing';
347
$this->Controller->action = 'cache_parsing';
349
$View = new View($this->Controller);
350
$result = $View->render('index');
352
$this->assertNoPattern('/cake:nocache/', $result);
353
$this->assertNoPattern('/php echo/', $result);
355
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
356
$this->assertTrue(file_exists($filename));
360
$this->Controller->cache_parsing();
361
$this->Controller->params = array(
362
'controller' => 'cache_test',
363
'action' => 'cache_parsing',
368
$this->Controller->cacheAction = array(
369
'some_other_action' => 21600
371
$this->Controller->here = '/cacheTest/cache_parsing';
372
$this->Controller->action = 'cache_parsing';
374
$View = new View($this->Controller);
375
$result = $View->render('index');
377
$this->assertNoPattern('/cake:nocache/', $result);
378
$this->assertNoPattern('/php echo/', $result);
380
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
381
$this->assertFalse(file_exists($filename));
385
* test with named and pass args.
389
function testCacheWithNamedAndPassedArgs() {
392
$this->Controller->cache_parsing();
393
$this->Controller->params = array(
394
'controller' => 'cache_test',
395
'action' => 'cache_parsing',
397
'pass' => array(1, 2),
403
$this->Controller->cacheAction = array(
404
'cache_parsing' => 21600
406
$this->Controller->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
407
$this->Controller->action = 'cache_parsing';
409
$View = new View($this->Controller);
410
$result = $View->render('index');
412
$this->assertNoPattern('/cake:nocache/', $result);
413
$this->assertNoPattern('/php echo/', $result);
415
$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
416
$this->assertTrue(file_exists($filename));
421
* test that custom routes are respected when generating cache files.
425
function testCacheWithCustomRoutes() {
427
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
429
$this->Controller->cache_parsing();
430
$this->Controller->params = array(
432
'controller' => 'cache_test',
433
'action' => 'cache_parsing',
438
$this->Controller->cacheAction = array(
439
'cache_parsing' => 21600
441
$this->Controller->here = '/en/cache_test/cache_parsing';
442
$this->Controller->action = 'cache_parsing';
444
$View = new View($this->Controller);
445
$result = $View->render('index');
447
$this->assertNoPattern('/cake:nocache/', $result);
448
$this->assertNoPattern('/php echo/', $result);
450
$filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
451
$this->assertTrue(file_exists($filename));
456
* test ControllerName contains AppName
458
* This test verifys view cache is created correctly when the app name is contained in part of the controller name.
459
* (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
460
* apps url would look somehing like http://localhost/cache/cacheTest/cache_name
464
function testCacheBaseNameControllerName() {
465
$this->Controller->cache_parsing();
466
$this->Controller->cacheAction = array(
467
'cache_name' => 21600
469
$this->Controller->params = array(
470
'controller' => 'cacheTest',
471
'action' => 'cache_name',
476
$this->Controller->here = '/cache/cacheTest/cache_name';
477
$this->Controller->action = 'cache_name';
478
$this->Controller->base = '/cache';
480
$View = new View($this->Controller);
481
$result = $View->render('index');
483
$this->assertNoPattern('/cake:nocache/', $result);
484
$this->assertNoPattern('/php echo/', $result);
486
$filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
487
$this->assertTrue(file_exists($filename));
492
* testCacheEmptySections method
494
* This test must be uncommented/fixed in next release (1.2+)
499
function testCacheEmptySections () {
500
$this->Controller->cache_parsing();
501
$this->Controller->cacheAction = array('cacheTest' => 21600);
502
$this->Controller->here = '/cacheTest/cache_empty_sections';
503
$this->Controller->action = 'cache_empty_sections';
504
$this->Controller->layout = 'cache_empty_sections';
505
$this->Controller->viewPath = 'posts';
507
$View = new View($this->Controller);
508
$result = $View->render('cache_empty_sections');
509
$this->assertNoPattern('/cake:nocache/', $result);
510
$this->assertNoPattern('/php echo/', $result);
511
$this->assertPattern(
512
'@</title>\s*</head>\s*' .
515
'cached count is: 3\s*' .
516
'</body>@', $result);
518
$filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
519
$this->assertTrue(file_exists($filename));
520
$contents = file_get_contents($filename);
521
$this->assertNoPattern('/cake:nocache/', $contents);
522
$this->assertPattern(
523
'@<head>\s*<title>Posts</title>\s*' .
524
"<\?php \$x = 1; \?>\s*" .
527
"<\?php \$x\+\+; \?>\s*" .
528
"<\?php \$x\+\+; \?>\s*" .
530
"<\?php \$y = 1; \?>\s*" .
531
"<\?php echo 'cached count is:' . \$x; \?>\s*" .