統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / View / Helper / CacheHelperTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (18.967 KB)

1
<?php
2
/**
3
 * CacheHelperTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.View.Helper
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('Controller', 'Controller');
20
App::uses('Model', 'Model');
21
App::uses('View', 'View');
22
App::uses('CacheHelper', 'View/Helper');
23

    
24
/**
25
 * CacheTestController class
26
 *
27
 * @package       Cake.Test.Case.View.Helper
28
 */
29
class CacheTestController extends Controller {
30

    
31
/**
32
 * helpers property
33
 *
34
 * @var array
35
 */
36
        public $helpers = array('Html', 'Cache');
37

    
38
/**
39
 * cache_parsing method
40
 *
41
 * @return void
42
 */
43
        public function cache_parsing() {
44
                $this->viewPath = 'Posts';
45
                $this->layout = 'cache_layout';
46
                $this->set('variable', 'variableValue');
47
                $this->set('superman', 'clark kent');
48
                $this->set('batman', 'bruce wayne');
49
                $this->set('spiderman', 'peter parker');
50
        }
51

    
52
}
53

    
54
/**
55
 * CacheHelperTest class
56
 *
57
 * @package       Cake.Test.Case.View.Helper
58
 */
59
class CacheHelperTest extends CakeTestCase {
60

    
61
/**
62
 * Checks if TMP/views is writable, and skips the case if it is not.
63
 *
64
 * @return void
65
 */
66
        public function skip() {
67
                if (!is_writable(TMP . 'cache' . DS . 'views' . DS)) {
68
                        $this->markTestSkipped('TMP/views is not writable %s');
69
                }
70
        }
71

    
72
/**
73
 * setUp method
74
 *
75
 * @return void
76
 */
77
        public function setUp() {
78
                parent::setUp();
79
                $_GET = array();
80
                $request = new CakeRequest();
81
                $this->Controller = new CacheTestController($request);
82
                $View = new View($this->Controller);
83
                $this->Cache = new CacheHelper($View);
84
                Configure::write('Cache.check', true);
85
                Configure::write('Cache.disable', false);
86
                App::build(array(
87
                        'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
88
                ), App::RESET);
89
        }
90

    
91
/**
92
 * tearDown method
93
 *
94
 * @return void
95
 */
96
        public function tearDown() {
97
                clearCache();
98
                unset($this->Cache);
99
                parent::tearDown();
100
        }
101

    
102
/**
103
 * test cache parsing with no cake:nocache tags in view file.
104
 *
105
 * @return void
106
 */
107
        public function testLayoutCacheParsingNoTagsInView() {
108
                $this->Controller->cache_parsing();
109
                $this->Controller->request->addParams(array(
110
                        'controller' => 'cache_test',
111
                        'action' => 'cache_parsing',
112
                        'pass' => array(),
113
                        'named' => array()
114
                ));
115
                $this->Controller->cacheAction = 21600;
116
                $this->Controller->request->here = '/cacheTest/cache_parsing';
117
                $this->Controller->request->action = 'cache_parsing';
118

    
119
                $View = new View($this->Controller);
120
                $result = $View->render('index');
121
                $this->assertNotRegExp('/cake:nocache/', $result);
122
                $this->assertNotRegExp('/php echo/', $result);
123

    
124
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
125
                $this->assertTrue(file_exists($filename));
126

    
127
                $contents = file_get_contents($filename);
128
                $this->assertRegExp('/php echo \$variable/', $contents);
129
                $this->assertRegExp('/php echo microtime()/', $contents);
130
                $this->assertRegExp('/clark kent/', $result);
131

    
132
                unlink($filename);
133
        }
134

    
135
/**
136
 * test cache parsing with non-latin characters in current route
137
 *
138
 * @return void
139
 */
140
        public function testCacheNonLatinCharactersInRoute() {
141
                $this->Controller->cache_parsing();
142
                $this->Controller->request->addParams(array(
143
                        'controller' => 'cache_test',
144
                        'action' => 'cache_parsing',
145
                        'pass' => array('風街ろまん'),
146
                        'named' => array()
147
                ));
148
                $this->Controller->cacheAction = 21600;
149
                $this->Controller->request->here = '/posts/view/風街ろまん';
150
                $this->Controller->action = 'view';
151

    
152
                $View = new View($this->Controller);
153
                $View->render('index');
154

    
155
                $filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
156
                $this->assertTrue(file_exists($filename));
157

    
158
                unlink($filename);
159
        }
160

    
161
/**
162
 * Test cache parsing with cake:nocache tags in view file.
163
 *
164
 * @return void
165
 */
166
        public function testLayoutCacheParsingWithTagsInView() {
167
                $this->Controller->cache_parsing();
168
                $this->Controller->request->addParams(array(
169
                        'controller' => 'cache_test',
170
                        'action' => 'cache_parsing',
171
                        'pass' => array(),
172
                        'named' => array()
173
                ));
174
                $this->Controller->cacheAction = 21600;
175
                $this->Controller->request->here = '/cacheTest/cache_parsing';
176
                $this->Controller->action = 'cache_parsing';
177

    
178
                $View = new View($this->Controller);
179
                $result = $View->render('test_nocache_tags');
180
                $this->assertNotRegExp('/cake:nocache/', $result);
181
                $this->assertNotRegExp('/php echo/', $result);
182

    
183
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
184
                $this->assertTrue(file_exists($filename));
185

    
186
                $contents = file_get_contents($filename);
187
                $this->assertRegExp('/if \(is_writable\(TMP\)\)\:/', $contents);
188
                $this->assertRegExp('/php echo \$variable/', $contents);
189
                $this->assertRegExp('/php echo microtime()/', $contents);
190
                $this->assertNotRegExp('/cake:nocache/', $contents);
191

    
192
                unlink($filename);
193
        }
194

    
195
/**
196
 * test that multiple <!--nocache--> tags function with multiple nocache tags in the layout.
197
 *
198
 * @return void
199
 */
200
        public function testMultipleNoCacheTagsInViewfile() {
201
                $this->Controller->cache_parsing();
202
                $this->Controller->request->addParams(array(
203
                        'controller' => 'cache_test',
204
                        'action' => 'cache_parsing',
205
                        'pass' => array(),
206
                        'named' => array()
207
                ));
208
                $this->Controller->cacheAction = 21600;
209
                $this->Controller->request->here = '/cacheTest/cache_parsing';
210
                $this->Controller->action = 'cache_parsing';
211

    
212
                $View = new View($this->Controller);
213
                $result = $View->render('multiple_nocache');
214

    
215
                $this->assertNotRegExp('/cake:nocache/', $result);
216
                $this->assertNotRegExp('/php echo/', $result);
217

    
218
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
219
                $this->assertTrue(file_exists($filename));
220

    
221
                $contents = file_get_contents($filename);
222
                $this->assertNotRegExp('/cake:nocache/', $contents);
223
                unlink($filename);
224
        }
225

    
226
/**
227
 * testComplexNoCache method
228
 *
229
 * @return void
230
 */
231
        public function testComplexNoCache() {
232
                $this->Controller->cache_parsing();
233
                $this->Controller->request->addParams(array(
234
                        'controller' => 'cache_test',
235
                        'action' => 'cache_complex',
236
                        'pass' => array(),
237
                        'named' => array()
238
                ));
239
                $this->Controller->cacheAction = array('cache_complex' => 21600);
240
                $this->Controller->request->here = '/cacheTest/cache_complex';
241
                $this->Controller->action = 'cache_complex';
242
                $this->Controller->layout = 'multi_cache';
243
                $this->Controller->viewPath = 'Posts';
244

    
245
                $View = new View($this->Controller);
246
                $result = $View->render('sequencial_nocache');
247

    
248
                $this->assertNotRegExp('/cake:nocache/', $result);
249
                $this->assertNotRegExp('/php echo/', $result);
250
                $this->assertRegExp('/A\. Layout Before Content/', $result);
251
                $this->assertRegExp('/B\. In Plain Element/', $result);
252
                $this->assertRegExp('/C\. Layout After Test Element/', $result);
253
                $this->assertRegExp('/D\. In View File/', $result);
254
                $this->assertRegExp('/E\. Layout After Content/', $result);
255
                $this->assertRegExp('/F\. In Element With No Cache Tags/', $result);
256
                $this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $result);
257
                $this->assertNotRegExp('/1\. layout before content/', $result);
258
                $this->assertNotRegExp('/2\. in plain element/', $result);
259
                $this->assertNotRegExp('/3\. layout after test element/', $result);
260
                $this->assertNotRegExp('/4\. in view file/', $result);
261
                $this->assertNotRegExp('/5\. layout after content/', $result);
262
                $this->assertNotRegExp('/6\. in element with no cache tags/', $result);
263
                $this->assertNotRegExp('/7\. layout after content and after element with no cache tags/', $result);
264

    
265
                $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
266
                $this->assertTrue(file_exists($filename));
267
                $contents = file_get_contents($filename);
268
                unlink($filename);
269

    
270
                $this->assertRegExp('/A\. Layout Before Content/', $contents);
271
                $this->assertNotRegExp('/B\. In Plain Element/', $contents);
272
                $this->assertRegExp('/C\. Layout After Test Element/', $contents);
273
                $this->assertRegExp('/D\. In View File/', $contents);
274
                $this->assertRegExp('/E\. Layout After Content/', $contents);
275
                $this->assertRegExp('/F\. In Element With No Cache Tags/', $contents);
276
                $this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
277
                $this->assertRegExp('/1\. layout before content/', $contents);
278
                $this->assertNotRegExp('/2\. in plain element/', $contents);
279
                $this->assertRegExp('/3\. layout after test element/', $contents);
280
                $this->assertRegExp('/4\. in view file/', $contents);
281
                $this->assertRegExp('/5\. layout after content/', $contents);
282
                $this->assertRegExp('/6\. in element with no cache tags/', $contents);
283
                $this->assertRegExp('/7\. layout after content and after element with no cache tags/', $contents);
284
        }
285

    
286
/**
287
 * test cache of view vars
288
 *
289
 * @return void
290
 */
291
        public function testCacheViewVars() {
292
                $this->Controller->cache_parsing();
293
                $this->Controller->request->addParams(array(
294
                        'controller' => 'cache_test',
295
                        'action' => 'cache_parsing',
296
                        'pass' => array(),
297
                        'named' => array()
298
                ));
299
                $this->Controller->request->here = '/cacheTest/cache_parsing';
300
                $this->Controller->cacheAction = 21600;
301

    
302
                $View = new View($this->Controller);
303
                $result = $View->render('index');
304
                $this->assertNotRegExp('/cake:nocache/', $result);
305
                $this->assertNotRegExp('/php echo/', $result);
306

    
307
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
308
                $this->assertTrue(file_exists($filename));
309

    
310
                $contents = file_get_contents($filename);
311
                $this->assertRegExp('/\$this\-\>viewVars/', $contents);
312
                $this->assertRegExp('/extract\(\$this\-\>viewVars, EXTR_SKIP\);/', $contents);
313
                $this->assertRegExp('/php echo \$variable/', $contents);
314

    
315
                unlink($filename);
316
        }
317

    
318
/**
319
 * Test that callback code is generated correctly.
320
 *
321
 * @return void
322
 */
323
        public function testCacheCallbacks() {
324
                $this->Controller->request->addParams(array(
325
                        'controller' => 'cache_test',
326
                        'action' => 'cache_parsing',
327
                        'pass' => array(),
328
                        'named' => array()
329
                ));
330
                $this->Controller->cacheAction = array(
331
                        'cache_parsing' => array(
332
                                'duration' => 21600,
333
                                'callbacks' => true
334
                        )
335
                );
336
                $this->Controller->request->here = '/cacheTest/cache_parsing';
337
                $this->Controller->cache_parsing();
338

    
339
                $View = new View($this->Controller);
340
                $View->render('index');
341

    
342
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
343
                $this->assertTrue(file_exists($filename));
344

    
345
                $contents = file_get_contents($filename);
346

    
347
                $this->assertRegExp('/\$controller->startupProcess\(\);/', $contents);
348

    
349
                unlink($filename);
350
        }
351

    
352
/**
353
 * test cacheAction set to a boolean
354
 *
355
 * @return void
356
 */
357
        public function testCacheActionArray() {
358
                $this->Controller->request->addParams(array(
359
                        'controller' => 'cache_test',
360
                        'action' => 'cache_parsing',
361
                        'pass' => array(),
362
                        'named' => array()
363
                ));
364
                $this->Controller->request->here = '/cache_test/cache_parsing';
365
                $this->Controller->cacheAction = array(
366
                        'cache_parsing' => 21600
367
                );
368

    
369
                $this->Controller->cache_parsing();
370

    
371
                $View = new View($this->Controller);
372
                $result = $View->render('index');
373

    
374
                $this->assertNotRegExp('/cake:nocache/', $result);
375
                $this->assertNotRegExp('/php echo/', $result);
376

    
377
                $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
378
                $this->assertTrue(file_exists($filename));
379
                unlink($filename);
380
        }
381

    
382
/**
383
 * Test that cacheAction works with camelcased controller names.
384
 *
385
 * @return void
386
 */
387
        public function testCacheActionArrayCamelCase() {
388
                $this->Controller->request->addParams(array(
389
                        'controller' => 'cache_test',
390
                        'action' => 'cache_parsing',
391
                        'pass' => array(),
392
                        'named' => array()
393
                ));
394
                $this->Controller->cacheAction = array(
395
                        'cache_parsing' => 21600
396
                );
397
                $this->Controller->request->here = '/cacheTest/cache_parsing';
398
                $this->Controller->cache_parsing();
399

    
400
                $View = new View($this->Controller);
401
                $result = $View->render('index');
402

    
403
                $this->assertNotRegExp('/cake:nocache/', $result);
404
                $this->assertNotRegExp('/php echo/', $result);
405

    
406
                $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
407
                $this->assertTrue(file_exists($filename));
408
                unlink($filename);
409
        }
410

    
411
/**
412
 * test with named and pass args.
413
 *
414
 * @return void
415
 */
416
        public function testCacheWithNamedAndPassedArgs() {
417
                Router::reload();
418

    
419
                $this->Controller->cache_parsing();
420
                $this->Controller->request->addParams(array(
421
                        'controller' => 'cache_test',
422
                        'action' => 'cache_parsing',
423
                        'pass' => array(1, 2),
424
                        'named' => array(
425
                                'name' => 'mark',
426
                                'ice' => 'cream'
427
                        )
428
                ));
429
                $this->Controller->cacheAction = array(
430
                        'cache_parsing' => 21600
431
                );
432
                $this->Controller->request->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
433

    
434
                $View = new View($this->Controller);
435
                $result = $View->render('index');
436

    
437
                $this->assertNotRegExp('/cake:nocache/', $result);
438
                $this->assertNotRegExp('/php echo/', $result);
439

    
440
                $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
441
                $this->assertTrue(file_exists($filename));
442
                unlink($filename);
443
        }
444

    
445
/**
446
 * Test that query string parameters are included in the cache filename.
447
 *
448
 * @return void
449
 */
450
        public function testCacheWithQueryStringParams() {
451
                Router::reload();
452

    
453
                $this->Controller->cache_parsing();
454
                $this->Controller->request->addParams(array(
455
                        'controller' => 'cache_test',
456
                        'action' => 'cache_parsing',
457
                        'pass' => array(),
458
                        'named' => array()
459
                ));
460
                $this->Controller->request->query = array('q' => 'cakephp');
461
                $this->Controller->cacheAction = array(
462
                        'cache_parsing' => 21600
463
                );
464
                $this->Controller->request->here = '/cache_test/cache_parsing';
465

    
466
                $View = new View($this->Controller);
467
                $result = $View->render('index');
468

    
469
                $this->assertNotRegExp('/cake:nocache/', $result);
470
                $this->assertNotRegExp('/php echo/', $result);
471

    
472
                $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_q_cakephp.php';
473
                $this->assertTrue(file_exists($filename), 'Missing cache file ' . $filename);
474
                unlink($filename);
475
        }
476

    
477
/**
478
 * test that custom routes are respected when generating cache files.
479
 *
480
 * @return void
481
 */
482
        public function testCacheWithCustomRoutes() {
483
                Router::reload();
484
                Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
485

    
486
                $this->Controller->cache_parsing();
487
                $this->Controller->request->addParams(array(
488
                        'lang' => 'en',
489
                        'controller' => 'cache_test',
490
                        'action' => 'cache_parsing',
491
                        'pass' => array(),
492
                        'named' => array()
493
                ));
494
                $this->Controller->cacheAction = array(
495
                        'cache_parsing' => 21600
496
                );
497
                $this->Controller->request->here = '/en/cache_test/cache_parsing';
498
                $this->Controller->action = 'cache_parsing';
499

    
500
                $View = new View($this->Controller);
501
                $result = $View->render('index');
502

    
503
                $this->assertNotRegExp('/cake:nocache/', $result);
504
                $this->assertNotRegExp('/php echo/', $result);
505

    
506
                $filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
507
                $this->assertTrue(file_exists($filename));
508
                unlink($filename);
509
        }
510

    
511
/**
512
 * test ControllerName contains AppName
513
 *
514
 * This test verifies view cache is created correctly when the app name is contained in part of the controller name.
515
 * (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
516
 * apps URL would look something like http://localhost/cache/cacheTest/cache_name
517
 *
518
 * @return void
519
 */
520
        public function testCacheBaseNameControllerName() {
521
                $this->Controller->cache_parsing();
522
                $this->Controller->cacheAction = array(
523
                        'cache_name' => 21600
524
                );
525
                $this->Controller->params = array(
526
                        'controller' => 'cacheTest',
527
                        'action' => 'cache_name',
528
                        'pass' => array(),
529
                        'named' => array()
530
                );
531
                $this->Controller->here = '/cache/cacheTest/cache_name';
532
                $this->Controller->action = 'cache_name';
533
                $this->Controller->base = '/cache';
534

    
535
                $View = new View($this->Controller);
536
                $result = $View->render('index');
537

    
538
                $this->assertNotRegExp('/cake:nocache/', $result);
539
                $this->assertNotRegExp('/php echo/', $result);
540

    
541
                $filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
542
                $this->assertTrue(file_exists($filename));
543
                unlink($filename);
544
        }
545

    
546
/**
547
 * test that afterRender checks the conditions correctly.
548
 *
549
 * @return void
550
 */
551
        public function testAfterRenderConditions() {
552
                Configure::write('Cache.check', true);
553
                $View = new View($this->Controller);
554
                $View->cacheAction = '+1 day';
555
                $View->output = 'test';
556

    
557
                $Cache = $this->getMock('CacheHelper', array('_parseContent'), array($View));
558
                $Cache->expects($this->once())
559
                        ->method('_parseContent')
560
                        ->with('posts/index', 'content')
561
                        ->will($this->returnValue(''));
562

    
563
                $Cache->afterRenderFile('posts/index', 'content');
564

    
565
                Configure::write('Cache.check', false);
566
                $Cache->afterRender('posts/index');
567

    
568
                Configure::write('Cache.check', true);
569
                $View->cacheAction = false;
570
                $Cache->afterRender('posts/index');
571
        }
572

    
573
/**
574
 * test that afterRender checks the conditions correctly.
575
 *
576
 * @return void
577
 */
578
        public function testAfterLayoutConditions() {
579
                Configure::write('Cache.check', true);
580
                $View = new View($this->Controller);
581
                $View->cacheAction = '+1 day';
582
                $View->output = 'test';
583

    
584
                $Cache = $this->getMock('CacheHelper', array('cache'), array($View));
585
                $Cache->expects($this->once())
586
                        ->method('cache')
587
                        ->with('posts/index', $View->output)
588
                        ->will($this->returnValue(''));
589

    
590
                $Cache->afterLayout('posts/index');
591

    
592
                Configure::write('Cache.check', false);
593
                $Cache->afterLayout('posts/index');
594

    
595
                Configure::write('Cache.check', true);
596
                $View->cacheAction = false;
597
                $Cache->afterLayout('posts/index');
598
        }
599

    
600
/**
601
 * testCacheEmptySections method
602
 *
603
 * This test must be uncommented/fixed in next release (1.2+)
604
 *
605
 * @return void
606
 */
607
        public function testCacheEmptySections() {
608
                $this->Controller->cache_parsing();
609
                $this->Controller->params = array(
610
                        'controller' => 'cacheTest',
611
                        'action' => 'cache_empty_sections',
612
                        'pass' => array(),
613
                        'named' => array()
614
                );
615
                $this->Controller->cacheAction = array('cache_empty_sections' => 21600);
616
                $this->Controller->here = '/cacheTest/cache_empty_sections';
617
                $this->Controller->action = 'cache_empty_sections';
618
                $this->Controller->layout = 'cache_empty_sections';
619
                $this->Controller->viewPath = 'Posts';
620

    
621
                $View = new View($this->Controller);
622
                $result = $View->render('cache_empty_sections');
623
                $this->assertNotRegExp('/nocache/', $result);
624
                $this->assertNotRegExp('/php echo/', $result);
625
                $this->assertRegExp(
626
                        '@</title>\s*</head>\s*' .
627
                        '<body>\s*' .
628
                        'View Content\s*' .
629
                        'cached count is: 3\s*' .
630
                        '</body>@', $result);
631

    
632
                $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
633
                $this->assertTrue(file_exists($filename));
634
                $contents = file_get_contents($filename);
635
                $this->assertNotRegExp('/nocache/', $contents);
636
                $this->assertRegExp(
637
                        '@<head>\s*<title>Posts</title>\s*' .
638
                        '<\?php \$x \= 1; \?>\s*' .
639
                        '</head>\s*' .
640
                        '<body>\s*' .
641
                        '<\?php \$x\+\+; \?>\s*' .
642
                        '<\?php \$x\+\+; \?>\s*' .
643
                        'View Content\s*' .
644
                        '<\?php \$y = 1; \?>\s*' .
645
                        '<\?php echo \'cached count is: \' . \$x; \?>\s*' .
646
                        '@', $contents);
647
                unlink($filename);
648
        }
649
}