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