HtmlHelperTest.php 61 KB

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