UrlHelperTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 and data uris
  198. *
  199. * @return void
  200. */
  201. public function testAssetUrlDataUri()
  202. {
  203. $request = $this->Helper->request
  204. ->withAttribute('base', 'subdir')
  205. ->withAttribute('webroot', 'subdir/');
  206. $this->Helper->request = $request;
  207. Router::pushRequest($request);
  208. $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
  209. '/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
  210. $result = $this->Helper->assetUrl($data);
  211. $this->assertSame($data, $result);
  212. $data = 'data:image/png;base64,<evil>';
  213. $result = $this->Helper->assetUrl($data);
  214. $this->assertHtml(h($data), $result);
  215. }
  216. /**
  217. * Test assetUrl with no rewriting.
  218. *
  219. * @return void
  220. */
  221. public function testAssetUrlNoRewrite()
  222. {
  223. $this->Helper->request = $this->Helper->request
  224. ->withAttribute('base', '/cake_dev/index.php')
  225. ->withAttribute('webroot', '/cake_dev/app/webroot/')
  226. ->withRequestTarget('/cake_dev/index.php/tasks');
  227. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  228. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  229. $this->assertEquals($expected, $result);
  230. }
  231. /**
  232. * Test assetUrl with plugins.
  233. *
  234. * @return void
  235. */
  236. public function testAssetUrlPlugin()
  237. {
  238. $this->Helper->webroot = '';
  239. Plugin::load('TestPlugin');
  240. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  241. $this->assertEquals('test_plugin/style.css', $result);
  242. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  243. $this->assertEquals('TestPlugin.style.css', $result);
  244. Plugin::unload('TestPlugin');
  245. }
  246. /**
  247. * test assetUrl and Asset.timestamp = force
  248. *
  249. * @return void
  250. */
  251. public function testAssetUrlTimestampForce()
  252. {
  253. $this->Helper->webroot = '';
  254. Configure::write('Asset.timestamp', 'force');
  255. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  256. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  257. }
  258. /**
  259. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  260. *
  261. * @return void
  262. */
  263. public function testAssetTimestampConfigureOverride()
  264. {
  265. $this->Helper->webroot = '';
  266. Configure::write('Asset.timestamp', 'force');
  267. $timestamp = false;
  268. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  269. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  270. }
  271. /**
  272. * test assetTimestamp with plugins and themes
  273. *
  274. * @return void
  275. */
  276. public function testAssetTimestampPluginsAndThemes()
  277. {
  278. Configure::write('Asset.timestamp', 'force');
  279. Plugin::load(['TestPlugin']);
  280. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  281. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  282. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  283. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  284. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  285. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  286. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  287. $this->assertRegExp('#/test_theme/js/non_existant.js$#', $result, 'No error on missing file');
  288. }
  289. /**
  290. * test script()
  291. *
  292. * @return void
  293. */
  294. public function testScript()
  295. {
  296. Router::connect('/:controller/:action/*');
  297. $this->Helper->webroot = '';
  298. $result = $this->Helper->script(
  299. [
  300. 'controller' => 'js',
  301. 'action' => 'post',
  302. '_ext' => 'js'
  303. ],
  304. ['fullBase' => true]
  305. );
  306. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  307. }
  308. /**
  309. * Test script and Asset.timestamp = force
  310. *
  311. * @return void
  312. */
  313. public function testScriptTimestampForce()
  314. {
  315. $this->Helper->webroot = '';
  316. Configure::write('Asset.timestamp', 'force');
  317. $result = $this->Helper->script('script.js');
  318. $this->assertRegExp('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  319. }
  320. /**
  321. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  322. *
  323. * @return void
  324. */
  325. public function testScriptTimestampConfigureOverride()
  326. {
  327. Configure::write('Asset.timestamp', 'force');
  328. $timestamp = false;
  329. $result = $this->Helper->script('script.js', ['timestamp' => $timestamp]);
  330. $this->assertEquals(Configure::read('App.jsBaseUrl') . 'script.js', $result);
  331. }
  332. /**
  333. * test image()
  334. *
  335. * @return void
  336. */
  337. public function testImage()
  338. {
  339. $result = $this->Helper->image('foo.jpg');
  340. $this->assertEquals('img/foo.jpg', $result);
  341. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  342. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  343. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  344. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  345. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  346. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  347. $result = $this->Helper->image('dir/big+tall/image.jpg');
  348. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  349. $result = $this->Helper->image('cid:foo.jpg');
  350. $this->assertEquals('cid:foo.jpg', $result);
  351. $result = $this->Helper->image('CID:foo.jpg');
  352. $this->assertEquals('CID:foo.jpg', $result);
  353. }
  354. /**
  355. * Test image with `Asset.timestamp` = force
  356. *
  357. * @return void
  358. */
  359. public function testImageTimestampForce()
  360. {
  361. Configure::write('Asset.timestamp', 'force');
  362. $result = $this->Helper->image('cake.icon.png');
  363. $this->assertRegExp('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  364. }
  365. /**
  366. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  367. *
  368. * @return void
  369. */
  370. public function testImageTimestampConfigureOverride()
  371. {
  372. Configure::write('Asset.timestamp', 'force');
  373. $timestamp = false;
  374. $result = $this->Helper->image('cake.icon.png', ['timestamp' => $timestamp]);
  375. $this->assertEquals('img/cake.icon.png', $result);
  376. }
  377. /**
  378. * test css
  379. *
  380. * @return void
  381. */
  382. public function testCss()
  383. {
  384. $result = $this->Helper->css('style');
  385. $this->assertEquals('css/style.css', $result);
  386. }
  387. /**
  388. * Test css with `Asset.timestamp` = force
  389. *
  390. * @return void
  391. */
  392. public function testCssTimestampForce()
  393. {
  394. Configure::write('Asset.timestamp', 'force');
  395. $result = $this->Helper->css('cake.generic');
  396. $this->assertRegExp('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  397. }
  398. /**
  399. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  400. *
  401. * @return void
  402. */
  403. public function testCssTimestampConfigureOverride()
  404. {
  405. Configure::write('Asset.timestamp', 'force');
  406. $timestamp = false;
  407. $result = $this->Helper->css('cake.generic', ['timestamp' => $timestamp]);
  408. $this->assertEquals('css/cake.generic.css', $result);
  409. }
  410. /**
  411. * Test generating paths with webroot().
  412. *
  413. * @return void
  414. */
  415. public function testWebrootPaths()
  416. {
  417. $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/');
  418. $result = $this->Helper->webroot('/img/cake.power.gif');
  419. $expected = '/img/cake.power.gif';
  420. $this->assertEquals($expected, $result);
  421. $this->Helper->theme = 'TestTheme';
  422. $result = $this->Helper->webroot('/img/cake.power.gif');
  423. $expected = '/test_theme/img/cake.power.gif';
  424. $this->assertEquals($expected, $result);
  425. $result = $this->Helper->webroot('/img/test.jpg');
  426. $expected = '/test_theme/img/test.jpg';
  427. $this->assertEquals($expected, $result);
  428. $webRoot = Configure::read('App.wwwRoot');
  429. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  430. $result = $this->Helper->webroot('/img/cake.power.gif');
  431. $expected = '/test_theme/img/cake.power.gif';
  432. $this->assertEquals($expected, $result);
  433. $result = $this->Helper->webroot('/img/test.jpg');
  434. $expected = '/test_theme/img/test.jpg';
  435. $this->assertEquals($expected, $result);
  436. $result = $this->Helper->webroot('/img/cake.icon.gif');
  437. $expected = '/img/cake.icon.gif';
  438. $this->assertEquals($expected, $result);
  439. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  440. $expected = '/img/cake.icon.gif?some=param';
  441. $this->assertEquals($expected, $result);
  442. Configure::write('App.wwwRoot', $webRoot);
  443. }
  444. }