HtmlHelperTest.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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 1.2.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\Plugin;
  19. use Cake\Http\ServerRequest;
  20. use Cake\I18n\Date;
  21. use Cake\Routing\Route\DashedRoute;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Filesystem;
  25. use Cake\View\Helper\HtmlHelper;
  26. use Cake\View\View;
  27. use function Cake\Core\h;
  28. /**
  29. * HtmlHelperTest class
  30. */
  31. class HtmlHelperTest extends TestCase
  32. {
  33. /**
  34. * Regexp for CDATA start block
  35. *
  36. * @var string
  37. */
  38. protected $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  39. /**
  40. * Regexp for CDATA end block
  41. *
  42. * @var string
  43. */
  44. protected $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  45. /**
  46. * Helper to be tested
  47. *
  48. * @var \Cake\View\Helper\HtmlHelper
  49. */
  50. protected $Html;
  51. /**
  52. * Mocked view
  53. *
  54. * @var \Cake\View\View|\PHPUnit\Framework\MockObject\MockObject
  55. */
  56. protected $View;
  57. /**
  58. * setUp method
  59. */
  60. public function setUp(): void
  61. {
  62. parent::setUp();
  63. $request = new ServerRequest([
  64. 'webroot' => '',
  65. ]);
  66. Router::reload();
  67. Router::setRequest($request);
  68. $this->View = $this->getMockBuilder(View::class)
  69. ->onlyMethods(['append'])
  70. ->setConstructorArgs([$request])
  71. ->getMock();
  72. $this->Html = new HtmlHelper($this->View);
  73. $this->loadPlugins(['TestTheme']);
  74. static::setAppNamespace();
  75. Configure::write('Asset.timestamp', false);
  76. $builder = Router::createRouteBuilder('/');
  77. $builder->fallbacks(DashedRoute::class);
  78. }
  79. /**
  80. * tearDown method
  81. */
  82. public function tearDown(): void
  83. {
  84. parent::tearDown();
  85. $this->clearPlugins();
  86. unset($this->Html, $this->View);
  87. }
  88. /**
  89. * testLink method
  90. */
  91. public function testLink(): void
  92. {
  93. Router::reload();
  94. $builder = Router::createRouteBuilder('/');
  95. $builder->connect('/{controller}', ['action' => 'index']);
  96. $builder->connect('/{controller}/{action}/*');
  97. Router::setRequest(new ServerRequest());
  98. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', ''));
  99. $result = $this->Html->link('/home');
  100. $expected = ['a' => ['href' => '/home'], 'preg:/\/home/', '/a'];
  101. $this->assertHtml($expected, $result);
  102. $result = $this->Html->link(['controller' => 'Users', 'action' => 'login', '<[You]>']);
  103. $expected = [
  104. 'a' => ['href' => '/Users/login/%3C%5BYou%5D%3E'],
  105. 'preg:/\/Users\/login\/&lt;\[You\]&gt;/',
  106. '/a',
  107. ];
  108. $this->assertHtml($expected, $result);
  109. $result = $this->Html->link('Posts', ['controller' => 'Posts', 'action' => 'index', '_full' => true]);
  110. $expected = ['a' => ['href' => Router::fullBaseUrl() . '/Posts'], 'Posts', '/a'];
  111. $this->assertHtml($expected, $result);
  112. $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
  113. $expected = [
  114. 'a' => [
  115. 'href' => '/home',
  116. 'data-confirm-message' => 'Are you sure you want to do this?',
  117. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  118. ],
  119. 'Home',
  120. '/a',
  121. ];
  122. $this->assertHtml($expected, $result);
  123. $this->Html->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { window.location="/";};']);
  124. $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
  125. $expected = [
  126. 'a' => [
  127. 'href' => '/home',
  128. 'data-confirm-message' => 'Are you sure you want to do this?',
  129. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ window\.location=&quot;\/&quot;;\};/',
  130. ],
  131. 'Home',
  132. '/a',
  133. ];
  134. $this->assertHtml($expected, $result);
  135. $this->Html->setTemplates(['confirmJs' => '{{confirm}}']);
  136. $result = $this->Html->link('Home', '/home', ['confirm' => 'Confirm\'s "nightmares"']);
  137. $expected = [
  138. 'a' => [
  139. 'href' => '/home',
  140. 'data-confirm-message' => 'Confirm&#039;s &quot;nightmares&quot;',
  141. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  142. ],
  143. 'Home',
  144. '/a',
  145. ];
  146. $this->assertHtml($expected, $result);
  147. $result = $this->Html->link('Home', '/home', ['onclick' => 'someFunction();']);
  148. $expected = [
  149. 'a' => ['href' => '/home', 'onclick' => 'someFunction();'],
  150. 'Home',
  151. '/a',
  152. ];
  153. $this->assertHtml($expected, $result);
  154. $result = $this->Html->link('Next >', '#');
  155. $expected = [
  156. 'a' => ['href' => '#'],
  157. 'Next &gt;',
  158. '/a',
  159. ];
  160. $this->assertHtml($expected, $result);
  161. $result = $this->Html->link('Next >', '#', ['escape' => true]);
  162. $expected = [
  163. 'a' => ['href' => '#'],
  164. 'Next &gt;',
  165. '/a',
  166. ];
  167. $this->assertHtml($expected, $result);
  168. $result = $this->Html->link('Next >', '#', ['escape' => 'utf-8']);
  169. $expected = [
  170. 'a' => ['href' => '#'],
  171. 'Next &gt;',
  172. '/a',
  173. ];
  174. $this->assertHtml($expected, $result);
  175. $result = $this->Html->link('Next >', '#', ['escape' => false]);
  176. $expected = [
  177. 'a' => ['href' => '#'],
  178. 'Next >',
  179. '/a',
  180. ];
  181. $this->assertHtml($expected, $result);
  182. $result = $this->Html->link('Next >', '#', [
  183. 'title' => 'to escape &#8230; or not escape?',
  184. 'escape' => false,
  185. ]);
  186. $expected = [
  187. 'a' => ['href' => '#', 'title' => 'to escape &#8230; or not escape?'],
  188. 'Next >',
  189. '/a',
  190. ];
  191. $this->assertHtml($expected, $result);
  192. $result = $this->Html->link('Next >', '#', [
  193. 'title' => 'to escape &#8230; or not escape?',
  194. 'escape' => true,
  195. ]);
  196. $expected = [
  197. 'a' => ['href' => '#', 'title' => 'to escape &amp;#8230; or not escape?'],
  198. 'Next &gt;',
  199. '/a',
  200. ];
  201. $this->assertHtml($expected, $result);
  202. $result = $this->Html->link('Next >', '#', [
  203. 'title' => 'Next >',
  204. 'escapeTitle' => false,
  205. ]);
  206. $expected = [
  207. 'a' => ['href' => '#', 'title' => 'Next &gt;'],
  208. 'Next >',
  209. '/a',
  210. ];
  211. $this->assertHtml($expected, $result);
  212. $result = $this->Html->link('Original size', [
  213. 'controller' => 'Images', 'action' => 'view', 3, '?' => ['height' => 100, 'width' => 200],
  214. ]);
  215. $expected = [
  216. 'a' => ['href' => '/Images/view/3?height=100&amp;width=200'],
  217. 'Original size',
  218. '/a',
  219. ];
  220. $this->assertHtml($expected, $result);
  221. Configure::write('Asset.timestamp', false);
  222. $result = $this->Html->link($this->Html->image('test.gif'), '#', ['escape' => false]);
  223. $expected = [
  224. 'a' => ['href' => '#'],
  225. 'img' => ['src' => 'img/test.gif', 'alt' => ''],
  226. '/a',
  227. ];
  228. $this->assertHtml($expected, $result);
  229. $result = $this->Html->link($this->Html->image('test.gif'), '#', [
  230. 'title' => 'hey "howdy"',
  231. 'escapeTitle' => false,
  232. ]);
  233. $expected = [
  234. 'a' => ['href' => '#', 'title' => 'hey &quot;howdy&quot;'],
  235. 'img' => ['src' => 'img/test.gif', 'alt' => ''],
  236. '/a',
  237. ];
  238. $this->assertHtml($expected, $result);
  239. $result = $this->Html->image('test.gif', ['url' => '#']);
  240. $expected = [
  241. 'a' => ['href' => '#'],
  242. 'img' => ['src' => 'img/test.gif', 'alt' => ''],
  243. '/a',
  244. ];
  245. $this->assertHtml($expected, $result);
  246. $result = $this->Html->link($this->Html->image('../favicon.ico'), '#', ['escape' => false]);
  247. $expected = [
  248. 'a' => ['href' => '#'],
  249. 'img' => ['src' => 'img/../favicon.ico', 'alt' => ''],
  250. '/a',
  251. ];
  252. $this->assertHtml($expected, $result);
  253. $result = $this->Html->image('../favicon.ico', ['url' => '#']);
  254. $expected = [
  255. 'a' => ['href' => '#'],
  256. 'img' => ['src' => 'img/../favicon.ico', 'alt' => ''],
  257. '/a',
  258. ];
  259. $this->assertHtml($expected, $result);
  260. $result = $this->Html->link('http://www.example.org?param1=value1&param2=value2');
  261. $expected = ['a' => ['href' => 'http://www.example.org?param1=value1&amp;param2=value2'], 'http://www.example.org?param1=value1&amp;param2=value2', '/a'];
  262. $this->assertHtml($expected, $result);
  263. $result = $this->Html->link('alert', 'javascript:alert(\'cakephp\');');
  264. $expected = ['a' => ['href' => 'javascript:alert(&#039;cakephp&#039;);'], 'alert', '/a'];
  265. $this->assertHtml($expected, $result);
  266. $result = $this->Html->link('write me', 'mailto:example@cakephp.org');
  267. $expected = ['a' => ['href' => 'mailto:example@cakephp.org'], 'write me', '/a'];
  268. $this->assertHtml($expected, $result);
  269. $result = $this->Html->link('call me on 0123465-798', 'tel:0123465-798');
  270. $expected = ['a' => ['href' => 'tel:0123465-798'], 'call me on 0123465-798', '/a'];
  271. $this->assertHtml($expected, $result);
  272. $result = $this->Html->link('text me on 0123465-798', 'sms:0123465-798');
  273. $expected = ['a' => ['href' => 'sms:0123465-798'], 'text me on 0123465-798', '/a'];
  274. $this->assertHtml($expected, $result);
  275. $result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello there');
  276. $expected = ['a' => ['href' => 'sms:0123465-798?body=hello there'], 'say hello to 0123465-798', '/a'];
  277. $this->assertHtml($expected, $result);
  278. $result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello "cakephp"');
  279. $expected = ['a' => ['href' => 'sms:0123465-798?body=hello &quot;cakephp&quot;'], 'say hello to 0123465-798', '/a'];
  280. $this->assertHtml($expected, $result);
  281. $result = $this->Html->link('Home', '/', ['fullBase' => true]);
  282. $expected = ['a' => ['href' => 'http://localhost/'], 'Home', '/a'];
  283. $this->assertHtml($expected, $result);
  284. $result = $this->Html->link('Home', '/', ['fullBase' => false]);
  285. $expected = ['a' => ['href' => '/'], 'Home', '/a'];
  286. $this->assertHtml($expected, $result);
  287. }
  288. public function testLinkFromPath(): void
  289. {
  290. $result = $this->Html->linkFromPath('Index', 'Articles::index');
  291. $expected = '<a href="/articles">Index</a>';
  292. $this->assertSame($result, $expected);
  293. $result = $this->Html->linkFromPath('View', 'Articles::view', [3]);
  294. $expected = '<a href="/articles/view/3">View</a>';
  295. $this->assertSame($result, $expected);
  296. }
  297. /**
  298. * testImageTag method
  299. */
  300. public function testImageTag(): void
  301. {
  302. $builder = Router::createRouteBuilder('/');
  303. $builder->connect('/:controller', ['action' => 'index']);
  304. $builder->connect('/:controller/:action/*');
  305. $result = $this->Html->image('test.gif');
  306. $expected = ['img' => ['src' => 'img/test.gif', 'alt' => '']];
  307. $this->assertHtml($expected, $result);
  308. $result = $this->Html->image('http://google.com/logo.gif');
  309. $expected = ['img' => ['src' => 'http://google.com/logo.gif', 'alt' => '']];
  310. $this->assertHtml($expected, $result);
  311. $result = $this->Html->image('//google.com/logo.gif');
  312. $expected = ['img' => ['src' => '//google.com/logo.gif', 'alt' => '']];
  313. $this->assertHtml($expected, $result);
  314. $result = $this->Html->image(['controller' => 'Test', 'action' => 'view', 1, '_ext' => 'gif']);
  315. $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
  316. $this->assertHtml($expected, $result);
  317. $result = $this->Html->image('/test/view/1.gif');
  318. $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
  319. $this->assertHtml($expected, $result);
  320. $result = $this->Html->image('cid:cakephp_logo');
  321. $expected = ['img' => ['src' => 'cid:cakephp_logo', 'alt' => '']];
  322. $this->assertHtml($expected, $result);
  323. $result = $this->Html->image('x:"><script>alert(1)</script>');
  324. $expected = ['img' => ['src' => 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', 'alt' => '']];
  325. $this->assertHtml($expected, $result);
  326. $result = $this->Html->image('//google.com/"><script>alert(1)</script>');
  327. $expected = ['img' => ['src' => '//google.com/&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', 'alt' => '']];
  328. $this->assertHtml($expected, $result);
  329. $result = $this->Html->image([
  330. 'controller' => 'Images',
  331. 'action' => 'display',
  332. 'test',
  333. '?' => ['one' => 'two', 'three' => 'four'],
  334. ]);
  335. $expected = ['img' => ['src' => '/images/display/test?one=two&amp;three=four', 'alt' => '']];
  336. $this->assertHtml($expected, $result);
  337. }
  338. /**
  339. * Ensure that data URIs don't get base paths set.
  340. */
  341. public function testImageDataUriBaseDir(): void
  342. {
  343. $request = $this->View->getRequest()
  344. ->withAttribute('base', 'subdir')
  345. ->withAttribute('webroot', 'subdir/');
  346. $this->View->setRequest($request);
  347. Router::setRequest($request);
  348. $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
  349. '/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
  350. $result = $this->Html->image($data);
  351. $expected = ['img' => ['src' => $data, 'alt' => '']];
  352. $this->assertHtml($expected, $result);
  353. $data = 'data:image/png;base64,<evil>';
  354. $result = $this->Html->image($data);
  355. $expected = ['img' => ['src' => h($data), 'alt' => '']];
  356. $this->assertHtml($expected, $result);
  357. }
  358. /**
  359. * Test image() with query strings.
  360. */
  361. public function testImageQueryString(): void
  362. {
  363. $result = $this->Html->image('test.gif?one=two&three=four');
  364. $expected = ['img' => ['src' => 'img/test.gif?one=two&amp;three=four', 'alt' => '']];
  365. $this->assertHtml($expected, $result);
  366. }
  367. /**
  368. * Test that image works with pathPrefix.
  369. */
  370. public function testImagePathPrefix(): void
  371. {
  372. $result = $this->Html->image('test.gif', ['pathPrefix' => '/my/custom/path/']);
  373. $expected = ['img' => ['src' => '/my/custom/path/test.gif', 'alt' => '']];
  374. $this->assertHtml($expected, $result);
  375. $result = $this->Html->image('test.gif', ['pathPrefix' => 'http://cakephp.org/assets/img/']);
  376. $expected = ['img' => ['src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '']];
  377. $this->assertHtml($expected, $result);
  378. $result = $this->Html->image('test.gif', ['pathPrefix' => '//cakephp.org/assets/img/']);
  379. $expected = ['img' => ['src' => '//cakephp.org/assets/img/test.gif', 'alt' => '']];
  380. $this->assertHtml($expected, $result);
  381. $previousConfig = Configure::read('App.imageBaseUrl');
  382. Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
  383. $result = $this->Html->image('test.gif');
  384. $expected = ['img' => ['src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '']];
  385. $this->assertHtml($expected, $result);
  386. Configure::write('App.imageBaseUrl', $previousConfig);
  387. }
  388. /**
  389. * Test that image() works with fullBase and a webroot not equal to /
  390. */
  391. public function testImageWithFullBase(): void
  392. {
  393. $result = $this->Html->image('test.gif', ['fullBase' => true]);
  394. $here = $this->Html->Url->build('/', ['fullBase' => true]);
  395. $expected = ['img' => ['src' => $here . 'img/test.gif', 'alt' => '']];
  396. $this->assertHtml($expected, $result);
  397. $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
  398. $here = $this->Html->Url->build('/', ['fullBase' => true]);
  399. $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
  400. $this->assertHtml($expected, $result);
  401. $request = Router::getRequest()
  402. ->withAttribute('webroot', '/myproject/')
  403. ->withAttribute('base', '/myproject');
  404. Router::setRequest($request);
  405. $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
  406. $expected = ['img' => ['src' => 'http://localhost/myproject/img/sub/test.gif', 'alt' => '']];
  407. $this->assertHtml($expected, $result);
  408. }
  409. /**
  410. * test image() with Asset.timestamp
  411. */
  412. public function testImageWithTimestampping(): void
  413. {
  414. Configure::write('Asset.timestamp', 'force');
  415. $request = Router::getRequest()->withAttribute('webroot', '/');
  416. Router::setRequest($request);
  417. $result = $this->Html->image('cake.icon.png');
  418. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  419. $this->assertHtml($expected, $result);
  420. Configure::write('debug', false);
  421. Configure::write('Asset.timestamp', 'force');
  422. $result = $this->Html->image('cake.icon.png');
  423. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  424. $this->assertHtml($expected, $result);
  425. $request = Router::getRequest()->withAttribute('webroot', '/testing/longer/');
  426. Router::setRequest($request);
  427. $result = $this->Html->image('cake.icon.png');
  428. $expected = [
  429. 'img' => ['src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => ''],
  430. ];
  431. $this->assertHtml($expected, $result);
  432. }
  433. /**
  434. * Tests creation of an image tag using a theme and asset timestamping
  435. */
  436. public function testImageTagWithTheme(): void
  437. {
  438. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  439. $testfile = WWW_ROOT . 'test_theme/img/__cake_test_image.gif';
  440. $fs = new Filesystem();
  441. $fs->dumpFile($testfile, '');
  442. Configure::write('Asset.timestamp', true);
  443. Configure::write('debug', true);
  444. $request = Router::getRequest()->withAttribute('webroot', '/');
  445. Router::setRequest($request);
  446. $this->Html->Url->getView()->setTheme('TestTheme');
  447. $result = $this->Html->image('__cake_test_image.gif');
  448. $expected = [
  449. 'img' => [
  450. 'src' => 'preg:/\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  451. 'alt' => '',
  452. ],
  453. ];
  454. $this->assertHtml($expected, $result);
  455. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  456. Router::setRequest($request);
  457. $result = $this->Html->image('__cake_test_image.gif');
  458. $expected = [
  459. 'img' => [
  460. 'src' => 'preg:/\/testing\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  461. 'alt' => '',
  462. ],
  463. ];
  464. $this->assertHtml($expected, $result);
  465. // phpcs:ignore
  466. @unlink($testfile);
  467. }
  468. /**
  469. * test theme assets in main webroot path
  470. */
  471. public function testThemeAssetsInMainWebrootPath(): void
  472. {
  473. Configure::write('App.wwwRoot', TEST_APP . 'webroot/');
  474. $this->Html->Url->getView()->setTheme('TestTheme');
  475. $result = $this->Html->css('webroot_test');
  476. $expected = [
  477. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/webroot_test\.css/'],
  478. ];
  479. $this->assertHtml($expected, $result);
  480. $this->Html->getView()->setTheme('TestTheme');
  481. $result = $this->Html->css('theme_webroot');
  482. $expected = [
  483. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/theme_webroot\.css/'],
  484. ];
  485. $this->assertHtml($expected, $result);
  486. }
  487. /**
  488. * testStyle method
  489. */
  490. public function testStyle(): void
  491. {
  492. $result = $this->Html->style(['display' => 'none', 'margin' => '10px']);
  493. $this->assertSame('display:none; margin:10px;', $result);
  494. $result = $this->Html->style(['display' => 'none', 'margin' => '10px'], false);
  495. $this->assertSame("display:none;\nmargin:10px;", $result);
  496. }
  497. /**
  498. * testCssLink method
  499. */
  500. public function testCssLink(): void
  501. {
  502. $result = $this->Html->css('screen');
  503. $expected = [
  504. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
  505. ];
  506. $this->assertHtml($expected, $result);
  507. $result = $this->Html->css('screen.css', ['once' => false]);
  508. $this->assertHtml($expected, $result);
  509. $this->loadPlugins(['TestPlugin']);
  510. $result = $this->Html->css('TestPlugin.style', ['plugin' => false]);
  511. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  512. $this->assertHtml($expected, $result);
  513. $this->removePlugins(['TestPlugin']);
  514. $result = $this->Html->css('my.css.library');
  515. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  516. $this->assertHtml($expected, $result);
  517. $result = $this->Html->css('screen.css?1234');
  518. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  519. $this->assertHtml($expected, $result);
  520. $result = $this->Html->css('screen.css?with=param&other=param');
  521. $expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
  522. $this->assertHtml($expected, $result);
  523. $result = $this->Html->css('x:"><script>alert(1)</script>');
  524. $expected['link']['href'] = 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
  525. $this->assertHtml($expected, $result);
  526. $result = $this->Html->css('http://whatever.com/screen.css?1234&a=b');
  527. $expected['link']['href'] = 'http://whatever.com/screen.css?1234&amp;a=b';
  528. $this->assertHtml($expected, $result);
  529. Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
  530. $result = $this->Html->css('cake.generic');
  531. $expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
  532. $this->assertHtml($expected, $result);
  533. $result = $this->Html->css('//example.com/css/cake.generic.css');
  534. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  535. $this->assertHtml($expected, $result);
  536. $result = explode("\n", trim($this->Html->css(['cake', 'vendor.generic'])));
  537. $expected['link']['href'] = 'preg:/.*css\/cake\.css/';
  538. $this->assertHtml($expected, $result[0]);
  539. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  540. $this->assertHtml($expected, $result[1]);
  541. $this->assertCount(2, $result);
  542. $this->View->expects($this->exactly(2))
  543. ->method('append')
  544. ->with(
  545. ...self::withConsecutive(
  546. ['css', $this->matchesRegularExpression('/css_in_head.css/')],
  547. ['css', $this->matchesRegularExpression('/more_css_in_head.css/')]
  548. )
  549. );
  550. $result = $this->Html->css('css_in_head', ['block' => true]);
  551. $this->assertNull($result);
  552. $result = $this->Html->css('more_css_in_head', ['block' => true]);
  553. $this->assertNull($result);
  554. $result = $this->Html->css('import-screen', ['rel' => 'import']);
  555. $expected = [
  556. '<style',
  557. 'preg:/@import url\(.*css\/import-screen\.css\);/',
  558. '/style',
  559. ];
  560. $this->assertHtml($expected, $result);
  561. }
  562. /**
  563. * Test that css() includes CSP nonces if available.
  564. */
  565. public function testCssWithCspNonce(): void
  566. {
  567. $nonce = 'r@nd0mV4lue';
  568. $request = $this->View->getRequest()->withAttribute('cspStyleNonce', $nonce);
  569. $this->View->setRequest($request);
  570. $result = $this->Html->css('app');
  571. $expected = [
  572. 'link' => ['rel' => 'stylesheet', 'href' => 'css/app.css', 'nonce' => $nonce],
  573. ];
  574. $this->assertHtml($expected, $result);
  575. }
  576. /**
  577. * Test css() with once option.
  578. */
  579. public function testCssLinkOnce(): void
  580. {
  581. $result = $this->Html->css('screen', ['once' => true]);
  582. $expected = [
  583. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
  584. ];
  585. $this->assertHtml($expected, $result);
  586. // Default is once=true
  587. $result = $this->Html->css('screen');
  588. $this->assertNull($result);
  589. $result = $this->Html->css('screen', ['once' => false]);
  590. $expected = [
  591. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
  592. ];
  593. $this->assertHtml($expected, $result);
  594. }
  595. /**
  596. * testCssWithFullBase method
  597. */
  598. public function testCssWithFullBase(): void
  599. {
  600. Configure::write('Asset.filter.css', false);
  601. $here = $this->Html->Url->build('/', ['fullBase' => true]);
  602. $result = $this->Html->css('screen', ['fullBase' => true]);
  603. $expected = [
  604. 'link' => ['rel' => 'stylesheet', 'href' => $here . 'css/screen.css'],
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * testPluginCssLink method
  610. */
  611. public function testPluginCssLink(): void
  612. {
  613. $this->loadPlugins(['TestPlugin']);
  614. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  615. $expected = [
  616. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/'],
  617. ];
  618. $this->assertHtml($expected, $result);
  619. $result = $this->Html->css('TestPlugin.test_plugin_asset.css', ['once' => false]);
  620. $this->assertHtml($expected, $result);
  621. $result = $this->Html->css('TestPlugin.my.css.library');
  622. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  623. $this->assertHtml($expected, $result);
  624. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  625. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  626. $this->assertHtml($expected, $result);
  627. $result = explode("\n", trim($this->Html->css(
  628. ['TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'],
  629. ['once' => false]
  630. )));
  631. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  632. $this->assertHtml($expected, $result[0]);
  633. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  634. $this->assertHtml($expected, $result[1]);
  635. $this->assertCount(2, $result);
  636. $this->removePlugins(['TestPlugin']);
  637. }
  638. /**
  639. * test use of css() and timestamping
  640. */
  641. public function testCssTimestamping(): void
  642. {
  643. Configure::write('debug', true);
  644. Configure::write('Asset.timestamp', true);
  645. $expected = [
  646. 'link' => ['rel' => 'stylesheet', 'href' => ''],
  647. ];
  648. $result = $this->Html->css('cake.generic', ['once' => false]);
  649. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  650. $this->assertHtml($expected, $result);
  651. Configure::write('debug', false);
  652. $result = $this->Html->css('cake.generic', ['once' => false]);
  653. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  654. $this->assertHtml($expected, $result);
  655. Configure::write('Asset.timestamp', 'force');
  656. $result = $this->Html->css('cake.generic', ['once' => false]);
  657. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  658. $this->assertHtml($expected, $result);
  659. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  660. Router::setRequest($request);
  661. $result = $this->Html->css('cake.generic', ['once' => false]);
  662. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  663. $this->assertHtml($expected, $result);
  664. $request = Router::getRequest()->withAttribute('webroot', '/testing/longer/');
  665. Router::setRequest($request);
  666. $result = $this->Html->css('cake.generic', ['once' => false]);
  667. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  668. $this->assertHtml($expected, $result);
  669. }
  670. /**
  671. * test use of css() and timestamping with plugin syntax
  672. */
  673. public function testPluginCssTimestamping(): void
  674. {
  675. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  676. Configure::write('debug', true);
  677. Configure::write('Asset.timestamp', true);
  678. $expected = [
  679. 'link' => ['rel' => 'stylesheet', 'href' => ''],
  680. ];
  681. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  682. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  683. $this->assertHtml($expected, $result);
  684. $result = $this->Html->css('Company/TestPluginThree.company', ['once' => false]);
  685. $expected['link']['href'] = 'preg:/.*company\/test_plugin_three\/css\/company\.css\?[0-9]+/';
  686. $this->assertHtml($expected, $result);
  687. Configure::write('debug', false);
  688. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  689. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  690. $this->assertHtml($expected, $result);
  691. Configure::write('Asset.timestamp', 'force');
  692. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  693. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  694. $this->assertHtml($expected, $result);
  695. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  696. Router::setRequest($request);
  697. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  698. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  699. $this->assertHtml($expected, $result);
  700. $request = Router::getRequest()->withAttribute('webroot', '/testing/longer/');
  701. Router::setRequest($request);
  702. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  703. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  704. $this->assertHtml($expected, $result);
  705. $this->removePlugins(['TestPlugin', 'Company/TestPluginThree']);
  706. }
  707. /**
  708. * Resource names must be treated differently for css() and script()
  709. */
  710. public function testBufferedCssAndScriptWithIdenticalResourceName(): void
  711. {
  712. $this->View->expects($this->exactly(2))
  713. ->method('append')
  714. ->with(
  715. ...self::withConsecutive(
  716. ['css', $this->stringContains('test.min.css')],
  717. ['script', $this->stringContains('test.min.js')]
  718. )
  719. );
  720. $this->Html->css('test.min', ['block' => true]);
  721. $this->Html->script('test.min', ['block' => true]);
  722. }
  723. /**
  724. * test timestamp enforcement for script tags.
  725. */
  726. public function testScriptTimestamping(): void
  727. {
  728. $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
  729. Configure::write('debug', true);
  730. Configure::write('Asset.timestamp', true);
  731. touch(WWW_ROOT . 'js/__cake_js_test.js');
  732. $timestamp = substr((string)strtotime('now'), 0, 8);
  733. $result = $this->Html->script('__cake_js_test', ['once' => false]);
  734. $this->assertMatchesRegularExpression('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  735. Configure::write('debug', false);
  736. Configure::write('Asset.timestamp', 'force');
  737. $result = $this->Html->script('__cake_js_test', ['once' => false]);
  738. $this->assertMatchesRegularExpression('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  739. unlink(WWW_ROOT . 'js/__cake_js_test.js');
  740. Configure::write('Asset.timestamp', false);
  741. }
  742. /**
  743. * test timestamp enforcement for script tags with plugin syntax.
  744. */
  745. public function testPluginScriptTimestamping(): void
  746. {
  747. $this->loadPlugins(['TestPlugin']);
  748. $pluginPath = Plugin::path('TestPlugin');
  749. $pluginJsPath = $pluginPath . 'webroot/js';
  750. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  751. Configure::write('debug', true);
  752. Configure::write('Asset.timestamp', true);
  753. touch($pluginJsPath . DS . '__cake_js_test.js');
  754. $timestamp = substr((string)strtotime('now'), 0, 8);
  755. $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
  756. $this->assertMatchesRegularExpression('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  757. Configure::write('debug', false);
  758. Configure::write('Asset.timestamp', 'force');
  759. $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
  760. $this->assertMatchesRegularExpression('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  761. unlink($pluginJsPath . DS . '__cake_js_test.js');
  762. Configure::write('Asset.timestamp', false);
  763. $this->removePlugins(['TestPlugin']);
  764. }
  765. /**
  766. * test that scripts added with uses() are only ever included once.
  767. * test script tag generation
  768. */
  769. public function testScript(): void
  770. {
  771. $result = $this->Html->script('foo');
  772. $expected = [
  773. 'script' => ['src' => 'js/foo.js'],
  774. ];
  775. $this->assertHtml($expected, $result);
  776. $result = $this->Html->script(['foobar', 'bar']);
  777. $expected = [
  778. ['script' => ['src' => 'js/foobar.js']],
  779. '/script',
  780. ['script' => ['src' => 'js/bar.js']],
  781. '/script',
  782. ];
  783. $this->assertHtml($expected, $result);
  784. $result = $this->Html->script('jquery-1.3');
  785. $expected = [
  786. 'script' => ['src' => 'js/jquery-1.3.js'],
  787. ];
  788. $this->assertHtml($expected, $result);
  789. $result = $this->Html->script('test.json');
  790. $expected = [
  791. 'script' => ['src' => 'js/test.json.js'],
  792. ];
  793. $this->assertHtml($expected, $result);
  794. $result = $this->Html->script('http://example.com/test.json');
  795. $expected = [
  796. 'script' => ['src' => 'http://example.com/test.json'],
  797. ];
  798. $this->assertHtml($expected, $result);
  799. $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
  800. $expected = [
  801. 'script' => ['src' => '/plugin/js/jquery-1.3.2.js?someparam=foo'],
  802. ];
  803. $this->assertHtml($expected, $result);
  804. $result = $this->Html->script('test.json.js?foo=bar');
  805. $expected = [
  806. 'script' => ['src' => 'js/test.json.js?foo=bar'],
  807. ];
  808. $this->assertHtml($expected, $result);
  809. $result = $this->Html->script('test.json.js?foo=bar&other=test');
  810. $expected = [
  811. 'script' => ['src' => 'js/test.json.js?foo=bar&amp;other=test'],
  812. ];
  813. $this->assertHtml($expected, $result);
  814. $result = $this->Html->script('//domain.com/test.json.js?foo=bar&other=test');
  815. $expected = [
  816. 'script' => ['src' => '//domain.com/test.json.js?foo=bar&amp;other=test'],
  817. ];
  818. $this->assertHtml($expected, $result);
  819. $result = $this->Html->script('https://domain.com/test.json.js?foo=bar&other=test');
  820. $expected = [
  821. 'script' => ['src' => 'https://domain.com/test.json.js?foo=bar&amp;other=test'],
  822. ];
  823. $this->assertHtml($expected, $result);
  824. $result = $this->Html->script('x:"><script>alert(1)</script>');
  825. $expected = [
  826. 'script' => ['src' => 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;'],
  827. ];
  828. $this->assertHtml($expected, $result);
  829. $result = $this->Html->script('foo2', ['pathPrefix' => '/my/custom/path/']);
  830. $expected = [
  831. 'script' => ['src' => '/my/custom/path/foo2.js'],
  832. ];
  833. $this->assertHtml($expected, $result);
  834. $result = $this->Html->script('foo3', ['pathPrefix' => 'http://cakephp.org/assets/js/']);
  835. $expected = [
  836. 'script' => ['src' => 'http://cakephp.org/assets/js/foo3.js'],
  837. ];
  838. $this->assertHtml($expected, $result);
  839. $previousConfig = Configure::read('App.jsBaseUrl');
  840. Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
  841. $result = $this->Html->script('foo4');
  842. $expected = [
  843. 'script' => ['src' => '//cdn.cakephp.org/js/foo4.js'],
  844. ];
  845. $this->assertHtml($expected, $result);
  846. Configure::write('App.jsBaseUrl', $previousConfig);
  847. $result = $this->Html->script('foo');
  848. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  849. $result = $this->Html->script(['foo', 'bar', 'baz']);
  850. $this->assertDoesNotMatchRegularExpression('/foo.js/', $result);
  851. $result = $this->Html->script('foo', ['once' => false]);
  852. $this->assertNotNull($result);
  853. $result = $this->Html->script('jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
  854. $expected = [
  855. 'script' => ['src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
  856. ];
  857. $this->assertHtml($expected, $result);
  858. $result = $this->Html->script('defered', ['defer']);
  859. $expected = [
  860. 'script' => ['src' => 'js/defered.js', 'defer' => 'defer'],
  861. ];
  862. $this->assertHtml($expected, $result);
  863. }
  864. /**
  865. * Test that content-security-policy nonces are applied if the request attribute
  866. * is present.
  867. */
  868. public function testScriptCspNonce(): void
  869. {
  870. $nonce = 'r@ndomV4lue';
  871. $request = $this->View->getRequest()
  872. ->withAttribute('cspScriptNonce', $nonce);
  873. $this->View->setRequest($request);
  874. $result = $this->Html->script('app.js', ['defer' => true, 'encoding' => 'utf-8']);
  875. $expected = [
  876. 'script' => ['src' => 'js/app.js', 'defer' => 'defer', 'encoding' => 'utf-8', 'nonce' => $nonce],
  877. ];
  878. $this->assertHtml($expected, $result);
  879. }
  880. /**
  881. * test that plugin scripts added with uses() are only ever included once.
  882. * test script tag generation with plugin syntax
  883. */
  884. public function testPluginScript(): void
  885. {
  886. $this->loadPlugins(['TestPlugin']);
  887. $result = $this->Html->script('TestPlugin.foo');
  888. $expected = [
  889. 'script' => ['src' => 'test_plugin/js/foo.js'],
  890. ];
  891. $this->assertHtml($expected, $result);
  892. $result = $this->Html->script(['TestPlugin.foobar', 'TestPlugin.bar']);
  893. $expected = [
  894. ['script' => ['src' => 'test_plugin/js/foobar.js']],
  895. '/script',
  896. ['script' => ['src' => 'test_plugin/js/bar.js']],
  897. '/script',
  898. ];
  899. $this->assertHtml($expected, $result);
  900. $result = $this->Html->script('TestPlugin.jquery-1.3');
  901. $expected = [
  902. 'script' => ['src' => 'test_plugin/js/jquery-1.3.js'],
  903. ];
  904. $this->assertHtml($expected, $result);
  905. $result = $this->Html->script('TestPlugin.test.json');
  906. $expected = [
  907. 'script' => ['src' => 'test_plugin/js/test.json.js'],
  908. ];
  909. $this->assertHtml($expected, $result);
  910. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  911. $expected = [
  912. 'script' => ['src' => 'test_plugin/jquery-1.3.2.js?someparam=foo'],
  913. ];
  914. $this->assertHtml($expected, $result);
  915. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  916. $expected = [
  917. 'script' => ['src' => 'test_plugin/js/test.json.js?foo=bar'],
  918. ];
  919. $this->assertHtml($expected, $result);
  920. $result = $this->Html->script('TestPlugin.foo');
  921. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  922. $result = $this->Html->script(['TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz']);
  923. $this->assertDoesNotMatchRegularExpression('/test_plugin\/js\/foo.js/', $result);
  924. $result = $this->Html->script('TestPlugin.foo', ['once' => false]);
  925. $this->assertNotNull($result);
  926. $result = $this->Html->script('TestPlugin.jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
  927. $expected = [
  928. 'script' => ['src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
  929. ];
  930. $this->assertHtml($expected, $result);
  931. $this->removePlugins(['TestPlugin']);
  932. }
  933. /**
  934. * test that script() works with blocks.
  935. */
  936. public function testScriptWithBlocks(): void
  937. {
  938. $this->View->expects($this->exactly(2))
  939. ->method('append')
  940. ->with(
  941. ...self::withConsecutive(
  942. ['script', $this->matchesRegularExpression('/script_in_head.js/')],
  943. ['headScripts', $this->matchesRegularExpression('/second_script.js/')]
  944. )
  945. );
  946. $result = $this->Html->script('script_in_head', ['block' => true]);
  947. $this->assertNull($result);
  948. $result = $this->Html->script('second_script', ['block' => 'headScripts']);
  949. $this->assertNull($result);
  950. }
  951. /**
  952. * testScriptWithFullBase method
  953. */
  954. public function testScriptWithFullBase(): void
  955. {
  956. $here = $this->Html->Url->build('/', ['fullBase' => true]);
  957. $result = $this->Html->script('foo', ['fullBase' => true]);
  958. $expected = [
  959. 'script' => ['src' => $here . 'js/foo.js'],
  960. ];
  961. $this->assertHtml($expected, $result);
  962. $result = $this->Html->script(['foobar', 'bar'], ['fullBase' => true]);
  963. $expected = [
  964. ['script' => ['src' => $here . 'js/foobar.js']],
  965. '/script',
  966. ['script' => ['src' => $here . 'js/bar.js']],
  967. '/script',
  968. ];
  969. $this->assertHtml($expected, $result);
  970. }
  971. /**
  972. * test a script file in the webroot/theme dir.
  973. */
  974. public function testScriptInTheme(): void
  975. {
  976. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  977. $testfile = WWW_ROOT . 'test_theme/js/__test_js.js';
  978. $fs = new Filesystem();
  979. $fs->dumpFile($testfile, '');
  980. $request = Router::getRequest()->withAttribute('webroot', '/');
  981. Router::setRequest($request);
  982. $this->Html->Url->getView()->setTheme('TestTheme');
  983. $result = $this->Html->script('__test_js.js');
  984. $expected = [
  985. 'script' => ['src' => '/test_theme/js/__test_js.js'],
  986. ];
  987. $this->assertHtml($expected, $result);
  988. // phpcs:ignore
  989. @unlink($testfile);
  990. }
  991. /**
  992. * test Script block generation
  993. */
  994. public function testScriptBlock(): void
  995. {
  996. $result = $this->Html->scriptBlock('window.foo = 2;');
  997. $expected = [
  998. '<script',
  999. 'window.foo = 2;',
  1000. '/script',
  1001. ];
  1002. $this->assertHtml($expected, $result);
  1003. $result = $this->Html->scriptBlock('window.foo = 2;', ['type' => 'text/x-handlebars-template']);
  1004. $expected = [
  1005. 'script' => ['type' => 'text/x-handlebars-template'],
  1006. 'window.foo = 2;',
  1007. '/script',
  1008. ];
  1009. $this->assertHtml($expected, $result);
  1010. $result = $this->Html->scriptBlock('window.foo = 2;');
  1011. $expected = [
  1012. '<script',
  1013. 'window.foo = 2;',
  1014. '/script',
  1015. ];
  1016. $this->assertHtml($expected, $result);
  1017. $this->View->expects($this->exactly(2))
  1018. ->method('append')
  1019. ->with(
  1020. ...self::withConsecutive(
  1021. ['script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/')],
  1022. ['scriptTop', $this->stringContains('alert(')]
  1023. )
  1024. );
  1025. $result = $this->Html->scriptBlock('window.foo = 2;', ['block' => true]);
  1026. $this->assertNull($result);
  1027. $result = $this->Html->scriptBlock('alert("hi")', ['block' => 'scriptTop']);
  1028. $this->assertNull($result);
  1029. $result = $this->Html->scriptBlock('window.foo = 2;', ['encoding' => 'utf-8']);
  1030. $expected = [
  1031. 'script' => ['encoding' => 'utf-8'],
  1032. 'window.foo = 2;',
  1033. '/script',
  1034. ];
  1035. $this->assertHtml($expected, $result);
  1036. }
  1037. /**
  1038. * Ensure that scriptBlock() uses CSP nonces.
  1039. */
  1040. public function testScriptBlockCspNonce(): void
  1041. {
  1042. $nonce = 'r@ndomV4lue';
  1043. $request = $this->View->getRequest()
  1044. ->withAttribute('cspScriptNonce', $nonce);
  1045. $this->View->setRequest($request);
  1046. $result = $this->Html->scriptBlock('window.foo = 2;');
  1047. $expected = [
  1048. 'script' => ['nonce' => $nonce],
  1049. 'window.foo = 2;',
  1050. '/script',
  1051. ];
  1052. $this->assertHtml($expected, $result);
  1053. }
  1054. /**
  1055. * test script tag output buffering when using scriptStart() and scriptEnd();
  1056. */
  1057. public function testScriptStartAndScriptEnd(): void
  1058. {
  1059. $this->Html->scriptStart();
  1060. echo 'this is some javascript';
  1061. $result = $this->Html->scriptEnd();
  1062. $expected = [
  1063. '<script',
  1064. 'this is some javascript',
  1065. '/script',
  1066. ];
  1067. $this->assertHtml($expected, $result);
  1068. $this->Html->scriptStart();
  1069. echo 'this is some javascript';
  1070. $result = $this->Html->scriptEnd();
  1071. $expected = [
  1072. '<script',
  1073. 'this is some javascript',
  1074. '/script',
  1075. ];
  1076. $this->assertHtml($expected, $result);
  1077. }
  1078. /**
  1079. * testCharsetTag method
  1080. */
  1081. public function testCharsetTag(): void
  1082. {
  1083. Configure::write('App.encoding', null);
  1084. $result = $this->Html->charset();
  1085. $expected = ['meta' => ['charset' => 'utf-8']];
  1086. $this->assertHtml($expected, $result);
  1087. Configure::write('App.encoding', 'ISO-8859-1');
  1088. $result = $this->Html->charset();
  1089. $expected = ['meta' => ['charset' => 'iso-8859-1']];
  1090. $this->assertHtml($expected, $result);
  1091. $result = $this->Html->charset('UTF-7');
  1092. $expected = ['meta' => ['charset' => 'UTF-7']];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * testNestedList method
  1097. */
  1098. public function testNestedList(): void
  1099. {
  1100. $list = [
  1101. 'Item 1',
  1102. 'Item 2' => [
  1103. 'Item 2.1',
  1104. ],
  1105. 'Item 3',
  1106. 'Item 4' => [
  1107. 'Item 4.1',
  1108. 'Item 4.2',
  1109. 'Item 4.3' => [
  1110. 'Item 4.3.1',
  1111. 'Item 4.3.2',
  1112. ],
  1113. ],
  1114. 'Item 5' => [
  1115. 'Item 5.1',
  1116. 'Item 5.2',
  1117. ],
  1118. ];
  1119. $result = $this->Html->nestedList($list);
  1120. $expected = [
  1121. '<ul',
  1122. '<li', 'Item 1', '/li',
  1123. '<li', 'Item 2',
  1124. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1125. '/li',
  1126. '<li', 'Item 3', '/li',
  1127. '<li', 'Item 4',
  1128. '<ul',
  1129. '<li', 'Item 4.1', '/li',
  1130. '<li', 'Item 4.2', '/li',
  1131. '<li', 'Item 4.3',
  1132. '<ul',
  1133. '<li', 'Item 4.3.1', '/li',
  1134. '<li', 'Item 4.3.2', '/li',
  1135. '/ul',
  1136. '/li',
  1137. '/ul',
  1138. '/li',
  1139. '<li', 'Item 5',
  1140. '<ul',
  1141. '<li', 'Item 5.1', '/li',
  1142. '<li', 'Item 5.2', '/li',
  1143. '/ul',
  1144. '/li',
  1145. '/ul',
  1146. ];
  1147. $this->assertHtml($expected, $result);
  1148. $result = $this->Html->nestedList($list);
  1149. $this->assertHtml($expected, $result);
  1150. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1151. $expected = [
  1152. '<ol',
  1153. '<li', 'Item 1', '/li',
  1154. '<li', 'Item 2',
  1155. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1156. '/li',
  1157. '<li', 'Item 3', '/li',
  1158. '<li', 'Item 4',
  1159. '<ol',
  1160. '<li', 'Item 4.1', '/li',
  1161. '<li', 'Item 4.2', '/li',
  1162. '<li', 'Item 4.3',
  1163. '<ol',
  1164. '<li', 'Item 4.3.1', '/li',
  1165. '<li', 'Item 4.3.2', '/li',
  1166. '/ol',
  1167. '/li',
  1168. '/ol',
  1169. '/li',
  1170. '<li', 'Item 5',
  1171. '<ol',
  1172. '<li', 'Item 5.1', '/li',
  1173. '<li', 'Item 5.2', '/li',
  1174. '/ol',
  1175. '/li',
  1176. '/ol',
  1177. ];
  1178. $this->assertHtml($expected, $result);
  1179. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1180. $this->assertHtml($expected, $result);
  1181. $result = $this->Html->nestedList($list, ['class' => 'list']);
  1182. $expected = [
  1183. ['ul' => ['class' => 'list']],
  1184. '<li', 'Item 1', '/li',
  1185. '<li', 'Item 2',
  1186. ['ul' => ['class' => 'list']], '<li', 'Item 2.1', '/li', '/ul',
  1187. '/li',
  1188. '<li', 'Item 3', '/li',
  1189. '<li', 'Item 4',
  1190. ['ul' => ['class' => 'list']],
  1191. '<li', 'Item 4.1', '/li',
  1192. '<li', 'Item 4.2', '/li',
  1193. '<li', 'Item 4.3',
  1194. ['ul' => ['class' => 'list']],
  1195. '<li', 'Item 4.3.1', '/li',
  1196. '<li', 'Item 4.3.2', '/li',
  1197. '/ul',
  1198. '/li',
  1199. '/ul',
  1200. '/li',
  1201. '<li', 'Item 5',
  1202. ['ul' => ['class' => 'list']],
  1203. '<li', 'Item 5.1', '/li',
  1204. '<li', 'Item 5.2', '/li',
  1205. '/ul',
  1206. '/li',
  1207. '/ul',
  1208. ];
  1209. $this->assertHtml($expected, $result);
  1210. $result = $this->Html->nestedList($list, [], ['class' => 'item']);
  1211. $expected = [
  1212. '<ul',
  1213. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1214. ['li' => ['class' => 'item']], 'Item 2',
  1215. '<ul', ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1216. '/li',
  1217. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1218. ['li' => ['class' => 'item']], 'Item 4',
  1219. '<ul',
  1220. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1221. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1222. ['li' => ['class' => 'item']], 'Item 4.3',
  1223. '<ul',
  1224. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1225. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1226. '/ul',
  1227. '/li',
  1228. '/ul',
  1229. '/li',
  1230. ['li' => ['class' => 'item']], 'Item 5',
  1231. '<ul',
  1232. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1233. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1234. '/ul',
  1235. '/li',
  1236. '/ul',
  1237. ];
  1238. $this->assertHtml($expected, $result);
  1239. $result = $this->Html->nestedList($list, [], ['even' => 'even', 'odd' => 'odd']);
  1240. $expected = [
  1241. '<ul',
  1242. ['li' => ['class' => 'odd']], 'Item 1', '/li',
  1243. ['li' => ['class' => 'even']], 'Item 2',
  1244. '<ul', ['li' => ['class' => 'odd']], 'Item 2.1', '/li', '/ul',
  1245. '/li',
  1246. ['li' => ['class' => 'odd']], 'Item 3', '/li',
  1247. ['li' => ['class' => 'even']], 'Item 4',
  1248. '<ul',
  1249. ['li' => ['class' => 'odd']], 'Item 4.1', '/li',
  1250. ['li' => ['class' => 'even']], 'Item 4.2', '/li',
  1251. ['li' => ['class' => 'odd']], 'Item 4.3',
  1252. '<ul',
  1253. ['li' => ['class' => 'odd']], 'Item 4.3.1', '/li',
  1254. ['li' => ['class' => 'even']], 'Item 4.3.2', '/li',
  1255. '/ul',
  1256. '/li',
  1257. '/ul',
  1258. '/li',
  1259. ['li' => ['class' => 'odd']], 'Item 5',
  1260. '<ul',
  1261. ['li' => ['class' => 'odd']], 'Item 5.1', '/li',
  1262. ['li' => ['class' => 'even']], 'Item 5.2', '/li',
  1263. '/ul',
  1264. '/li',
  1265. '/ul',
  1266. ];
  1267. $this->assertHtml($expected, $result);
  1268. $result = $this->Html->nestedList($list, ['class' => 'list'], ['class' => 'item']);
  1269. $expected = [
  1270. ['ul' => ['class' => 'list']],
  1271. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1272. ['li' => ['class' => 'item']], 'Item 2',
  1273. ['ul' => ['class' => 'list']], ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1274. '/li',
  1275. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1276. ['li' => ['class' => 'item']], 'Item 4',
  1277. ['ul' => ['class' => 'list']],
  1278. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1279. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1280. ['li' => ['class' => 'item']], 'Item 4.3',
  1281. ['ul' => ['class' => 'list']],
  1282. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1283. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1284. '/ul',
  1285. '/li',
  1286. '/ul',
  1287. '/li',
  1288. ['li' => ['class' => 'item']], 'Item 5',
  1289. ['ul' => ['class' => 'list']],
  1290. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1291. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1292. '/ul',
  1293. '/li',
  1294. '/ul',
  1295. ];
  1296. $this->assertHtml($expected, $result);
  1297. }
  1298. /**
  1299. * testMeta method
  1300. */
  1301. public function testMeta(): void
  1302. {
  1303. Router::createRouteBuilder('/')->connect('/:controller', ['action' => 'index']);
  1304. $result = $this->Html->meta('this is an rss feed', ['controller' => 'Posts', '_ext' => 'rss']);
  1305. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1306. $this->assertHtml($expected, $result);
  1307. $result = $this->Html->meta('rss', ['controller' => 'Posts', '_ext' => 'rss'], ['title' => 'this is an rss feed']);
  1308. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1309. $this->assertHtml($expected, $result);
  1310. $result = $this->Html->meta('atom', ['controller' => 'Posts', '_ext' => 'xml']);
  1311. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1312. $this->assertHtml($expected, $result);
  1313. $result = $this->Html->meta('nonexistent');
  1314. $expected = ['<meta'];
  1315. $this->assertHtml($expected, $result);
  1316. $result = $this->Html->meta('nonexistent', 'some content');
  1317. $expected = ['meta' => ['name' => 'nonexistent', 'content' => 'some content']];
  1318. $this->assertHtml($expected, $result);
  1319. $result = $this->Html->meta('nonexistent', '/posts.xpp', ['type' => 'atom']);
  1320. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'nonexistent']];
  1321. $this->assertHtml($expected, $result);
  1322. $result = $this->Html->meta('atom', ['controller' => 'Posts', '_ext' => 'xml'], ['link' => '/articles.rss']);
  1323. $expected = ['link' => ['href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1324. $this->assertHtml($expected, $result);
  1325. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1326. $expected = ['meta' => ['name' => 'keywords', 'content' => 'these, are, some, meta, keywords']];
  1327. $this->assertHtml($expected, $result);
  1328. $result = $this->Html->meta('description', 'this is the meta description');
  1329. $expected = ['meta' => ['name' => 'description', 'content' => 'this is the meta description']];
  1330. $this->assertHtml($expected, $result);
  1331. $result = $this->Html->meta('robots', 'ALL');
  1332. $expected = ['meta' => ['name' => 'robots', 'content' => 'ALL']];
  1333. $this->assertHtml($expected, $result);
  1334. $result = $this->Html->meta('viewport', 'width=device-width');
  1335. $expected = [
  1336. 'meta' => ['name' => 'viewport', 'content' => 'width=device-width'],
  1337. ];
  1338. $this->assertHtml($expected, $result);
  1339. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
  1340. $expected = [
  1341. 'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP'],
  1342. ];
  1343. $this->assertHtml($expected, $result);
  1344. $result = $this->Html->meta(['link' => 'http://example.com/manifest', 'rel' => 'manifest']);
  1345. $expected = [
  1346. 'link' => ['href' => 'http://example.com/manifest', 'rel' => 'manifest'],
  1347. ];
  1348. $this->assertHtml($expected, $result);
  1349. }
  1350. /**
  1351. * @return array
  1352. */
  1353. public static function dataMetaLinksProvider(): array
  1354. {
  1355. return [
  1356. ['canonical', ['controller' => 'Posts', 'action' => 'show'], '/posts/show'],
  1357. ['first', ['controller' => 'Posts', 'action' => 'index'], '/posts'],
  1358. ['last', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 10]], '/posts?page=10'],
  1359. ['prev', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 4]], '/posts?page=4'],
  1360. ['next', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 6]], '/posts?page=6'],
  1361. ];
  1362. }
  1363. /**
  1364. * test canonical and pagination meta links
  1365. *
  1366. * @param string $type
  1367. * @param array $url
  1368. * @param string $expectedUrl
  1369. * @dataProvider dataMetaLinksProvider
  1370. */
  1371. public function testMetaLinks($type, array $url, $expectedUrl): void
  1372. {
  1373. $result = $this->Html->meta($type, $url);
  1374. $expected = ['link' => ['href' => $expectedUrl, 'rel' => $type]];
  1375. $this->assertHtml($expected, $result);
  1376. }
  1377. /**
  1378. * Test generating favicon's with meta()
  1379. */
  1380. public function testMetaIcon(): void
  1381. {
  1382. $result = $this->Html->meta('icon', 'favicon.ico');
  1383. $expected = [
  1384. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1385. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $result = $this->Html->meta('icon');
  1389. $expected = [
  1390. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1391. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1392. ];
  1393. $this->assertHtml($expected, $result);
  1394. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1395. $url = '/favicon.png?one=two&amp;three=four';
  1396. $expected = [
  1397. 'link' => [
  1398. 'href' => $url,
  1399. 'type' => 'image/x-icon',
  1400. 'rel' => 'icon',
  1401. ],
  1402. [
  1403. 'link' => [
  1404. 'href' => $url,
  1405. 'type' => 'image/x-icon',
  1406. 'rel' => 'shortcut icon',
  1407. ],
  1408. ],
  1409. ];
  1410. $this->assertHtml($expected, $result);
  1411. $result = $this->Html->meta('icon', 'x:"><script>alert(1)</script>');
  1412. $url = 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
  1413. $expected = [
  1414. 'link' => [
  1415. 'href' => $url,
  1416. 'type' => 'image/x-icon',
  1417. 'rel' => 'icon',
  1418. ],
  1419. [
  1420. 'link' => [
  1421. 'href' => $url,
  1422. 'type' => 'image/x-icon',
  1423. 'rel' => 'shortcut icon',
  1424. ],
  1425. ],
  1426. ];
  1427. $this->assertHtml($expected, $result);
  1428. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  1429. Router::setRequest($request);
  1430. $result = $this->Html->meta('icon');
  1431. $expected = [
  1432. 'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1433. ['link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1434. ];
  1435. $this->assertHtml($expected, $result);
  1436. }
  1437. /**
  1438. * Test generating favicon's with meta() with theme
  1439. */
  1440. public function testMetaIconWithTheme(): void
  1441. {
  1442. $this->Html->Url->getView()->setTheme('TestTheme');
  1443. $result = $this->Html->meta('icon', 'favicon.ico');
  1444. $expected = [
  1445. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1446. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1447. ];
  1448. $this->assertHtml($expected, $result);
  1449. $result = $this->Html->meta('icon');
  1450. $expected = [
  1451. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1452. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1453. ];
  1454. $this->assertHtml($expected, $result);
  1455. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  1456. Router::setRequest($request);
  1457. $result = $this->Html->meta('icon');
  1458. $expected = [
  1459. 'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1460. ['link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1461. ];
  1462. $this->assertHtml($expected, $result);
  1463. }
  1464. /**
  1465. * Test the inline and block options for meta()
  1466. */
  1467. public function testMetaWithBlocks(): void
  1468. {
  1469. $this->View->expects($this->exactly(2))
  1470. ->method('append')
  1471. ->with(
  1472. ...self::withConsecutive(
  1473. ['meta', $this->stringContains('robots')],
  1474. ['metaTags', $this->stringContains('favicon.ico')]
  1475. )
  1476. );
  1477. $result = $this->Html->meta('robots', 'ALL', ['block' => true]);
  1478. $this->assertNull($result);
  1479. $result = $this->Html->meta('icon', 'favicon.ico', ['block' => 'metaTags']);
  1480. $this->assertNull($result);
  1481. }
  1482. /**
  1483. * Test meta() with custom tag and block argument
  1484. */
  1485. public function testMetaCustomWithBlock(): void
  1486. {
  1487. $this->View->expects($this->exactly(2))
  1488. ->method('append')
  1489. ->with(
  1490. ...self::withConsecutive(
  1491. ['meta', $this->stringContains('og:site_name')],
  1492. ['meta', $this->stringContains('og:description')]
  1493. )
  1494. );
  1495. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP', 'block' => true]);
  1496. $this->assertNull($result, 'compact style should work');
  1497. $result = $this->Html->meta(['property' => 'og:description', 'content' => 'CakePHP'], null, ['block' => true]);
  1498. $this->assertNull($result, 'backwards compat style should work.');
  1499. }
  1500. /**
  1501. * testTableHeaders method
  1502. */
  1503. public function testTableHeaders(): void
  1504. {
  1505. $result = $this->Html->tableHeaders(['ID', 'Name', 'Date']);
  1506. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1507. $this->assertHtml($expected, $result);
  1508. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1509. $resultComma = $this->Html->tableHeaders(['ID', ['Name', ['class' => 'highlight']], 'Date']);
  1510. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight']], 'Date']);
  1511. $this->assertHtml($expected, $resultComma);
  1512. $this->assertHtml($expected, $resultAssoc);
  1513. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1514. $resultComma = $this->Html->tableHeaders(['ID', ['Name', ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1515. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1516. $this->assertHtml($expected, $resultComma);
  1517. $this->assertHtml($expected, $resultAssoc);
  1518. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1519. $resultComma = $this->Html->tableHeaders(['ID', ['Name', []], 'Date']);
  1520. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => []], 'Date']);
  1521. $this->assertHtml($expected, $resultComma);
  1522. $this->assertHtml($expected, $resultAssoc);
  1523. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1524. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => ['class' => 'highlight']], 'Date']);
  1525. $this->assertHtml($expected, $resultAssoc);
  1526. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1527. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1528. $this->assertHtml($expected, $resultAssoc);
  1529. $expected = ['<tr', '<th', 'ID', '/th', '<th', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1530. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => []], 'Date']);
  1531. $this->assertHtml($expected, $resultAssoc);
  1532. }
  1533. /**
  1534. * testTableCells method
  1535. */
  1536. public function testTableCells(): void
  1537. {
  1538. $tr = [
  1539. 'td content 1',
  1540. ['td content 2', ['width' => '100px']],
  1541. ['td content 3', ['width' => '100px']],
  1542. ];
  1543. $result = $this->Html->tableCells($tr);
  1544. $expected = [
  1545. '<tr',
  1546. '<td', 'td content 1', '/td',
  1547. ['td' => ['width' => '100px']], 'td content 2', '/td',
  1548. ['td' => ['width' => 'preg:/100px/']], 'td content 3', '/td',
  1549. '/tr',
  1550. ];
  1551. $this->assertHtml($expected, $result);
  1552. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1553. $result = $this->Html->tableCells($tr, null, null, true);
  1554. $expected = [
  1555. '<tr',
  1556. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1557. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1558. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1559. '/tr',
  1560. ];
  1561. $this->assertHtml($expected, $result);
  1562. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1563. $result = $this->Html->tableCells($tr, true);
  1564. $expected = [
  1565. '<tr',
  1566. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1567. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1568. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1569. '/tr',
  1570. ];
  1571. $this->assertHtml($expected, $result);
  1572. $tr = [
  1573. ['td content 1', 'td content 2', 'td content 3'],
  1574. ['td content 1', 'td content 2', 'td content 3'],
  1575. ['td content 1', 'td content 2', 'td content 3'],
  1576. ];
  1577. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1578. $expected = "<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
  1579. $this->assertSame($expected, $result);
  1580. $tr = [
  1581. ['td content 1', 'td content 2', 'td content 3'],
  1582. ['td content 1', 'td content 2', 'td content 3'],
  1583. ['td content 1', 'td content 2', 'td content 3'],
  1584. ['td content 1', 'td content 2', 'td content 3'],
  1585. ];
  1586. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1587. $expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
  1588. $this->assertSame($expected, $result);
  1589. $tr = [
  1590. ['td content 1', 'td content 2', 'td content 3'],
  1591. ['td content 1', 'td content 2', 'td content 3'],
  1592. ['td content 1', 'td content 2', 'td content 3'],
  1593. ];
  1594. $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1595. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
  1596. $expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
  1597. $this->assertSame($expected, $result);
  1598. $tr = [
  1599. 'td content 1',
  1600. 'td content 2',
  1601. ['td content 3', ['class' => 'foo']],
  1602. ];
  1603. $result = $this->Html->tableCells($tr, null, null, true);
  1604. $expected = [
  1605. '<tr',
  1606. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1607. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1608. ['td' => ['class' => 'foo column-3']], 'td content 3', '/td',
  1609. '/tr',
  1610. ];
  1611. $this->assertHtml($expected, $result);
  1612. $tr = [
  1613. new Date('2020-08-27'),
  1614. ];
  1615. $result = $this->Html->tableCells($tr);
  1616. $expected = [
  1617. '<tr',
  1618. '<td', '8/27/20', '/td',
  1619. '/tr',
  1620. ];
  1621. $this->assertHtml($expected, $result);
  1622. $tr = 'string';
  1623. $result = $this->Html->tableCells($tr);
  1624. $expected = [
  1625. '<tr',
  1626. '<td', 'string', '/td',
  1627. '/tr',
  1628. ];
  1629. $this->assertHtml($expected, $result);
  1630. }
  1631. /**
  1632. * testTag method
  1633. */
  1634. public function testTag(): void
  1635. {
  1636. $result = $this->Html->tag('div', 'text');
  1637. $this->assertHtml(['<div', 'text', '/div'], $result);
  1638. $result = $this->Html->tag('div', '<text>', ['class' => 'class-name', 'escape' => true]);
  1639. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1640. $this->assertHtml($expected, $result);
  1641. }
  1642. /**
  1643. * testDiv method
  1644. */
  1645. public function testDiv(): void
  1646. {
  1647. $result = $this->Html->div('class-name');
  1648. $expected = ['div' => ['class' => 'class-name']];
  1649. $this->assertHtml($expected, $result);
  1650. $result = $this->Html->div('class-name', 'text');
  1651. $expected = ['div' => ['class' => 'class-name'], 'text', '/div'];
  1652. $this->assertHtml($expected, $result);
  1653. $result = $this->Html->div('class-name', '<text>', ['escape' => true]);
  1654. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1655. $this->assertHtml($expected, $result);
  1656. $evilKey = '><script>alert(1)</script>';
  1657. $options = [$evilKey => 'some value'];
  1658. $result = $this->Html->div('class-name', '', $options);
  1659. $expected = '<div &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value" class="class-name"></div>';
  1660. $this->assertSame($expected, $result);
  1661. }
  1662. /**
  1663. * testPara method
  1664. */
  1665. public function testPara(): void
  1666. {
  1667. $result = $this->Html->para('class-name', null);
  1668. $expected = ['p' => ['class' => 'class-name']];
  1669. $this->assertHtml($expected, $result);
  1670. $result = $this->Html->para('class-name', '');
  1671. $expected = ['p' => ['class' => 'class-name'], '/p'];
  1672. $this->assertHtml($expected, $result);
  1673. $result = $this->Html->para('class-name', 'text');
  1674. $expected = ['p' => ['class' => 'class-name'], 'text', '/p'];
  1675. $this->assertHtml($expected, $result);
  1676. $result = $this->Html->para('class-name', '<text>', ['escape' => true]);
  1677. $expected = ['p' => ['class' => 'class-name'], '&lt;text&gt;', '/p'];
  1678. $this->assertHtml($expected, $result);
  1679. $result = $this->Html->para('class-name', 'text"', ['escape' => false]);
  1680. $expected = ['p' => ['class' => 'class-name'], 'text"', '/p'];
  1681. $this->assertHtml($expected, $result);
  1682. $result = $this->Html->para(null, null);
  1683. $expected = ['p' => []];
  1684. $this->assertHtml($expected, $result);
  1685. $result = $this->Html->para(null, 'text');
  1686. $expected = ['p' => [], 'text', '/p'];
  1687. $this->assertHtml($expected, $result);
  1688. }
  1689. /**
  1690. * testMedia method
  1691. */
  1692. public function testMedia(): void
  1693. {
  1694. $result = $this->Html->media('video.webm');
  1695. $expected = ['video' => ['src' => 'files/video.webm'], '/video'];
  1696. $this->assertHtml($expected, $result);
  1697. $result = $this->Html->media('video.webm', [
  1698. 'text' => 'Your browser does not support the HTML5 Video element.',
  1699. ]);
  1700. $expected = ['video' => ['src' => 'files/video.webm'], 'Your browser does not support the HTML5 Video element.', '/video'];
  1701. $this->assertHtml($expected, $result);
  1702. $result = $this->Html->media('video.webm', ['autoload' => true, 'muted' => 'muted']);
  1703. $expected = [
  1704. 'video' => [
  1705. 'src' => 'files/video.webm',
  1706. 'autoload' => 'autoload',
  1707. 'muted' => 'muted',
  1708. ],
  1709. '/video',
  1710. ];
  1711. $this->assertHtml($expected, $result);
  1712. $result = $this->Html->media(
  1713. ['video.webm', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
  1714. ['pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.']
  1715. );
  1716. $expected = [
  1717. 'video' => ['poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'],
  1718. ['source' => ['src' => 'videos/video.webm', 'type' => 'video/webm']],
  1719. ['source' => ['src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;']],
  1720. 'Your browser does not support the HTML5 Video element.',
  1721. '/video',
  1722. ];
  1723. $this->assertHtml($expected, $result);
  1724. $result = $this->Html->media('video.ogv', ['tag' => 'video']);
  1725. $expected = ['video' => ['src' => 'files/video.ogv'], '/video'];
  1726. $this->assertHtml($expected, $result);
  1727. $result = $this->Html->media('audio.mp3');
  1728. $expected = ['audio' => ['src' => 'files/audio.mp3'], '/audio'];
  1729. $this->assertHtml($expected, $result);
  1730. $result = $this->Html->media(
  1731. [['src' => 'video.mov', 'type' => 'video/mp4'], 'video.webm']
  1732. );
  1733. $expected = [
  1734. '<video',
  1735. ['source' => ['src' => 'files/video.mov', 'type' => 'video/mp4']],
  1736. ['source' => ['src' => 'files/video.webm', 'type' => 'video/webm']],
  1737. '/video',
  1738. ];
  1739. $this->assertHtml($expected, $result);
  1740. $result = $this->Html->media(null, ['src' => 'video.webm']);
  1741. $expected = [
  1742. 'video' => ['src' => 'files/video.webm'],
  1743. '/video',
  1744. ];
  1745. $this->assertHtml($expected, $result);
  1746. }
  1747. /**
  1748. * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
  1749. */
  1750. public function testCssAndScriptWithSameName(): void
  1751. {
  1752. $result = $this->Html->css('foo');
  1753. $expected = [
  1754. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/'],
  1755. ];
  1756. $this->assertHtml($expected, $result);
  1757. $result = $this->Html->script('foo');
  1758. $expected = [
  1759. 'script' => ['src' => 'js/foo.js'],
  1760. ];
  1761. $this->assertHtml($expected, $result);
  1762. }
  1763. }