UrlHelperTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. $this->clearPlugins();
  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. $this->removePlugins(['TestPlugin']);
  244. }
  245. /**
  246. * Tests assetUrl() with full base URL.
  247. *
  248. * @return void
  249. */
  250. public function testAssetUrlFullBase()
  251. {
  252. $result = $this->Helper->assetUrl('img/foo.jpg', ['fullBase' => true]);
  253. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  254. $result = $this->Helper->assetUrl('img/foo.jpg', ['fullBase' => 'https://xyz/']);
  255. $this->assertEquals('https://xyz/img/foo.jpg', $result);
  256. }
  257. /**
  258. * test assetUrl and Asset.timestamp = force
  259. *
  260. * @return void
  261. */
  262. public function testAssetUrlTimestampForce()
  263. {
  264. $this->Helper->webroot = '';
  265. Configure::write('Asset.timestamp', 'force');
  266. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  267. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  268. }
  269. /**
  270. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  271. *
  272. * @return void
  273. */
  274. public function testAssetTimestampConfigureOverride()
  275. {
  276. $this->Helper->webroot = '';
  277. Configure::write('Asset.timestamp', 'force');
  278. $timestamp = false;
  279. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  280. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  281. }
  282. /**
  283. * test assetTimestamp with plugins and themes
  284. *
  285. * @return void
  286. */
  287. public function testAssetTimestampPluginsAndThemes()
  288. {
  289. Configure::write('Asset.timestamp', 'force');
  290. $this->loadPlugins(['TestPlugin']);
  291. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  292. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  293. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  294. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  295. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  296. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  297. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  298. $this->assertRegExp('#/test_theme/js/non_existant.js$#', $result, 'No error on missing file');
  299. }
  300. /**
  301. * test script()
  302. *
  303. * @return void
  304. */
  305. public function testScript()
  306. {
  307. Router::connect('/:controller/:action/*');
  308. $this->Helper->webroot = '';
  309. $result = $this->Helper->script(
  310. [
  311. 'controller' => 'js',
  312. 'action' => 'post',
  313. '_ext' => 'js',
  314. ],
  315. ['fullBase' => true]
  316. );
  317. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  318. }
  319. /**
  320. * Test script and Asset.timestamp = force
  321. *
  322. * @return void
  323. */
  324. public function testScriptTimestampForce()
  325. {
  326. $this->Helper->webroot = '';
  327. Configure::write('Asset.timestamp', 'force');
  328. $result = $this->Helper->script('script.js');
  329. $this->assertRegExp('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  330. }
  331. /**
  332. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  333. *
  334. * @return void
  335. */
  336. public function testScriptTimestampConfigureOverride()
  337. {
  338. Configure::write('Asset.timestamp', 'force');
  339. $timestamp = false;
  340. $result = $this->Helper->script('script.js', ['timestamp' => $timestamp]);
  341. $this->assertEquals(Configure::read('App.jsBaseUrl') . 'script.js', $result);
  342. }
  343. /**
  344. * test image()
  345. *
  346. * @return void
  347. */
  348. public function testImage()
  349. {
  350. $result = $this->Helper->image('foo.jpg');
  351. $this->assertEquals('img/foo.jpg', $result);
  352. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  353. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  354. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  355. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  356. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  357. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  358. $result = $this->Helper->image('dir/big+tall/image.jpg');
  359. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  360. $result = $this->Helper->image('cid:foo.jpg');
  361. $this->assertEquals('cid:foo.jpg', $result);
  362. $result = $this->Helper->image('CID:foo.jpg');
  363. $this->assertEquals('CID:foo.jpg', $result);
  364. }
  365. /**
  366. * Test image with `Asset.timestamp` = force
  367. *
  368. * @return void
  369. */
  370. public function testImageTimestampForce()
  371. {
  372. Configure::write('Asset.timestamp', 'force');
  373. $result = $this->Helper->image('cake.icon.png');
  374. $this->assertRegExp('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  375. }
  376. /**
  377. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  378. *
  379. * @return void
  380. */
  381. public function testImageTimestampConfigureOverride()
  382. {
  383. Configure::write('Asset.timestamp', 'force');
  384. $timestamp = false;
  385. $result = $this->Helper->image('cake.icon.png', ['timestamp' => $timestamp]);
  386. $this->assertEquals('img/cake.icon.png', $result);
  387. }
  388. /**
  389. * test css
  390. *
  391. * @return void
  392. */
  393. public function testCss()
  394. {
  395. $result = $this->Helper->css('style');
  396. $this->assertEquals('css/style.css', $result);
  397. }
  398. /**
  399. * Test css with `Asset.timestamp` = force
  400. *
  401. * @return void
  402. */
  403. public function testCssTimestampForce()
  404. {
  405. Configure::write('Asset.timestamp', 'force');
  406. $result = $this->Helper->css('cake.generic');
  407. $this->assertRegExp('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  408. }
  409. /**
  410. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  411. *
  412. * @return void
  413. */
  414. public function testCssTimestampConfigureOverride()
  415. {
  416. Configure::write('Asset.timestamp', 'force');
  417. $timestamp = false;
  418. $result = $this->Helper->css('cake.generic', ['timestamp' => $timestamp]);
  419. $this->assertEquals('css/cake.generic.css', $result);
  420. }
  421. /**
  422. * Test generating paths with webroot().
  423. *
  424. * @return void
  425. */
  426. public function testWebrootPaths()
  427. {
  428. $this->View->setRequest(
  429. $this->View->getRequest()->withAttribute('webroot', '/')
  430. );
  431. $result = $this->Helper->webroot('/img/cake.power.gif');
  432. $expected = '/img/cake.power.gif';
  433. $this->assertEquals($expected, $result);
  434. $this->Helper->getView()->setTheme('TestTheme');
  435. $result = $this->Helper->webroot('/img/cake.power.gif');
  436. $expected = '/test_theme/img/cake.power.gif';
  437. $this->assertEquals($expected, $result);
  438. $result = $this->Helper->webroot('/img/test.jpg');
  439. $expected = '/test_theme/img/test.jpg';
  440. $this->assertEquals($expected, $result);
  441. $webRoot = Configure::read('App.wwwRoot');
  442. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  443. $result = $this->Helper->webroot('/img/cake.power.gif');
  444. $expected = '/test_theme/img/cake.power.gif';
  445. $this->assertEquals($expected, $result);
  446. $result = $this->Helper->webroot('/img/test.jpg');
  447. $expected = '/test_theme/img/test.jpg';
  448. $this->assertEquals($expected, $result);
  449. $result = $this->Helper->webroot('/img/cake.icon.gif');
  450. $expected = '/img/cake.icon.gif';
  451. $this->assertEquals($expected, $result);
  452. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  453. $expected = '/img/cake.icon.gif?some=param';
  454. $this->assertEquals($expected, $result);
  455. Configure::write('App.wwwRoot', $webRoot);
  456. }
  457. /**
  458. * Test plugin based assets will NOT use the plugin name
  459. *
  460. * @return void
  461. */
  462. public function testPluginAssetsPrependImageBaseUrl()
  463. {
  464. $cdnPrefix = 'https://cdn.example.com/';
  465. $imageBaseUrl = Configure::read('App.imageBaseUrl');
  466. $jsBaseUrl = Configure::read('App.jsBaseUrl');
  467. $cssBaseUrl = Configure::read('App.cssBaseUrl');
  468. Configure::write('App.imageBaseUrl', $cdnPrefix);
  469. $result = $this->Helper->image('TestTheme.text.jpg');
  470. $expected = $cdnPrefix . 'text.jpg';
  471. $this->assertSame($expected, $result);
  472. Configure::write('App.jsBaseUrl', $cdnPrefix);
  473. $result = $this->Helper->script('TestTheme.app.js');
  474. $expected = $cdnPrefix . 'app.js';
  475. $this->assertSame($expected, $result);
  476. Configure::write('App.cssBaseUrl', $cdnPrefix);
  477. $result = $this->Helper->css('TestTheme.app.css');
  478. $expected = $cdnPrefix . 'app.css';
  479. $this->assertSame($expected, $result);
  480. }
  481. }