HtmlHelperTest.php 75 KB

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