HtmlHelperTest.php 79 KB

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