HtmlHelperTest.php 76 KB

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