MyHelperUrlCacheTest.php 8.0 KB

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