HtmlHelperTest.php 76 KB

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