HtmlHelperTest.php 74 KB

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