Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 645 lines (560 sloc) 19.235 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
<?php
/**
* CacheHelperTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('Controller', 'Controller');
App::uses('Model', 'Model');
App::uses('View', 'View');
App::uses('CacheHelper', 'View/Helper');

/**
* CacheTestController class
*
* @package Cake.Test.Case.View.Helper
*/
class CacheTestController extends Controller {

/**
* helpers property
*
* @var array
*/
public $helpers = array('Html', 'Cache');

/**
* cache_parsing method
*
* @return void
*/
public function cache_parsing() {
$this->viewPath = 'Posts';
$this->layout = 'cache_layout';
$this->set('variable', 'variableValue');
$this->set('superman', 'clark kent');
$this->set('batman', 'bruce wayne');
$this->set('spiderman', 'peter parker');
}
}

/**
* CacheHelperTest class
*
* @package Cake.Test.Case.View.Helper
*/
class CacheHelperTest extends CakeTestCase {

/**
* Checks if TMP/views is writable, and skips the case if it is not.
*
* @return void
*/
public function skip() {
if (!is_writable(TMP . 'cache' . DS . 'views' . DS)) {
$this->markTestSkipped('TMP/views is not writable %s');
}
}

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$_GET = array();
$request = new CakeRequest();
$this->Controller = new CacheTestController($request);
$View = new View($this->Controller);
$this->Cache = new CacheHelper($View);
Configure::write('Cache.check', true);
Configure::write('Cache.disable', false);
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
), true);
}

/**
* tearDown method
*
* @return void
*/
public function tearDown() {
clearCache();
unset($this->Cache);
parent::tearDown();
}

/**
* test cache parsing with no cake:nocache tags in view file.
*
* @return void
*/
public function testLayoutCacheParsingNoTagsInView() {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = 21600;
$this->Controller->request->here = '/cacheTest/cache_parsing';
$this->Controller->request->action = 'cache_parsing';

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

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

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

@unlink($filename);
}

/**
* test cache parsing with non-latin characters in current route
*
* @return void
*/
public function testCacheNonLatinCharactersInRoute() {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array('風街ろまん'),
'named' => array()
));
$this->Controller->cacheAction = 21600;
$this->Controller->request->here = '/posts/view/風街ろまん';
$this->Controller->action = 'view';

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

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

@unlink($filename);
}

/**
* Test cache parsing with cake:nocache tags in view file.
*
* @return void
*/
public function testLayoutCacheParsingWithTagsInView() {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = 21600;
$this->Controller->request->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

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

@unlink($filename);
}

/**
* test that multiple <!--nocache--> tags function with multiple nocache tags in the layout.
*
* @return void
*/
public function testMultipleNoCacheTagsInViewfile() {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = 21600;
$this->Controller->request->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

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

$contents = file_get_contents($filename);
$this->assertNotRegExp('/cake:nocache/', $contents);
@unlink($filename);
}

/**
* testComplexNoCache method
*
* @return void
*/
public function testComplexNoCache () {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_complex',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = array('cache_complex' => 21600);
$this->Controller->request->here = '/cacheTest/cache_complex';
$this->Controller->action = 'cache_complex';
$this->Controller->layout = 'multi_cache';
$this->Controller->viewPath = 'Posts';

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

$this->assertNotRegExp('/cake:nocache/', $result);
$this->assertNotRegExp('/php echo/', $result);
$this->assertRegExp('/A\. Layout Before Content/', $result);
$this->assertRegExp('/B\. In Plain Element/', $result);
$this->assertRegExp('/C\. Layout After Test Element/', $result);
$this->assertRegExp('/D\. In View File/', $result);
$this->assertRegExp('/E\. Layout After Content/', $result);
$this->assertRegExp('/F\. In Element With No Cache Tags/', $result);
$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $result);
$this->assertNotRegExp('/1\. layout before content/', $result);
$this->assertNotRegExp('/2\. in plain element/', $result);
$this->assertNotRegExp('/3\. layout after test element/', $result);
$this->assertNotRegExp('/4\. in view file/', $result);
$this->assertNotRegExp('/5\. layout after content/', $result);
$this->assertNotRegExp('/6\. in element with no cache tags/', $result);
$this->assertNotRegExp('/7\. layout after content and after element with no cache tags/', $result);

$filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
$this->assertTrue(file_exists($filename));
$contents = file_get_contents($filename);
@unlink($filename);

$this->assertRegExp('/A\. Layout Before Content/', $contents);
$this->assertNotRegExp('/B\. In Plain Element/', $contents);
$this->assertRegExp('/C\. Layout After Test Element/', $contents);
$this->assertRegExp('/D\. In View File/', $contents);
$this->assertRegExp('/E\. Layout After Content/', $contents);
$this->assertRegExp('/F\. In Element With No Cache Tags/', $contents);
$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
$this->assertRegExp('/1\. layout before content/', $contents);
$this->assertNotRegExp('/2\. in plain element/', $contents);
$this->assertRegExp('/3\. layout after test element/', $contents);
$this->assertRegExp('/4\. in view file/', $contents);
$this->assertRegExp('/5\. layout after content/', $contents);
$this->assertRegExp('/6\. in element with no cache tags/', $contents);
$this->assertRegExp('/7\. layout after content and after element with no cache tags/', $contents);
}

/**
* test cache of view vars
*
* @return void
*/
public function testCacheViewVars() {
$this->Controller->cache_parsing();
$this->Controller->params = array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

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

@unlink($filename);
}

/**
* Test that callback code is generated correctly.
*
* @return void
*/
public function testCacheCallbacks() {
$this->Controller->cache_parsing();
$this->Controller->params = array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = array(
'cache_parsing' => array(
'duration' => 21600,
'callbacks' => true
)
);
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

$contents = file_get_contents($filename);

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

@unlink($filename);
}

/**
* test cacheAction set to a boolean
*
* @return void
*/
public function testCacheActionArray() {
$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = array(
'cache_parsing' => 21600
);
$this->Controller->request->here = '/cache_test/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);


$this->Controller->cache_parsing();
$this->Controller->cacheAction = array(
'cache_parsing' => 21600
);
$this->Controller->request->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);


$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = array(
'some_other_action' => 21600
);
$this->Controller->request->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertFalse(file_exists($filename));
}

/**
* test with named and pass args.
*
* @return void
*/
public function testCacheWithNamedAndPassedArgs() {
Router::reload();

$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(1, 2),
'named' => array(
'name' => 'mark',
'ice' => 'cream'
)
));
$this->Controller->cacheAction = array(
'cache_parsing' => 21600
);
$this->Controller->request->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';

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

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

$filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
}

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

$this->Controller->cache_parsing();
$this->Controller->request->addParams(array(
'lang' => 'en',
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
));
$this->Controller->cacheAction = array(
'cache_parsing' => 21600
);
$this->Controller->request->here = '/en/cache_test/cache_parsing';
$this->Controller->action = 'cache_parsing';

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

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

$filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
}

/**
* test ControllerName contains AppName
*
* This test verifys view cache is created correctly when the app name is contained in part of the controller name.
* (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
* apps url would look somehing like http://localhost/cache/cacheTest/cache_name
*
* @return void
**/
public function testCacheBaseNameControllerName() {
$this->Controller->cache_parsing();
$this->Controller->cacheAction = array(
'cache_name' => 21600
);
$this->Controller->params = array(
'controller' => 'cacheTest',
'action' => 'cache_name',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->here = '/cache/cacheTest/cache_name';
$this->Controller->action = 'cache_name';
$this->Controller->base = '/cache';

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

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

$filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
    }

/**
* test that afterRender checks the conditions correctly.
*
* @return void
*/
public function testAfterRenderConditions() {
Configure::write('Cache.check', true);
$View = new View($this->Controller);
$View->cacheAction = '+1 day';
$View->output = 'test';

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

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

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

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

/**
* test that afterRender checks the conditions correctly.
*
* @return void
*/
public function testAfterLayoutConditions() {
Configure::write('Cache.check', true);
$View = new View($this->Controller);
$View->cacheAction = '+1 day';
$View->output = 'test';

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

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

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

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

/**
* testCacheEmptySections method
*
* This test must be uncommented/fixed in next release (1.2+)
*
* @return void
*/
public function testCacheEmptySections() {
$this->Controller->cache_parsing();
$this->Controller->params = array(
'controller' => 'cacheTest',
'action' => 'cache_empty_sections',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = array('cache_empty_sections' => 21600);
$this->Controller->here = '/cacheTest/cache_empty_sections';
$this->Controller->action = 'cache_empty_sections';
$this->Controller->layout = 'cache_empty_sections';
$this->Controller->viewPath = 'Posts';

$View = new View($this->Controller);
$result = $View->render('cache_empty_sections');
$this->assertNotRegExp('/nocache/', $result);
$this->assertNotRegExp('/php echo/', $result);
$this->assertRegExp(
'@</title>\s*</head>\s*' .
'<body>\s*' .
'View Content\s*' .
'cached count is: 3\s*' .
'</body>@', $result);

$filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
$this->assertTrue(file_exists($filename));
$contents = file_get_contents($filename);
$this->assertNotRegExp('/nocache/', $contents);
$this->assertRegExp(
'@<head>\s*<title>Posts</title>\s*' .
'<\?php \$x \= 1; \?>\s*' .
'</head>\s*' .
'<body>\s*' .
'<\?php \$x\+\+; \?>\s*' .
'<\?php \$x\+\+; \?>\s*' .
'View Content\s*' .
'<\?php \$y = 1; \?>\s*' .
'<\?php echo \'cached count is: \' . \$x; \?>\s*' .
'@', $contents);
@unlink($filename);
}
}
Something went wrong with that request. Please try again.