HtmlHelperTest.php 74 KB

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