HtmlHelperTest.php 80 KB

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