UrlHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Network\Request;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\View\Helper\UrlHelper;
  22. use Cake\View\View;
  23. /**
  24. * UrlHelperTest class
  25. *
  26. */
  27. class UrlHelperTest extends TestCase
  28. {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Router::reload();
  38. $this->View = new View();
  39. $this->Helper = new UrlHelper($this->View);
  40. $this->Helper->request = new Request();
  41. Configure::write('App.namespace', 'TestApp');
  42. Plugin::load(['TestTheme']);
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown()
  50. {
  51. parent::tearDown();
  52. Configure::delete('Asset');
  53. Plugin::unload();
  54. unset($this->Helper, $this->View);
  55. }
  56. /**
  57. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  58. *
  59. * @return void
  60. */
  61. public function testUrlConversion()
  62. {
  63. Router::connect('/:controller/:action/*');
  64. $result = $this->Helper->build('/controller/action/1');
  65. $this->assertEquals('/controller/action/1', $result);
  66. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  67. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  68. $result = $this->Helper->build(['controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"']);
  69. $this->assertEquals("/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  70. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  71. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  72. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  73. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  74. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  75. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  76. $result = $this->Helper->build([
  77. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  78. ]);
  79. $this->assertEquals("/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  80. $result = $this->Helper->build([
  81. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  82. '?' => ['one' => 'value', 'two' => 'value', 'three' => 'purple']
  83. ]);
  84. $this->assertEquals("/posts/index?page=1&amp;one=value&amp;two=value&amp;three=purple", $result);
  85. }
  86. /**
  87. * test assetTimestamp application
  88. *
  89. * @return void
  90. */
  91. public function testAssetTimestamp()
  92. {
  93. Configure::write('Foo.bar', 'test');
  94. Configure::write('Asset.timestamp', false);
  95. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  96. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  97. Configure::write('Asset.timestamp', true);
  98. Configure::write('debug', false);
  99. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  100. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  101. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  102. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  103. Configure::write('Asset.timestamp', true);
  104. Configure::write('debug', true);
  105. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  106. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  107. Configure::write('Asset.timestamp', 'force');
  108. Configure::write('debug', false);
  109. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  110. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  111. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  112. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  113. $this->Helper->request->webroot = '/some/dir/';
  114. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  115. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  116. }
  117. /**
  118. * test assetUrl application
  119. *
  120. * @return void
  121. */
  122. public function testAssetUrl()
  123. {
  124. Router::connect('/:controller/:action/*');
  125. $this->Helper->webroot = '';
  126. $result = $this->Helper->assetUrl(
  127. [
  128. 'controller' => 'js',
  129. 'action' => 'post',
  130. '_ext' => 'js'
  131. ],
  132. ['fullBase' => true]
  133. );
  134. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  135. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  136. $this->assertEquals('img/foo.jpg', $result);
  137. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  138. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  139. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  140. $this->assertEquals('style.css', $result);
  141. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  142. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  143. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  144. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  145. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  146. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  147. }
  148. /**
  149. * Test assetUrl with no rewriting.
  150. *
  151. * @return void
  152. */
  153. public function testAssetUrlNoRewrite()
  154. {
  155. $this->Helper->request->addPaths([
  156. 'base' => '/cake_dev/index.php',
  157. 'webroot' => '/cake_dev/app/webroot/',
  158. 'here' => '/cake_dev/index.php/tasks',
  159. ]);
  160. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  161. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  162. $this->assertEquals($expected, $result);
  163. }
  164. /**
  165. * Test assetUrl with plugins.
  166. *
  167. * @return void
  168. */
  169. public function testAssetUrlPlugin()
  170. {
  171. $this->Helper->webroot = '';
  172. Plugin::load('TestPlugin');
  173. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  174. $this->assertEquals('test_plugin/style.css', $result);
  175. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  176. $this->assertEquals('TestPlugin.style.css', $result);
  177. Plugin::unload('TestPlugin');
  178. }
  179. /**
  180. * test assetUrl and Asset.timestamp = force
  181. *
  182. * @return void
  183. */
  184. public function testAssetUrlTimestampForce()
  185. {
  186. $this->Helper->webroot = '';
  187. Configure::write('Asset.timestamp', 'force');
  188. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  189. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  190. }
  191. /**
  192. * test assetTimestamp with plugins and themes
  193. *
  194. * @return void
  195. */
  196. public function testAssetTimestampPluginsAndThemes()
  197. {
  198. Configure::write('Asset.timestamp', 'force');
  199. Plugin::load(['TestPlugin']);
  200. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  201. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  202. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  203. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  204. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  205. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  206. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  207. $this->assertRegExp('#/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  208. }
  209. /**
  210. * Test generating paths with webroot().
  211. *
  212. * @return void
  213. */
  214. public function testWebrootPaths()
  215. {
  216. $this->Helper->request->webroot = '/';
  217. $result = $this->Helper->webroot('/img/cake.power.gif');
  218. $expected = '/img/cake.power.gif';
  219. $this->assertEquals($expected, $result);
  220. $this->Helper->theme = 'TestTheme';
  221. $result = $this->Helper->webroot('/img/cake.power.gif');
  222. $expected = '/test_theme/img/cake.power.gif';
  223. $this->assertEquals($expected, $result);
  224. $result = $this->Helper->webroot('/img/test.jpg');
  225. $expected = '/test_theme/img/test.jpg';
  226. $this->assertEquals($expected, $result);
  227. $webRoot = Configure::read('App.wwwRoot');
  228. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  229. $result = $this->Helper->webroot('/img/cake.power.gif');
  230. $expected = '/test_theme/img/cake.power.gif';
  231. $this->assertEquals($expected, $result);
  232. $result = $this->Helper->webroot('/img/test.jpg');
  233. $expected = '/test_theme/img/test.jpg';
  234. $this->assertEquals($expected, $result);
  235. $result = $this->Helper->webroot('/img/cake.icon.gif');
  236. $expected = '/img/cake.icon.gif';
  237. $this->assertEquals($expected, $result);
  238. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  239. $expected = '/img/cake.icon.gif?some=param';
  240. $this->assertEquals($expected, $result);
  241. Configure::write('App.wwwRoot', $webRoot);
  242. }
  243. }