UrlHelperTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Exception\CakeException;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Routing\Route\DashedRoute;
  21. use Cake\Routing\Router;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\View\Helper\UrlHelper;
  24. use Cake\View\View;
  25. use TestApp\Routing\Asset;
  26. use function Cake\Core\h;
  27. /**
  28. * UrlHelperTest class
  29. */
  30. class UrlHelperTest extends TestCase
  31. {
  32. /**
  33. * @var \Cake\View\Helper\UrlHelper
  34. */
  35. protected $Helper;
  36. /**
  37. * @var \Cake\View\View
  38. */
  39. protected $View;
  40. /**
  41. * @var \Cake\Routing\RouteBuilder
  42. */
  43. protected $builder;
  44. /**
  45. * setUp method
  46. */
  47. public function setUp(): void
  48. {
  49. parent::setUp();
  50. Router::reload();
  51. $request = new ServerRequest();
  52. Router::setRequest($request);
  53. $this->View = new View($request);
  54. $this->Helper = new UrlHelper($this->View);
  55. static::setAppNamespace();
  56. $this->loadPlugins(['TestTheme']);
  57. $this->builder = Router::createRouteBuilder('/');
  58. $this->builder->fallbacks(DashedRoute::class);
  59. }
  60. /**
  61. * tearDown method
  62. */
  63. public function tearDown(): void
  64. {
  65. parent::tearDown();
  66. $this->clearPlugins();
  67. unset($this->Helper, $this->View);
  68. }
  69. /**
  70. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  71. */
  72. public function testBuildUrlConversion(): void
  73. {
  74. $this->builder->connect('/:controller/:action/*');
  75. $result = $this->Helper->build('/controller/action/1');
  76. $this->assertSame('/controller/action/1', $result);
  77. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  78. $this->assertSame('/controller/action/1?one=1&amp;two=2', $result);
  79. $result = $this->Helper->build(['controller' => 'Posts', 'action' => 'index', '?' => ['page' => '1" onclick="alert(\'XSS\');"']]);
  80. $this->assertSame('/posts?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22', $result);
  81. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  82. $this->assertSame('/controller/action/1/param:this+one+more', $result);
  83. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  84. $this->assertSame('/controller/action/1/param:this%20one%20more', $result);
  85. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  86. $this->assertSame('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  87. $result = $this->Helper->build([
  88. 'controller' => 'Posts', 'action' => 'index',
  89. '?' => ['param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'],
  90. ]);
  91. $this->assertSame('/posts?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524', $result);
  92. $result = $this->Helper->build([
  93. 'controller' => 'Posts', 'action' => 'index',
  94. '?' => ['one' => 'value', 'two' => 'value', 'three' => 'purple', 'page' => '1'],
  95. ]);
  96. $this->assertSame('/posts?one=value&amp;two=value&amp;three=purple&amp;page=1', $result);
  97. }
  98. /**
  99. * ensure that build factors in base paths.
  100. */
  101. public function testBuildBasePath(): void
  102. {
  103. $this->builder->connect('/:controller/:action/*');
  104. $request = new ServerRequest([
  105. 'params' => [
  106. 'action' => 'index',
  107. 'plugin' => null,
  108. 'controller' => 'Subscribe',
  109. ],
  110. 'url' => '/subscribe',
  111. 'base' => '/magazine',
  112. 'webroot' => '/magazine/',
  113. ]);
  114. Router::setRequest($request);
  115. $this->assertSame('/magazine/subscribe', $this->Helper->build());
  116. $this->assertSame(
  117. '/magazine/articles/add',
  118. $this->Helper->build(['controller' => 'Articles', 'action' => 'add'])
  119. );
  120. }
  121. public function testBuildUrlConversionUnescaped(): void
  122. {
  123. $result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
  124. $this->assertSame('/controller/action/1?one=1&two=2', $result);
  125. $result = $this->Helper->build([
  126. 'controller' => 'Posts',
  127. 'action' => 'view',
  128. '?' => [
  129. 'k' => 'v',
  130. '1' => '2',
  131. 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24',
  132. ],
  133. ], ['escape' => false]);
  134. $this->assertSame('/posts/view?k=v&1=2&param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524', $result);
  135. }
  136. public function testBuildFromPath(): void
  137. {
  138. $result = $this->Helper->buildFromPath('Articles::index');
  139. $expected = '/articles';
  140. $this->assertSame($result, $expected);
  141. $result = $this->Helper->buildFromPath('Articles::view', [3]);
  142. $expected = '/articles/view/3';
  143. $this->assertSame($result, $expected);
  144. }
  145. /**
  146. * test assetTimestamp application
  147. */
  148. public function testAssetTimestamp(): void
  149. {
  150. Configure::write('Foo.bar', 'test');
  151. Configure::write('Asset.timestamp', false);
  152. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  153. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  154. Configure::write('Asset.timestamp', true);
  155. Configure::write('debug', false);
  156. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  157. $this->assertSame('/%3Cb%3E/cake.generic.css', $result);
  158. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  159. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  160. Configure::write('Asset.timestamp', true);
  161. Configure::write('debug', true);
  162. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  163. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  164. Configure::write('Asset.timestamp', 'force');
  165. Configure::write('debug', false);
  166. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  167. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  168. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  169. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  170. $request = $this->View->getRequest()->withAttribute('webroot', '/some/dir/');
  171. $this->View->setRequest($request);
  172. Router::setRequest($request);
  173. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  174. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  175. }
  176. /**
  177. * test assetUrl application
  178. */
  179. public function testAssetUrl(): void
  180. {
  181. $result = $this->Helper->assetUrl('js/post.js', ['fullBase' => true]);
  182. $this->assertSame(Router::fullBaseUrl() . '/js/post.js', $result);
  183. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  184. $this->assertSame('img/foo.jpg', $result);
  185. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  186. $this->assertSame(Router::fullBaseUrl() . '/foo.jpg', $result);
  187. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  188. $this->assertSame('style.css', $result);
  189. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  190. $this->assertSame('dir/sub%20dir/my%20image.jpg', $result);
  191. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  192. $this->assertSame('foo.jpg?one=two&amp;three=four', $result);
  193. $result = $this->Helper->assetUrl('x:"><script>alert(1)</script>');
  194. $this->assertSame('x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', $result);
  195. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  196. $this->assertSame('dir/big%2Btall/image.jpg', $result);
  197. }
  198. /**
  199. * Test assetUrl and data uris
  200. */
  201. public function testAssetUrlDataUri(): void
  202. {
  203. $request = $this->View->getRequest()
  204. ->withAttribute('base', 'subdir')
  205. ->withAttribute('webroot', 'subdir/');
  206. $this->View->setRequest($request);
  207. Router::setRequest($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->assertSame(h($data), $result);
  215. }
  216. /**
  217. * Test assetUrl with no rewriting.
  218. */
  219. public function testAssetUrlNoRewrite(): void
  220. {
  221. $request = Router::getRequest()
  222. ->withAttribute('base', '/cake_dev/index.php')
  223. ->withAttribute('webroot', '/cake_dev/app/webroot/')
  224. ->withRequestTarget('/cake_dev/index.php/tasks');
  225. Router::setRequest($request);
  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->assertSame($expected, $result);
  229. }
  230. /**
  231. * Test assetUrl with plugins.
  232. */
  233. public function testAssetUrlPlugin(): void
  234. {
  235. $this->loadPlugins(['TestPlugin']);
  236. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  237. $this->assertSame('test_plugin/style.css', $result);
  238. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  239. $this->assertSame('TestPlugin.style.css', $result);
  240. $this->removePlugins(['TestPlugin']);
  241. }
  242. /**
  243. * Tests assetUrl() with full base URL.
  244. */
  245. public function testAssetUrlFullBase(): void
  246. {
  247. $result = $this->Helper->assetUrl('img/foo.jpg', ['fullBase' => true]);
  248. $this->assertSame(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  249. $result = $this->Helper->assetUrl('img/foo.jpg', ['fullBase' => 'https://xyz/']);
  250. $this->assertSame('https://xyz/img/foo.jpg', $result);
  251. }
  252. /**
  253. * test assetUrl and Asset.timestamp = force
  254. */
  255. public function testAssetUrlTimestampForce(): void
  256. {
  257. Configure::write('Asset.timestamp', 'force');
  258. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  259. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  260. }
  261. /**
  262. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  263. */
  264. public function testAssetTimestampConfigureOverride(): void
  265. {
  266. Configure::write('Asset.timestamp', 'force');
  267. $timestamp = false;
  268. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  269. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  270. }
  271. /**
  272. * test assetTimestamp with plugins and themes
  273. */
  274. public function testAssetTimestampPluginsAndThemes(): void
  275. {
  276. Configure::write('Asset.timestamp', 'force');
  277. $this->loadPlugins(['TestTheme', 'TestPlugin']);
  278. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  279. $this->assertMatchesRegularExpression('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  280. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  281. $this->assertMatchesRegularExpression('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  282. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  283. $this->assertMatchesRegularExpression('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  284. $result = $this->Helper->assetTimestamp('/test_theme/js/nonexistent.js');
  285. $this->assertMatchesRegularExpression('#/test_theme/js/nonexistent.js$#', $result, 'No error on missing file');
  286. }
  287. /**
  288. * test script()
  289. */
  290. public function testScript(): void
  291. {
  292. $result = $this->Helper->script(
  293. 'post.js',
  294. ['fullBase' => true]
  295. );
  296. $this->assertSame(Router::fullBaseUrl() . '/js/post.js', $result);
  297. }
  298. /**
  299. * Test script and Asset.timestamp = force
  300. */
  301. public function testScriptTimestampForce(): void
  302. {
  303. Configure::write('Asset.timestamp', 'force');
  304. $result = $this->Helper->script('script.js');
  305. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  306. }
  307. /**
  308. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  309. */
  310. public function testScriptTimestampConfigureOverride(): void
  311. {
  312. Configure::write('Asset.timestamp', 'force');
  313. $timestamp = false;
  314. $result = $this->Helper->script('script.js', ['timestamp' => $timestamp]);
  315. $this->assertSame(Configure::read('App.jsBaseUrl') . 'script.js', $result);
  316. }
  317. /**
  318. * test image()
  319. */
  320. public function testImage(): void
  321. {
  322. $result = $this->Helper->image('foo.jpg');
  323. $this->assertSame('img/foo.jpg', $result);
  324. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  325. $this->assertSame(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  326. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  327. $this->assertSame('img/dir/sub%20dir/my%20image.jpg', $result);
  328. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  329. $this->assertSame('img/foo.jpg?one=two&amp;three=four', $result);
  330. $result = $this->Helper->image('dir/big+tall/image.jpg');
  331. $this->assertSame('img/dir/big%2Btall/image.jpg', $result);
  332. $result = $this->Helper->image('cid:foo.jpg');
  333. $this->assertSame('cid:foo.jpg', $result);
  334. $result = $this->Helper->image('CID:foo.jpg');
  335. $this->assertSame('CID:foo.jpg', $result);
  336. }
  337. /**
  338. * Test image with `Asset.timestamp` = force
  339. */
  340. public function testImageTimestampForce(): void
  341. {
  342. Configure::write('Asset.timestamp', 'force');
  343. $result = $this->Helper->image('cake.icon.png');
  344. $this->assertMatchesRegularExpression('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  345. }
  346. /**
  347. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  348. */
  349. public function testImageTimestampConfigureOverride(): void
  350. {
  351. Configure::write('Asset.timestamp', 'force');
  352. $timestamp = false;
  353. $result = $this->Helper->image('cake.icon.png', ['timestamp' => $timestamp]);
  354. $this->assertSame('img/cake.icon.png', $result);
  355. }
  356. /**
  357. * test css
  358. */
  359. public function testCss(): void
  360. {
  361. $result = $this->Helper->css('style');
  362. $this->assertSame('css/style.css', $result);
  363. }
  364. /**
  365. * Test css with `Asset.timestamp` = force
  366. */
  367. public function testCssTimestampForce(): void
  368. {
  369. Configure::write('Asset.timestamp', 'force');
  370. $result = $this->Helper->css('cake.generic');
  371. $this->assertMatchesRegularExpression('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  372. }
  373. /**
  374. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  375. */
  376. public function testCssTimestampConfigureOverride(): void
  377. {
  378. Configure::write('Asset.timestamp', 'force');
  379. $timestamp = false;
  380. $result = $this->Helper->css('cake.generic', ['timestamp' => $timestamp]);
  381. $this->assertSame('css/cake.generic.css', $result);
  382. }
  383. /**
  384. * Test generating paths with webroot().
  385. */
  386. public function testWebrootPaths(): void
  387. {
  388. $request = $this->View->getRequest()->withAttribute('webroot', '/');
  389. $this->View->setRequest(
  390. $request
  391. );
  392. Router::setRequest($request);
  393. $result = $this->Helper->webroot('/img/cake.power.gif');
  394. $expected = '/img/cake.power.gif';
  395. $this->assertSame($expected, $result);
  396. $this->Helper->getView()->setTheme('TestTheme');
  397. $result = $this->Helper->webroot('/img/cake.power.gif');
  398. $expected = '/test_theme/img/cake.power.gif';
  399. $this->assertSame($expected, $result);
  400. $result = $this->Helper->webroot('/img/test.jpg');
  401. $expected = '/test_theme/img/test.jpg';
  402. $this->assertSame($expected, $result);
  403. $webRoot = Configure::read('App.wwwRoot');
  404. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  405. $result = $this->Helper->webroot('/img/cake.power.gif');
  406. $expected = '/test_theme/img/cake.power.gif';
  407. $this->assertSame($expected, $result);
  408. $result = $this->Helper->webroot('/img/test.jpg');
  409. $expected = '/test_theme/img/test.jpg';
  410. $this->assertSame($expected, $result);
  411. $result = $this->Helper->webroot('/img/cake.icon.gif');
  412. $expected = '/img/cake.icon.gif';
  413. $this->assertSame($expected, $result);
  414. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  415. $expected = '/img/cake.icon.gif?some=param';
  416. $this->assertSame($expected, $result);
  417. Configure::write('App.wwwRoot', $webRoot);
  418. }
  419. /**
  420. * Test plugin based assets will NOT use the plugin name
  421. */
  422. public function testPluginAssetsPrependImageBaseUrl(): void
  423. {
  424. $cdnPrefix = 'https://cdn.example.com/';
  425. Configure::write('App.imageBaseUrl', $cdnPrefix);
  426. $result = $this->Helper->image('TestTheme.text.jpg');
  427. $expected = $cdnPrefix . 'text.jpg';
  428. $this->assertSame($expected, $result);
  429. Configure::write('App.jsBaseUrl', $cdnPrefix);
  430. $result = $this->Helper->script('TestTheme.app.js');
  431. $expected = $cdnPrefix . 'app.js';
  432. $this->assertSame($expected, $result);
  433. Configure::write('App.cssBaseUrl', $cdnPrefix);
  434. $result = $this->Helper->css('TestTheme.app.css');
  435. $expected = $cdnPrefix . 'app.css';
  436. $this->assertSame($expected, $result);
  437. }
  438. /**
  439. * Test if an app Asset class is being loaded
  440. */
  441. public function testAppAssetPresent(): void
  442. {
  443. $Url = new UrlHelper($this->View, ['assetUrlClassName' => Asset::class]);
  444. $result = $Url->assetUrl('cake.generic.css', ['pathPrefix' => '/']);
  445. $this->assertSame('/cake.generic.css?appHash', $result);
  446. $result = $Url->css('cake.generic', ['pathPrefix' => '/']);
  447. $this->assertSame('/cake.generic.css?appHash', $result);
  448. $result = $Url->script('cake.generic', ['pathPrefix' => '/']);
  449. $this->assertSame('/cake.generic.js?appHash', $result);
  450. $result = $Url->image('cake.generic.png', ['pathPrefix' => '/']);
  451. $this->assertSame('/cake.generic.png?appHash', $result);
  452. }
  453. /**
  454. * Test if UrlHelper fails to load with wrong asset URL class name
  455. */
  456. public function testAppAssetPresentWrong(): void
  457. {
  458. $this->expectException(CakeException::class);
  459. new UrlHelper($this->View, ['assetUrlClassName' => 'InexistentClass']);
  460. }
  461. }