HtmlHelperTest.php 73 KB

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