CacheHelperTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <?php
  2. /**
  3. * CacheHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('Model', 'Model');
  21. App::uses('View', 'View');
  22. App::uses('CacheHelper', 'View/Helper');
  23. /**
  24. * CacheTestController class
  25. *
  26. * @package Cake.Test.Case.View.Helper
  27. */
  28. class CacheTestController extends Controller {
  29. /**
  30. * helpers property
  31. *
  32. * @var array
  33. */
  34. public $helpers = array('Html', 'Cache');
  35. /**
  36. * cache_parsing method
  37. *
  38. * @return void
  39. */
  40. public function cache_parsing() {
  41. $this->viewPath = 'Posts';
  42. $this->layout = 'cache_layout';
  43. $this->set('variable', 'variableValue');
  44. $this->set('superman', 'clark kent');
  45. $this->set('batman', 'bruce wayne');
  46. $this->set('spiderman', 'peter parker');
  47. }
  48. }
  49. /**
  50. * CacheHelperTest class
  51. *
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class CacheHelperTest extends CakeTestCase {
  55. /**
  56. * Checks if TMP/views is writable, and skips the case if it is not.
  57. *
  58. * @return void
  59. */
  60. public function skip() {
  61. $this->skipUnless(is_writable(TMP . 'cache' . DS . 'views' . DS), 'TMP/views is not writable %s');
  62. }
  63. /**
  64. * setUp method
  65. *
  66. * @return void
  67. */
  68. public function setUp() {
  69. parent::setUp();
  70. $_GET = array();
  71. $request = new CakeRequest();
  72. $this->Controller = new CacheTestController($request);
  73. $View = new View($this->Controller);
  74. $this->Cache = new CacheHelper($View);
  75. Configure::write('Cache.check', true);
  76. Configure::write('Cache.disable', false);
  77. App::build(array(
  78. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  79. ), true);
  80. }
  81. /**
  82. * tearDown method
  83. *
  84. * @return void
  85. */
  86. public function tearDown() {
  87. clearCache();
  88. unset($this->Cache);
  89. parent::tearDown();
  90. }
  91. /**
  92. * test cache parsing with no cake:nocache tags in view file.
  93. *
  94. * @return void
  95. */
  96. public function testLayoutCacheParsingNoTagsInView() {
  97. $this->Controller->cache_parsing();
  98. $this->Controller->request->addParams(array(
  99. 'controller' => 'cache_test',
  100. 'action' => 'cache_parsing',
  101. 'url' => array(),
  102. 'pass' => array(),
  103. 'named' => array()
  104. ));
  105. $this->Controller->cacheAction = 21600;
  106. $this->Controller->request->here = '/cacheTest/cache_parsing';
  107. $this->Controller->request->action = 'cache_parsing';
  108. $View = new View($this->Controller);
  109. $result = $View->render('index');
  110. $this->assertNoPattern('/cake:nocache/', $result);
  111. $this->assertNoPattern('/php echo/', $result);
  112. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  113. $this->assertTrue(file_exists($filename));
  114. $contents = file_get_contents($filename);
  115. $this->assertPattern('/php echo \$variable/', $contents);
  116. $this->assertPattern('/php echo microtime()/', $contents);
  117. $this->assertPattern('/clark kent/', $result);
  118. @unlink($filename);
  119. }
  120. /**
  121. * test cache parsing with non-latin characters in current route
  122. *
  123. * @return void
  124. */
  125. public function testCacheNonLatinCharactersInRoute() {
  126. $this->Controller->cache_parsing();
  127. $this->Controller->request->addParams(array(
  128. 'controller' => 'cache_test',
  129. 'action' => 'cache_parsing',
  130. 'url' => array(),
  131. 'pass' => array('風街ろまん'),
  132. 'named' => array()
  133. ));
  134. $this->Controller->cacheAction = 21600;
  135. $this->Controller->request->here = '/posts/view/風街ろまん';
  136. $this->Controller->action = 'view';
  137. $View = new View($this->Controller);
  138. $result = $View->render('index');
  139. $filename = CACHE . 'views' . DS . 'posts_view_風街ろまん.php';
  140. $this->assertTrue(file_exists($filename));
  141. @unlink($filename);
  142. }
  143. /**
  144. * Test cache parsing with cake:nocache tags in view file.
  145. *
  146. * @return void
  147. */
  148. public function testLayoutCacheParsingWithTagsInView() {
  149. $this->Controller->cache_parsing();
  150. $this->Controller->request->addParams(array(
  151. 'controller' => 'cache_test',
  152. 'action' => 'cache_parsing',
  153. 'url' => array(),
  154. 'pass' => array(),
  155. 'named' => array()
  156. ));
  157. $this->Controller->cacheAction = 21600;
  158. $this->Controller->request->here = '/cacheTest/cache_parsing';
  159. $this->Controller->action = 'cache_parsing';
  160. $View = new View($this->Controller);
  161. $result = $View->render('test_nocache_tags');
  162. $this->assertNoPattern('/cake:nocache/', $result);
  163. $this->assertNoPattern('/php echo/', $result);
  164. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  165. $this->assertTrue(file_exists($filename));
  166. $contents = file_get_contents($filename);
  167. $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
  168. $this->assertPattern('/php echo \$variable/', $contents);
  169. $this->assertPattern('/php echo microtime()/', $contents);
  170. $this->assertNoPattern('/cake:nocache/', $contents);
  171. @unlink($filename);
  172. }
  173. /**
  174. * test that multiple <!--nocache--> tags function with multiple nocache tags in the layout.
  175. *
  176. * @return void
  177. */
  178. public function testMultipleNoCacheTagsInViewfile() {
  179. $this->Controller->cache_parsing();
  180. $this->Controller->request->addParams(array(
  181. 'controller' => 'cache_test',
  182. 'action' => 'cache_parsing',
  183. 'url' => array(),
  184. 'pass' => array(),
  185. 'named' => array()
  186. ));
  187. $this->Controller->cacheAction = 21600;
  188. $this->Controller->request->here = '/cacheTest/cache_parsing';
  189. $this->Controller->action = 'cache_parsing';
  190. $View = new View($this->Controller);
  191. $result = $View->render('multiple_nocache');
  192. $this->assertNoPattern('/cake:nocache/', $result);
  193. $this->assertNoPattern('/php echo/', $result);
  194. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  195. $this->assertTrue(file_exists($filename));
  196. $contents = file_get_contents($filename);
  197. $this->assertNoPattern('/cake:nocache/', $contents);
  198. @unlink($filename);
  199. }
  200. /**
  201. * testComplexNoCache method
  202. *
  203. * @return void
  204. */
  205. public function testComplexNoCache () {
  206. $this->Controller->cache_parsing();
  207. $this->Controller->request->addParams(array(
  208. 'controller' => 'cache_test',
  209. 'action' => 'cache_complex',
  210. 'url' => array(),
  211. 'pass' => array(),
  212. 'named' => array()
  213. ));
  214. $this->Controller->cacheAction = array('cache_complex' => 21600);
  215. $this->Controller->request->here = '/cacheTest/cache_complex';
  216. $this->Controller->action = 'cache_complex';
  217. $this->Controller->layout = 'multi_cache';
  218. $this->Controller->viewPath = 'Posts';
  219. $View = new View($this->Controller);
  220. $result = $View->render('sequencial_nocache');
  221. $this->assertNoPattern('/cake:nocache/', $result);
  222. $this->assertNoPattern('/php echo/', $result);
  223. $this->assertPattern('/A\. Layout Before Content/', $result);
  224. $this->assertPattern('/B\. In Plain Element/', $result);
  225. $this->assertPattern('/C\. Layout After Test Element/', $result);
  226. $this->assertPattern('/D\. In View File/', $result);
  227. $this->assertPattern('/E\. Layout After Content/', $result);
  228. //$this->assertPattern('/F\. In Element With No Cache Tags/', $result);
  229. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $result);
  230. $this->assertNoPattern('/1\. layout before content/', $result);
  231. $this->assertNoPattern('/2\. in plain element/', $result);
  232. $this->assertNoPattern('/3\. layout after test element/', $result);
  233. $this->assertNoPattern('/4\. in view file/', $result);
  234. $this->assertNoPattern('/5\. layout after content/', $result);
  235. //$this->assertNoPattern('/6\. in element with no cache tags/', $result);
  236. $this->assertNoPattern('/7\. layout after content and after element with no cache tags/', $result);
  237. $filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
  238. $this->assertTrue(file_exists($filename));
  239. $contents = file_get_contents($filename);
  240. @unlink($filename);
  241. $this->assertPattern('/A\. Layout Before Content/', $contents);
  242. $this->assertNoPattern('/B\. In Plain Element/', $contents);
  243. $this->assertPattern('/C\. Layout After Test Element/', $contents);
  244. $this->assertPattern('/D\. In View File/', $contents);
  245. $this->assertPattern('/E\. Layout After Content/', $contents);
  246. //$this->assertPattern('/F\. In Element With No Cache Tags/', $contents);
  247. $this->assertPattern('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
  248. $this->assertPattern('/1\. layout before content/', $contents);
  249. $this->assertNoPattern('/2\. in plain element/', $contents);
  250. $this->assertPattern('/3\. layout after test element/', $contents);
  251. $this->assertPattern('/4\. in view file/', $contents);
  252. $this->assertPattern('/5\. layout after content/', $contents);
  253. //$this->assertPattern('/6\. in element with no cache tags/', $contents);
  254. $this->assertPattern('/7\. layout after content and after element with no cache tags/', $contents);
  255. }
  256. /**
  257. * test cache of view vars
  258. *
  259. * @return void
  260. */
  261. public function testCacheViewVars() {
  262. $this->Controller->cache_parsing();
  263. $this->Controller->params = array(
  264. 'controller' => 'cache_test',
  265. 'action' => 'cache_parsing',
  266. 'url' => array(),
  267. 'pass' => array(),
  268. 'named' => array()
  269. );
  270. $this->Controller->cacheAction = 21600;
  271. $this->Controller->here = '/cacheTest/cache_parsing';
  272. $this->Controller->action = 'cache_parsing';
  273. $View = new View($this->Controller);
  274. $result = $View->render('index');
  275. $this->assertNoPattern('/cake:nocache/', $result);
  276. $this->assertNoPattern('/php echo/', $result);
  277. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  278. $this->assertTrue(file_exists($filename));
  279. $contents = file_get_contents($filename);
  280. $this->assertPattern('/\$this\-\>viewVars/', $contents);
  281. $this->assertPattern('/extract\(\$this\-\>viewVars, EXTR_SKIP\);/', $contents);
  282. $this->assertPattern('/php echo \$variable/', $contents);
  283. @unlink($filename);
  284. }
  285. /**
  286. * Test that callback code is generated correctly.
  287. *
  288. * @return void
  289. */
  290. public function testCacheCallbacks() {
  291. $this->Controller->cache_parsing();
  292. $this->Controller->params = array(
  293. 'controller' => 'cache_test',
  294. 'action' => 'cache_parsing',
  295. 'url' => array(),
  296. 'pass' => array(),
  297. 'named' => array()
  298. );
  299. $this->Controller->cacheAction = array(
  300. 'cache_parsing' => array(
  301. 'duration' => 21600,
  302. 'callbacks' => true
  303. )
  304. );
  305. $this->Controller->here = '/cacheTest/cache_parsing';
  306. $this->Controller->action = 'cache_parsing';
  307. $View = new View($this->Controller);
  308. $result = $View->render('index');
  309. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  310. $this->assertTrue(file_exists($filename));
  311. $contents = file_get_contents($filename);
  312. $this->assertPattern('/\$controller->startupProcess\(\);/', $contents);
  313. @unlink($filename);
  314. }
  315. /**
  316. * test cacheAction set to a boolean
  317. *
  318. * @return void
  319. */
  320. public function testCacheActionArray() {
  321. $this->Controller->cache_parsing();
  322. $this->Controller->request->addParams(array(
  323. 'controller' => 'cache_test',
  324. 'action' => 'cache_parsing',
  325. 'url' => array(),
  326. 'pass' => array(),
  327. 'named' => array()
  328. ));
  329. $this->Controller->cacheAction = array(
  330. 'cache_parsing' => 21600
  331. );
  332. $this->Controller->request->here = '/cache_test/cache_parsing';
  333. $this->Controller->action = 'cache_parsing';
  334. $View = new View($this->Controller);
  335. $result = $View->render('index');
  336. $this->assertNoPattern('/cake:nocache/', $result);
  337. $this->assertNoPattern('/php echo/', $result);
  338. $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing.php';
  339. $this->assertTrue(file_exists($filename));
  340. @unlink($filename);
  341. $this->Controller->cache_parsing();
  342. $this->Controller->cacheAction = array(
  343. 'cache_parsing' => 21600
  344. );
  345. $this->Controller->request->here = '/cacheTest/cache_parsing';
  346. $this->Controller->action = 'cache_parsing';
  347. $View = new View($this->Controller);
  348. $result = $View->render('index');
  349. $this->assertNoPattern('/cake:nocache/', $result);
  350. $this->assertNoPattern('/php echo/', $result);
  351. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  352. $this->assertTrue(file_exists($filename));
  353. @unlink($filename);
  354. $this->Controller->cache_parsing();
  355. $this->Controller->request->addParams(array(
  356. 'controller' => 'cache_test',
  357. 'action' => 'cache_parsing',
  358. 'url' => array(),
  359. 'pass' => array(),
  360. 'named' => array()
  361. ));
  362. $this->Controller->cacheAction = array(
  363. 'some_other_action' => 21600
  364. );
  365. $this->Controller->request->here = '/cacheTest/cache_parsing';
  366. $this->Controller->action = 'cache_parsing';
  367. $View = new View($this->Controller);
  368. $result = $View->render('index');
  369. $this->assertNoPattern('/cake:nocache/', $result);
  370. $this->assertNoPattern('/php echo/', $result);
  371. $filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
  372. $this->assertFalse(file_exists($filename));
  373. }
  374. /**
  375. * test with named and pass args.
  376. *
  377. * @return void
  378. */
  379. public function testCacheWithNamedAndPassedArgs() {
  380. Router::reload();
  381. $this->Controller->cache_parsing();
  382. $this->Controller->request->addParams(array(
  383. 'controller' => 'cache_test',
  384. 'action' => 'cache_parsing',
  385. 'url' => array(),
  386. 'pass' => array(1, 2),
  387. 'named' => array(
  388. 'name' => 'mark',
  389. 'ice' => 'cream'
  390. )
  391. ));
  392. $this->Controller->cacheAction = array(
  393. 'cache_parsing' => 21600
  394. );
  395. $this->Controller->request->here = '/cache_test/cache_parsing/1/2/name:mark/ice:cream';
  396. $View = new View($this->Controller);
  397. $result = $View->render('index');
  398. $this->assertNoPattern('/cake:nocache/', $result);
  399. $this->assertNoPattern('/php echo/', $result);
  400. $filename = CACHE . 'views' . DS . 'cache_test_cache_parsing_1_2_name_mark_ice_cream.php';
  401. $this->assertTrue(file_exists($filename));
  402. @unlink($filename);
  403. }
  404. /**
  405. * test that custom routes are respected when generating cache files.
  406. *
  407. * @return void
  408. */
  409. public function testCacheWithCustomRoutes() {
  410. Router::reload();
  411. Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
  412. $this->Controller->cache_parsing();
  413. $this->Controller->request->addParams(array(
  414. 'lang' => 'en',
  415. 'controller' => 'cache_test',
  416. 'action' => 'cache_parsing',
  417. 'url' => array(),
  418. 'pass' => array(),
  419. 'named' => array()
  420. ));
  421. $this->Controller->cacheAction = array(
  422. 'cache_parsing' => 21600
  423. );
  424. $this->Controller->request->here = '/en/cache_test/cache_parsing';
  425. $this->Controller->action = 'cache_parsing';
  426. $View = new View($this->Controller);
  427. $result = $View->render('index');
  428. $this->assertNoPattern('/cake:nocache/', $result);
  429. $this->assertNoPattern('/php echo/', $result);
  430. $filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
  431. $this->assertTrue(file_exists($filename));
  432. @unlink($filename);
  433. }
  434. /**
  435. * test ControllerName contains AppName
  436. *
  437. * This test verifys view cache is created correctly when the app name is contained in part of the controller name.
  438. * (webapp Name) base name is 'cache' controller is 'cacheTest' action is 'cache_name'
  439. * apps url would look somehing like http://localhost/cache/cacheTest/cache_name
  440. *
  441. * @return void
  442. **/
  443. public function testCacheBaseNameControllerName() {
  444. $this->Controller->cache_parsing();
  445. $this->Controller->cacheAction = array(
  446. 'cache_name' => 21600
  447. );
  448. $this->Controller->params = array(
  449. 'controller' => 'cacheTest',
  450. 'action' => 'cache_name',
  451. 'url' => array(),
  452. 'pass' => array(),
  453. 'named' => array()
  454. );
  455. $this->Controller->here = '/cache/cacheTest/cache_name';
  456. $this->Controller->action = 'cache_name';
  457. $this->Controller->base = '/cache';
  458. $View = new View($this->Controller);
  459. $result = $View->render('index');
  460. $this->assertNoPattern('/cake:nocache/', $result);
  461. $this->assertNoPattern('/php echo/', $result);
  462. $filename = CACHE . 'views' . DS . 'cache_cachetest_cache_name.php';
  463. $this->assertTrue(file_exists($filename));
  464. @unlink($filename);
  465. }
  466. /**
  467. * test that afterRender checks the conditions correctly.
  468. *
  469. * @return void
  470. */
  471. public function testAfterRenderConditions() {
  472. Configure::write('Cache.check', true);
  473. $View = new View($this->Controller);
  474. $View->cacheAction = '+1 day';
  475. $View->output = 'test';
  476. $Cache = $this->getMock('CacheHelper', array('cache'), array($View));
  477. $Cache->expects($this->once())->method('cache')
  478. ->with('posts/index', $View->output, false);
  479. $Cache->afterRender('posts/index');
  480. Configure::write('Cache.check', false);
  481. $Cache->afterRender('posts/index');
  482. Configure::write('Cache.check', true);
  483. $View->cacheAction = false;
  484. $Cache->afterRender('posts/index');
  485. }
  486. /**
  487. * test that afterRender checks the conditions correctly.
  488. *
  489. * @return void
  490. */
  491. public function testAfterLayoutConditions() {
  492. Configure::write('Cache.check', true);
  493. $View = new View($this->Controller);
  494. $View->cacheAction = '+1 day';
  495. $View->output = 'test';
  496. $Cache = $this->getMock('CacheHelper', array('cache'), array($View));
  497. $Cache->expects($this->once())->method('cache')
  498. ->with('posts/index', $View->output, true);
  499. $Cache->afterLayout('posts/index');
  500. Configure::write('Cache.check', false);
  501. $Cache->afterLayout('posts/index');
  502. Configure::write('Cache.check', true);
  503. $View->cacheAction = false;
  504. $Cache->afterLayout('posts/index');
  505. }
  506. /**
  507. * testCacheEmptySections method
  508. *
  509. * This test must be uncommented/fixed in next release (1.2+)
  510. *
  511. * @return void
  512. */
  513. public function testCacheEmptySections() {
  514. $this->Controller->cache_parsing();
  515. $this->Controller->params = array(
  516. 'controller' => 'cacheTest',
  517. 'action' => 'cache_empty_sections',
  518. 'url' => array(),
  519. 'pass' => array(),
  520. 'named' => array()
  521. );
  522. $this->Controller->cacheAction = array('cache_empty_sections' => 21600);
  523. $this->Controller->here = '/cacheTest/cache_empty_sections';
  524. $this->Controller->action = 'cache_empty_sections';
  525. $this->Controller->layout = 'cache_empty_sections';
  526. $this->Controller->viewPath = 'Posts';
  527. $View = new View($this->Controller);
  528. $result = $View->render('cache_empty_sections');
  529. $this->assertNoPattern('/nocache/', $result);
  530. $this->assertNoPattern('/php echo/', $result);
  531. $this->assertPattern(
  532. '@</title>\s*</head>\s*' .
  533. '<body>\s*' .
  534. 'View Content\s*' .
  535. 'cached count is: 3\s*' .
  536. '</body>@', $result);
  537. $filename = CACHE . 'views' . DS . 'cachetest_cache_empty_sections.php';
  538. $this->assertTrue(file_exists($filename));
  539. $contents = file_get_contents($filename);
  540. $this->assertNoPattern('/nocache/', $contents);
  541. $this->assertPattern(
  542. '@<head>\s*<title>Posts</title>\s*' .
  543. '<\?php \$x \= 1; \?>\s*' .
  544. '</head>\s*' .
  545. '<body>\s*' .
  546. '<\?php \$x\+\+; \?>\s*' .
  547. '<\?php \$x\+\+; \?>\s*' .
  548. 'View Content\s*' .
  549. '<\?php \$y = 1; \?>\s*' .
  550. '<\?php echo \'cached count is: \' . \$x; \?>\s*' .
  551. '@', $contents);
  552. @unlink($filename);
  553. }
  554. }