~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/view/helpers/cache.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 cacheAction set to a boolean
 
313
 *
 
314
 * @return void
 
315
 */
 
316
        function testCacheActionArray() {
 
317
                $this->Controller->cache_parsing();
 
318
                $this->Controller->params = array(
 
319
                        'controller' => 'cache_test',
 
320
                        'action' => 'cache_parsing',
 
321
                        'url' => array(),
 
322
                        'pass' => array(),
 
323
                        'named' => array()
 
324
                );
 
325
                $this->Controller->cacheAction = array(
 
326
                        'cache_parsing' => 21600
 
327
                );
 
328
                $this->Controller->here = '/cache_test/cache_parsing';
 
329
                $this->Controller->action = 'cache_parsing';
 
330
 
 
331
                $View = new View($this->Controller);
 
332
                $result = $View->render('index');
 
333
 
 
334
                $this->assertNoPattern('/cake:nocache/', $result);
 
335
                $this->assertNoPattern('/php echo/', $result);
 
336
 
 
337
                $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
 
338
                $this->assertTrue(file_exists($filename));
 
339
                @unlink($filename);
 
340
 
 
341
 
 
342
                $this->Controller->cache_parsing();
 
343
                $this->Controller->cacheAction = array(
 
344
                        'cache_parsing' => 21600
 
345
                );
 
346
                $this->Controller->here = '/cacheTest/cache_parsing';
 
347
                $this->Controller->action = 'cache_parsing';
 
348
 
 
349
                $View = new View($this->Controller);
 
350
                $result = $View->render('index');
 
351
 
 
352
                $this->assertNoPattern('/cake:nocache/', $result);
 
353
                $this->assertNoPattern('/php echo/', $result);
 
354
 
 
355
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
 
356
                $this->assertTrue(file_exists($filename));
 
357
                @unlink($filename);
 
358
 
 
359
 
 
360
                $this->Controller->cache_parsing();
 
361
                $this->Controller->params = array(
 
362
                        'controller' => 'cache_test',
 
363
                        'action' => 'cache_parsing',
 
364
                        'url' => array(),
 
365
                        'pass' => array(),
 
366
                        'named' => array()
 
367
                );
 
368
                $this->Controller->cacheAction = array(
 
369
                        'some_other_action' => 21600
 
370
                );
 
371
                $this->Controller->here = '/cacheTest/cache_parsing';
 
372
                $this->Controller->action = 'cache_parsing';
 
373
 
 
374
                $View = new View($this->Controller);
 
375
                $result = $View->render('index');
 
376
 
 
377
                $this->assertNoPattern('/cake:nocache/', $result);
 
378
                $this->assertNoPattern('/php echo/', $result);
 
379
 
 
380
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
 
381
                $this->assertFalse(file_exists($filename));
 
382
        }
 
383
 
 
384
/**
 
385
 * test with named and pass args.
 
386
 *
 
387
 * @return void
 
388
 */
 
389
        function testCacheWithNamedAndPassedArgs() {
 
390
                Router::reload();
 
391
 
 
392
                $this->Controller->cache_parsing();
 
393
                $this->Controller->params = array(
 
394
                        'controller' => 'cache_test',
 
395
                        'action' => 'cache_parsing',
 
396
                        'url' => array(),
 
397
                        'pass' => array(1, 2),
 
398
                        'named' => array(
 
399
                                'name' => 'mark',
 
400
                                'ice' => 'cream'
 
401
                        )
 
402
                );
 
403
                $this->Controller->cacheAction = array(
 
404
                        'cache_parsing' => 21600
 
405
                );
 
406
                $this->Controller->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
 
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 . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
 
416
                $this->assertTrue(file_exists($filename));
 
417
                @unlink($filename);
 
418
        }
 
419
 
 
420
/**
 
421
 * test that custom routes are respected when generating cache files.
 
422
 *
 
423
 * @return void
 
424
 */
 
425
        function testCacheWithCustomRoutes() {
 
426
                Router::reload();
 
427
                Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
 
428
                
 
429
                $this->Controller->cache_parsing();
 
430
                $this->Controller->params = array(
 
431
                        'lang' => 'en',
 
432
                        'controller' => 'cache_test',
 
433
                        'action' => 'cache_parsing',
 
434
                        'url' => array(),
 
435
                        'pass' => array(),
 
436
                        'named' => array()
 
437
                );
 
438
                $this->Controller->cacheAction = array(
 
439
                        'cache_parsing' => 21600
 
440
                );
 
441
                $this->Controller->here = '/en/cache_test/cache_parsing';
 
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 . 'en_cache_test_cache_parsing.php';
 
451
                $this->assertTrue(file_exists($filename));
 
452
                @unlink($filename);
 
453
        }
 
454
 
 
455
/**
 
456
 * test ControllerName contains AppName
 
457
 *
 
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
 
461
 *
 
462
 * @return void
 
463
 **/
 
464
        function testCacheBaseNameControllerName() {
 
465
                $this->Controller->cache_parsing();
 
466
                $this->Controller->cacheAction = array(
 
467
                        'cache_name' => 21600
 
468
                );
 
469
                $this->Controller->params = array(
 
470
                        'controller' => 'cacheTest',
 
471
                        'action' => 'cache_name',
 
472
                        'url' => array(),
 
473
                        'pass' => array(),
 
474
                        'named' => array()
 
475
                );
 
476
                $this->Controller->here = '/cache/cacheTest/cache_name';
 
477
                $this->Controller->action = 'cache_name';
 
478
                $this->Controller->base = '/cache';
 
479
 
 
480
                $View = new View($this->Controller);
 
481
                $result = $View->render('index');
 
482
 
 
483
                $this->assertNoPattern('/cake:nocache/', $result);
 
484
                $this->assertNoPattern('/php echo/', $result);
 
485
 
 
486
                $filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
 
487
                $this->assertTrue(file_exists($filename));
 
488
                @unlink($filename);
 
489
    }
 
490
 
 
491
/**
 
492
 * testCacheEmptySections method
 
493
 *
 
494
 * This test must be uncommented/fixed in next release (1.2+)
 
495
 *
 
496
 * @return void
 
497
 * @access public
 
498
 *
 
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';
 
506
 
 
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*' .
 
513
                        '<body>\s*' .
 
514
                        'View Content\s*' .
 
515
                        'cached count is: 3\s*' .
 
516
                        '</body>@', $result);
 
517
 
 
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*" .
 
525
                        '</head>\s*' .
 
526
                        '<body>\s*' .
 
527
                        "<\?php \$x\+\+; \?>\s*" .
 
528
                        "<\?php \$x\+\+; \?>\s*" .
 
529
                        'View Content\s*' .
 
530
                        "<\?php \$y = 1; \?>\s*" .
 
531
                        "<\?php echo 'cached count is:' . \$x; \?>\s*" .
 
532
                        '@', $contents);
 
533
                @unlink($filename);
 
534
        }
 
535
*/
 
536
}