PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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