UrlHelperTest.php 18 KB

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