HtmlHelperTest.php 62 KB

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