CacheHelperTest.php 19 KB

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