PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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