HtmlHelperTest.php 82 KB

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