HtmlHelperTest.php 84 KB

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