HtmlHelperTest.php 82 KB

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