HtmlHelperTest.php 74 KB

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