PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mfriesen/kaching-php
PHP | 463 lines | 254 code | 50 blank | 159 comment | 0 complexity | 80a4a9c9387a33c3f7a15707563d52d6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * CacheHelperTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs.view.helpers
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Core', array('Controller', 'Model', 'View'));
  21. App::import('Helper', 'Cache');
  22. /**
  23. * CacheTestController class
  24. *
  25. * @package cake
  26. * @subpackage cake.tests.cases.libs.view.helpers
  27. */
  28. class CacheTestController extends Controller {
  29. /**
  30. * helpers property
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. var $helpers = array('Html', 'Cache');
  36. /**
  37. * cache_parsing method
  38. *
  39. * @access public
  40. * @return void
  41. */
  42. function cache_parsing() {
  43. $this->viewPath = 'posts';
  44. $this->layout = 'cache_layout';
  45. $this->set('variable', 'variableValue');
  46. $this->set('superman', 'clark kent');
  47. $this->set('batman', 'bruce wayne');
  48. $this->set('spiderman', 'peter parker');
  49. }
  50. }
  51. /**
  52. * CacheHelperTest class
  53. *
  54. * @package cake
  55. * @subpackage cake.tests.cases.libs.view.helpers
  56. */
  57. class CacheHelperTest extends CakeTestCase {
  58. /**
  59. * Checks if TMP/views is writable, and skips the case if it is not.
  60. *
  61. * @return void
  62. */
  63. function skip() {
  64. $this->skipUnless(is_writable(TMP . 'cache' . DS . 'views' . DS), 'TMP/views is not writable %s');
  65. }
  66. /**
  67. * setUp method
  68. *
  69. * @access public
  70. * @return void
  71. */
  72. function setUp() {
  73. $this->Controller = new CacheTestController();
  74. $this->Cache = new CacheHelper();
  75. $this->_cacheSettings = Configure::read('Cache');
  76. Configure::write('Cache.check', true);
  77. Configure::write('Cache.disable', false);
  78. }
  79. /**
  80. * Start Case - switch view paths
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. function startCase() {
  86. App::build(array(
  87. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  88. ), true);
  89. }
  90. /**
  91. * End Case - restore view Paths
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. function endCase() {
  97. App::build();
  98. }
  99. /**
  100. * tearDown method
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. function tearDown() {
  106. clearCache();
  107. unset($this->Cache);
  108. Configure::write('Cache', $this->_cacheSettings);
  109. }
  110. /**
  111. * test cache parsing with no cake:nocache tags in view file.
  112. *
  113. * @access public
  114. * @return void
  115. */
  116. function testLayoutCacheParsingNoTagsInView() {
  117. $this->Controller->cache_parsing();
  118. $this->Controller->params = array(
  119. 'controller' => 'cache_test',
  120. 'action' => 'cache_parsing',
  121. 'url' => array(),
  122. 'pass' => array(),
  123. 'named' => array()
  124. );
  125. $this->Controller->cacheAction = 21600;
  126. $this->Controller->here = '/cacheTest/cache_parsing';
  127. $this->Controller->action = 'cache_parsing';
  128. $View = new View($this->Controller);
  129. $result = $View->render('index');
  130. $this->assertNoPattern('/cake:nocache/', $result);
  131. $this->assertNoPattern('/php echo/', $result);
  132. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  133. $this->assertTrue(file_exists($filename));
  134. $contents = file_get_contents($filename);
  135. $this->assertPattern('/php echo \$variable/', $contents);
  136. $this->assertPattern('/php echo microtime()/', $contents);
  137. $this->assertPattern('/clark kent/', $result);
  138. @unlink($filename);
  139. }
  140. /**
  141. * test cache parsing with non-latin characters in current route
  142. *
  143. * @access public
  144. * @return void
  145. */
  146. function testCacheNonLatinCharactersInRoute() {
  147. $this->Controller->cache_parsing();
  148. $this->Controller->params = array(
  149. 'controller' => 'cache_test',
  150. 'action' => 'cache_parsing',
  151. 'url' => array(),
  152. 'pass' => array('風街ろまん'),
  153. 'named' => array()
  154. );
  155. $this->Controller->cacheAction = 21600;
  156. $this->Controller->here = '/posts/view/風街ろまん';
  157. $this->Controller->action = 'view';
  158. $View = new View($this->Controller);
  159. $result = $View->render('index');
  160. $filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
  161. $this->assertTrue(file_exists($filename));
  162. @unlink($filename);
  163. }
  164. /**
  165. * Test cache parsing with cake:nocache tags in view file.
  166. *
  167. * @access public
  168. * @return void
  169. */
  170. function testLayoutCacheParsingWithTagsInView() {
  171. $this->Controller->cache_parsing();
  172. $this->Controller->params = array(
  173. 'controller' => 'cache_test',
  174. 'action' => 'cache_parsing',
  175. 'url' => array(),
  176. 'pass' => array(),
  177. 'named' => array()
  178. );
  179. $this->Controller->cacheAction = 21600;
  180. $this->Controller->here = '/cacheTest/cache_parsing';
  181. $this->Controller->action = 'cache_parsing';
  182. $View = new View($this->Controller);
  183. $result = $View->render('test_nocache_tags');
  184. $this->assertNoPattern('/cake:nocache/', $result);
  185. $this->assertNoPattern('/php echo/', $result);
  186. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  187. $this->assertTrue(file_exists($filename));
  188. $contents = file_get_contents($filename);
  189. $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
  190. $this->assertPattern('/php echo \$variable/', $contents);
  191. $this->assertPattern('/php echo microtime()/', $contents);
  192. $this->assertNoPattern('/cake:nocache/', $contents);
  193. @unlink($filename);
  194. }
  195. /**
  196. * test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
  197. *
  198. * @return void
  199. */
  200. function testMultipleNoCacheTagsInViewfile() {
  201. $this->Controller->cache_parsing();
  202. $this->Controller->params = array(
  203. 'controller' => 'cache_test',
  204. 'action' => 'cache_parsing',
  205. 'url' => array(),
  206. 'pass' => array(),
  207. 'named' => array()
  208. );
  209. $this->Controller->cacheAction = 21600;
  210. $this->Controller->here = '/cacheTest/cache_parsing';
  211. $this->Controller->action = 'cache_parsing';
  212. $View = new View($this->Controller);
  213. $result = $View->render('multiple_nocache');
  214. $this->assertNoPattern('/cake:nocache/', $result);
  215. $this->assertNoPattern('/php echo/', $result);
  216. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  217. $this->assertTrue(file_exists($filename));
  218. $contents = file_get_contents($filename);
  219. $this->assertNoPattern('/cake:nocache/', $contents);
  220. @unlink($filename);
  221. }
  222. /**
  223. * testComplexNoCache method
  224. *
  225. * @return void
  226. * @access public
  227. */
  228. function testComplexNoCache () {
  229. $this->Controller->cache_parsing();
  230. $this->Controller->params = array(
  231. 'controller' => 'cache_test',
  232. 'action' => 'cache_complex',
  233. 'url' => array(),
  234. 'pass' => array(),
  235. 'named' => array()
  236. );
  237. $this->Controller->cacheAction = array('cache_complex' => 21600);
  238. $this->Controller->here = '/cacheTest/cache_complex';
  239. $this->Controller->action = 'cache_complex';
  240. $this->Controller->layout = 'multi_cache';
  241. $this->Controller->viewPath = 'posts';
  242. $View = new View($this->Controller);
  243. $result = $View->render('sequencial_nocache');
  244. $this->assertNoPattern('/cake:nocache/', $result);
  245. $this->assertNoPattern('/php echo/', $result);
  246. $this->assertPattern('/A\. Layout Before Content/', $result);
  247. $this->assertPattern('/B\. In Plain Element/', $result);
  248. $this->assertPattern('/C\. Layout After Test Element/', $result);
  249. $this->assertPattern('/D\. In View File/', $result);
  250. $this->assertPattern('/E\. Layout After Content/', $result);
  251. //$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
  252. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
  253. $this->assertNoPattern('/1\. layout before content/', $result);
  254. $this->assertNoPattern('/2\. in plain element/', $result);
  255. $this->assertNoPattern('/3\. layout after test element/', $result);
  256. $this->assertNoPattern('/4\. in view file/', $result);
  257. $this->assertNoPattern('/5\. layout after content/', $result);
  258. //$this->assertNoPattern('/6\. in element with no cache tags/', $result);
  259. $this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
  260. $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
  261. $this->assertTrue(file_exists($filename));
  262. $contents = file_get_contents($filename);
  263. @unlink($filename);
  264. $this->assertPattern('/A\. Layout Before Content/', $contents);
  265. $this->assertNoPattern('/B\. In Plain Element/', $contents);
  266. $this->assertPattern('/C\. Layout After Test Element/', $contents);
  267. $this->assertPattern('/D\. In View File/', $contents);
  268. $this->assertPattern('/E\. Layout After Content/', $contents);
  269. //$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
  270. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
  271. $this->assertPattern('/1\. layout before content/', $contents);
  272. $this->assertNoPattern('/2\. in plain element/', $contents);
  273. $this->assertPattern('/3\. layout after test element/', $contents);
  274. $this->assertPattern('/4\. in view file/', $contents);
  275. $this->assertPattern('/5\. layout after content/', $contents);
  276. //$this->assertPattern('/6\. in element with no cache tags/', $contents);
  277. $this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
  278. }
  279. /**
  280. * test cacheAction set to a boolean
  281. *
  282. * @return void
  283. */
  284. function testCacheActionArray() {
  285. $this->Controller->cache_parsing();
  286. $this->Controller->params = array(
  287. 'controller' => 'cache_test',
  288. 'action' => 'cache_parsing',
  289. 'url' => array(),
  290. 'pass' => array(),
  291. 'named' => array()
  292. );
  293. $this->Controller->cacheAction = array(
  294. 'cache_parsing' => 21600
  295. );
  296. $this->Controller->here = '/cache_test/cache_parsing';
  297. $this->Controller->action = 'cache_parsing';
  298. $View = new View($this->Controller);
  299. $result = $View->render('index');
  300. $this->assertNoPattern('/cake:nocache/', $result);
  301. $this->assertNoPattern('/php echo/', $result);
  302. $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
  303. $this->assertTrue(file_exists($filename));
  304. @unlink($filename);
  305. $this->Controller->cache_parsing();
  306. $this->Controller->cacheAction = array(
  307. 'cache_parsing' => 21600
  308. );
  309. $this->Controller->here = '/cacheTest/cache_parsing';
  310. $this->Controller->action = 'cache_parsing';
  311. $View = new View($this->Controller);
  312. $result = $View->render('index');
  313. $this->assertNoPattern('/cake:nocache/', $result);
  314. $this->assertNoPattern('/php echo/', $result);
  315. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  316. $this->assertTrue(file_exists($filename));
  317. @unlink($filename);
  318. $this->Controller->cache_parsing();
  319. $this->Controller->params = array(
  320. 'controller' => 'cache_test',
  321. 'action' => 'cache_parsing',
  322. 'url' => array(),
  323. 'pass' => array(),
  324. 'named' => array()
  325. );
  326. $this->Controller->cacheAction = array(
  327. 'some_other_action' => 21600
  328. );
  329. $this->Controller->here = '/cacheTest/cache_parsing';
  330. $this->Controller->action = 'cache_parsing';
  331. $View = new View($this->Controller);
  332. $result = $View->render('index');
  333. $this->assertNoPattern('/cake:nocache/', $result);
  334. $this->assertNoPattern('/php echo/', $result);
  335. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  336. $this->assertFalse(file_exists($filename));
  337. }
  338. /**
  339. * test that custom routes are respected when generating cache files.
  340. *
  341. * @return void
  342. */
  343. function testCacheWithCustomRoutes() {
  344. Router::reload();
  345. Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
  346. $this->Controller->cache_parsing();
  347. $this->Controller->params = array(
  348. 'lang' => 'en',
  349. 'controller' => 'cache_test',
  350. 'action' => 'cache_parsing',
  351. 'url' => array(),
  352. 'pass' => array(),
  353. 'named' => array()
  354. );
  355. $this->Controller->cacheAction = array(
  356. 'cache_parsing' => 21600
  357. );
  358. $this->Controller->here = '/en/cache_test/cache_parsing';
  359. $this->Controller->action = 'cache_parsing';
  360. $View = new View($this->Controller);
  361. $result = $View->render('index');
  362. $this->assertNoPattern('/cake:nocache/', $result);
  363. $this->assertNoPattern('/php echo/', $result);
  364. $filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
  365. $this->assertTrue(file_exists($filename));
  366. @unlink($filename);
  367. }
  368. /**
  369. * testCacheEmptySections method
  370. *
  371. * This test must be uncommented/fixed in next release (1.2+)
  372. *
  373. * @return void
  374. * @access public
  375. *
  376. function testCacheEmptySections () {
  377. $this->Controller->cache_parsing();
  378. $this->Controller->cacheAction = array('cacheTest' => 21600);
  379. $this->Controller->here = '/cacheTest/cache_empty_sections';
  380. $this->Controller->action = 'cache_empty_sections';
  381. $this->Controller->layout = 'cache_empty_sections';
  382. $this->Controller->viewPath = 'posts';
  383. $View = new View($this->Controller);
  384. $result = $View->render('cache_empty_sections');
  385. $this->assertNoPattern('/cake:nocache/', $result);
  386. $this->assertNoPattern('/php echo/', $result);
  387. $this->assertPattern(
  388. '@</title>\s*</head>\s*' .
  389. '<body>\s*' .
  390. 'View Content\s*' .
  391. 'cached count is: 3\s*' .
  392. '</body>@', $result);
  393. $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
  394. $this->assertTrue(file_exists($filename));
  395. $contents = file_get_contents($filename);
  396. $this->assertNoPattern('/cake:nocache/', $contents);
  397. $this->assertPattern(
  398. '@<head>\s*<title>Posts</title>\s*' .
  399. "<\?php \$x = 1; \?>\s*" .
  400. '</head>\s*' .
  401. '<body>\s*' .
  402. "<\?php \$x\+\+; \?>\s*" .
  403. "<\?php \$x\+\+; \?>\s*" .
  404. 'View Content\s*' .
  405. "<\?php \$y = 1; \?>\s*" .
  406. "<\?php echo 'cached count is:' . \$x; \?>\s*" .
  407. '@', $contents);
  408. @unlink($filename);
  409. }
  410. */
  411. }