HtmlHelperTest.php 73 KB

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