/lib/Cake/Test/Case/View/Helper/CacheHelperTest.php
PHP | 651 lines | 427 code | 85 blank | 139 comment | 1 complexity | 8008705d9316681d57afad2f6f4ab26d MD5 | raw file
- <?php
- /**
- * CacheHelperTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html 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)
- ), App::RESET);
- }
- /**
- * 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',
- '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',
- 'pass' => array('風街ろまん'),
- 'named' => array()
- ));
- $this->Controller->cacheAction = 21600;
- $this->Controller->request->here = '/posts/view/風街ろまん';
- $this->Controller->action = 'view';
- $View = new View($this->Controller);
- $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',
- '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',
- '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',
- '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);
-