UrlHelperTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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\Http\ServerRequest;
  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. * @var \Cake\View\Helper\UrlHelper
  30. */
  31. public $Helper;
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. Router::reload();
  41. $this->View = new View();
  42. $this->Helper = new UrlHelper($this->View);
  43. $this->Helper->request = new ServerRequest();
  44. static::setAppNamespace();
  45. Plugin::load(['TestTheme']);
  46. }
  47. /**
  48. * tearDown method
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. parent::tearDown();
  55. Configure::delete('Asset');
  56. Plugin::unload();
  57. unset($this->Helper, $this->View);
  58. }
  59. /**
  60. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  61. *
  62. * @return void
  63. */
  64. public function testUrlConversion()
  65. {
  66. Router::connect('/:controller/:action/*');
  67. $result = $this->Helper->build('/controller/action/1');
  68. $this->assertEquals('/controller/action/1', $result);
  69. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  70. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  71. $result = $this->Helper->build(['controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"']);
  72. $this->assertEquals('/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22', $result);
  73. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  74. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  75. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  76. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  77. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  78. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  79. $result = $this->Helper->build([
  80. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  81. ]);
  82. $this->assertEquals('/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524', $result);
  83. $result = $this->Helper->build([
  84. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  85. '?' => ['one' => 'value', 'two' => 'value', 'three' => 'purple']
  86. ]);
  87. $this->assertEquals('/posts/index?one=value&amp;two=value&amp;three=purple&amp;page=1', $result);
  88. }
  89. /**
  90. * @return void
  91. */
  92. public function testUrlConversionUnescaped()
  93. {
  94. $result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
  95. $this->assertEquals('/controller/action/1?one=1&two=2', $result);
  96. $result = $this->Helper->build([
  97. 'controller' => 'posts',
  98. 'action' => 'view',
  99. 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24',
  100. '?' => [
  101. 'k' => 'v',
  102. '1' => '2'
  103. ]
  104. ], ['escape' => false]);
  105. $this->assertEquals('/posts/view?k=v&1=2&param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524', $result);
  106. }
  107. /**
  108. * test assetTimestamp application
  109. *
  110. * @return void
  111. */
  112. public function testAssetTimestamp()
  113. {
  114. Configure::write('Foo.bar', 'test');
  115. Configure::write('Asset.timestamp', false);
  116. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  117. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  118. Configure::write('Asset.timestamp', true);
  119. Configure::write('debug', false);
  120. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  121. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  122. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  123. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  124. Configure::write('Asset.timestamp', true);
  125. Configure::write('debug', true);
  126. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  127. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  128. Configure::write('Asset.timestamp', 'force');
  129. Configure::write('debug', false);
  130. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  131. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  132. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  133. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  134. $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/some/dir/');
  135. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  136. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  137. }
  138. /**
  139. * test assetUrl application
  140. *
  141. * @return void
  142. */
  143. public function testAssetUrl()
  144. {
  145. Router::connect('/:controller/:action/*');
  146. $this->Helper->webroot = '';
  147. $result = $this->Helper->assetUrl(
  148. [
  149. 'controller' => 'js',
  150. 'action' => 'post',
  151. '_ext' => 'js'
  152. ],
  153. ['fullBase' => true]
  154. );
  155. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  156. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  157. $this->assertEquals('img/foo.jpg', $result);
  158. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  159. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  160. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  161. $this->assertEquals('style.css', $result);
  162. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  163. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  164. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  165. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  166. $result = $this->Helper->assetUrl('x:"><script>alert(1)</script>');
  167. $this->assertEquals('x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', $result);
  168. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  169. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  170. }
  171. /**
  172. * Test assetUrl with no rewriting.
  173. *
  174. * @return void
  175. */
  176. public function testAssetUrlNoRewrite()
  177. {
  178. $this->Helper->request = $this->Helper->request
  179. ->withAttribute('base', '/cake_dev/index.php')
  180. ->withAttribute('webroot', '/cake_dev/app/webroot/')
  181. ->withRequestTarget('/cake_dev/index.php/tasks');
  182. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  183. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  184. $this->assertEquals($expected, $result);
  185. }
  186. /**
  187. * Test assetUrl with plugins.
  188. *
  189. * @return void
  190. */
  191. public function testAssetUrlPlugin()
  192. {
  193. $this->Helper->webroot = '';
  194. Plugin::load('TestPlugin');
  195. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  196. $this->assertEquals('test_plugin/style.css', $result);
  197. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  198. $this->assertEquals('TestPlugin.style.css', $result);
  199. Plugin::unload('TestPlugin');
  200. }
  201. /**
  202. * test assetUrl and Asset.timestamp = force
  203. *
  204. * @return void
  205. */
  206. public function testAssetUrlTimestampForce()
  207. {
  208. $this->Helper->webroot = '';
  209. Configure::write('Asset.timestamp', 'force');
  210. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  211. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  212. }
  213. /**
  214. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  215. *
  216. * @return void
  217. */
  218. public function testAssetTimestampConfigureOverride()
  219. {
  220. $this->Helper->webroot = '';
  221. Configure::write('Asset.timestamp', 'force');
  222. $timestamp = false;
  223. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  224. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  225. }
  226. /**
  227. * test assetTimestamp with plugins and themes
  228. *
  229. * @return void
  230. */
  231. public function testAssetTimestampPluginsAndThemes()
  232. {
  233. Configure::write('Asset.timestamp', 'force');
  234. Plugin::load(['TestPlugin']);
  235. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  236. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  237. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  238. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  239. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  240. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  241. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  242. $this->assertRegExp('#/test_theme/js/non_existant.js$#', $result, 'No error on missing file');
  243. }
  244. /**
  245. * test script()
  246. *
  247. * @return void
  248. */
  249. public function testScript()
  250. {
  251. Router::connect('/:controller/:action/*');
  252. $this->Helper->webroot = '';
  253. $result = $this->Helper->script(
  254. [
  255. 'controller' => 'js',
  256. 'action' => 'post',
  257. '_ext' => 'js'
  258. ],
  259. ['fullBase' => true]
  260. );
  261. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  262. }
  263. /**
  264. * Test script and Asset.timestamp = force
  265. *
  266. * @return void
  267. */
  268. public function testScriptTimestampForce()
  269. {
  270. $this->Helper->webroot = '';
  271. Configure::write('Asset.timestamp', 'force');
  272. $result = $this->Helper->script('script.js');
  273. $this->assertRegExp('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  274. }
  275. /**
  276. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  277. *
  278. * @return void
  279. */
  280. public function testScriptTimestampConfigureOverride()
  281. {
  282. Configure::write('Asset.timestamp', 'force');
  283. $timestamp = false;
  284. $result = $this->Helper->script('script.js', ['timestamp' => $timestamp]);
  285. $this->assertEquals(Configure::read('App.jsBaseUrl') . 'script.js', $result);
  286. }
  287. /**
  288. * test image()
  289. *
  290. * @return void
  291. */
  292. public function testImage()
  293. {
  294. $result = $this->Helper->image('foo.jpg');
  295. $this->assertEquals('img/foo.jpg', $result);
  296. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  297. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  298. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  299. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  300. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  301. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  302. $result = $this->Helper->image('dir/big+tall/image.jpg');
  303. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  304. $result = $this->Helper->image('cid:foo.jpg');
  305. $this->assertEquals('cid:foo.jpg', $result);
  306. $result = $this->Helper->image('CID:foo.jpg');
  307. $this->assertEquals('CID:foo.jpg', $result);
  308. }
  309. /**
  310. * Test image with `Asset.timestamp` = force
  311. *
  312. * @return void
  313. */
  314. public function testImageTimestampForce()
  315. {
  316. Configure::write('Asset.timestamp', 'force');
  317. $result = $this->Helper->image('cake.icon.png');
  318. $this->assertRegExp('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  319. }
  320. /**
  321. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  322. *
  323. * @return void
  324. */
  325. public function testImageTimestampConfigureOverride()
  326. {
  327. Configure::write('Asset.timestamp', 'force');
  328. $timestamp = false;
  329. $result = $this->Helper->image('cake.icon.png', ['timestamp' => $timestamp]);
  330. $this->assertEquals('img/cake.icon.png', $result);
  331. }
  332. /**
  333. * test css
  334. *
  335. * @return void
  336. */
  337. public function testCss()
  338. {
  339. $result = $this->Helper->css('style');
  340. $this->assertEquals('css/style.css', $result);
  341. }
  342. /**
  343. * Test css with `Asset.timestamp` = force
  344. *
  345. * @return void
  346. */
  347. public function testCssTimestampForce()
  348. {
  349. Configure::write('Asset.timestamp', 'force');
  350. $result = $this->Helper->css('cake.generic');
  351. $this->assertRegExp('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  352. }
  353. /**
  354. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  355. *
  356. * @return void
  357. */
  358. public function testCssTimestampConfigureOverride()
  359. {
  360. Configure::write('Asset.timestamp', 'force');
  361. $timestamp = false;
  362. $result = $this->Helper->css('cake.generic', ['timestamp' => $timestamp]);
  363. $this->assertEquals('css/cake.generic.css', $result);
  364. }
  365. /**
  366. * Test generating paths with webroot().
  367. *
  368. * @return void
  369. */
  370. public function testWebrootPaths()
  371. {
  372. $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/');
  373. $result = $this->Helper->webroot('/img/cake.power.gif');
  374. $expected = '/img/cake.power.gif';
  375. $this->assertEquals($expected, $result);
  376. $this->Helper->theme = 'TestTheme';
  377. $result = $this->Helper->webroot('/img/cake.power.gif');
  378. $expected = '/test_theme/img/cake.power.gif';
  379. $this->assertEquals($expected, $result);
  380. $result = $this->Helper->webroot('/img/test.jpg');
  381. $expected = '/test_theme/img/test.jpg';
  382. $this->assertEquals($expected, $result);
  383. $webRoot = Configure::read('App.wwwRoot');
  384. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  385. $result = $this->Helper->webroot('/img/cake.power.gif');
  386. $expected = '/test_theme/img/cake.power.gif';
  387. $this->assertEquals($expected, $result);
  388. $result = $this->Helper->webroot('/img/test.jpg');
  389. $expected = '/test_theme/img/test.jpg';
  390. $this->assertEquals($expected, $result);
  391. $result = $this->Helper->webroot('/img/cake.icon.gif');
  392. $expected = '/img/cake.icon.gif';
  393. $this->assertEquals($expected, $result);
  394. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  395. $expected = '/img/cake.icon.gif?some=param';
  396. $this->assertEquals($expected, $result);
  397. Configure::write('App.wwwRoot', $webRoot);
  398. }
  399. }