HtmlHelperTest.php 84 KB

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