PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/cache.test.php

http://github.com/klevo/wildflower
PHP | 365 lines | 181 code | 35 blank | 149 comment | 1 complexity | e63dec649e771ff95e241069aa4ff431 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * CacheHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  18. * @package cake
  19. * @subpackage cake.tests.cases.libs.view.helpers
  20. * @since CakePHP(tm) v 1.2.0.4206
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  27. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  28. }
  29. App::import('Core', array('Controller', 'Model', 'View'));
  30. App::import('Helper', 'Cache');
  31. /**
  32. * CacheTestController class
  33. *
  34. * @package cake
  35. * @subpackage cake.tests.cases.libs.view.helpers
  36. */
  37. class CacheTestController extends Controller {
  38. /**
  39. * helpers property
  40. *
  41. * @var array
  42. * @access public
  43. */
  44. var $helpers = array('Html', 'Cache');
  45. /**
  46. * cache_parsing method
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function cache_parsing() {
  52. $this->viewPath = 'posts';
  53. $this->layout = 'cache_layout';
  54. $this->set('variable', 'variableValue');
  55. $this->set('superman', 'clark kent');
  56. $this->set('batman', 'bruce wayne');
  57. $this->set('spiderman', 'peter parker');
  58. }
  59. }
  60. /**
  61. * CacheHelperTest class
  62. *
  63. * @package cake
  64. * @subpackage cake.tests.cases.libs.view.helpers
  65. */
  66. class CacheHelperTest extends CakeTestCase {
  67. /**
  68. * setUp method
  69. *
  70. * @access public
  71. * @return void
  72. */
  73. function setUp() {
  74. $this->Controller = new CacheTestController();
  75. $this->Cache = new CacheHelper();
  76. $this->_cacheSettings = Configure::read('Cache');
  77. Configure::write('Cache.check', true);
  78. Configure::write('Cache.disable', false);
  79. }
  80. /**
  81. * Start Case - switch view paths
  82. *
  83. * @access public
  84. * @return void
  85. */
  86. function startCase() {
  87. $this->_viewPaths = Configure::read('viewPaths');
  88. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  89. }
  90. /**
  91. * End Case - restore view Paths
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. function endCase() {
  97. Configure::write('viewPaths', $this->_viewPaths);
  98. }
  99. /**
  100. * tearDown method
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. function tearDown() {
  106. unset($this->Cache);
  107. Configure::write('Cache', $this->_cacheSettings);
  108. }
  109. /**
  110. * test cache parsing with no cake:nocache tags in view file.
  111. *
  112. * @access public
  113. * @return void
  114. */
  115. function testLayoutCacheParsingNoTagsInView() {
  116. $this->Controller->cache_parsing();
  117. $this->Controller->cacheAction = 21600;
  118. $this->Controller->here = '/cacheTest/cache_parsing';
  119. $this->Controller->action = 'cache_parsing';
  120. $View = new View($this->Controller);
  121. $result = $View->render('index');
  122. $this->assertNoPattern('/cake:nocache/', $result);
  123. $this->assertNoPattern('/php echo/', $result);
  124. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  125. $this->assertTrue(file_exists($filename));
  126. $contents = file_get_contents($filename);
  127. $this->assertPattern('/php echo \$variable/', $contents);
  128. $this->assertPattern('/php echo microtime()/', $contents);
  129. $this->assertPattern('/clark kent/', $result);
  130. @unlink($filename);
  131. }
  132. /**
  133. * Test cache parsing with cake:nocache tags in view file.
  134. *
  135. * @access public
  136. * @return void
  137. */
  138. function testLayoutCacheParsingWithTagsInView() {
  139. $this->Controller->cache_parsing();
  140. $this->Controller->cacheAction = 21600;
  141. $this->Controller->here = '/cacheTest/cache_parsing';
  142. $this->Controller->action = 'cache_parsing';
  143. $View = new View($this->Controller);
  144. $result = $View->render('test_nocache_tags');
  145. $this->assertNoPattern('/cake:nocache/', $result);
  146. $this->assertNoPattern('/php echo/', $result);
  147. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  148. $this->assertTrue(file_exists($filename));
  149. $contents = file_get_contents($filename);
  150. $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
  151. $this->assertPattern('/php echo \$variable/', $contents);
  152. $this->assertPattern('/php echo microtime()/', $contents);
  153. $this->assertNoPattern('/cake:nocache/', $contents);
  154. @unlink($filename);
  155. }
  156. /**
  157. * test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
  158. *
  159. * @return void
  160. **/
  161. function testMultipleNoCacheTagsInViewfile() {
  162. $this->Controller->cache_parsing();
  163. $this->Controller->cacheAction = 21600;
  164. $this->Controller->here = '/cacheTest/cache_parsing';
  165. $this->Controller->action = 'cache_parsing';
  166. $View = new View($this->Controller);
  167. $result = $View->render('multiple_nocache');
  168. $this->assertNoPattern('/cake:nocache/', $result);
  169. $this->assertNoPattern('/php echo/', $result);
  170. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  171. $this->assertTrue(file_exists($filename));
  172. $contents = file_get_contents($filename);
  173. $this->assertNoPattern('/cake:nocache/', $contents);
  174. @unlink($filename);
  175. }
  176. /**
  177. * testComplexNoCache method
  178. *
  179. * @return void
  180. * @access public
  181. */
  182. function testComplexNoCache () {
  183. $this->Controller->cache_parsing();
  184. $this->Controller->cacheAction = array('cache_complex' => 21600);
  185. $this->Controller->here = '/cacheTest/cache_complex';
  186. $this->Controller->action = 'cache_complex';
  187. $this->Controller->layout = 'multi_cache';
  188. $this->Controller->viewPath = 'posts';
  189. $View = new View($this->Controller);
  190. $result = $View->render('sequencial_nocache');
  191. $this->assertNoPattern('/cake:nocache/', $result);
  192. $this->assertNoPattern('/php echo/', $result);
  193. $this->assertPattern('/A\. Layout Before Content/', $result);
  194. $this->assertPattern('/B\. In Plain Element/', $result);
  195. $this->assertPattern('/C\. Layout After Test Element/', $result);
  196. $this->assertPattern('/D\. In View File/', $result);
  197. $this->assertPattern('/E\. Layout After Content/', $result);
  198. //$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
  199. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
  200. $this->assertNoPattern('/1\. layout before content/', $result);
  201. $this->assertNoPattern('/2\. in plain element/', $result);
  202. $this->assertNoPattern('/3\. layout after test element/', $result);
  203. $this->assertNoPattern('/4\. in view file/', $result);
  204. $this->assertNoPattern('/5\. layout after content/', $result);
  205. //$this->assertNoPattern('/6\. in element with no cache tags/', $result);
  206. $this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
  207. $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
  208. $this->assertTrue(file_exists($filename));
  209. $contents = file_get_contents($filename);
  210. @unlink($filename);
  211. $this->assertPattern('/A\. Layout Before Content/', $contents);
  212. $this->assertNoPattern('/B\. In Plain Element/', $contents);
  213. $this->assertPattern('/C\. Layout After Test Element/', $contents);
  214. $this->assertPattern('/D\. In View File/', $contents);
  215. $this->assertPattern('/E\. Layout After Content/', $contents);
  216. //$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
  217. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
  218. $this->assertPattern('/1\. layout before content/', $contents);
  219. $this->assertNoPattern('/2\. in plain element/', $contents);
  220. $this->assertPattern('/3\. layout after test element/', $contents);
  221. $this->assertPattern('/4\. in view file/', $contents);
  222. $this->assertPattern('/5\. layout after content/', $contents);
  223. //$this->assertPattern('/6\. in element with no cache tags/', $contents);
  224. $this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
  225. }
  226. /**
  227. * test cacheAction set to a boolean
  228. *
  229. * @return void
  230. **/
  231. function testCacheActionArray() {
  232. $this->Controller->cache_parsing();
  233. $this->Controller->cacheAction = array(
  234. 'cache_parsing' => 21600
  235. );
  236. $this->Controller->here = '/cache_test/cache_parsing';
  237. $this->Controller->action = 'cache_parsing';
  238. $View = new View($this->Controller);
  239. $result = $View->render('index');
  240. $this->assertNoPattern('/cake:nocache/', $result);
  241. $this->assertNoPattern('/php echo/', $result);
  242. $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
  243. $this->assertTrue(file_exists($filename));
  244. @unlink($filename);
  245. $this->Controller->cache_parsing();
  246. $this->Controller->cacheAction = array(
  247. 'cache_parsing/' => 21600
  248. );
  249. $this->Controller->here = '/cacheTest/cache_parsing';
  250. $this->Controller->action = 'cache_parsing';
  251. $View = new View($this->Controller);
  252. $result = $View->render('index');
  253. $this->assertNoPattern('/cake:nocache/', $result);
  254. $this->assertNoPattern('/php echo/', $result);
  255. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  256. $this->assertTrue(file_exists($filename));
  257. @unlink($filename);
  258. $this->Controller->cache_parsing();
  259. $this->Controller->cacheAction = array(
  260. 'cache_parsing/33' => 21600
  261. );
  262. $this->Controller->here = '/cacheTest/cache_parsing/33';
  263. $this->Controller->action = 'cache_parsing';
  264. $View = new View($this->Controller);
  265. $result = $View->render('index');
  266. $this->assertNoPattern('/cake:nocache/', $result);
  267. $this->assertNoPattern('/php echo/', $result);
  268. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing_33.php';
  269. $this->assertTrue(file_exists($filename));
  270. @unlink($filename);
  271. $this->Controller->cache_parsing();
  272. $this->Controller->cacheAction = array(
  273. 'cache_parsing/33' => 21600
  274. );
  275. $this->Controller->here = '/cacheTest/cache_parsing';
  276. $this->Controller->action = 'cache_parsing';
  277. $View = new View($this->Controller);
  278. $result = $View->render('index');
  279. $this->assertNoPattern('/cake:nocache/', $result);
  280. $this->assertNoPattern('/php echo/', $result);
  281. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  282. $this->assertFalse(file_exists($filename));
  283. }
  284. /**
  285. * testCacheEmptySections method
  286. *
  287. * This test must be uncommented/fixed in next release (1.2+)
  288. *
  289. * @return void
  290. * @access public
  291. *
  292. function testCacheEmptySections () {
  293. $this->Controller->cache_parsing();
  294. $this->Controller->cacheAction = array('cacheTest' => 21600);
  295. $this->Controller->here = '/cacheTest/cache_empty_sections';
  296. $this->Controller->action = 'cache_empty_sections';
  297. $this->Controller->layout = 'cache_empty_sections';
  298. $this->Controller->viewPath = 'posts';
  299. $View = new View($this->Controller);
  300. $result = $View->render('cache_empty_sections');
  301. $this->assertNoPattern('/cake:nocache/', $result);
  302. $this->assertNoPattern('/php echo/', $result);
  303. $this->assertPattern(
  304. '@</title>\s*</head>\s*' .
  305. '<body>\s*' .
  306. 'View Content\s*' .
  307. 'cached count is: 3\s*' .
  308. '</body>@', $result);
  309. $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
  310. $this->assertTrue(file_exists($filename));
  311. $contents = file_get_contents($filename);
  312. $this->assertNoPattern('/cake:nocache/', $contents);
  313. $this->assertPattern(
  314. '@<head>\s*<title>Posts</title>\s*' .
  315. "<\?php \$x = 1; \?>\s*" .
  316. '</head>\s*' .
  317. '<body>\s*' .
  318. "<\?php \$x\+\+; \?>\s*" .
  319. "<\?php \$x\+\+; \?>\s*" .
  320. 'View Content\s*' .
  321. "<\?php \$y = 1; \?>\s*" .
  322. "<\?php echo 'cached count is:' . \$x; \?>\s*" .
  323. '@', $contents);
  324. @unlink($filename);
  325. }
  326. */
  327. }
  328. ?>