UrlHelperTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 testBuildUrlConversion()
  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. * ensure that build factors in base paths.
  91. *
  92. * @return void
  93. */
  94. public function testBuildBasePath()
  95. {
  96. Router::connect('/:controller/:action/*');
  97. $request = new ServerRequest([
  98. 'params' => [
  99. 'action' => 'index',
  100. 'plugin' => null,
  101. 'controller' => 'subscribe',
  102. ],
  103. 'url' => '/subscribe',
  104. 'base' => '/magazine',
  105. 'webroot' => '/magazine/'
  106. ]);
  107. Router::pushRequest($request);
  108. $this->assertEquals('/magazine/subscribe', $this->Helper->build());
  109. $this->assertEquals(
  110. '/magazine/articles/add',
  111. $this->Helper->build(['controller' => 'articles', 'action' => 'add'])
  112. );
  113. }
  114. /**
  115. * @return void
  116. */
  117. public function testBuildUrlConversionUnescaped()
  118. {
  119. $result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
  120. $this->assertEquals('/controller/action/1?one=1&two=2', $result);
  121. $result = $this->Helper->build([
  122. 'controller' => 'posts',
  123. 'action' => 'view',
  124. 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24',
  125. '?' => [
  126. 'k' => 'v',
  127. '1' => '2'
  128. ]
  129. ], ['escape' => false]);
  130. $this->assertEquals('/posts/view?k=v&1=2&param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524', $result);
  131. }
  132. /**
  133. * test assetTimestamp application
  134. *
  135. * @return void
  136. */
  137. public function testAssetTimestamp()
  138. {
  139. Configure::write('Foo.bar', 'test');
  140. Configure::write('Asset.timestamp', false);
  141. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  142. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  143. Configure::write('Asset.timestamp', true);
  144. Configure::write('debug', false);
  145. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  146. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  147. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  148. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  149. Configure::write('Asset.timestamp', true);
  150. Configure::write('debug', true);
  151. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  152. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  153. Configure::write('Asset.timestamp', 'force');
  154. Configure::write('debug', false);
  155. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  156. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  157. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  158. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  159. $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/some/dir/');
  160. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  161. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  162. }
  163. /**
  164. * test assetUrl application
  165. *
  166. * @return void
  167. */
  168. public function testAssetUrl()
  169. {
  170. Router::connect('/:controller/:action/*');
  171. $this->Helper->webroot = '';
  172. $result = $this->Helper->assetUrl(
  173. [
  174. 'controller' => 'js',
  175. 'action' => 'post',
  176. '_ext' => 'js'
  177. ],
  178. ['fullBase' => true]
  179. );
  180. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  181. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  182. $this->assertEquals('img/foo.jpg', $result);
  183. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  184. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  185. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  186. $this->assertEquals('style.css', $result);
  187. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  188. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  189. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  190. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  191. $result = $this->Helper->assetUrl('x:"><script>alert(1)</script>');
  192. $this->assertEquals('x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', $result);
  193. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  194. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  195. }
  196. /**
  197. * Test assetUrl with no rewriting.
  198. *
  199. * @return void
  200. */
  201. public function testAssetUrlNoRewrite()
  202. {
  203. $this->Helper->request = $this->Helper->request
  204. ->withAttribute('base', '/cake_dev/index.php')
  205. ->withAttribute('webroot', '/cake_dev/app/webroot/')
  206. ->withRequestTarget('/cake_dev/index.php/tasks');
  207. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  208. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  209. $this->assertEquals($expected, $result);
  210. }
  211. /**
  212. * Test assetUrl with plugins.
  213. *
  214. * @return void
  215. */
  216. public function testAssetUrlPlugin()
  217. {
  218. $this->Helper->webroot = '';
  219. Plugin::load('TestPlugin');
  220. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  221. $this->assertEquals('test_plugin/style.css', $result);
  222. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  223. $this->assertEquals('TestPlugin.style.css', $result);
  224. Plugin::unload('TestPlugin');
  225. }
  226. /**
  227. * test assetUrl and Asset.timestamp = force
  228. *
  229. * @return void
  230. */
  231. public function testAssetUrlTimestampForce()
  232. {
  233. $this->Helper->webroot = '';
  234. Configure::write('Asset.timestamp', 'force');
  235. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  236. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  237. }
  238. /**
  239. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  240. *
  241. * @return void
  242. */
  243. public function testAssetTimestampConfigureOverride()
  244. {
  245. $this->Helper->webroot = '';
  246. Configure::write('Asset.timestamp', 'force');
  247. $timestamp = false;
  248. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  249. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  250. }
  251. /**
  252. * test assetTimestamp with plugins and themes
  253. *
  254. * @return void
  255. */
  256. public function testAssetTimestampPluginsAndThemes()
  257. {
  258. Configure::write('Asset.timestamp', 'force');
  259. Plugin::load(['TestPlugin']);
  260. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  261. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  262. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  263. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  264. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  265. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  266. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  267. $this->assertRegExp('#/test_theme/js/non_existant.js$#', $result, 'No error on missing file');
  268. }
  269. /**
  270. * test script()
  271. *
  272. * @return void
  273. */
  274. public function testScript()
  275. {
  276. Router::connect('/:controller/:action/*');
  277. $this->Helper->webroot = '';
  278. $result = $this->Helper->script(
  279. [
  280. 'controller' => 'js',
  281. 'action' => 'post',
  282. '_ext' => 'js'
  283. ],
  284. ['fullBase' => true]
  285. );
  286. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  287. }
  288. /**
  289. * Test script and Asset.timestamp = force
  290. *
  291. * @return void
  292. */
  293. public function testScriptTimestampForce()
  294. {
  295. $this->Helper->webroot = '';
  296. Configure::write('Asset.timestamp', 'force');
  297. $result = $this->Helper->script('script.js');
  298. $this->assertRegExp('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  299. }
  300. /**
  301. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  302. *
  303. * @return void
  304. */
  305. public function testScriptTimestampConfigureOverride()
  306. {
  307. Configure::write('Asset.timestamp', 'force');
  308. $timestamp = false;
  309. $result = $this->Helper->script('script.js', ['timestamp' => $timestamp]);
  310. $this->assertEquals(Configure::read('App.jsBaseUrl') . 'script.js', $result);
  311. }
  312. /**
  313. * test image()
  314. *
  315. * @return void
  316. */
  317. public function testImage()
  318. {
  319. $result = $this->Helper->image('foo.jpg');
  320. $this->assertEquals('img/foo.jpg', $result);
  321. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  322. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  323. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  324. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  325. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  326. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  327. $result = $this->Helper->image('dir/big+tall/image.jpg');
  328. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  329. $result = $this->Helper->image('cid:foo.jpg');
  330. $this->assertEquals('cid:foo.jpg', $result);
  331. $result = $this->Helper->image('CID:foo.jpg');
  332. $this->assertEquals('CID:foo.jpg', $result);
  333. }
  334. /**
  335. * Test image with `Asset.timestamp` = force
  336. *
  337. * @return void
  338. */
  339. public function testImageTimestampForce()
  340. {
  341. Configure::write('Asset.timestamp', 'force');
  342. $result = $this->Helper->image('cake.icon.png');
  343. $this->assertRegExp('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  344. }
  345. /**
  346. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  347. *
  348. * @return void
  349. */
  350. public function testImageTimestampConfigureOverride()
  351. {
  352. Configure::write('Asset.timestamp', 'force');
  353. $timestamp = false;
  354. $result = $this->Helper->image('cake.icon.png', ['timestamp' => $timestamp]);
  355. $this->assertEquals('img/cake.icon.png', $result);
  356. }
  357. /**
  358. * test css
  359. *
  360. * @return void
  361. */
  362. public function testCss()
  363. {
  364. $result = $this->Helper->css('style');
  365. $this->assertEquals('css/style.css', $result);
  366. }
  367. /**
  368. * Test css with `Asset.timestamp` = force
  369. *
  370. * @return void
  371. */
  372. public function testCssTimestampForce()
  373. {
  374. Configure::write('Asset.timestamp', 'force');
  375. $result = $this->Helper->css('cake.generic');
  376. $this->assertRegExp('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  377. }
  378. /**
  379. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  380. *
  381. * @return void
  382. */
  383. public function testCssTimestampConfigureOverride()
  384. {
  385. Configure::write('Asset.timestamp', 'force');
  386. $timestamp = false;
  387. $result = $this->Helper->css('cake.generic', ['timestamp' => $timestamp]);
  388. $this->assertEquals('css/cake.generic.css', $result);
  389. }
  390. /**
  391. * Test generating paths with webroot().
  392. *
  393. * @return void
  394. */
  395. public function testWebrootPaths()
  396. {
  397. $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/');
  398. $result = $this->Helper->webroot('/img/cake.power.gif');
  399. $expected = '/img/cake.power.gif';
  400. $this->assertEquals($expected, $result);
  401. $this->Helper->theme = 'TestTheme';
  402. $result = $this->Helper->webroot('/img/cake.power.gif');
  403. $expected = '/test_theme/img/cake.power.gif';
  404. $this->assertEquals($expected, $result);
  405. $result = $this->Helper->webroot('/img/test.jpg');
  406. $expected = '/test_theme/img/test.jpg';
  407. $this->assertEquals($expected, $result);
  408. $webRoot = Configure::read('App.wwwRoot');
  409. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  410. $result = $this->Helper->webroot('/img/cake.power.gif');
  411. $expected = '/test_theme/img/cake.power.gif';
  412. $this->assertEquals($expected, $result);
  413. $result = $this->Helper->webroot('/img/test.jpg');
  414. $expected = '/test_theme/img/test.jpg';
  415. $this->assertEquals($expected, $result);
  416. $result = $this->Helper->webroot('/img/cake.icon.gif');
  417. $expected = '/img/cake.icon.gif';
  418. $this->assertEquals($expected, $result);
  419. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  420. $expected = '/img/cake.icon.gif?some=param';
  421. $this->assertEquals($expected, $result);
  422. Configure::write('App.wwwRoot', $webRoot);
  423. }
  424. }