MyHelperUrlCacheTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 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->assertTrue(is_a($this->HtmlHelper, 'MyHelper'));
  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(array('controller' => 'posts'));
  34. $this->assertEqual($url, '/posts');
  35. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), UrlCacheManager::$cache);
  36. $this->HtmlHelper->afterLayout('foo');
  37. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  38. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47' => '/posts'), $cache);
  39. }
  40. public function testUrlFull() {
  41. $url = $this->HtmlHelper->url(array('controller' => 'posts'), true);
  42. $this->assertPattern('/http:\/\/(.*)\/posts/', $url);
  43. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  44. $this->assertPattern('/http:\/\/(.*)\/posts/', UrlCacheManager::$cache['8f45f5c31d138d700742b01ccb673e1e']);
  45. $this->HtmlHelper->afterLayout('foo');
  46. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  47. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  48. $this->assertPattern('/http:\/\/(.*)\/posts/', $cache['8f45f5c31d138d700742b01ccb673e1e']);
  49. }
  50. public function testUrlRelativeAndFull() {
  51. $this->HtmlHelper->url(array('controller' => 'posts'));
  52. $this->HtmlHelper->url(array('controller' => 'posts'), true);
  53. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  54. $this->HtmlHelper->afterLayout('foo');
  55. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  56. $this->assertEqual(array('779e416667ffd33e75ac19e0952abb47', '8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  57. }
  58. public function testUrlWithParams() {
  59. $this->HtmlHelper->url(array('controller' => 'posts'), true);
  60. $this->HtmlHelper->url(array('controller' => 'posts', 'action' => 'view', '3'));
  61. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys(UrlCacheManager::$cache));
  62. $this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys(UrlCacheManager::$cachePage));
  63. $this->HtmlHelper->afterLayout('foo');
  64. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  65. $this->assertEqual(array('8f45f5c31d138d700742b01ccb673e1e'), array_keys($cache));
  66. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  67. $this->assertEqual(array('e2ff5470228f80f98b2be7ddbcab340d'), array_keys($cache));
  68. }
  69. public function testUrlWithDifferentOrders() {
  70. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view'));
  71. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools'));
  72. $this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys(UrlCacheManager::$cache));
  73. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', '3'));
  74. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', '3'));
  75. $this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys(UrlCacheManager::$cachePage));
  76. $this->HtmlHelper->afterLayout('foo');
  77. $cache = Cache::read(UrlCacheManager::$cacheKey, '_cake_core_');
  78. $this->assertEqual(array('0ba1e9d8ab27a564450be9aa93bd4a1c'), array_keys($cache));
  79. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  80. $this->assertEqual(array('f5509ea5e31562b541948d89b4d65700'), array_keys($cache));
  81. }
  82. public function testUrlWithDifferentValues() {
  83. $url = $this->HtmlHelper->url(array('plugin' => false, 'controller' => 'posts', 'action' => 'details', 1));
  84. $url2 = $this->HtmlHelper->url(array('plugin' => null, 'controller' => 'posts', 'action' => 'details', '1'));
  85. $url3 = $this->HtmlHelper->url(array('plugin' => '', 'controller' => 'posts', 'action' => 'details', true));
  86. $this->assertEqual($url, $url2);
  87. $this->assertEqual($url2, $url3);
  88. $this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys(UrlCacheManager::$cachePage));
  89. $this->HtmlHelper->afterLayout('foo');
  90. $cache = Cache::read(UrlCacheManager::$cachePageKey, '_cake_core_');
  91. $this->assertEqual(array('f0fdde123fe5958781cfad4284ee59c4'), array_keys($cache));
  92. }
  93. public function testGlobalCache() {
  94. $res = UrlCacheManager::get(array('controller' => 'posts', 'action' => 'index'), false);
  95. $this->assertEqual(false, $res);
  96. $url = $this->HtmlHelper->url(array('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(array('controller' => 'posts', 'action' => 'index'), false);
  101. $this->assertEqual($url, $res);
  102. $this->HtmlHelper->afterLayout('foo');
  103. }
  104. public function testUrlWithVerbosePrefixes() {
  105. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true));
  106. $url2 = $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'admin' => true));
  107. $this->assertEqual(array('3016e3546edf1c2152905da5a42ea13f'), array_keys(UrlCacheManager::$cache));
  108. $this->assertEqual('/admin/tools/posts/view', $url);
  109. $this->assertEqual($url, $url2);
  110. $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'admin' => true, 'some' => 'param'));
  111. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'admin' => true));
  112. $this->assertEqual(array('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 = array('controller', 'plugin', 'action', 'prefix');
  122. $this->HtmlHelper->beforeRender('foo');
  123. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin'));
  124. $this->assertEqual(array('41d5d7eb9442adbe76e6c7ebbb02ecc7'), array_keys(UrlCacheManager::$cache));
  125. $this->assertEqual('/admin/tools/posts/view', $url);
  126. $url = $this->HtmlHelper->url(array('plugin' => 'tools', 'controller' => 'posts', 'action' => 'view', 'prefix' => 'admin', 'some' => 'param'));
  127. $this->HtmlHelper->url(array('action' => 'view', 'controller' => 'posts', 'plugin' => 'tools', 'some' => 'param', 'prefix' => 'admin'));
  128. $this->assertEqual('/admin/tools/posts/view/some:param', $url);
  129. $this->assertEqual(array('6a7091b88c8132ebb5461851808d318a'), array_keys(UrlCacheManager::$cachePage));
  130. $this->HtmlHelper->afterLayout('foo');
  131. }
  132. }