HtmlHelperTest.php 74 KB

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