HtmlHelperTest.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Filesystem\File;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper\HtmlHelper;
  23. /**
  24. * HtmlHelperTest class
  25. */
  26. class HtmlHelperTest extends TestCase
  27. {
  28. /**
  29. * Regexp for CDATA start block
  30. *
  31. * @var string
  32. */
  33. public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  34. /**
  35. * Regexp for CDATA end block
  36. *
  37. * @var string
  38. */
  39. public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  40. /**
  41. * Helper to be tested
  42. *
  43. * @var \Cake\View\Helper\HtmlHelper
  44. */
  45. public $Html;
  46. /**
  47. * Mocked view
  48. *
  49. * @var \Cake\View\View|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. public $View;
  52. /**
  53. * setUp method
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. parent::setUp();
  60. $request = new ServerRequest([
  61. 'webroot' => '',
  62. ]);
  63. $this->View = $this->getMockBuilder('Cake\View\View')
  64. ->setMethods(['append'])
  65. ->setConstructorArgs([$request])
  66. ->getMock();
  67. $this->Html = new HtmlHelper($this->View);
  68. $this->loadPlugins(['TestTheme']);
  69. static::setAppNamespace();
  70. Configure::write('Asset.timestamp', false);
  71. }
  72. /**
  73. * tearDown method
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. parent::tearDown();
  80. Plugin::unload();
  81. unset($this->Html, $this->View);
  82. }
  83. /**
  84. * testDocType method
  85. *
  86. * @return void
  87. */
  88. public function testDocType()
  89. {
  90. $result = $this->Html->docType();
  91. $expected = '<!DOCTYPE html>';
  92. $this->assertEquals($expected, $result);
  93. $result = $this->Html->docType('html4-strict');
  94. $expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
  95. $this->assertEquals($expected, $result);
  96. $this->assertNull($this->Html->docType('non-existing-doctype'));
  97. }
  98. /**
  99. * testLink method
  100. *
  101. * @return void
  102. */
  103. public function testLink()
  104. {
  105. Router::connect('/:controller/:action/*');
  106. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', ''));
  107. $result = $this->Html->link('/home');
  108. $expected = ['a' => ['href' => '/home'], 'preg:/\/home/', '/a'];
  109. $this->assertHtml($expected, $result);
  110. $result = $this->Html->link(['action' => 'login', '<[You]>']);
  111. $expected = [
  112. 'a' => ['href' => '/login/%3C%5BYou%5D%3E'],
  113. 'preg:/\/login\/&lt;\[You\]&gt;/',
  114. '/a'
  115. ];
  116. $this->assertHtml($expected, $result);
  117. Router::reload();
  118. Router::connect('/:controller', ['action' => 'index']);
  119. Router::connect('/:controller/:action/*');
  120. $result = $this->Html->link('Posts', ['controller' => 'posts', 'action' => 'index', '_full' => true]);
  121. $expected = ['a' => ['href' => Router::fullBaseUrl() . '/posts'], 'Posts', '/a'];
  122. $this->assertHtml($expected, $result);
  123. $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
  124. $expected = [
  125. 'a' => ['href' => '/home', 'onclick' => 'if (confirm(&quot;Are you sure you want to do this?&quot;)) { return true; } return false;'],
  126. 'Home',
  127. '/a'
  128. ];
  129. $this->assertHtml($expected, $result);
  130. $this->Html->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { window.location="/";};']);
  131. $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
  132. $expected = [
  133. 'a' => ['href' => '/home', 'onclick' => 'preg:/if \(confirm\(&quot;Are you sure you want to do this\?&quot;\)\) \{ window\.location=&quot;\/&quot;;\};/'],
  134. 'Home',
  135. '/a'
  136. ];
  137. $this->assertHtml($expected, $result);
  138. $this->Html->setTemplates(['confirmJs' => '{{confirm}}']);
  139. $result = $this->Html->link('Home', '/home', ['escape' => false, 'confirm' => 'Confirm\'s "nightmares"']);
  140. $expected = [
  141. 'a' => ['href' => '/home', 'onclick' => 'if (confirm(&quot;Confirm&#039;s \&quot;nightmares\&quot;&quot;)) { return true; } return false;'],
  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. /**
  288. * testImageTag method
  289. *
  290. * @return void
  291. */
  292. public function testImageTag()
  293. {
  294. Router::connect('/:controller', ['action' => 'index']);
  295. Router::connect('/:controller/:action/*');
  296. $result = $this->Html->image('test.gif');
  297. $expected = ['img' => ['src' => 'img/test.gif', 'alt' => '']];
  298. $this->assertHtml($expected, $result);
  299. $result = $this->Html->image('http://google.com/logo.gif');
  300. $expected = ['img' => ['src' => 'http://google.com/logo.gif', 'alt' => '']];
  301. $this->assertHtml($expected, $result);
  302. $result = $this->Html->image('//google.com/logo.gif');
  303. $expected = ['img' => ['src' => '//google.com/logo.gif', 'alt' => '']];
  304. $this->assertHtml($expected, $result);
  305. $result = $this->Html->image(['controller' => 'test', 'action' => 'view', 1, '_ext' => 'gif']);
  306. $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
  307. $this->assertHtml($expected, $result);
  308. $result = $this->Html->image('/test/view/1.gif');
  309. $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
  310. $this->assertHtml($expected, $result);
  311. $result = $this->Html->image('cid:cakephp_logo');
  312. $expected = ['img' => ['src' => 'cid:cakephp_logo', 'alt' => '']];
  313. $this->assertHtml($expected, $result);
  314. $result = $this->Html->image('x:"><script>alert(1)</script>');
  315. $expected = ['img' => ['src' => 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', 'alt' => '']];
  316. $this->assertHtml($expected, $result);
  317. $result = $this->Html->image('//google.com/"><script>alert(1)</script>');
  318. $expected = ['img' => ['src' => '//google.com/&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;', 'alt' => '']];
  319. $this->assertHtml($expected, $result);
  320. }
  321. /**
  322. * Ensure that data URIs don't get base paths set.
  323. *
  324. * @return void
  325. */
  326. public function testImageDataUriBaseDir()
  327. {
  328. $request = $this->View->getRequest()
  329. ->withAttribute('base', 'subdir')
  330. ->withAttribute('webroot', 'subdir/');
  331. $this->View->setRequest($request);
  332. Router::pushRequest($request);
  333. $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
  334. '/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
  335. $result = $this->Html->image($data);
  336. $expected = ['img' => ['src' => $data, 'alt' => '']];
  337. $this->assertHtml($expected, $result);
  338. $data = 'data:image/png;base64,<evil>';
  339. $result = $this->Html->image($data);
  340. $expected = ['img' => ['src' => h($data), 'alt' => '']];
  341. $this->assertHtml($expected, $result);
  342. }
  343. /**
  344. * Test image() with query strings.
  345. *
  346. * @return void
  347. */
  348. public function testImageQueryString()
  349. {
  350. $result = $this->Html->image('test.gif?one=two&three=four');
  351. $expected = ['img' => ['src' => 'img/test.gif?one=two&amp;three=four', 'alt' => '']];
  352. $this->assertHtml($expected, $result);
  353. $result = $this->Html->image([
  354. 'controller' => 'images',
  355. 'action' => 'display',
  356. 'test',
  357. '?' => ['one' => 'two', 'three' => 'four']
  358. ]);
  359. $expected = ['img' => ['src' => '/images/display/test?one=two&amp;three=four', 'alt' => '']];
  360. $this->assertHtml($expected, $result);
  361. }
  362. /**
  363. * Test that image works with pathPrefix.
  364. *
  365. * @return void
  366. */
  367. public function testImagePathPrefix()
  368. {
  369. $result = $this->Html->image('test.gif', ['pathPrefix' => '/my/custom/path/']);
  370. $expected = ['img' => ['src' => '/my/custom/path/test.gif', 'alt' => '']];
  371. $this->assertHtml($expected, $result);
  372. $result = $this->Html->image('test.gif', ['pathPrefix' => 'http://cakephp.org/assets/img/']);
  373. $expected = ['img' => ['src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '']];
  374. $this->assertHtml($expected, $result);
  375. $result = $this->Html->image('test.gif', ['pathPrefix' => '//cakephp.org/assets/img/']);
  376. $expected = ['img' => ['src' => '//cakephp.org/assets/img/test.gif', 'alt' => '']];
  377. $this->assertHtml($expected, $result);
  378. $previousConfig = Configure::read('App.imageBaseUrl');
  379. Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
  380. $result = $this->Html->image('test.gif');
  381. $expected = ['img' => ['src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '']];
  382. $this->assertHtml($expected, $result);
  383. Configure::write('App.imageBaseUrl', $previousConfig);
  384. }
  385. /**
  386. * Test that image() works with fullBase and a webroot not equal to /
  387. *
  388. * @return void
  389. */
  390. public function testImageWithFullBase()
  391. {
  392. $result = $this->Html->image('test.gif', ['fullBase' => true]);
  393. $here = $this->Html->Url->build('/', 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('/', true);
  398. $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
  399. $this->assertHtml($expected, $result);
  400. $this->View->setRequest($this->View->getRequest()
  401. ->withAttribute('webroot', '/myproject/')
  402. ->withAttribute('base', '/myproject'));
  403. $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
  404. $here = $this->Html->Url->build('/', true);
  405. $expected = ['img' => ['src' => $here . 'myproject/img/sub/test.gif', 'alt' => '']];
  406. $this->assertHtml($expected, $result);
  407. }
  408. /**
  409. * test image() with Asset.timestamp
  410. *
  411. * @return void
  412. */
  413. public function testImageWithTimestampping()
  414. {
  415. Configure::write('Asset.timestamp', 'force');
  416. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
  417. $result = $this->Html->image('cake.icon.png');
  418. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  419. $this->assertHtml($expected, $result);
  420. Configure::write('debug', false);
  421. Configure::write('Asset.timestamp', 'force');
  422. $result = $this->Html->image('cake.icon.png');
  423. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  424. $this->assertHtml($expected, $result);
  425. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
  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. * @return void
  436. */
  437. public function testImageTagWithTheme()
  438. {
  439. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  440. $testfile = WWW_ROOT . 'test_theme/img/__cake_test_image.gif';
  441. $File = new File($testfile, true);
  442. Configure::write('Asset.timestamp', true);
  443. Configure::write('debug', true);
  444. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
  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. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
  455. $result = $this->Html->image('__cake_test_image.gif');
  456. $expected = [
  457. 'img' => [
  458. 'src' => 'preg:/\/testing\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  459. 'alt' => ''
  460. ]];
  461. $this->assertHtml($expected, $result);
  462. $File->delete();
  463. }
  464. /**
  465. * test theme assets in main webroot path
  466. *
  467. * @return void
  468. */
  469. public function testThemeAssetsInMainWebrootPath()
  470. {
  471. Configure::write('App.wwwRoot', TEST_APP . 'webroot/');
  472. $this->Html->Url->getView()->setTheme('TestTheme');
  473. $result = $this->Html->css('webroot_test');
  474. $expected = [
  475. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/webroot_test\.css/']
  476. ];
  477. $this->assertHtml($expected, $result);
  478. $this->Html->getView()->setTheme('TestTheme');
  479. $result = $this->Html->css('theme_webroot');
  480. $expected = [
  481. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/theme_webroot\.css/']
  482. ];
  483. $this->assertHtml($expected, $result);
  484. }
  485. /**
  486. * testStyle method
  487. *
  488. * @return void
  489. */
  490. public function testStyle()
  491. {
  492. $result = $this->Html->style(['display' => 'none', 'margin' => '10px']);
  493. $this->assertEquals('display:none; margin:10px;', $result);
  494. $result = $this->Html->style(['display' => 'none', 'margin' => '10px'], false);
  495. $this->assertEquals("display:none;\nmargin:10px;", $result);
  496. }
  497. /**
  498. * testCssLink method
  499. *
  500. * @return void
  501. */
  502. public function testCssLink()
  503. {
  504. $result = $this->Html->css('screen');
  505. $expected = [
  506. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/']
  507. ];
  508. $this->assertHtml($expected, $result);
  509. $result = $this->Html->css('screen.css', ['once' => false]);
  510. $this->assertHtml($expected, $result);
  511. $this->loadPlugins(['TestPlugin']);
  512. $result = $this->Html->css('TestPlugin.style', ['plugin' => false]);
  513. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  514. $this->assertHtml($expected, $result);
  515. Plugin::unload('TestPlugin');
  516. $result = $this->Html->css('my.css.library');
  517. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  518. $this->assertHtml($expected, $result);
  519. $result = $this->Html->css('screen.css?1234');
  520. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  521. $this->assertHtml($expected, $result);
  522. $result = $this->Html->css('screen.css?with=param&other=param');
  523. $expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
  524. $this->assertHtml($expected, $result);
  525. $result = $this->Html->css('x:"><script>alert(1)</script>');
  526. $expected['link']['href'] = 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
  527. $this->assertHtml($expected, $result);
  528. $result = $this->Html->css('http://whatever.com/screen.css?1234');
  529. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  530. $this->assertHtml($expected, $result);
  531. Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
  532. $result = $this->Html->css('cake.generic');
  533. $expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
  534. $this->assertHtml($expected, $result);
  535. $result = $this->Html->css('//example.com/css/cake.generic.css');
  536. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  537. $this->assertHtml($expected, $result);
  538. $result = explode("\n", trim($this->Html->css(['cake', 'vendor.generic'])));
  539. $expected['link']['href'] = 'preg:/.*css\/cake\.css/';
  540. $this->assertHtml($expected, $result[0]);
  541. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  542. $this->assertHtml($expected, $result[1]);
  543. $this->assertCount(2, $result);
  544. $this->View->expects($this->at(0))
  545. ->method('append')
  546. ->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
  547. $this->View->expects($this->at(1))
  548. ->method('append')
  549. ->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
  550. $result = $this->Html->css('css_in_head', ['block' => true]);
  551. $this->assertNull($result);
  552. $result = $this->Html->css('more_css_in_head', ['block' => true]);
  553. $this->assertNull($result);
  554. $result = $this->Html->css('import-screen', ['rel' => 'import']);
  555. $expected = [
  556. '<style',
  557. 'preg:/@import url\(.*css\/import-screen\.css\);/',
  558. '/style'
  559. ];
  560. $this->assertHtml($expected, $result);
  561. }
  562. /**
  563. * Test css() with once option.
  564. *
  565. * @return void
  566. */
  567. public function testCssLinkOnce()
  568. {
  569. $result = $this->Html->css('screen', ['once' => true]);
  570. $expected = [
  571. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/']
  572. ];
  573. $this->assertHtml($expected, $result);
  574. // Default is once=true
  575. $result = $this->Html->css('screen');
  576. $this->assertEquals('', $result);
  577. $result = $this->Html->css('screen', ['once' => false]);
  578. $expected = [
  579. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/']
  580. ];
  581. $this->assertHtml($expected, $result);
  582. }
  583. /**
  584. * testCssWithFullBase method
  585. *
  586. * @return void
  587. */
  588. public function testCssWithFullBase()
  589. {
  590. Configure::write('Asset.filter.css', false);
  591. $here = $this->Html->Url->build('/', true);
  592. $result = $this->Html->css('screen', ['fullBase' => true]);
  593. $expected = [
  594. 'link' => ['rel' => 'stylesheet', 'href' => $here . 'css/screen.css']
  595. ];
  596. $this->assertHtml($expected, $result);
  597. }
  598. /**
  599. * testPluginCssLink method
  600. *
  601. * @return void
  602. */
  603. public function testPluginCssLink()
  604. {
  605. $this->loadPlugins(['TestPlugin']);
  606. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  607. $expected = [
  608. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/']
  609. ];
  610. $this->assertHtml($expected, $result);
  611. $result = $this->Html->css('TestPlugin.test_plugin_asset.css', ['once' => false]);
  612. $this->assertHtml($expected, $result);
  613. $result = $this->Html->css('TestPlugin.my.css.library');
  614. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  615. $this->assertHtml($expected, $result);
  616. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  617. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  618. $this->assertHtml($expected, $result);
  619. $result = explode("\n", trim($this->Html->css(
  620. ['TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'],
  621. ['once' => false]
  622. )));
  623. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  624. $this->assertHtml($expected, $result[0]);
  625. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  626. $this->assertHtml($expected, $result[1]);
  627. $this->assertCount(2, $result);
  628. Plugin::unload('TestPlugin');
  629. }
  630. /**
  631. * test use of css() and timestamping
  632. *
  633. * @return void
  634. */
  635. public function testCssTimestamping()
  636. {
  637. Configure::write('debug', true);
  638. Configure::write('Asset.timestamp', true);
  639. $expected = [
  640. 'link' => ['rel' => 'stylesheet', 'href' => '']
  641. ];
  642. $result = $this->Html->css('cake.generic', ['once' => false]);
  643. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  644. $this->assertHtml($expected, $result);
  645. Configure::write('debug', false);
  646. $result = $this->Html->css('cake.generic', ['once' => false]);
  647. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  648. $this->assertHtml($expected, $result);
  649. Configure::write('Asset.timestamp', 'force');
  650. $result = $this->Html->css('cake.generic', ['once' => false]);
  651. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  652. $this->assertHtml($expected, $result);
  653. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
  654. $result = $this->Html->css('cake.generic', ['once' => false]);
  655. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  656. $this->assertHtml($expected, $result);
  657. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
  658. $result = $this->Html->css('cake.generic', ['once' => false]);
  659. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  660. $this->assertHtml($expected, $result);
  661. }
  662. /**
  663. * test use of css() and timestamping with plugin syntax
  664. *
  665. * @return void
  666. */
  667. public function testPluginCssTimestamping()
  668. {
  669. $this->loadPlugins(['TestPlugin']);
  670. Configure::write('debug', true);
  671. Configure::write('Asset.timestamp', true);
  672. $expected = [
  673. 'link' => ['rel' => 'stylesheet', 'href' => '']
  674. ];
  675. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  676. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  677. $this->assertHtml($expected, $result);
  678. Configure::write('debug', false);
  679. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  680. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  681. $this->assertHtml($expected, $result);
  682. Configure::write('Asset.timestamp', 'force');
  683. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  684. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  685. $this->assertHtml($expected, $result);
  686. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
  687. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  688. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  689. $this->assertHtml($expected, $result);
  690. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
  691. $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
  692. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  693. $this->assertHtml($expected, $result);
  694. Plugin::unload('TestPlugin');
  695. }
  696. /**
  697. * Resource names must be treated differently for css() and script()
  698. *
  699. * @return void
  700. */
  701. public function testBufferedCssAndScriptWithIdenticalResourceName()
  702. {
  703. $this->View->expects($this->at(0))
  704. ->method('append')
  705. ->with('css', $this->stringContains('test.min.css'));
  706. $this->View->expects($this->at(1))
  707. ->method('append')
  708. ->with('script', $this->stringContains('test.min.js'));
  709. $this->Html->css('test.min', ['block' => true]);
  710. $this->Html->script('test.min', ['block' => true]);
  711. }
  712. /**
  713. * test timestamp enforcement for script tags.
  714. *
  715. * @return void
  716. */
  717. public function testScriptTimestamping()
  718. {
  719. $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
  720. Configure::write('debug', true);
  721. Configure::write('Asset.timestamp', true);
  722. touch(WWW_ROOT . 'js/__cake_js_test.js');
  723. $timestamp = substr(strtotime('now'), 0, 8);
  724. $result = $this->Html->script('__cake_js_test', ['once' => false]);
  725. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  726. Configure::write('debug', false);
  727. Configure::write('Asset.timestamp', 'force');
  728. $result = $this->Html->script('__cake_js_test', ['once' => false]);
  729. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  730. unlink(WWW_ROOT . 'js/__cake_js_test.js');
  731. Configure::write('Asset.timestamp', false);
  732. }
  733. /**
  734. * test timestamp enforcement for script tags with plugin syntax.
  735. *
  736. * @return void
  737. */
  738. public function testPluginScriptTimestamping()
  739. {
  740. $this->loadPlugins(['TestPlugin']);
  741. $pluginPath = Plugin::path('TestPlugin');
  742. $pluginJsPath = $pluginPath . 'webroot/js';
  743. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  744. Configure::write('debug', true);
  745. Configure::write('Asset.timestamp', true);
  746. touch($pluginJsPath . DS . '__cake_js_test.js');
  747. $timestamp = substr(strtotime('now'), 0, 8);
  748. $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
  749. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  750. Configure::write('debug', false);
  751. Configure::write('Asset.timestamp', 'force');
  752. $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
  753. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  754. unlink($pluginJsPath . DS . '__cake_js_test.js');
  755. Configure::write('Asset.timestamp', false);
  756. Plugin::unload('TestPlugin');
  757. }
  758. /**
  759. * test that scripts added with uses() are only ever included once.
  760. * test script tag generation
  761. *
  762. * @return void
  763. */
  764. public function testScript()
  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->assertNotRegExp('/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 plugin scripts added with uses() are only ever included once.
  846. * test script tag generation with plugin syntax
  847. *
  848. * @return void
  849. */
  850. public function testPluginScript()
  851. {
  852. $this->loadPlugins(['TestPlugin']);
  853. $result = $this->Html->script('TestPlugin.foo');
  854. $expected = [
  855. 'script' => ['src' => 'test_plugin/js/foo.js']
  856. ];
  857. $this->assertHtml($expected, $result);
  858. $result = $this->Html->script(['TestPlugin.foobar', 'TestPlugin.bar']);
  859. $expected = [
  860. ['script' => ['src' => 'test_plugin/js/foobar.js']],
  861. '/script',
  862. ['script' => ['src' => 'test_plugin/js/bar.js']],
  863. '/script',
  864. ];
  865. $this->assertHtml($expected, $result);
  866. $result = $this->Html->script('TestPlugin.jquery-1.3');
  867. $expected = [
  868. 'script' => ['src' => 'test_plugin/js/jquery-1.3.js']
  869. ];
  870. $this->assertHtml($expected, $result);
  871. $result = $this->Html->script('TestPlugin.test.json');
  872. $expected = [
  873. 'script' => ['src' => 'test_plugin/js/test.json.js']
  874. ];
  875. $this->assertHtml($expected, $result);
  876. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  877. $expected = [
  878. 'script' => ['src' => 'test_plugin/jquery-1.3.2.js?someparam=foo']
  879. ];
  880. $this->assertHtml($expected, $result);
  881. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  882. $expected = [
  883. 'script' => ['src' => 'test_plugin/js/test.json.js?foo=bar']
  884. ];
  885. $this->assertHtml($expected, $result);
  886. $result = $this->Html->script('TestPlugin.foo');
  887. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  888. $result = $this->Html->script(['TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz']);
  889. $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
  890. $result = $this->Html->script('TestPlugin.foo', ['once' => false]);
  891. $this->assertNotNull($result);
  892. $result = $this->Html->script('TestPlugin.jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
  893. $expected = [
  894. 'script' => ['src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8']
  895. ];
  896. $this->assertHtml($expected, $result);
  897. Plugin::unload('TestPlugin');
  898. }
  899. /**
  900. * test that script() works with blocks.
  901. *
  902. * @return void
  903. */
  904. public function testScriptWithBlocks()
  905. {
  906. $this->View->expects($this->at(0))
  907. ->method('append')
  908. ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
  909. $this->View->expects($this->at(1))
  910. ->method('append')
  911. ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
  912. $result = $this->Html->script('script_in_head', ['block' => true]);
  913. $this->assertNull($result);
  914. $result = $this->Html->script('second_script', ['block' => 'headScripts']);
  915. $this->assertNull($result);
  916. }
  917. /**
  918. * testScriptWithFullBase method
  919. *
  920. * @return void
  921. */
  922. public function testScriptWithFullBase()
  923. {
  924. $here = $this->Html->Url->build('/', true);
  925. $result = $this->Html->script('foo', ['fullBase' => true]);
  926. $expected = [
  927. 'script' => ['src' => $here . 'js/foo.js']
  928. ];
  929. $this->assertHtml($expected, $result);
  930. $result = $this->Html->script(['foobar', 'bar'], ['fullBase' => true]);
  931. $expected = [
  932. ['script' => ['src' => $here . 'js/foobar.js']],
  933. '/script',
  934. ['script' => ['src' => $here . 'js/bar.js']],
  935. '/script',
  936. ];
  937. $this->assertHtml($expected, $result);
  938. }
  939. /**
  940. * test a script file in the webroot/theme dir.
  941. *
  942. * @return void
  943. */
  944. public function testScriptInTheme()
  945. {
  946. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  947. $testfile = WWW_ROOT . '/test_theme/js/__test_js.js';
  948. $File = new File($testfile, true);
  949. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
  950. $this->Html->Url->getView()->setTheme('TestTheme');
  951. $result = $this->Html->script('__test_js.js');
  952. $expected = [
  953. 'script' => ['src' => '/test_theme/js/__test_js.js']
  954. ];
  955. $this->assertHtml($expected, $result);
  956. $File->delete();
  957. }
  958. /**
  959. * test Script block generation
  960. *
  961. * @return void
  962. */
  963. public function testScriptBlock()
  964. {
  965. $result = $this->Html->scriptBlock('window.foo = 2;');
  966. $expected = [
  967. '<script',
  968. 'window.foo = 2;',
  969. '/script',
  970. ];
  971. $this->assertHtml($expected, $result);
  972. $result = $this->Html->scriptBlock('window.foo = 2;', ['type' => 'text/x-handlebars-template']);
  973. $expected = [
  974. 'script' => ['type' => 'text/x-handlebars-template'],
  975. 'window.foo = 2;',
  976. '/script',
  977. ];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => false]);
  980. $expected = [
  981. '<script',
  982. 'window.foo = 2;',
  983. '/script',
  984. ];
  985. $this->assertHtml($expected, $result);
  986. $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => true]);
  987. $expected = [
  988. '<script',
  989. $this->cDataStart,
  990. 'window.foo = 2;',
  991. $this->cDataEnd,
  992. '/script',
  993. ];
  994. $this->assertHtml($expected, $result);
  995. $this->View->expects($this->at(0))
  996. ->method('append')
  997. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  998. $this->View->expects($this->at(1))
  999. ->method('append')
  1000. ->with('scriptTop', $this->stringContains('alert('));
  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;', ['safe' => false, '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. * test script tag output buffering when using scriptStart() and scriptEnd();
  1015. *
  1016. * @return void
  1017. */
  1018. public function testScriptStartAndScriptEnd()
  1019. {
  1020. $result = $this->Html->scriptStart();
  1021. $this->assertNull($result);
  1022. echo 'this is some javascript';
  1023. $result = $this->Html->scriptEnd();
  1024. $expected = [
  1025. '<script',
  1026. 'this is some javascript',
  1027. '/script'
  1028. ];
  1029. $this->assertHtml($expected, $result);
  1030. $result = $this->Html->scriptStart(['safe' => false]);
  1031. $this->assertNull($result);
  1032. echo 'this is some javascript';
  1033. $result = $this->Html->scriptEnd();
  1034. $expected = [
  1035. '<script',
  1036. 'this is some javascript',
  1037. '/script'
  1038. ];
  1039. $this->assertHtml($expected, $result);
  1040. $result = $this->Html->scriptStart(['safe' => true]);
  1041. $this->assertNull($result);
  1042. echo 'this is some javascript';
  1043. $result = $this->Html->scriptEnd();
  1044. $expected = [
  1045. '<script',
  1046. $this->cDataStart,
  1047. 'this is some javascript',
  1048. $this->cDataEnd,
  1049. '/script'
  1050. ];
  1051. $this->assertHtml($expected, $result);
  1052. $result = $this->Html->scriptStart(['safe' => true, 'type' => 'text/x-handlebars-template']);
  1053. $this->assertNull($result);
  1054. echo 'this is some template';
  1055. $result = $this->Html->scriptEnd();
  1056. $expected = [
  1057. 'script' => ['type' => 'text/x-handlebars-template'],
  1058. $this->cDataStart,
  1059. 'this is some template',
  1060. $this->cDataEnd,
  1061. '/script'
  1062. ];
  1063. $this->assertHtml($expected, $result);
  1064. $this->View->expects($this->once())
  1065. ->method('append');
  1066. $result = $this->Html->scriptStart(['safe' => false, 'block' => true]);
  1067. $this->assertNull($result);
  1068. echo 'this is some javascript';
  1069. $result = $this->Html->scriptEnd();
  1070. $this->assertNull($result);
  1071. }
  1072. /**
  1073. * testCharsetTag method
  1074. *
  1075. * @return void
  1076. */
  1077. public function testCharsetTag()
  1078. {
  1079. Configure::write('App.encoding', null);
  1080. $result = $this->Html->charset();
  1081. $expected = ['meta' => ['charset' => 'utf-8']];
  1082. $this->assertHtml($expected, $result);
  1083. Configure::write('App.encoding', 'ISO-8859-1');
  1084. $result = $this->Html->charset();
  1085. $expected = ['meta' => ['charset' => 'iso-8859-1']];
  1086. $this->assertHtml($expected, $result);
  1087. $result = $this->Html->charset('UTF-7');
  1088. $expected = ['meta' => ['charset' => 'UTF-7']];
  1089. $this->assertHtml($expected, $result);
  1090. }
  1091. /**
  1092. * testGetCrumb and addCrumb method
  1093. *
  1094. * @group deprecated
  1095. * @return void
  1096. */
  1097. public function testBreadcrumb()
  1098. {
  1099. $this->deprecated(function () {
  1100. $this->assertNull($this->Html->getCrumbs());
  1101. $this->Html->addCrumb('First', '#first');
  1102. $this->Html->addCrumb('Second', '#second');
  1103. $this->Html->addCrumb('Third', '#third');
  1104. $result = $this->Html->getCrumbs();
  1105. $expected = [
  1106. ['a' => ['href' => '#first']],
  1107. 'First',
  1108. '/a',
  1109. '&raquo;',
  1110. ['a' => ['href' => '#second']],
  1111. 'Second',
  1112. '/a',
  1113. '&raquo;',
  1114. ['a' => ['href' => '#third']],
  1115. 'Third',
  1116. '/a',
  1117. ];
  1118. $this->assertHtml($expected, $result);
  1119. $result = $this->Html->getCrumbs(' &gt; ');
  1120. $expected = [
  1121. ['a' => ['href' => '#first']],
  1122. 'First',
  1123. '/a',
  1124. ' &gt; ',
  1125. ['a' => ['href' => '#second']],
  1126. 'Second',
  1127. '/a',
  1128. ' &gt; ',
  1129. ['a' => ['href' => '#third']],
  1130. 'Third',
  1131. '/a',
  1132. ];
  1133. $this->assertHtml($expected, $result);
  1134. $this->Html->addCrumb('Fourth', null);
  1135. $result = $this->Html->getCrumbs();
  1136. $expected = [
  1137. ['a' => ['href' => '#first']],
  1138. 'First',
  1139. '/a',
  1140. '&raquo;',
  1141. ['a' => ['href' => '#second']],
  1142. 'Second',
  1143. '/a',
  1144. '&raquo;',
  1145. ['a' => ['href' => '#third']],
  1146. 'Third',
  1147. '/a',
  1148. '&raquo;',
  1149. 'Fourth'
  1150. ];
  1151. $this->assertHtml($expected, $result);
  1152. $this->Html->addCrumb('Fifth', [
  1153. 'plugin' => false,
  1154. 'controller' => 'controller',
  1155. 'action' => 'action',
  1156. ]);
  1157. $result = $this->Html->getCrumbs('-', 'Start');
  1158. $expected = [
  1159. ['a' => ['href' => '/']],
  1160. 'Start',
  1161. '/a',
  1162. '-',
  1163. ['a' => ['href' => '#first']],
  1164. 'First',
  1165. '/a',
  1166. '-',
  1167. ['a' => ['href' => '#second']],
  1168. 'Second',
  1169. '/a',
  1170. '-',
  1171. ['a' => ['href' => '#third']],
  1172. 'Third',
  1173. '/a',
  1174. '-',
  1175. 'Fourth',
  1176. '-',
  1177. ['a' => ['href' => '/controller/action']],
  1178. 'Fifth',
  1179. '/a',
  1180. ];
  1181. $this->assertHtml($expected, $result);
  1182. });
  1183. }
  1184. /**
  1185. * Test the array form of $startText
  1186. *
  1187. * @group deprecated
  1188. * @return void
  1189. */
  1190. public function testGetCrumbFirstLink()
  1191. {
  1192. $this->deprecated(function () {
  1193. $result = $this->Html->getCrumbList([], 'Home');
  1194. $expected = [
  1195. '<ul',
  1196. ['li' => ['class' => 'first']],
  1197. ['a' => ['href' => '/']], 'Home', '/a',
  1198. '/li',
  1199. '/ul'
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. $this->Html->addCrumb('First', '#first');
  1203. $this->Html->addCrumb('Second', '#second');
  1204. $result = $this->Html->getCrumbs(' - ', ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
  1205. $expected = [
  1206. ['a' => ['href' => '/home']],
  1207. 'img' => ['src' => '/home.png'],
  1208. '/a',
  1209. ' - ',
  1210. ['a' => ['href' => '#first']],
  1211. 'First',
  1212. '/a',
  1213. ' - ',
  1214. ['a' => ['href' => '#second']],
  1215. 'Second',
  1216. '/a',
  1217. ];
  1218. $this->assertHtml($expected, $result);
  1219. });
  1220. }
  1221. /**
  1222. * testNestedList method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testNestedList()
  1227. {
  1228. $list = [
  1229. 'Item 1',
  1230. 'Item 2' => [
  1231. 'Item 2.1'
  1232. ],
  1233. 'Item 3',
  1234. 'Item 4' => [
  1235. 'Item 4.1',
  1236. 'Item 4.2',
  1237. 'Item 4.3' => [
  1238. 'Item 4.3.1',
  1239. 'Item 4.3.2'
  1240. ]
  1241. ],
  1242. 'Item 5' => [
  1243. 'Item 5.1',
  1244. 'Item 5.2'
  1245. ]
  1246. ];
  1247. $result = $this->Html->nestedList($list);
  1248. $expected = [
  1249. '<ul',
  1250. '<li', 'Item 1', '/li',
  1251. '<li', 'Item 2',
  1252. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1253. '/li',
  1254. '<li', 'Item 3', '/li',
  1255. '<li', 'Item 4',
  1256. '<ul',
  1257. '<li', 'Item 4.1', '/li',
  1258. '<li', 'Item 4.2', '/li',
  1259. '<li', 'Item 4.3',
  1260. '<ul',
  1261. '<li', 'Item 4.3.1', '/li',
  1262. '<li', 'Item 4.3.2', '/li',
  1263. '/ul',
  1264. '/li',
  1265. '/ul',
  1266. '/li',
  1267. '<li', 'Item 5',
  1268. '<ul',
  1269. '<li', 'Item 5.1', '/li',
  1270. '<li', 'Item 5.2', '/li',
  1271. '/ul',
  1272. '/li',
  1273. '/ul'
  1274. ];
  1275. $this->assertHtml($expected, $result);
  1276. $result = $this->Html->nestedList($list);
  1277. $this->assertHtml($expected, $result);
  1278. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1279. $expected = [
  1280. '<ol',
  1281. '<li', 'Item 1', '/li',
  1282. '<li', 'Item 2',
  1283. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1284. '/li',
  1285. '<li', 'Item 3', '/li',
  1286. '<li', 'Item 4',
  1287. '<ol',
  1288. '<li', 'Item 4.1', '/li',
  1289. '<li', 'Item 4.2', '/li',
  1290. '<li', 'Item 4.3',
  1291. '<ol',
  1292. '<li', 'Item 4.3.1', '/li',
  1293. '<li', 'Item 4.3.2', '/li',
  1294. '/ol',
  1295. '/li',
  1296. '/ol',
  1297. '/li',
  1298. '<li', 'Item 5',
  1299. '<ol',
  1300. '<li', 'Item 5.1', '/li',
  1301. '<li', 'Item 5.2', '/li',
  1302. '/ol',
  1303. '/li',
  1304. '/ol'
  1305. ];
  1306. $this->assertHtml($expected, $result);
  1307. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1308. $this->assertHtml($expected, $result);
  1309. $result = $this->Html->nestedList($list, ['class' => 'list']);
  1310. $expected = [
  1311. ['ul' => ['class' => 'list']],
  1312. '<li', 'Item 1', '/li',
  1313. '<li', 'Item 2',
  1314. ['ul' => ['class' => 'list']], '<li', 'Item 2.1', '/li', '/ul',
  1315. '/li',
  1316. '<li', 'Item 3', '/li',
  1317. '<li', 'Item 4',
  1318. ['ul' => ['class' => 'list']],
  1319. '<li', 'Item 4.1', '/li',
  1320. '<li', 'Item 4.2', '/li',
  1321. '<li', 'Item 4.3',
  1322. ['ul' => ['class' => 'list']],
  1323. '<li', 'Item 4.3.1', '/li',
  1324. '<li', 'Item 4.3.2', '/li',
  1325. '/ul',
  1326. '/li',
  1327. '/ul',
  1328. '/li',
  1329. '<li', 'Item 5',
  1330. ['ul' => ['class' => 'list']],
  1331. '<li', 'Item 5.1', '/li',
  1332. '<li', 'Item 5.2', '/li',
  1333. '/ul',
  1334. '/li',
  1335. '/ul'
  1336. ];
  1337. $this->assertHtml($expected, $result);
  1338. $result = $this->Html->nestedList($list, [], ['class' => 'item']);
  1339. $expected = [
  1340. '<ul',
  1341. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1342. ['li' => ['class' => 'item']], 'Item 2',
  1343. '<ul', ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1344. '/li',
  1345. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1346. ['li' => ['class' => 'item']], 'Item 4',
  1347. '<ul',
  1348. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1349. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1350. ['li' => ['class' => 'item']], 'Item 4.3',
  1351. '<ul',
  1352. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1353. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1354. '/ul',
  1355. '/li',
  1356. '/ul',
  1357. '/li',
  1358. ['li' => ['class' => 'item']], 'Item 5',
  1359. '<ul',
  1360. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1361. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1362. '/ul',
  1363. '/li',
  1364. '/ul'
  1365. ];
  1366. $this->assertHtml($expected, $result);
  1367. $result = $this->Html->nestedList($list, [], ['even' => 'even', 'odd' => 'odd']);
  1368. $expected = [
  1369. '<ul',
  1370. ['li' => ['class' => 'odd']], 'Item 1', '/li',
  1371. ['li' => ['class' => 'even']], 'Item 2',
  1372. '<ul', ['li' => ['class' => 'odd']], 'Item 2.1', '/li', '/ul',
  1373. '/li',
  1374. ['li' => ['class' => 'odd']], 'Item 3', '/li',
  1375. ['li' => ['class' => 'even']], 'Item 4',
  1376. '<ul',
  1377. ['li' => ['class' => 'odd']], 'Item 4.1', '/li',
  1378. ['li' => ['class' => 'even']], 'Item 4.2', '/li',
  1379. ['li' => ['class' => 'odd']], 'Item 4.3',
  1380. '<ul',
  1381. ['li' => ['class' => 'odd']], 'Item 4.3.1', '/li',
  1382. ['li' => ['class' => 'even']], 'Item 4.3.2', '/li',
  1383. '/ul',
  1384. '/li',
  1385. '/ul',
  1386. '/li',
  1387. ['li' => ['class' => 'odd']], 'Item 5',
  1388. '<ul',
  1389. ['li' => ['class' => 'odd']], 'Item 5.1', '/li',
  1390. ['li' => ['class' => 'even']], 'Item 5.2', '/li',
  1391. '/ul',
  1392. '/li',
  1393. '/ul'
  1394. ];
  1395. $this->assertHtml($expected, $result);
  1396. $result = $this->Html->nestedList($list, ['class' => 'list'], ['class' => 'item']);
  1397. $expected = [
  1398. ['ul' => ['class' => 'list']],
  1399. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1400. ['li' => ['class' => 'item']], 'Item 2',
  1401. ['ul' => ['class' => 'list']], ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1402. '/li',
  1403. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1404. ['li' => ['class' => 'item']], 'Item 4',
  1405. ['ul' => ['class' => 'list']],
  1406. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1407. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1408. ['li' => ['class' => 'item']], 'Item 4.3',
  1409. ['ul' => ['class' => 'list']],
  1410. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1411. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1412. '/ul',
  1413. '/li',
  1414. '/ul',
  1415. '/li',
  1416. ['li' => ['class' => 'item']], 'Item 5',
  1417. ['ul' => ['class' => 'list']],
  1418. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1419. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1420. '/ul',
  1421. '/li',
  1422. '/ul'
  1423. ];
  1424. $this->assertHtml($expected, $result);
  1425. }
  1426. /**
  1427. * testMeta method
  1428. *
  1429. * @return void
  1430. */
  1431. public function testMeta()
  1432. {
  1433. Router::connect('/:controller', ['action' => 'index']);
  1434. $result = $this->Html->meta('this is an rss feed', ['controller' => 'posts', '_ext' => 'rss']);
  1435. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1436. $this->assertHtml($expected, $result);
  1437. $result = $this->Html->meta('rss', ['controller' => 'posts', '_ext' => 'rss'], ['title' => 'this is an rss feed']);
  1438. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1439. $this->assertHtml($expected, $result);
  1440. $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml']);
  1441. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1442. $this->assertHtml($expected, $result);
  1443. $result = $this->Html->meta('non-existing');
  1444. $expected = ['<meta'];
  1445. $this->assertHtml($expected, $result);
  1446. $result = $this->Html->meta('non-existing', 'some content');
  1447. $expected = ['meta' => ['name' => 'non-existing', 'content' => 'some content']];
  1448. $this->assertHtml($expected, $result);
  1449. $result = $this->Html->meta('non-existing', '/posts.xpp', ['type' => 'atom']);
  1450. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing']];
  1451. $this->assertHtml($expected, $result);
  1452. $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml'], ['link' => '/articles.rss']);
  1453. $expected = ['link' => ['href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1454. $this->assertHtml($expected, $result);
  1455. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1456. $expected = ['meta' => ['name' => 'keywords', 'content' => 'these, are, some, meta, keywords']];
  1457. $this->assertHtml($expected, $result);
  1458. $result = $this->Html->meta('description', 'this is the meta description');
  1459. $expected = ['meta' => ['name' => 'description', 'content' => 'this is the meta description']];
  1460. $this->assertHtml($expected, $result);
  1461. $result = $this->Html->meta('robots', 'ALL');
  1462. $expected = ['meta' => ['name' => 'robots', 'content' => 'ALL']];
  1463. $this->assertHtml($expected, $result);
  1464. $result = $this->Html->meta('viewport', 'width=device-width');
  1465. $expected = [
  1466. 'meta' => ['name' => 'viewport', 'content' => 'width=device-width']
  1467. ];
  1468. $this->assertHtml($expected, $result);
  1469. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
  1470. $expected = [
  1471. 'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP']
  1472. ];
  1473. $this->assertHtml($expected, $result);
  1474. $result = $this->Html->meta(['link' => 'http://example.com/manifest', 'rel' => 'manifest']);
  1475. $expected = [
  1476. 'link' => ['href' => 'http://example.com/manifest', 'rel' => 'manifest']
  1477. ];
  1478. $this->assertHtml($expected, $result);
  1479. }
  1480. /**
  1481. * @return array
  1482. */
  1483. public function dataMetaLinksProvider()
  1484. {
  1485. return [
  1486. ['canonical', ['controller' => 'posts', 'action' => 'show'], '/posts/show'],
  1487. ['first', ['controller' => 'posts', 'action' => 'index'], '/posts'],
  1488. ['last', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 10]], '/posts?page=10'],
  1489. ['prev', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 4]], '/posts?page=4'],
  1490. ['next', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 6]], '/posts?page=6']
  1491. ];
  1492. }
  1493. /**
  1494. * test canonical and pagination meta links
  1495. *
  1496. * @param string $type
  1497. * @param array $url
  1498. * @param string $expectedUrl
  1499. * @dataProvider dataMetaLinksProvider
  1500. */
  1501. public function testMetaLinks($type, array $url, $expectedUrl)
  1502. {
  1503. $result = $this->Html->meta($type, $url);
  1504. $expected = ['link' => ['href' => $expectedUrl, 'rel' => $type]];
  1505. $this->assertHtml($expected, $result);
  1506. }
  1507. /**
  1508. * Test generating favicon's with meta()
  1509. *
  1510. * @return void
  1511. */
  1512. public function testMetaIcon()
  1513. {
  1514. $result = $this->Html->meta('icon', 'favicon.ico');
  1515. $expected = [
  1516. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1517. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1518. ];
  1519. $this->assertHtml($expected, $result);
  1520. $result = $this->Html->meta('icon');
  1521. $expected = [
  1522. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1523. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1524. ];
  1525. $this->assertHtml($expected, $result);
  1526. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1527. $url = '/favicon.png?one=two&amp;three=four';
  1528. $expected = [
  1529. 'link' => [
  1530. 'href' => $url,
  1531. 'type' => 'image/x-icon',
  1532. 'rel' => 'icon'
  1533. ],
  1534. [
  1535. 'link' => [
  1536. 'href' => $url,
  1537. 'type' => 'image/x-icon',
  1538. 'rel' => 'shortcut icon'
  1539. ]
  1540. ]
  1541. ];
  1542. $this->assertHtml($expected, $result);
  1543. $result = $this->Html->meta('icon', 'x:"><script>alert(1)</script>');
  1544. $url = 'x:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
  1545. $expected = [
  1546. 'link' => [
  1547. 'href' => $url,
  1548. 'type' => 'image/x-icon',
  1549. 'rel' => 'icon'
  1550. ],
  1551. [
  1552. 'link' => [
  1553. 'href' => $url,
  1554. 'type' => 'image/x-icon',
  1555. 'rel' => 'shortcut icon'
  1556. ]
  1557. ]
  1558. ];
  1559. $this->assertHtml($expected, $result);
  1560. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
  1561. $result = $this->Html->meta('icon');
  1562. $expected = [
  1563. 'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1564. ['link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1565. ];
  1566. $this->assertHtml($expected, $result);
  1567. }
  1568. /**
  1569. * Test generating favicon's with meta() with theme
  1570. *
  1571. * @return void
  1572. */
  1573. public function testMetaIconWithTheme()
  1574. {
  1575. $this->Html->Url->getView()->setTheme('TestTheme');
  1576. $result = $this->Html->meta('icon', 'favicon.ico');
  1577. $expected = [
  1578. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1579. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1580. ];
  1581. $this->assertHtml($expected, $result);
  1582. $result = $this->Html->meta('icon');
  1583. $expected = [
  1584. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1585. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1586. ];
  1587. $this->assertHtml($expected, $result);
  1588. $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
  1589. $result = $this->Html->meta('icon');
  1590. $expected = [
  1591. 'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1592. ['link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1593. ];
  1594. $this->assertHtml($expected, $result);
  1595. }
  1596. /**
  1597. * Test the inline and block options for meta()
  1598. *
  1599. * @return void
  1600. */
  1601. public function testMetaWithBlocks()
  1602. {
  1603. $this->View->expects($this->at(0))
  1604. ->method('append')
  1605. ->with('meta', $this->stringContains('robots'));
  1606. $this->View->expects($this->at(1))
  1607. ->method('append')
  1608. ->with('metaTags', $this->stringContains('favicon.ico'));
  1609. $result = $this->Html->meta('robots', 'ALL', ['block' => true]);
  1610. $this->assertNull($result);
  1611. $result = $this->Html->meta('icon', 'favicon.ico', ['block' => 'metaTags']);
  1612. $this->assertNull($result);
  1613. }
  1614. /**
  1615. * Test meta() with custom tag and block argument
  1616. */
  1617. public function testMetaCustomWithBlock()
  1618. {
  1619. $this->View->expects($this->at(0))
  1620. ->method('append')
  1621. ->with('meta', $this->stringContains('og:site_name'));
  1622. $this->View->expects($this->at(1))
  1623. ->method('append')
  1624. ->with('meta', $this->stringContains('og:description'));
  1625. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP', 'block' => true]);
  1626. $this->assertNull($result, 'compact style should work');
  1627. $result = $this->Html->meta(['property' => 'og:description', 'content' => 'CakePHP'], null, ['block' => true]);
  1628. $this->assertNull($result, 'backwards compat style should work.');
  1629. }
  1630. /**
  1631. * testTableHeaders method
  1632. *
  1633. * @return void
  1634. */
  1635. public function testTableHeaders()
  1636. {
  1637. $result = $this->Html->tableHeaders(['ID', 'Name', 'Date']);
  1638. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1639. $this->assertHtml($expected, $result);
  1640. $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight']], 'Date']);
  1641. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1642. $this->assertHtml($expected, $result);
  1643. $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1644. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1645. $this->assertHtml($expected, $result);
  1646. $result = $this->Html->tableHeaders(['ID', ['Name' => []], 'Date']);
  1647. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1648. $this->assertHtml($expected, $result);
  1649. }
  1650. /**
  1651. * testTableCells method
  1652. *
  1653. * @return void
  1654. */
  1655. public function testTableCells()
  1656. {
  1657. $tr = [
  1658. 'td content 1',
  1659. ['td content 2', ['width' => '100px']],
  1660. ['td content 3', ['width' => '100px']]
  1661. ];
  1662. $result = $this->Html->tableCells($tr);
  1663. $expected = [
  1664. '<tr',
  1665. '<td', 'td content 1', '/td',
  1666. ['td' => ['width' => '100px']], 'td content 2', '/td',
  1667. ['td' => ['width' => 'preg:/100px/']], 'td content 3', '/td',
  1668. '/tr'
  1669. ];
  1670. $this->assertHtml($expected, $result);
  1671. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1672. $result = $this->Html->tableCells($tr, null, null, true);
  1673. $expected = [
  1674. '<tr',
  1675. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1676. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1677. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1678. '/tr'
  1679. ];
  1680. $this->assertHtml($expected, $result);
  1681. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1682. $result = $this->Html->tableCells($tr, true);
  1683. $expected = [
  1684. '<tr',
  1685. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1686. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1687. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1688. '/tr'
  1689. ];
  1690. $this->assertHtml($expected, $result);
  1691. $tr = [
  1692. ['td content 1', 'td content 2', 'td content 3'],
  1693. ['td content 1', 'td content 2', 'td content 3'],
  1694. ['td content 1', 'td content 2', 'td content 3']
  1695. ];
  1696. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1697. $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>";
  1698. $this->assertEquals($expected, $result);
  1699. $tr = [
  1700. ['td content 1', 'td content 2', 'td content 3'],
  1701. ['td content 1', 'td content 2', 'td content 3'],
  1702. ['td content 1', 'td content 2', 'td content 3'],
  1703. ['td content 1', 'td content 2', 'td content 3']
  1704. ];
  1705. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1706. $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>";
  1707. $this->assertEquals($expected, $result);
  1708. $tr = [
  1709. ['td content 1', 'td content 2', 'td content 3'],
  1710. ['td content 1', 'td content 2', 'td content 3'],
  1711. ['td content 1', 'td content 2', 'td content 3']
  1712. ];
  1713. $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1714. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
  1715. $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>";
  1716. $this->assertEquals($expected, $result);
  1717. $tr = [
  1718. 'td content 1',
  1719. 'td content 2',
  1720. ['td content 3', ['class' => 'foo']]
  1721. ];
  1722. $result = $this->Html->tableCells($tr, null, null, true);
  1723. $expected = [
  1724. '<tr',
  1725. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1726. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1727. ['td' => ['class' => 'foo column-3']], 'td content 3', '/td',
  1728. '/tr'
  1729. ];
  1730. $this->assertHtml($expected, $result);
  1731. }
  1732. /**
  1733. * testTag method
  1734. *
  1735. * @return void
  1736. */
  1737. public function testTag()
  1738. {
  1739. $result = $this->Html->tag('div');
  1740. $this->assertHtml('<div', $result);
  1741. $result = $this->Html->tag('div', 'text');
  1742. $this->assertHtml(['<div', 'text', '/div'], $result);
  1743. $result = $this->Html->tag('div', '<text>', ['class' => 'class-name', 'escape' => true]);
  1744. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1745. $this->assertHtml($expected, $result);
  1746. $result = $this->Html->tag(false, '<em>stuff</em>');
  1747. $this->assertEquals('<em>stuff</em>', $result);
  1748. $result = $this->Html->tag(null, '<em>stuff</em>');
  1749. $this->assertEquals('<em>stuff</em>', $result);
  1750. $result = $this->Html->tag('', '<em>stuff</em>');
  1751. $this->assertEquals('<em>stuff</em>', $result);
  1752. }
  1753. /**
  1754. * testDiv method
  1755. *
  1756. * @return void
  1757. */
  1758. public function testDiv()
  1759. {
  1760. $result = $this->Html->div('class-name');
  1761. $expected = ['div' => ['class' => 'class-name']];
  1762. $this->assertHtml($expected, $result);
  1763. $result = $this->Html->div('class-name', 'text');
  1764. $expected = ['div' => ['class' => 'class-name'], 'text', '/div'];
  1765. $this->assertHtml($expected, $result);
  1766. $result = $this->Html->div('class-name', '<text>', ['escape' => true]);
  1767. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1768. $this->assertHtml($expected, $result);
  1769. $evilKey = "><script>alert(1)</script>";
  1770. $options = [$evilKey => 'some value'];
  1771. $result = $this->Html->div('class-name', '', $options);
  1772. $expected = '<div &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value" class="class-name"></div>';
  1773. $this->assertEquals($expected, $result);
  1774. }
  1775. /**
  1776. * testPara method
  1777. *
  1778. * @return void
  1779. */
  1780. public function testPara()
  1781. {
  1782. $result = $this->Html->para('class-name', '');
  1783. $expected = ['p' => ['class' => 'class-name']];
  1784. $this->assertHtml($expected, $result);
  1785. $result = $this->Html->para('class-name', 'text');
  1786. $expected = ['p' => ['class' => 'class-name'], 'text', '/p'];
  1787. $this->assertHtml($expected, $result);
  1788. $result = $this->Html->para('class-name', '<text>', ['escape' => true]);
  1789. $expected = ['p' => ['class' => 'class-name'], '&lt;text&gt;', '/p'];
  1790. $this->assertHtml($expected, $result);
  1791. $result = $this->Html->para('class-name', 'text"', ['escape' => false]);
  1792. $expected = ['p' => ['class' => 'class-name'], 'text"', '/p'];
  1793. $this->assertHtml($expected, $result);
  1794. }
  1795. /**
  1796. * testMedia method
  1797. *
  1798. * @return void
  1799. */
  1800. public function testMedia()
  1801. {
  1802. $result = $this->Html->media('video.webm');
  1803. $expected = ['video' => ['src' => 'files/video.webm'], '/video'];
  1804. $this->assertHtml($expected, $result);
  1805. $result = $this->Html->media('video.webm', [
  1806. 'text' => 'Your browser does not support the HTML5 Video element.'
  1807. ]);
  1808. $expected = ['video' => ['src' => 'files/video.webm'], 'Your browser does not support the HTML5 Video element.', '/video'];
  1809. $this->assertHtml($expected, $result);
  1810. $result = $this->Html->media('video.webm', ['autoload', 'muted' => 'muted']);
  1811. $expected = [
  1812. 'video' => [
  1813. 'src' => 'files/video.webm',
  1814. 'autoload' => 'autoload',
  1815. 'muted' => 'muted'
  1816. ],
  1817. '/video'
  1818. ];
  1819. $this->assertHtml($expected, $result);
  1820. $result = $this->Html->media(
  1821. ['video.webm', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
  1822. ['pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.']
  1823. );
  1824. $expected = [
  1825. 'video' => ['poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'],
  1826. ['source' => ['src' => 'videos/video.webm', 'type' => 'video/webm']],
  1827. ['source' => ['src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;']],
  1828. 'Your browser does not support the HTML5 Video element.',
  1829. '/video'
  1830. ];
  1831. $this->assertHtml($expected, $result);
  1832. $result = $this->Html->media('video.ogv', ['tag' => 'video']);
  1833. $expected = ['video' => ['src' => 'files/video.ogv'], '/video'];
  1834. $this->assertHtml($expected, $result);
  1835. $result = $this->Html->media('audio.mp3');
  1836. $expected = ['audio' => ['src' => 'files/audio.mp3'], '/audio'];
  1837. $this->assertHtml($expected, $result);
  1838. $result = $this->Html->media(
  1839. [['src' => 'video.mov', 'type' => 'video/mp4'], 'video.webm']
  1840. );
  1841. $expected = [
  1842. '<video',
  1843. ['source' => ['src' => 'files/video.mov', 'type' => 'video/mp4']],
  1844. ['source' => ['src' => 'files/video.webm', 'type' => 'video/webm']],
  1845. '/video'
  1846. ];
  1847. $this->assertHtml($expected, $result);
  1848. $result = $this->Html->media(null, ['src' => 'video.webm']);
  1849. $expected = [
  1850. 'video' => ['src' => 'files/video.webm'],
  1851. '/video'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * testCrumbList method
  1857. *
  1858. * @group deprecated
  1859. * @return void
  1860. */
  1861. public function testCrumbList()
  1862. {
  1863. $this->deprecated(function () {
  1864. $this->assertNull($this->Html->getCrumbList());
  1865. $this->Html->addCrumb('Home', '/', ['class' => 'home']);
  1866. $this->Html->addCrumb('Some page', '/some_page');
  1867. $this->Html->addCrumb('Another page');
  1868. $result = $this->Html->getCrumbList(
  1869. ['class' => 'breadcrumbs']
  1870. );
  1871. $expected = [
  1872. ['ul' => ['class' => 'breadcrumbs']],
  1873. ['li' => ['class' => 'first']],
  1874. ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
  1875. '/li',
  1876. '<li',
  1877. ['a' => ['href' => '/some_page']], 'Some page', '/a',
  1878. '/li',
  1879. ['li' => ['class' => 'last']],
  1880. 'Another page',
  1881. '/li',
  1882. '/ul'
  1883. ];
  1884. $this->assertHtml($expected, $result);
  1885. });
  1886. }
  1887. /**
  1888. * Test getCrumbList startText
  1889. *
  1890. * @group deprecated
  1891. * @return void
  1892. */
  1893. public function testCrumbListFirstLink()
  1894. {
  1895. $this->deprecated(function () {
  1896. $this->Html->addCrumb('First', '#first');
  1897. $this->Html->addCrumb('Second', '#second');
  1898. $result = $this->Html->getCrumbList([], 'Home');
  1899. $expected = [
  1900. '<ul',
  1901. ['li' => ['class' => 'first']],
  1902. ['a' => ['href' => '/']], 'Home', '/a',
  1903. '/li',
  1904. '<li',
  1905. ['a' => ['href' => '#first']], 'First', '/a',
  1906. '/li',
  1907. ['li' => ['class' => 'last']],
  1908. ['a' => ['href' => '#second']], 'Second', '/a',
  1909. '/li',
  1910. '/ul'
  1911. ];
  1912. $this->assertHtml($expected, $result);
  1913. $result = $this->Html->getCrumbList([], ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
  1914. $expected = [
  1915. '<ul',
  1916. ['li' => ['class' => 'first']],
  1917. ['a' => ['href' => '/home']], 'img' => ['src' => '/home.png'], '/a',
  1918. '/li',
  1919. '<li',
  1920. ['a' => ['href' => '#first']], 'First', '/a',
  1921. '/li',
  1922. ['li' => ['class' => 'last']],
  1923. ['a' => ['href' => '#second']], 'Second', '/a',
  1924. '/li',
  1925. '/ul'
  1926. ];
  1927. $this->assertHtml($expected, $result);
  1928. });
  1929. }
  1930. /**
  1931. * test getCrumbList() in Twitter Bootstrap style.
  1932. *
  1933. * @group deprecated
  1934. * @return void
  1935. */
  1936. public function testCrumbListBootstrapStyle()
  1937. {
  1938. $this->deprecated(function () {
  1939. $this->Html->addCrumb('Home', '/', ['class' => 'home']);
  1940. $this->Html->addCrumb('Library', '/lib');
  1941. $this->Html->addCrumb('Data');
  1942. $result = $this->Html->getCrumbList([
  1943. 'class' => 'breadcrumb',
  1944. 'separator' => '<span class="divider">-</span>',
  1945. 'firstClass' => false,
  1946. 'lastClass' => 'active'
  1947. ]);
  1948. $expected = [
  1949. ['ul' => ['class' => 'breadcrumb']],
  1950. '<li',
  1951. ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
  1952. ['span' => ['class' => 'divider']], '-', '/span',
  1953. '/li',
  1954. '<li',
  1955. ['a' => ['href' => '/lib']], 'Library', '/a',
  1956. ['span' => ['class' => 'divider']], '-', '/span',
  1957. '/li',
  1958. ['li' => ['class' => 'active']], 'Data', '/li',
  1959. '/ul'
  1960. ];
  1961. $this->assertHtml($expected, $result);
  1962. });
  1963. }
  1964. /**
  1965. * Test GetCrumbList using style of Zurb Foundation.
  1966. *
  1967. * @group deprecated
  1968. * @return void
  1969. */
  1970. public function testCrumbListZurbStyle()
  1971. {
  1972. $this->deprecated(function () {
  1973. $this->Html->addCrumb('Home', '#');
  1974. $this->Html->addCrumb('Features', '#');
  1975. $this->Html->addCrumb('Gene Splicing', '#');
  1976. $this->Html->addCrumb('Home', '#');
  1977. $result = $this->Html->getCrumbList(
  1978. ['class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current']
  1979. );
  1980. $expected = [
  1981. ['ul' => ['class' => 'breadcrumbs']],
  1982. '<li',
  1983. ['a' => ['href' => '#']], 'Home', '/a',
  1984. '/li',
  1985. '<li',
  1986. ['a' => ['href' => '#']], 'Features', '/a',
  1987. '/li',
  1988. '<li',
  1989. ['a' => ['href' => '#']], 'Gene Splicing', '/a',
  1990. '/li',
  1991. ['li' => ['class' => 'current']],
  1992. ['a' => ['href' => '#']], 'Home', '/a',
  1993. '/li',
  1994. '/ul'
  1995. ];
  1996. $this->assertHtml($expected, $result, true);
  1997. });
  1998. }
  1999. /**
  2000. * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
  2001. *
  2002. * @return void
  2003. */
  2004. public function testCssAndScriptWithSameName()
  2005. {
  2006. $result = $this->Html->css('foo');
  2007. $expected = [
  2008. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/']
  2009. ];
  2010. $this->assertHtml($expected, $result);
  2011. $result = $this->Html->script('foo');
  2012. $expected = [
  2013. 'script' => ['src' => 'js/foo.js']
  2014. ];
  2015. $this->assertHtml($expected, $result);
  2016. }
  2017. }