HtmlHelperTest.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  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. }