UrlHelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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. class UrlHelperTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Router::reload();
  37. $this->View = new View();
  38. $this->Helper = new UrlHelper($this->View);
  39. $this->Helper->request = new Request();
  40. Configure::write('App.namespace', 'TestApp');
  41. Plugin::load(['TestTheme']);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. Configure::delete('Asset');
  52. Plugin::unload();
  53. unset($this->Helper, $this->View);
  54. }
  55. /**
  56. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  57. *
  58. * @return void
  59. */
  60. public function testUrlConversion()
  61. {
  62. Router::connect('/:controller/:action/*');
  63. $result = $this->Helper->build('/controller/action/1');
  64. $this->assertEquals('/controller/action/1', $result);
  65. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  66. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  67. $result = $this->Helper->build(['controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"']);
  68. $this->assertEquals("/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  69. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  70. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  71. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  72. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  73. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  74. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  75. $result = $this->Helper->build([
  76. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  77. ]);
  78. $this->assertEquals("/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  79. $result = $this->Helper->build([
  80. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  81. '?' => ['one' => 'value', 'two' => 'value', 'three' => 'purple']
  82. ]);
  83. $this->assertEquals("/posts/index?one=value&amp;two=value&amp;three=purple&amp;page=1", $result);
  84. }
  85. /**
  86. * test assetTimestamp application
  87. *
  88. * @return void
  89. */
  90. public function testAssetTimestamp()
  91. {
  92. Configure::write('Foo.bar', 'test');
  93. Configure::write('Asset.timestamp', false);
  94. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  95. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  96. Configure::write('Asset.timestamp', true);
  97. Configure::write('debug', false);
  98. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  99. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  100. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  101. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  102. Configure::write('Asset.timestamp', true);
  103. Configure::write('debug', true);
  104. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  105. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  106. Configure::write('Asset.timestamp', 'force');
  107. Configure::write('debug', false);
  108. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  109. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  110. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  111. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  112. $this->Helper->request->webroot = '/some/dir/';
  113. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  114. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  115. }
  116. /**
  117. * test assetUrl application
  118. *
  119. * @return void
  120. */
  121. public function testAssetUrl()
  122. {
  123. Router::connect('/:controller/:action/*');
  124. $this->Helper->webroot = '';
  125. $result = $this->Helper->assetUrl(
  126. [
  127. 'controller' => 'js',
  128. 'action' => 'post',
  129. '_ext' => 'js'
  130. ],
  131. ['fullBase' => true]
  132. );
  133. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  134. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  135. $this->assertEquals('img/foo.jpg', $result);
  136. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  137. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  138. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  139. $this->assertEquals('style.css', $result);
  140. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  141. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  142. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  143. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  144. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  145. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  146. }
  147. /**
  148. * Test assetUrl with no rewriting.
  149. *
  150. * @return void
  151. */
  152. public function testAssetUrlNoRewrite()
  153. {
  154. $this->Helper->request->addPaths([
  155. 'base' => '/cake_dev/index.php',
  156. 'webroot' => '/cake_dev/app/webroot/',
  157. 'here' => '/cake_dev/index.php/tasks',
  158. ]);
  159. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  160. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  161. $this->assertEquals($expected, $result);
  162. }
  163. /**
  164. * Test assetUrl with plugins.
  165. *
  166. * @return void
  167. */
  168. public function testAssetUrlPlugin()
  169. {
  170. $this->Helper->webroot = '';
  171. Plugin::load('TestPlugin');
  172. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  173. $this->assertEquals('test_plugin/style.css', $result);
  174. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  175. $this->assertEquals('TestPlugin.style.css', $result);
  176. Plugin::unload('TestPlugin');
  177. }
  178. /**
  179. * test assetUrl and Asset.timestamp = force
  180. *
  181. * @return void
  182. */
  183. public function testAssetUrlTimestampForce()
  184. {
  185. $this->Helper->webroot = '';
  186. Configure::write('Asset.timestamp', 'force');
  187. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  188. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  189. }
  190. /**
  191. * test assetTimestamp with plugins and themes
  192. *
  193. * @return void
  194. */
  195. public function testAssetTimestampPluginsAndThemes()
  196. {
  197. Configure::write('Asset.timestamp', 'force');
  198. Plugin::load(['TestPlugin']);
  199. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  200. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  201. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  202. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  203. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  204. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  205. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  206. $this->assertRegExp('#/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  207. }
  208. /**
  209. * test script()
  210. *
  211. * @return void
  212. */
  213. public function testScript()
  214. {
  215. Router::connect('/:controller/:action/*');
  216. $this->Helper->webroot = '';
  217. $result = $this->Helper->script(
  218. [
  219. 'controller' => 'js',
  220. 'action' => 'post',
  221. '_ext' => 'js'
  222. ],
  223. ['fullBase' => true]
  224. );
  225. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  226. }
  227. /**
  228. * test image()
  229. *
  230. * @return void
  231. */
  232. public function testImage()
  233. {
  234. $result = $this->Helper->image('foo.jpg');
  235. $this->assertEquals('img/foo.jpg', $result);
  236. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  237. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  238. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  239. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  240. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  241. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  242. $result = $this->Helper->image('dir/big+tall/image.jpg');
  243. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  244. $result = $this->Helper->image('cid:foo.jpg');
  245. $this->assertEquals('cid:foo.jpg', $result);
  246. $result = $this->Helper->image('CID:foo.jpg');
  247. $this->assertEquals('CID:foo.jpg', $result);
  248. }
  249. /**
  250. * test css
  251. *
  252. * @return void
  253. */
  254. public function testCss()
  255. {
  256. $result = $this->Helper->css('style');
  257. $this->assertEquals('css/style.css', $result);
  258. }
  259. /**
  260. * Test generating paths with webroot().
  261. *
  262. * @return void
  263. */
  264. public function testWebrootPaths()
  265. {
  266. $this->Helper->request->webroot = '/';
  267. $result = $this->Helper->webroot('/img/cake.power.gif');
  268. $expected = '/img/cake.power.gif';
  269. $this->assertEquals($expected, $result);
  270. $this->Helper->theme = 'TestTheme';
  271. $result = $this->Helper->webroot('/img/cake.power.gif');
  272. $expected = '/test_theme/img/cake.power.gif';
  273. $this->assertEquals($expected, $result);
  274. $result = $this->Helper->webroot('/img/test.jpg');
  275. $expected = '/test_theme/img/test.jpg';
  276. $this->assertEquals($expected, $result);
  277. $webRoot = Configure::read('App.wwwRoot');
  278. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  279. $result = $this->Helper->webroot('/img/cake.power.gif');
  280. $expected = '/test_theme/img/cake.power.gif';
  281. $this->assertEquals($expected, $result);
  282. $result = $this->Helper->webroot('/img/test.jpg');
  283. $expected = '/test_theme/img/test.jpg';
  284. $this->assertEquals($expected, $result);
  285. $result = $this->Helper->webroot('/img/cake.icon.gif');
  286. $expected = '/img/cake.icon.gif';
  287. $this->assertEquals($expected, $result);
  288. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  289. $expected = '/img/cake.icon.gif?some=param';
  290. $this->assertEquals($expected, $result);
  291. Configure::write('App.wwwRoot', $webRoot);
  292. }
  293. }