HtmlHelperTest.php 72 KB

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