HtmlHelperTest.php 82 KB

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