AssetTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Routing;
  17. use Cake\Core\Configure;
  18. use Cake\Http\ServerRequest;
  19. use Cake\Routing\Asset;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * AssetTest class
  24. */
  25. class AssetTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Routing\RouteBuilder
  29. */
  30. protected $builder;
  31. /**
  32. * setUp method
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. Router::reload();
  38. $request = new ServerRequest([
  39. 'webroot' => '/',
  40. ]);
  41. Router::setRequest($request);
  42. static::setAppNamespace();
  43. $this->loadPlugins(['TestTheme']);
  44. $this->builder = Router::createRouteBuilder('/');
  45. $this->builder->fallbacks();
  46. }
  47. /**
  48. * tearDown method
  49. */
  50. public function tearDown(): void
  51. {
  52. parent::tearDown();
  53. $this->clearPlugins();
  54. }
  55. /**
  56. * test assetTimestamp application
  57. */
  58. public function testAssetTimestamp(): void
  59. {
  60. Configure::write('Foo.bar', 'test');
  61. Configure::write('Asset.timestamp', false);
  62. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  63. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  64. Configure::write('Asset.timestamp', true);
  65. Configure::write('debug', false);
  66. $result = Asset::assetTimestamp('/%3Cb%3E/cake.generic.css');
  67. $this->assertSame('/%3Cb%3E/cake.generic.css', $result);
  68. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  69. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  70. Configure::write('Asset.timestamp', true);
  71. Configure::write('debug', true);
  72. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  73. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  74. Configure::write('Asset.timestamp', 'force');
  75. Configure::write('debug', false);
  76. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  77. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  78. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  79. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  80. $request = Router::getRequest()->withAttribute('webroot', '/some/dir/');
  81. Router::setRequest($request);
  82. $result = Asset::assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  83. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  84. }
  85. /**
  86. * test assetUrl application
  87. */
  88. public function testAssetUrl(): void
  89. {
  90. $this->builder->connect('/{controller}/{action}/*');
  91. $result = Asset::url('js/post.js', ['fullBase' => true]);
  92. $this->assertSame(Router::fullBaseUrl() . '/js/post.js', $result);
  93. $result = Asset::url('foo.jpg', ['pathPrefix' => 'img/']);
  94. $this->assertSame('/img/foo.jpg', $result);
  95. $result = Asset::url('foo.jpg', ['fullBase' => true]);
  96. $this->assertSame(Router::fullBaseUrl() . '/foo.jpg', $result);
  97. $result = Asset::url('style', ['ext' => '.css']);
  98. $this->assertSame('/style.css', $result);
  99. $result = Asset::url('dir/sub dir/my image', ['ext' => '.jpg']);
  100. $this->assertSame('/dir/sub%20dir/my%20image.jpg', $result);
  101. $result = Asset::url('foo.jpg?one=two&three=four');
  102. $this->assertSame('/foo.jpg?one=two&three=four', $result);
  103. // No HTML entities encoding is done
  104. $result = Asset::url('x:"><script>alert(1)</script>');
  105. $this->assertSame('x:"><script>alert(1)</script>', $result);
  106. // URL encoding is done
  107. $result = Asset::url('dir/big+tall/image', ['ext' => '.jpg']);
  108. $this->assertSame('/dir/big%2Btall/image.jpg', $result);
  109. $result = Asset::url('/posts/index/adbirawwy/page:6/sort:type/');
  110. $this->assertSame('/posts/index/adbirawwy/page%3A6/sort%3Atype/', $result);
  111. }
  112. /**
  113. * Test assetUrl and data uris
  114. */
  115. public function testAssetUrlDataUri(): void
  116. {
  117. $request = Router::getRequest()
  118. ->withAttribute('base', 'subdir')
  119. ->withAttribute('webroot', 'subdir/');
  120. Router::setRequest($request);
  121. $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
  122. '/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
  123. $result = Asset::url($data);
  124. $this->assertSame($data, $result);
  125. $data = 'data:image/png;base64,<evil>';
  126. $result = Asset::url($data);
  127. $this->assertSame($data, $result);
  128. }
  129. /**
  130. * Test assetUrl with no rewriting.
  131. */
  132. public function testAssetUrlNoRewrite(): void
  133. {
  134. $request = Router::getRequest()
  135. ->withAttribute('base', '/cake_dev/index.php')
  136. ->withAttribute('webroot', '/cake_dev/app/webroot/')
  137. ->withRequestTarget('/cake_dev/index.php/tasks');
  138. Router::setRequest($request);
  139. $result = Asset::url('img/cake.icon.png', ['fullBase' => true]);
  140. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  141. $this->assertSame($expected, $result);
  142. }
  143. /**
  144. * Test assetUrl with plugins.
  145. */
  146. public function testAssetUrlPlugin(): void
  147. {
  148. $this->loadPlugins(['TestPlugin']);
  149. $result = Asset::url('TestPlugin.style', ['ext' => '.css']);
  150. $this->assertSame('/test_plugin/style.css', $result);
  151. $result = Asset::url('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  152. $this->assertSame('/TestPlugin.style.css', $result);
  153. $this->removePlugins(['TestPlugin']);
  154. }
  155. /**
  156. * Tests assetUrl() with full base URL.
  157. */
  158. public function testAssetUrlFullBase(): void
  159. {
  160. $result = Asset::url('img/foo.jpg', ['fullBase' => true]);
  161. $this->assertSame(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  162. $result = Asset::url('img/foo.jpg', ['fullBase' => 'https://xyz/']);
  163. $this->assertSame('https://xyz/img/foo.jpg', $result);
  164. }
  165. /**
  166. * test assetUrl and Asset.timestamp = force
  167. */
  168. public function testAssetUrlTimestampForce(): void
  169. {
  170. Configure::write('Asset.timestamp', 'force');
  171. $result = Asset::url('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  172. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  173. }
  174. /**
  175. * Test assetTimestamp with timestamp option overriding `Asset.timestamp` in Configure.
  176. */
  177. public function testAssetTimestampConfigureOverride(): void
  178. {
  179. Configure::write('Asset.timestamp', 'force');
  180. $timestamp = false;
  181. $result = Asset::assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $timestamp);
  182. $this->assertSame(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  183. }
  184. /**
  185. * test assetTimestamp with plugins and themes
  186. */
  187. public function testAssetTimestampPluginsAndThemes(): void
  188. {
  189. Configure::write('Asset.timestamp', 'force');
  190. $this->loadPlugins(['TestTheme', 'TestPlugin', 'Company/TestPluginThree']);
  191. $result = Asset::assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  192. $this->assertMatchesRegularExpression('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  193. $result = Asset::assetTimestamp('/company/test_plugin_three/css/company.css');
  194. $this->assertMatchesRegularExpression('#/company/test_plugin_three/css/company.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  195. $result = Asset::assetTimestamp('/test_plugin/css/i_dont_exist.css');
  196. $this->assertMatchesRegularExpression('#/test_plugin/css/i_dont_exist.css$#', $result, 'No error on missing file');
  197. $result = Asset::assetTimestamp('/test_theme/js/theme.js');
  198. $this->assertMatchesRegularExpression('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  199. $result = Asset::assetTimestamp('/test_theme/js/nonexistent.js');
  200. $this->assertMatchesRegularExpression('#/test_theme/js/nonexistent.js$#', $result, 'No error on missing file');
  201. }
  202. /**
  203. * test script()
  204. */
  205. public function testScript(): void
  206. {
  207. $this->builder->connect('/{controller}/{action}/*');
  208. $result = Asset::scriptUrl('post.js', ['fullBase' => true]);
  209. $this->assertSame(Router::fullBaseUrl() . '/js/post.js', $result);
  210. }
  211. /**
  212. * Test script and Asset.timestamp = force
  213. */
  214. public function testScriptTimestampForce(): void
  215. {
  216. Configure::write('Asset.timestamp', 'force');
  217. $result = Asset::scriptUrl('script.js');
  218. $this->assertMatchesRegularExpression('/' . preg_quote(Configure::read('App.jsBaseUrl') . 'script.js?', '/') . '[0-9]+/', $result);
  219. }
  220. /**
  221. * Test script with timestamp option overriding `Asset.timestamp` in Configure
  222. */
  223. public function testScriptTimestampConfigureOverride(): void
  224. {
  225. Configure::write('Asset.timestamp', 'force');
  226. $timestamp = false;
  227. $result = Asset::scriptUrl('script.js', ['timestamp' => $timestamp]);
  228. $this->assertSame('/' . Configure::read('App.jsBaseUrl') . 'script.js', $result);
  229. }
  230. /**
  231. * test image()
  232. */
  233. public function testImage(): void
  234. {
  235. $result = Asset::imageUrl('foo.jpg');
  236. $this->assertSame('/img/foo.jpg', $result);
  237. $result = Asset::imageUrl('foo.jpg', ['fullBase' => true]);
  238. $this->assertSame(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  239. $result = Asset::imageUrl('dir/sub dir/my image.jpg');
  240. $this->assertSame('/img/dir/sub%20dir/my%20image.jpg', $result);
  241. $result = Asset::imageUrl('foo.jpg?one=two&three=four');
  242. $this->assertSame('/img/foo.jpg?one=two&three=four', $result);
  243. $result = Asset::imageUrl('dir/big+tall/image.jpg');
  244. $this->assertSame('/img/dir/big%2Btall/image.jpg', $result);
  245. $result = Asset::imageUrl('cid:foo.jpg');
  246. $this->assertSame('cid:foo.jpg', $result);
  247. $result = Asset::imageUrl('CID:foo.jpg');
  248. $this->assertSame('CID:foo.jpg', $result);
  249. }
  250. /**
  251. * Test image with `Asset.timestamp` = force
  252. */
  253. public function testImageTimestampForce(): void
  254. {
  255. Configure::write('Asset.timestamp', 'force');
  256. $result = Asset::imageUrl('cake.icon.png');
  257. $this->assertMatchesRegularExpression('/' . preg_quote('img/cake.icon.png?', '/') . '[0-9]+/', $result);
  258. }
  259. /**
  260. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  261. */
  262. public function testImageTimestampConfigureOverride(): void
  263. {
  264. Configure::write('Asset.timestamp', 'force');
  265. $timestamp = false;
  266. $result = Asset::imageUrl('cake.icon.png', ['timestamp' => $timestamp]);
  267. $this->assertSame('/img/cake.icon.png', $result);
  268. }
  269. /**
  270. * test css
  271. */
  272. public function testCss(): void
  273. {
  274. $result = Asset::cssUrl('style');
  275. $this->assertSame('/css/style.css', $result);
  276. }
  277. /**
  278. * Test css with `Asset.timestamp` = force
  279. */
  280. public function testCssTimestampForce(): void
  281. {
  282. Configure::write('Asset.timestamp', 'force');
  283. $result = Asset::cssUrl('cake.generic');
  284. $this->assertMatchesRegularExpression('/' . preg_quote('css/cake.generic.css?', '/') . '[0-9]+/', $result);
  285. }
  286. /**
  287. * Test image with timestamp option overriding `Asset.timestamp` in Configure
  288. */
  289. public function testCssTimestampConfigureOverride(): void
  290. {
  291. Configure::write('Asset.timestamp', 'force');
  292. $timestamp = false;
  293. $result = Asset::cssUrl('cake.generic', ['timestamp' => $timestamp]);
  294. $this->assertSame('/css/cake.generic.css', $result);
  295. }
  296. /**
  297. * Test generating paths with webroot().
  298. */
  299. public function testWebrootPaths(): void
  300. {
  301. $result = Asset::webroot('/img/cake.power.gif');
  302. $expected = '/img/cake.power.gif';
  303. $this->assertSame($expected, $result);
  304. $result = Asset::webroot('/img/cake.power.gif', ['theme' => 'TestTheme']);
  305. $expected = '/test_theme/img/cake.power.gif';
  306. $this->assertSame($expected, $result);
  307. Asset::setInflectionType('dasherize');
  308. $result = Asset::webroot('/img/test.jpg', ['theme' => 'TestTheme']);
  309. $expected = '/test-theme/img/test.jpg';
  310. $this->assertSame($expected, $result);
  311. Asset::setInflectionType('underscore');
  312. $webRoot = Configure::read('App.wwwRoot');
  313. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  314. $result = Asset::webroot('/img/cake.power.gif', ['theme' => 'TestTheme']);
  315. $expected = '/test_theme/img/cake.power.gif';
  316. $this->assertSame($expected, $result);
  317. $result = Asset::webroot('/img/test.jpg', ['theme' => 'TestTheme']);
  318. $expected = '/test_theme/img/test.jpg';
  319. $this->assertSame($expected, $result);
  320. $result = Asset::webroot('/img/cake.icon.gif', ['theme' => 'TestTheme']);
  321. $expected = '/img/cake.icon.gif';
  322. $this->assertSame($expected, $result);
  323. $result = Asset::webroot('/img/cake.icon.gif?some=param', ['theme' => 'TestTheme']);
  324. $expected = '/img/cake.icon.gif?some=param';
  325. $this->assertSame($expected, $result);
  326. Configure::write('App.wwwRoot', $webRoot);
  327. }
  328. /**
  329. * Test plugin based assets will NOT use the plugin name
  330. */
  331. public function testPluginAssetsPrependImageBaseUrl(): void
  332. {
  333. $cdnPrefix = 'https://cdn.example.com/';
  334. Configure::write('App.imageBaseUrl', $cdnPrefix . '{plugin}img/');
  335. $result = Asset::imageUrl('TestTheme.text.jpg');
  336. $expected = $cdnPrefix . 'test_theme/img/text.jpg';
  337. $this->assertSame($expected, $result);
  338. Configure::write('App.jsBaseUrl', $cdnPrefix . '{plugin}js/');
  339. $result = Asset::scriptUrl('TestTheme.app.js');
  340. $expected = $cdnPrefix . 'test_theme/js/app.js';
  341. $this->assertSame($expected, $result);
  342. Configure::write('App.cssBaseUrl', $cdnPrefix);
  343. $result = Asset::cssUrl('TestTheme.app.css');
  344. $expected = $cdnPrefix . 'app.css';
  345. $this->assertSame($expected, $result);
  346. }
  347. }