HtmlHelperTest.php 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  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\Filesystem\Filesystem;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\FrozenDate;
  22. use Cake\Routing\Route\DashedRoute;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  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');
  526. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  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('x:"><script>alert(1)</script>');
  810. $expected = [
  811. 'script' => ['src' => 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;'],
  812. ];
  813. $this->assertHtml($expected, $result);
  814. $result = $this->Html->script('foo2', ['pathPrefix' => '/my/custom/path/']);
  815. $expected = [
  816. 'script' => ['src' => '/my/custom/path/foo2.js'],
  817. ];
  818. $this->assertHtml($expected, $result);
  819. $result = $this->Html->script('foo3', ['pathPrefix' => 'http://cakephp.org/assets/js/']);
  820. $expected = [
  821. 'script' => ['src' => 'http://cakephp.org/assets/js/foo3.js'],
  822. ];
  823. $this->assertHtml($expected, $result);
  824. $previousConfig = Configure::read('App.jsBaseUrl');
  825. Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
  826. $result = $this->Html->script('foo4');
  827. $expected = [
  828. 'script' => ['src' => '//cdn.cakephp.org/js/foo4.js'],
  829. ];
  830. $this->assertHtml($expected, $result);
  831. Configure::write('App.jsBaseUrl', $previousConfig);
  832. $result = $this->Html->script('foo');
  833. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  834. $result = $this->Html->script(['foo', 'bar', 'baz']);
  835. $this->assertDoesNotMatchRegularExpression('/foo.js/', $result);
  836. $result = $this->Html->script('foo', ['once' => false]);
  837. $this->assertNotNull($result);
  838. $result = $this->Html->script('jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
  839. $expected = [
  840. 'script' => ['src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
  841. ];
  842. $this->assertHtml($expected, $result);
  843. }
  844. /**
  845. * Test that content-security-policy nonces are applied if the request attribute
  846. * is present.
  847. */
  848. public function testScriptCspNonce(): void
  849. {
  850. $nonce = 'r@ndomV4lue';
  851. $request = $this->View->getRequest()
  852. ->withAttribute('cspScriptNonce', $nonce);
  853. $this->View->setRequest($request);
  854. $result = $this->Html->script('app.js', ['defer' => true, 'encoding' => 'utf-8']);
  855. $expected = [
  856. 'script' => ['src' => 'js/app.js', 'defer' => 'defer', 'encoding' => 'utf-8', 'nonce' => $nonce],
  857. ];
  858. $this->assertHtml($expected, $result);
  859. }
  860. /**
  861. * test that plugin scripts added with uses() are only ever included once.
  862. * test script tag generation with plugin syntax
  863. */
  864. public function testPluginScript(): void
  865. {
  866. $this->loadPlugins(['TestPlugin']);
  867. $result = $this->Html->script('TestPlugin.foo');
  868. $expected = [
  869. 'script' => ['src' => 'test_plugin/js/foo.js'],
  870. ];
  871. $this->assertHtml($expected, $result);
  872. $result = $this->Html->script(['TestPlugin.foobar', 'TestPlugin.bar']);
  873. $expected = [
  874. ['script' => ['src' => 'test_plugin/js/foobar.js']],
  875. '/script',
  876. ['script' => ['src' => 'test_plugin/js/bar.js']],
  877. '/script',
  878. ];
  879. $this->assertHtml($expected, $result);
  880. $result = $this->Html->script('TestPlugin.jquery-1.3');
  881. $expected = [
  882. 'script' => ['src' => 'test_plugin/js/jquery-1.3.js'],
  883. ];
  884. $this->assertHtml($expected, $result);
  885. $result = $this->Html->script('TestPlugin.test.json');
  886. $expected = [
  887. 'script' => ['src' => 'test_plugin/js/test.json.js'],
  888. ];
  889. $this->assertHtml($expected, $result);
  890. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  891. $expected = [
  892. 'script' => ['src' => 'test_plugin/jquery-1.3.2.js?someparam=foo'],
  893. ];
  894. $this->assertHtml($expected, $result);
  895. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  896. $expected = [
  897. 'script' => ['src' => 'test_plugin/js/test.json.js?foo=bar'],
  898. ];
  899. $this->assertHtml($expected, $result);
  900. $result = $this->Html->script('TestPlugin.foo');
  901. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  902. $result = $this->Html->script(['TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz']);
  903. $this->assertDoesNotMatchRegularExpression('/test_plugin\/js\/foo.js/', $result);
  904. $result = $this->Html->script('TestPlugin.foo', ['once' => false]);
  905. $this->assertNotNull($result);
  906. $result = $this->Html->script('TestPlugin.jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
  907. $expected = [
  908. 'script' => ['src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
  909. ];
  910. $this->assertHtml($expected, $result);
  911. $this->removePlugins(['TestPlugin']);
  912. }
  913. /**
  914. * test that script() works with blocks.
  915. */
  916. public function testScriptWithBlocks(): void
  917. {
  918. $this->View->expects($this->exactly(2))
  919. ->method('append')
  920. ->withConsecutive(
  921. ['script', $this->matchesRegularExpression('/script_in_head.js/')],
  922. ['headScripts', $this->matchesRegularExpression('/second_script.js/')]
  923. );
  924. $result = $this->Html->script('script_in_head', ['block' => true]);
  925. $this->assertNull($result);
  926. $result = $this->Html->script('second_script', ['block' => 'headScripts']);
  927. $this->assertNull($result);
  928. }
  929. /**
  930. * testScriptWithFullBase method
  931. */
  932. public function testScriptWithFullBase(): void
  933. {
  934. $here = $this->Html->Url->build('/', ['fullBase' => true]);
  935. $result = $this->Html->script('foo', ['fullBase' => true]);
  936. $expected = [
  937. 'script' => ['src' => $here . 'js/foo.js'],
  938. ];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Html->script(['foobar', 'bar'], ['fullBase' => true]);
  941. $expected = [
  942. ['script' => ['src' => $here . 'js/foobar.js']],
  943. '/script',
  944. ['script' => ['src' => $here . 'js/bar.js']],
  945. '/script',
  946. ];
  947. $this->assertHtml($expected, $result);
  948. }
  949. /**
  950. * test a script file in the webroot/theme dir.
  951. */
  952. public function testScriptInTheme(): void
  953. {
  954. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  955. $testfile = WWW_ROOT . 'test_theme/js/__test_js.js';
  956. $fs = new Filesystem();
  957. $fs->dumpFile($testfile, '');
  958. $request = Router::getRequest()->withAttribute('webroot', '/');
  959. Router::setRequest($request);
  960. $this->Html->Url->getView()->setTheme('TestTheme');
  961. $result = $this->Html->script('__test_js.js');
  962. $expected = [
  963. 'script' => ['src' => '/test_theme/js/__test_js.js'],
  964. ];
  965. $this->assertHtml($expected, $result);
  966. // phpcs:ignore
  967. @unlink($testfile);
  968. }
  969. /**
  970. * test Script block generation
  971. */
  972. public function testScriptBlock(): void
  973. {
  974. $result = $this->Html->scriptBlock('window.foo = 2;');
  975. $expected = [
  976. '<script',
  977. 'window.foo = 2;',
  978. '/script',
  979. ];
  980. $this->assertHtml($expected, $result);
  981. $result = $this->Html->scriptBlock('window.foo = 2;', ['type' => 'text/x-handlebars-template']);
  982. $expected = [
  983. 'script' => ['type' => 'text/x-handlebars-template'],
  984. 'window.foo = 2;',
  985. '/script',
  986. ];
  987. $this->assertHtml($expected, $result);
  988. $result = $this->Html->scriptBlock('window.foo = 2;');
  989. $expected = [
  990. '<script',
  991. 'window.foo = 2;',
  992. '/script',
  993. ];
  994. $this->assertHtml($expected, $result);
  995. $this->View->expects($this->exactly(2))
  996. ->method('append')
  997. ->withConsecutive(
  998. ['script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/')],
  999. ['scriptTop', $this->stringContains('alert(')]
  1000. );
  1001. $result = $this->Html->scriptBlock('window.foo = 2;', ['block' => true]);
  1002. $this->assertNull($result);
  1003. $result = $this->Html->scriptBlock('alert("hi")', ['block' => 'scriptTop']);
  1004. $this->assertNull($result);
  1005. $result = $this->Html->scriptBlock('window.foo = 2;', ['encoding' => 'utf-8']);
  1006. $expected = [
  1007. 'script' => ['encoding' => 'utf-8'],
  1008. 'window.foo = 2;',
  1009. '/script',
  1010. ];
  1011. $this->assertHtml($expected, $result);
  1012. }
  1013. /**
  1014. * Ensure that scriptBlock() uses CSP nonces.
  1015. */
  1016. public function testScriptBlockCspNonce(): void
  1017. {
  1018. $nonce = 'r@ndomV4lue';
  1019. $request = $this->View->getRequest()
  1020. ->withAttribute('cspScriptNonce', $nonce);
  1021. $this->View->setRequest($request);
  1022. $result = $this->Html->scriptBlock('window.foo = 2;');
  1023. $expected = [
  1024. 'script' => ['nonce' => $nonce],
  1025. 'window.foo = 2;',
  1026. '/script',
  1027. ];
  1028. $this->assertHtml($expected, $result);
  1029. }
  1030. /**
  1031. * test script tag output buffering when using scriptStart() and scriptEnd();
  1032. */
  1033. public function testScriptStartAndScriptEnd(): void
  1034. {
  1035. $this->Html->scriptStart();
  1036. echo 'this is some javascript';
  1037. $result = $this->Html->scriptEnd();
  1038. $expected = [
  1039. '<script',
  1040. 'this is some javascript',
  1041. '/script',
  1042. ];
  1043. $this->assertHtml($expected, $result);
  1044. $this->Html->scriptStart();
  1045. echo 'this is some javascript';
  1046. $result = $this->Html->scriptEnd();
  1047. $expected = [
  1048. '<script',
  1049. 'this is some javascript',
  1050. '/script',
  1051. ];
  1052. $this->assertHtml($expected, $result);
  1053. }
  1054. /**
  1055. * testCharsetTag method
  1056. */
  1057. public function testCharsetTag(): void
  1058. {
  1059. Configure::write('App.encoding', null);
  1060. $result = $this->Html->charset();
  1061. $expected = ['meta' => ['charset' => 'utf-8']];
  1062. $this->assertHtml($expected, $result);
  1063. Configure::write('App.encoding', 'ISO-8859-1');
  1064. $result = $this->Html->charset();
  1065. $expected = ['meta' => ['charset' => 'iso-8859-1']];
  1066. $this->assertHtml($expected, $result);
  1067. $result = $this->Html->charset('UTF-7');
  1068. $expected = ['meta' => ['charset' => 'UTF-7']];
  1069. $this->assertHtml($expected, $result);
  1070. }
  1071. /**
  1072. * testNestedList method
  1073. */
  1074. public function testNestedList(): void
  1075. {
  1076. $list = [
  1077. 'Item 1',
  1078. 'Item 2' => [
  1079. 'Item 2.1',
  1080. ],
  1081. 'Item 3',
  1082. 'Item 4' => [
  1083. 'Item 4.1',
  1084. 'Item 4.2',
  1085. 'Item 4.3' => [
  1086. 'Item 4.3.1',
  1087. 'Item 4.3.2',
  1088. ],
  1089. ],
  1090. 'Item 5' => [
  1091. 'Item 5.1',
  1092. 'Item 5.2',
  1093. ],
  1094. ];
  1095. $result = $this->Html->nestedList($list);
  1096. $expected = [
  1097. '<ul',
  1098. '<li', 'Item 1', '/li',
  1099. '<li', 'Item 2',
  1100. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1101. '/li',
  1102. '<li', 'Item 3', '/li',
  1103. '<li', 'Item 4',
  1104. '<ul',
  1105. '<li', 'Item 4.1', '/li',
  1106. '<li', 'Item 4.2', '/li',
  1107. '<li', 'Item 4.3',
  1108. '<ul',
  1109. '<li', 'Item 4.3.1', '/li',
  1110. '<li', 'Item 4.3.2', '/li',
  1111. '/ul',
  1112. '/li',
  1113. '/ul',
  1114. '/li',
  1115. '<li', 'Item 5',
  1116. '<ul',
  1117. '<li', 'Item 5.1', '/li',
  1118. '<li', 'Item 5.2', '/li',
  1119. '/ul',
  1120. '/li',
  1121. '/ul',
  1122. ];
  1123. $this->assertHtml($expected, $result);
  1124. $result = $this->Html->nestedList($list);
  1125. $this->assertHtml($expected, $result);
  1126. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1127. $expected = [
  1128. '<ol',
  1129. '<li', 'Item 1', '/li',
  1130. '<li', 'Item 2',
  1131. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1132. '/li',
  1133. '<li', 'Item 3', '/li',
  1134. '<li', 'Item 4',
  1135. '<ol',
  1136. '<li', 'Item 4.1', '/li',
  1137. '<li', 'Item 4.2', '/li',
  1138. '<li', 'Item 4.3',
  1139. '<ol',
  1140. '<li', 'Item 4.3.1', '/li',
  1141. '<li', 'Item 4.3.2', '/li',
  1142. '/ol',
  1143. '/li',
  1144. '/ol',
  1145. '/li',
  1146. '<li', 'Item 5',
  1147. '<ol',
  1148. '<li', 'Item 5.1', '/li',
  1149. '<li', 'Item 5.2', '/li',
  1150. '/ol',
  1151. '/li',
  1152. '/ol',
  1153. ];
  1154. $this->assertHtml($expected, $result);
  1155. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1156. $this->assertHtml($expected, $result);
  1157. $result = $this->Html->nestedList($list, ['class' => 'list']);
  1158. $expected = [
  1159. ['ul' => ['class' => 'list']],
  1160. '<li', 'Item 1', '/li',
  1161. '<li', 'Item 2',
  1162. ['ul' => ['class' => 'list']], '<li', 'Item 2.1', '/li', '/ul',
  1163. '/li',
  1164. '<li', 'Item 3', '/li',
  1165. '<li', 'Item 4',
  1166. ['ul' => ['class' => 'list']],
  1167. '<li', 'Item 4.1', '/li',
  1168. '<li', 'Item 4.2', '/li',
  1169. '<li', 'Item 4.3',
  1170. ['ul' => ['class' => 'list']],
  1171. '<li', 'Item 4.3.1', '/li',
  1172. '<li', 'Item 4.3.2', '/li',
  1173. '/ul',
  1174. '/li',
  1175. '/ul',
  1176. '/li',
  1177. '<li', 'Item 5',
  1178. ['ul' => ['class' => 'list']],
  1179. '<li', 'Item 5.1', '/li',
  1180. '<li', 'Item 5.2', '/li',
  1181. '/ul',
  1182. '/li',
  1183. '/ul',
  1184. ];
  1185. $this->assertHtml($expected, $result);
  1186. $result = $this->Html->nestedList($list, [], ['class' => 'item']);
  1187. $expected = [
  1188. '<ul',
  1189. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1190. ['li' => ['class' => 'item']], 'Item 2',
  1191. '<ul', ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1192. '/li',
  1193. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1194. ['li' => ['class' => 'item']], 'Item 4',
  1195. '<ul',
  1196. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1197. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1198. ['li' => ['class' => 'item']], 'Item 4.3',
  1199. '<ul',
  1200. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1201. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1202. '/ul',
  1203. '/li',
  1204. '/ul',
  1205. '/li',
  1206. ['li' => ['class' => 'item']], 'Item 5',
  1207. '<ul',
  1208. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1209. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1210. '/ul',
  1211. '/li',
  1212. '/ul',
  1213. ];
  1214. $this->assertHtml($expected, $result);
  1215. $result = $this->Html->nestedList($list, [], ['even' => 'even', 'odd' => 'odd']);
  1216. $expected = [
  1217. '<ul',
  1218. ['li' => ['class' => 'odd']], 'Item 1', '/li',
  1219. ['li' => ['class' => 'even']], 'Item 2',
  1220. '<ul', ['li' => ['class' => 'odd']], 'Item 2.1', '/li', '/ul',
  1221. '/li',
  1222. ['li' => ['class' => 'odd']], 'Item 3', '/li',
  1223. ['li' => ['class' => 'even']], 'Item 4',
  1224. '<ul',
  1225. ['li' => ['class' => 'odd']], 'Item 4.1', '/li',
  1226. ['li' => ['class' => 'even']], 'Item 4.2', '/li',
  1227. ['li' => ['class' => 'odd']], 'Item 4.3',
  1228. '<ul',
  1229. ['li' => ['class' => 'odd']], 'Item 4.3.1', '/li',
  1230. ['li' => ['class' => 'even']], 'Item 4.3.2', '/li',
  1231. '/ul',
  1232. '/li',
  1233. '/ul',
  1234. '/li',
  1235. ['li' => ['class' => 'odd']], 'Item 5',
  1236. '<ul',
  1237. ['li' => ['class' => 'odd']], 'Item 5.1', '/li',
  1238. ['li' => ['class' => 'even']], 'Item 5.2', '/li',
  1239. '/ul',
  1240. '/li',
  1241. '/ul',
  1242. ];
  1243. $this->assertHtml($expected, $result);
  1244. $result = $this->Html->nestedList($list, ['class' => 'list'], ['class' => 'item']);
  1245. $expected = [
  1246. ['ul' => ['class' => 'list']],
  1247. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1248. ['li' => ['class' => 'item']], 'Item 2',
  1249. ['ul' => ['class' => 'list']], ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1250. '/li',
  1251. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1252. ['li' => ['class' => 'item']], 'Item 4',
  1253. ['ul' => ['class' => 'list']],
  1254. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1255. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1256. ['li' => ['class' => 'item']], 'Item 4.3',
  1257. ['ul' => ['class' => 'list']],
  1258. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1259. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1260. '/ul',
  1261. '/li',
  1262. '/ul',
  1263. '/li',
  1264. ['li' => ['class' => 'item']], 'Item 5',
  1265. ['ul' => ['class' => 'list']],
  1266. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1267. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1268. '/ul',
  1269. '/li',
  1270. '/ul',
  1271. ];
  1272. $this->assertHtml($expected, $result);
  1273. }
  1274. /**
  1275. * testMeta method
  1276. */
  1277. public function testMeta(): void
  1278. {
  1279. Router::createRouteBuilder('/')->connect('/:controller', ['action' => 'index']);
  1280. $result = $this->Html->meta('this is an rss feed', ['controller' => 'Posts', '_ext' => 'rss']);
  1281. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1282. $this->assertHtml($expected, $result);
  1283. $result = $this->Html->meta('rss', ['controller' => 'Posts', '_ext' => 'rss'], ['title' => 'this is an rss feed']);
  1284. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1285. $this->assertHtml($expected, $result);
  1286. $result = $this->Html->meta('atom', ['controller' => 'Posts', '_ext' => 'xml']);
  1287. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1288. $this->assertHtml($expected, $result);
  1289. $result = $this->Html->meta('nonexistent');
  1290. $expected = ['<meta'];
  1291. $this->assertHtml($expected, $result);
  1292. $result = $this->Html->meta('nonexistent', 'some content');
  1293. $expected = ['meta' => ['name' => 'nonexistent', 'content' => 'some content']];
  1294. $this->assertHtml($expected, $result);
  1295. $result = $this->Html->meta('nonexistent', '/posts.xpp', ['type' => 'atom']);
  1296. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'nonexistent']];
  1297. $this->assertHtml($expected, $result);
  1298. $result = $this->Html->meta('atom', ['controller' => 'Posts', '_ext' => 'xml'], ['link' => '/articles.rss']);
  1299. $expected = ['link' => ['href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1300. $this->assertHtml($expected, $result);
  1301. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1302. $expected = ['meta' => ['name' => 'keywords', 'content' => 'these, are, some, meta, keywords']];
  1303. $this->assertHtml($expected, $result);
  1304. $result = $this->Html->meta('description', 'this is the meta description');
  1305. $expected = ['meta' => ['name' => 'description', 'content' => 'this is the meta description']];
  1306. $this->assertHtml($expected, $result);
  1307. $result = $this->Html->meta('robots', 'ALL');
  1308. $expected = ['meta' => ['name' => 'robots', 'content' => 'ALL']];
  1309. $this->assertHtml($expected, $result);
  1310. $result = $this->Html->meta('viewport', 'width=device-width');
  1311. $expected = [
  1312. 'meta' => ['name' => 'viewport', 'content' => 'width=device-width'],
  1313. ];
  1314. $this->assertHtml($expected, $result);
  1315. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
  1316. $expected = [
  1317. 'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP'],
  1318. ];
  1319. $this->assertHtml($expected, $result);
  1320. $result = $this->Html->meta(['link' => 'http://example.com/manifest', 'rel' => 'manifest']);
  1321. $expected = [
  1322. 'link' => ['href' => 'http://example.com/manifest', 'rel' => 'manifest'],
  1323. ];
  1324. $this->assertHtml($expected, $result);
  1325. }
  1326. /**
  1327. * @return array
  1328. */
  1329. public function dataMetaLinksProvider(): array
  1330. {
  1331. return [
  1332. ['canonical', ['controller' => 'Posts', 'action' => 'show'], '/posts/show'],
  1333. ['first', ['controller' => 'Posts', 'action' => 'index'], '/posts'],
  1334. ['last', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 10]], '/posts?page=10'],
  1335. ['prev', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 4]], '/posts?page=4'],
  1336. ['next', ['controller' => 'Posts', 'action' => 'index', '?' => ['page' => 6]], '/posts?page=6'],
  1337. ];
  1338. }
  1339. /**
  1340. * test canonical and pagination meta links
  1341. *
  1342. * @param string $type
  1343. * @param array $url
  1344. * @param string $expectedUrl
  1345. * @dataProvider dataMetaLinksProvider
  1346. */
  1347. public function testMetaLinks($type, array $url, $expectedUrl): void
  1348. {
  1349. $result = $this->Html->meta($type, $url);
  1350. $expected = ['link' => ['href' => $expectedUrl, 'rel' => $type]];
  1351. $this->assertHtml($expected, $result);
  1352. }
  1353. /**
  1354. * Test generating favicon's with meta()
  1355. */
  1356. public function testMetaIcon(): void
  1357. {
  1358. $result = $this->Html->meta('icon', 'favicon.ico');
  1359. $expected = [
  1360. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1361. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1362. ];
  1363. $this->assertHtml($expected, $result);
  1364. $result = $this->Html->meta('icon');
  1365. $expected = [
  1366. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1367. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1368. ];
  1369. $this->assertHtml($expected, $result);
  1370. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1371. $url = '/favicon.png?one=two&amp;three=four';
  1372. $expected = [
  1373. 'link' => [
  1374. 'href' => $url,
  1375. 'type' => 'image/x-icon',
  1376. 'rel' => 'icon',
  1377. ],
  1378. [
  1379. 'link' => [
  1380. 'href' => $url,
  1381. 'type' => 'image/x-icon',
  1382. 'rel' => 'shortcut icon',
  1383. ],
  1384. ],
  1385. ];
  1386. $this->assertHtml($expected, $result);
  1387. $result = $this->Html->meta('icon', 'x:"><script>alert(1)</script>');
  1388. $url = 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
  1389. $expected = [
  1390. 'link' => [
  1391. 'href' => $url,
  1392. 'type' => 'image/x-icon',
  1393. 'rel' => 'icon',
  1394. ],
  1395. [
  1396. 'link' => [
  1397. 'href' => $url,
  1398. 'type' => 'image/x-icon',
  1399. 'rel' => 'shortcut icon',
  1400. ],
  1401. ],
  1402. ];
  1403. $this->assertHtml($expected, $result);
  1404. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  1405. Router::setRequest($request);
  1406. $result = $this->Html->meta('icon');
  1407. $expected = [
  1408. 'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1409. ['link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1410. ];
  1411. $this->assertHtml($expected, $result);
  1412. }
  1413. /**
  1414. * Test generating favicon's with meta() with theme
  1415. */
  1416. public function testMetaIconWithTheme(): void
  1417. {
  1418. $this->Html->Url->getView()->setTheme('TestTheme');
  1419. $result = $this->Html->meta('icon', 'favicon.ico');
  1420. $expected = [
  1421. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1422. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1423. ];
  1424. $this->assertHtml($expected, $result);
  1425. $result = $this->Html->meta('icon');
  1426. $expected = [
  1427. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1428. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1429. ];
  1430. $this->assertHtml($expected, $result);
  1431. $request = Router::getRequest()->withAttribute('webroot', '/testing/');
  1432. Router::setRequest($request);
  1433. $result = $this->Html->meta('icon');
  1434. $expected = [
  1435. 'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1436. ['link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
  1437. ];
  1438. $this->assertHtml($expected, $result);
  1439. }
  1440. /**
  1441. * Test the inline and block options for meta()
  1442. */
  1443. public function testMetaWithBlocks(): void
  1444. {
  1445. $this->View->expects($this->exactly(2))
  1446. ->method('append')
  1447. ->withConsecutive(
  1448. ['meta', $this->stringContains('robots')],
  1449. ['metaTags', $this->stringContains('favicon.ico')]
  1450. );
  1451. $result = $this->Html->meta('robots', 'ALL', ['block' => true]);
  1452. $this->assertNull($result);
  1453. $result = $this->Html->meta('icon', 'favicon.ico', ['block' => 'metaTags']);
  1454. $this->assertNull($result);
  1455. }
  1456. /**
  1457. * Test meta() with custom tag and block argument
  1458. */
  1459. public function testMetaCustomWithBlock(): void
  1460. {
  1461. $this->View->expects($this->exactly(2))
  1462. ->method('append')
  1463. ->withConsecutive(
  1464. ['meta', $this->stringContains('og:site_name')],
  1465. ['meta', $this->stringContains('og:description')]
  1466. );
  1467. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP', 'block' => true]);
  1468. $this->assertNull($result, 'compact style should work');
  1469. $result = $this->Html->meta(['property' => 'og:description', 'content' => 'CakePHP'], null, ['block' => true]);
  1470. $this->assertNull($result, 'backwards compat style should work.');
  1471. }
  1472. /**
  1473. * testTableHeaders method
  1474. */
  1475. public function testTableHeaders(): void
  1476. {
  1477. $result = $this->Html->tableHeaders(['ID', 'Name', 'Date']);
  1478. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1479. $this->assertHtml($expected, $result);
  1480. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1481. $resultComma = $this->Html->tableHeaders(['ID', ['Name', ['class' => 'highlight']], 'Date']);
  1482. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight']], 'Date']);
  1483. $this->assertHtml($expected, $resultComma);
  1484. $this->assertHtml($expected, $resultAssoc);
  1485. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1486. $resultComma = $this->Html->tableHeaders(['ID', ['Name', ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1487. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1488. $this->assertHtml($expected, $resultComma);
  1489. $this->assertHtml($expected, $resultAssoc);
  1490. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1491. $resultComma = $this->Html->tableHeaders(['ID', ['Name', []], 'Date']);
  1492. $resultAssoc = $this->Html->tableHeaders(['ID', ['Name' => []], 'Date']);
  1493. $this->assertHtml($expected, $resultComma);
  1494. $this->assertHtml($expected, $resultAssoc);
  1495. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1496. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => ['class' => 'highlight']], 'Date']);
  1497. $this->assertHtml($expected, $resultAssoc);
  1498. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1499. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1500. $this->assertHtml($expected, $resultAssoc);
  1501. $expected = ['<tr', '<th', 'ID', '/th', '<th', '0', '/th', '<th', 'Date', '/th', '/tr'];
  1502. $resultAssoc = $this->Html->tableHeaders(['ID', ['0' => []], 'Date']);
  1503. $this->assertHtml($expected, $resultAssoc);
  1504. }
  1505. /**
  1506. * testTableCells method
  1507. */
  1508. public function testTableCells(): void
  1509. {
  1510. $tr = [
  1511. 'td content 1',
  1512. ['td content 2', ['width' => '100px']],
  1513. ['td content 3', ['width' => '100px']],
  1514. ];
  1515. $result = $this->Html->tableCells($tr);
  1516. $expected = [
  1517. '<tr',
  1518. '<td', 'td content 1', '/td',
  1519. ['td' => ['width' => '100px']], 'td content 2', '/td',
  1520. ['td' => ['width' => 'preg:/100px/']], 'td content 3', '/td',
  1521. '/tr',
  1522. ];
  1523. $this->assertHtml($expected, $result);
  1524. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1525. $result = $this->Html->tableCells($tr, null, null, true);
  1526. $expected = [
  1527. '<tr',
  1528. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1529. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1530. ['td' => ['class' => 'column-3']], '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, 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 = [
  1545. ['td content 1', 'td content 2', 'td content 3'],
  1546. ['td content 1', 'td content 2', 'td content 3'],
  1547. ['td content 1', 'td content 2', 'td content 3'],
  1548. ];
  1549. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1550. $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>";
  1551. $this->assertSame($expected, $result);
  1552. $tr = [
  1553. ['td content 1', 'td content 2', 'td content 3'],
  1554. ['td content 1', 'td content 2', 'td content 3'],
  1555. ['td content 1', 'td content 2', 'td content 3'],
  1556. ['td content 1', 'td content 2', 'td content 3'],
  1557. ];
  1558. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1559. $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>";
  1560. $this->assertSame($expected, $result);
  1561. $tr = [
  1562. ['td content 1', 'td content 2', 'td content 3'],
  1563. ['td content 1', 'td content 2', 'td content 3'],
  1564. ['td content 1', 'td content 2', 'td content 3'],
  1565. ];
  1566. $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1567. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
  1568. $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>";
  1569. $this->assertSame($expected, $result);
  1570. $tr = [
  1571. 'td content 1',
  1572. 'td content 2',
  1573. ['td content 3', ['class' => 'foo']],
  1574. ];
  1575. $result = $this->Html->tableCells($tr, null, null, true);
  1576. $expected = [
  1577. '<tr',
  1578. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1579. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1580. ['td' => ['class' => 'foo column-3']], 'td content 3', '/td',
  1581. '/tr',
  1582. ];
  1583. $this->assertHtml($expected, $result);
  1584. $tr = [
  1585. new FrozenDate('2020-08-27'),
  1586. ];
  1587. $result = $this->Html->tableCells($tr);
  1588. $expected = [
  1589. '<tr',
  1590. '<td', '8/27/20', '/td',
  1591. '/tr',
  1592. ];
  1593. $this->assertHtml($expected, $result);
  1594. $tr = 'string';
  1595. $result = $this->Html->tableCells($tr);
  1596. $expected = [
  1597. '<tr',
  1598. '<td', 'string', '/td',
  1599. '/tr',
  1600. ];
  1601. $this->assertHtml($expected, $result);
  1602. }
  1603. /**
  1604. * testTag method
  1605. */
  1606. public function testTag(): void
  1607. {
  1608. $result = $this->Html->tag('div', 'text');
  1609. $this->assertHtml(['<div', 'text', '/div'], $result);
  1610. $result = $this->Html->tag('div', '<text>', ['class' => 'class-name', 'escape' => true]);
  1611. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1612. $this->assertHtml($expected, $result);
  1613. }
  1614. /**
  1615. * testDiv method
  1616. */
  1617. public function testDiv(): void
  1618. {
  1619. $result = $this->Html->div('class-name');
  1620. $expected = ['div' => ['class' => 'class-name']];
  1621. $this->assertHtml($expected, $result);
  1622. $result = $this->Html->div('class-name', 'text');
  1623. $expected = ['div' => ['class' => 'class-name'], 'text', '/div'];
  1624. $this->assertHtml($expected, $result);
  1625. $result = $this->Html->div('class-name', '<text>', ['escape' => true]);
  1626. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1627. $this->assertHtml($expected, $result);
  1628. $evilKey = '><script>alert(1)</script>';
  1629. $options = [$evilKey => 'some value'];
  1630. $result = $this->Html->div('class-name', '', $options);
  1631. $expected = '<div &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value" class="class-name"></div>';
  1632. $this->assertSame($expected, $result);
  1633. }
  1634. /**
  1635. * testPara method
  1636. */
  1637. public function testPara(): void
  1638. {
  1639. $result = $this->Html->para('class-name', null);
  1640. $expected = ['p' => ['class' => 'class-name']];
  1641. $this->assertHtml($expected, $result);
  1642. $result = $this->Html->para('class-name', '');
  1643. $expected = ['p' => ['class' => 'class-name'], '/p'];
  1644. $this->assertHtml($expected, $result);
  1645. $result = $this->Html->para('class-name', 'text');
  1646. $expected = ['p' => ['class' => 'class-name'], 'text', '/p'];
  1647. $this->assertHtml($expected, $result);
  1648. $result = $this->Html->para('class-name', '<text>', ['escape' => true]);
  1649. $expected = ['p' => ['class' => 'class-name'], '&lt;text&gt;', '/p'];
  1650. $this->assertHtml($expected, $result);
  1651. $result = $this->Html->para('class-name', 'text"', ['escape' => false]);
  1652. $expected = ['p' => ['class' => 'class-name'], 'text"', '/p'];
  1653. $this->assertHtml($expected, $result);
  1654. $result = $this->Html->para(null, null);
  1655. $expected = ['p' => []];
  1656. $this->assertHtml($expected, $result);
  1657. $result = $this->Html->para(null, 'text');
  1658. $expected = ['p' => [], 'text', '/p'];
  1659. $this->assertHtml($expected, $result);
  1660. }
  1661. /**
  1662. * testMedia method
  1663. */
  1664. public function testMedia(): void
  1665. {
  1666. $result = $this->Html->media('video.webm');
  1667. $expected = ['video' => ['src' => 'files/video.webm'], '/video'];
  1668. $this->assertHtml($expected, $result);
  1669. $result = $this->Html->media('video.webm', [
  1670. 'text' => 'Your browser does not support the HTML5 Video element.',
  1671. ]);
  1672. $expected = ['video' => ['src' => 'files/video.webm'], 'Your browser does not support the HTML5 Video element.', '/video'];
  1673. $this->assertHtml($expected, $result);
  1674. $result = $this->Html->media('video.webm', ['autoload', 'muted' => 'muted']);
  1675. $expected = [
  1676. 'video' => [
  1677. 'src' => 'files/video.webm',
  1678. 'autoload' => 'autoload',
  1679. 'muted' => 'muted',
  1680. ],
  1681. '/video',
  1682. ];
  1683. $this->assertHtml($expected, $result);
  1684. $result = $this->Html->media(
  1685. ['video.webm', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
  1686. ['pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.']
  1687. );
  1688. $expected = [
  1689. 'video' => ['poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'],
  1690. ['source' => ['src' => 'videos/video.webm', 'type' => 'video/webm']],
  1691. ['source' => ['src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;']],
  1692. 'Your browser does not support the HTML5 Video element.',
  1693. '/video',
  1694. ];
  1695. $this->assertHtml($expected, $result);
  1696. $result = $this->Html->media('video.ogv', ['tag' => 'video']);
  1697. $expected = ['video' => ['src' => 'files/video.ogv'], '/video'];
  1698. $this->assertHtml($expected, $result);
  1699. $result = $this->Html->media('audio.mp3');
  1700. $expected = ['audio' => ['src' => 'files/audio.mp3'], '/audio'];
  1701. $this->assertHtml($expected, $result);
  1702. $result = $this->Html->media(
  1703. [['src' => 'video.mov', 'type' => 'video/mp4'], 'video.webm']
  1704. );
  1705. $expected = [
  1706. '<video',
  1707. ['source' => ['src' => 'files/video.mov', 'type' => 'video/mp4']],
  1708. ['source' => ['src' => 'files/video.webm', 'type' => 'video/webm']],
  1709. '/video',
  1710. ];
  1711. $this->assertHtml($expected, $result);
  1712. $result = $this->Html->media(null, ['src' => 'video.webm']);
  1713. $expected = [
  1714. 'video' => ['src' => 'files/video.webm'],
  1715. '/video',
  1716. ];
  1717. $this->assertHtml($expected, $result);
  1718. }
  1719. /**
  1720. * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
  1721. */
  1722. public function testCssAndScriptWithSameName(): void
  1723. {
  1724. $result = $this->Html->css('foo');
  1725. $expected = [
  1726. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/'],
  1727. ];
  1728. $this->assertHtml($expected, $result);
  1729. $result = $this->Html->script('foo');
  1730. $expected = [
  1731. 'script' => ['src' => 'js/foo.js'],
  1732. ];
  1733. $this->assertHtml($expected, $result);
  1734. }
  1735. }