MyHelperUrlCacheTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. App::uses('View', 'View');
  3. App::uses('Controller', 'Controller');
  4. App::uses('Cache', 'Cache');
  5. App::uses('MyHelper', 'Tools.View/Helper');
  6. App::uses('CakeRequest', 'Network');
  7. class MyHelperUrlCacheTest extends CakeTestCase {
  8. public $HtmlHelper = null;
  9. public function setUp() {
  10. parent::setUp();
  11. Configure::write('UrlCache.active', true);
  12. Configure::write('UrlCache.pageFiles', true);
  13. Configure::write('UrlCache.verbosePrefixes', true);
  14. Configure::write('Routing.prefixes', array('admin'));
  15. $this->HtmlHelper = new MyHelper(new View(new Controller(new CakeRequest('/test', false))));
  16. $this->HtmlHelper->beforeRender('foo');
  17. }
  18. public function tearDown() {
  19. Cache::delete(UrlCacheManager::$cacheKey, '_cake_core_');
  20. Cache::delete(UrlCacheManager::$cachePageKey, '_cake_core_');
  21. Configure::delete('UrlCache');
  22. parent::tearDown();
  23. }
  24. public function testInstance() {
  25. $this->assertTrue(is_a($this->HtmlHelper, 'MyHelper'));
  26. }
  27. public function testSettings() {
  28. $settings = Configure::read('UrlCache');
  29. $this->assertTrue($settings['active']);
  30. }
  31. public function testUrlRelative() {
  32. $url = $this->HtmlHelper->url(array('controller' => 'posts'));
  33. $this->assertEqual($url, '/posts');
  34. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), UrlCacheManager::$cache);
  35. $this->HtmlHelper->afterLayout('foo');
  36. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  37. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), $cache);
  38. }
  39. public function testUrlFull() {
  40. $url = $this->HtmlHelper->url(array('controller' => 'posts'), true);
  41. $this->assertPattern('/http:\/\/(.*)\/posts/', $url);
  42. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  43. $this->assertPattern('/http:\/\/(.*)\/posts/', UrlCacheManager::$cache['8f45f5c31d138d700742b01ccb673e1e']);
  44. $this->HtmlHelper->afterLayout('foo');
  45. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  46. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  47. $this->assertPattern('/http:\/\/(.*)\/posts/', $cache['8f45f5c31d138d700742b01ccb673e1e']);
  48. }
  49. public function testUrlRelativeAndFull() {
  50. $this->HtmlHelper->url(array('controller' => 'posts'));
  51. $this->HtmlHelper->url(array('controller' => 'posts'), true);
  52. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  53. $this->HtmlHelper->afterLayout('foo');
  54. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  55. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  56. }
  57. public function testUrlWithParams() {
  58. $this->HtmlHelper->url(array('controller' => 'posts'), true);
  59. $this->HtmlHelper->url(array('controller' => 'posts', 'action' => 'view', '3'));
  60. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  61. $this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys(UrlCacheManager::$cachePage));
  62. $this->HtmlHelper->afterLayout('foo');
  63. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  64. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  65. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  66. $this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys($cache));
  67. }
  68. public function testUrlWithDifferentOrders() {
  69. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view'));
  70. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools'));
  71. $this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys(UrlCacheManager::$cache));
  72. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', '3'));
  73. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', '3'));
  74. $this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys(UrlCacheManager::$cachePage));
  75. $this->HtmlHelper->afterLayout('foo');
  76. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  77. $this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys($cache));
  78. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  79. $this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys($cache));
  80. }
  81. public function testUrlWithDifferentValues() {
  82. $url = $this->HtmlHelper->url(array('plugin' => false, 'controller' => 'posts', 'action' => 'details', 1));
  83. $url2 = $this->HtmlHelper->url(array('plugin' => null, 'controller' => 'posts', 'action' => 'details', '1'));
  84. $url3 = $this->HtmlHelper->url(array('plugin' => '', 'controller' => 'posts', 'action' => 'details', true));
  85. $this->assertEqual($url, $url2);
  86. $this->assertEqual($url2, $url3);
  87. $this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys(UrlCacheManager::$cachePage));
  88. $this->HtmlHelper->afterLayout('foo');
  89. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  90. $this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys($cache));
  91. }
  92. public function testGlobalCache() {
  93. $res = UrlCacheManager::get(array('controller' => 'posts', 'action' => 'index'), false);
  94. $this->assertEqual(false, $res);
  95. $url = $this->HtmlHelper->url(array('controller' => 'posts', 'action' => 'index'));
  96. $this->HtmlHelper->afterLayout('foo');
  97. # on second page the same url will not trigger a miss but a hit
  98. $this->HtmlHelper->beforeRender('foo');
  99. $res = UrlCacheManager::get(array('controller' => 'posts', 'action' => 'index'), false);
  100. $this->assertEqual($url, $res);
  101. $this->HtmlHelper->afterLayout('foo');
  102. }
  103. public function testUrlWithVerbosePrefixes() {
  104. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true));
  105. $url2 = $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'admin' => true));
  106. $this->assertEqual(array('3016e3546edf1c2152905da5a42ea13f'), array_keys(UrlCacheManager::$cache));
  107. $this->assertEqual('/admin/tools/posts/view', $url);
  108. $this->assertEqual($url, $url2);
  109. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true, 'some' => 'param'));
  110. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'admin' => true));
  111. $this->assertEqual(array('25a924190d6e0ed09127d1904a286a95'), array_keys(UrlCacheManager::$cachePage));
  112. $this->HtmlHelper->afterLayout('foo');
  113. }
  114. public function testUrlWithoutVerbosePrefixes() {
  115. $this->skipIf(true, 'doesnt work yet');
  116. Configure::delete('UrlCache');
  117. Configure::write('UrlCache.active', true);
  118. Configure::write('UrlCache.pageFiles', true);
  119. Configure::write('UrlCache.verbosePrefixes', false);
  120. UrlCacheManager::$paramFields = array('controller', 'plugin', 'action', 'prefix');
  121. $this->HtmlHelper->beforeRender('foo');
  122. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin'));
  123. $this->assertEqual(array('41d5d7eb9442adbe76e6c7ebbb02ecc7'), array_keys(UrlCacheManager::$cache));
  124. $this->assertEqual('/admin/tools/posts/view', $url);
  125. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin', 'some' => 'param'));
  126. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'prefix' => 'admin'));
  127. $this->assertEqual('/admin/tools/posts/view/some:param', $url);
  128. $this->assertEqual(array('6a7091b88c8132ebb5461851808d318a'), array_keys(UrlCacheManager::$cachePage));
  129. $this->HtmlHelper->afterLayout('foo');
  130. }
  131. }