HtmlHelperTest.php 74 KB

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