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

/lib/Cake/Test/Case/View/Helper/CacheHelperTest.php

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