HtmlHelperTest.php 78 KB

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