HtmlHelperTest.php 73 KB

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