HtmlHelperTest.php 81 KB

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