PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/webpolis/liiv
PHP | 268 lines | 110 code | 12 blank | 146 comment | 1 complexity | 19e1b651ff4f0abfa677d1eb08936d13 MD5 | raw file
Possible License(s): GPL-2.0, 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-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  28. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  29. }
  30. App::import('Core', array('Controller', 'Model', 'View'));
  31. App::import('Helper', 'Cache');
  32. /**
  33. * TestCacheHelper class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases.libs.view.helpers
  37. */
  38. class TestCacheHelper extends CacheHelper {
  39. }
  40. /**
  41. * CacheTestController class
  42. *
  43. * @package cake
  44. * @subpackage cake.tests.cases.libs.view.helpers
  45. */
  46. class CacheTestController extends Controller {
  47. /**
  48. * helpers property
  49. *
  50. * @var array
  51. * @access public
  52. */
  53. var $helpers = array('Html', 'Cache');
  54. /**
  55. * cache_parsing method
  56. *
  57. * @access public
  58. * @return void
  59. */
  60. function cache_parsing() {
  61. $this->viewPath = 'posts';
  62. $this->layout = 'cache_layout';
  63. $this->set('variable', 'variableValue');
  64. $this->set('superman', 'clark kent');
  65. }
  66. }
  67. /**
  68. * CacheHelperTest class
  69. *
  70. * @package cake
  71. * @subpackage cake.tests.cases.libs.view.helpers
  72. */
  73. class CacheHelperTest extends CakeTestCase {
  74. /**
  75. * setUp method
  76. *
  77. * @access public
  78. * @return void
  79. */
  80. function setUp() {
  81. $this->Controller = new CacheTestController();
  82. $this->Cache = new TestCacheHelper();
  83. Configure::write('Cache.check', true);
  84. Configure::write('Cache.disable', false);
  85. }
  86. /**
  87. * Start Case - switch view paths
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. function startCase() {
  93. $this->_viewPaths = Configure::read('viewPaths');
  94. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  95. }
  96. /**
  97. * End Case - restore view Paths
  98. *
  99. * @access public
  100. * @return void
  101. */
  102. function endCase() {
  103. Configure::write('viewPaths', $this->_viewPaths);
  104. }
  105. /**
  106. * tearDown method
  107. *
  108. * @access public
  109. * @return void
  110. */
  111. function tearDown() {
  112. unset($this->Cache);
  113. }
  114. /**
  115. * test cache parsing with no cake:nocache tags in view file.
  116. *
  117. * @access public
  118. * @return void
  119. */
  120. function testLayoutCacheParsingNoTagsInView() {
  121. $this->Controller->cache_parsing();
  122. $this->Controller->cacheAction = 21600;
  123. $this->Controller->here = '/cacheTest/cache_parsing';
  124. $this->Controller->action = 'cache_parsing';
  125. $View = new View($this->Controller);
  126. $result = $View->render('index');
  127. $this->assertNoPattern('/cake:nocache/', $result);
  128. $this->assertNoPattern('/php echo/', $result);
  129. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  130. $this->assertTrue(file_exists($filename));
  131. $contents = file_get_contents($filename);
  132. $this->assertPattern('/php echo \$variable/', $contents);
  133. $this->assertPattern('/php echo microtime()/', $contents);
  134. $this->assertPattern('/clark kent/', $result);
  135. @unlink($filename);
  136. }
  137. /**
  138. * Test cache parsing with cake:nocache tags in view file.
  139. *
  140. * @access public
  141. * @return void
  142. */
  143. function testLayoutCacheParsingWithTagsInView() {
  144. $this->Controller->cache_parsing();
  145. $this->Controller->cacheAction = 21600;
  146. $this->Controller->here = '/cacheTest/cache_parsing';
  147. $this->Controller->action = 'cache_parsing';
  148. $View = new View($this->Controller);
  149. $result = $View->render('test_nocache_tags');
  150. $this->assertNoPattern('/cake:nocache/', $result);
  151. $this->assertNoPattern('/php echo/', $result);
  152. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  153. $this->assertTrue(file_exists($filename));
  154. $contents = file_get_contents($filename);
  155. $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
  156. $this->assertPattern('/php echo \$variable/', $contents);
  157. $this->assertPattern('/php echo microtime()/', $contents);
  158. @unlink($filename);
  159. }
  160. /**
  161. * testComplexNoCache method
  162. *
  163. * @return void
  164. * @access public
  165. */
  166. function testComplexNoCache () {
  167. $this->Controller->cache_parsing();
  168. $this->Controller->cacheAction = array('cacheTest' => 21600);
  169. $this->Controller->here = '/cacheTest/cache_complex';
  170. $this->Controller->action = 'cache_complex';
  171. $this->Controller->layout = 'multi_cache';
  172. $this->Controller->viewPath = 'posts';
  173. $View = new View($this->Controller);
  174. $result = $View->render('sequencial_nocache');
  175. $this->assertNoPattern('/cake:nocache/', $result);
  176. $this->assertNoPattern('/php echo/', $result);
  177. $this->assertPattern('/A\. Layout Before Content/', $result);
  178. $this->assertPattern('/B\. In Plain Element/', $result);
  179. $this->assertPattern('/C\. Layout After Test Element/', $result);
  180. $this->assertPattern('/D\. In View File/', $result);
  181. $this->assertPattern('/E\. Layout After Content/', $result);
  182. //$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
  183. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
  184. $this->assertNoPattern('/1\. layout before content/', $result);
  185. $this->assertNoPattern('/2\. in plain element/', $result);
  186. $this->assertNoPattern('/3\. layout after test element/', $result);
  187. $this->assertNoPattern('/4\. in view file/', $result);
  188. $this->assertNoPattern('/5\. layout after content/', $result);
  189. //$this->assertNoPattern('/6\. in element with no cache tags/', $result);
  190. $this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
  191. $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
  192. $this->assertTrue(file_exists($filename));
  193. $contents = file_get_contents($filename);
  194. @unlink($filename);
  195. $this->assertPattern('/A\. Layout Before Content/', $contents);
  196. $this->assertNoPattern('/B\. In Plain Element/', $contents);
  197. $this->assertPattern('/C\. Layout After Test Element/', $contents);
  198. $this->assertPattern('/D\. In View File/', $contents);
  199. $this->assertPattern('/E\. Layout After Content/', $contents);
  200. //$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
  201. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
  202. $this->assertPattern('/1\. layout before content/', $contents);
  203. $this->assertNoPattern('/2\. in plain element/', $contents);
  204. $this->assertPattern('/3\. layout after test element/', $contents);
  205. $this->assertPattern('/4\. in view file/', $contents);
  206. $this->assertPattern('/5\. layout after content/', $contents);
  207. //$this->assertPattern('/6\. in element with no cache tags/', $contents);
  208. $this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
  209. }
  210. /**
  211. * testCacheEmptySections method
  212. *
  213. * This test must be uncommented/fixed in next release (1.2+)
  214. *
  215. * @return void
  216. * @access public
  217. *
  218. function testCacheEmptySections () {
  219. $this->Controller->cache_parsing();
  220. $this->Controller->cacheAction = array('cacheTest' => 21600);
  221. $this->Controller->here = '/cacheTest/cache_empty_sections';
  222. $this->Controller->action = 'cache_empty_sections';
  223. $this->Controller->layout = 'cache_empty_sections';
  224. $this->Controller->viewPath = 'posts';
  225. $View = new View($this->Controller);
  226. $result = $View->render('cache_empty_sections');
  227. $this->assertNoPattern('/cake:nocache/', $result);
  228. $this->assertNoPattern('/php echo/', $result);
  229. $this->assertPattern(
  230. '@</title>\s*</head>\s*' .
  231. '<body>\s*' .
  232. 'View Content\s*' .
  233. 'cached count is: 3\s*' .
  234. '</body>@', $result);
  235. $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
  236. $this->assertTrue(file_exists($filename));
  237. $contents = file_get_contents($filename);
  238. $this->assertNoPattern('/cake:nocache/', $contents);
  239. $this->assertPattern(
  240. '@<head>\s*<title>Posts</title>\s*' .
  241. "<\?php \$x = 1; \?>\s*" .
  242. '</head>\s*' .
  243. '<body>\s*' .
  244. "<\?php \$x\+\+; \?>\s*" .
  245. "<\?php \$x\+\+; \?>\s*" .
  246. 'View Content\s*' .
  247. "<\?php \$y = 1; \?>\s*" .
  248. "<\?php echo 'cached count is:' . \$x; \?>\s*" .
  249. '@', $contents);
  250. @unlink($filename);
  251. }
  252. */
  253. }
  254. ?>