HtmlHelperTest.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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('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', false);
  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', true);
  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(array('display' => 'none', 'margin' => '10px'));
  422. $expected = 'display:none; margin:10px;';
  423. $this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
  424. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
  425. $lines = explode("\n", $result);
  426. $this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
  427. $this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
  428. }
  429. /**
  430. * testCssLink method
  431. *
  432. * @return void
  433. */
  434. public function testCssLink() {
  435. $result = $this->Html->css('screen');
  436. $expected = array(
  437. 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/')
  438. );
  439. $this->assertTags($result, $expected);
  440. $result = $this->Html->css('screen.css');
  441. $this->assertTags($result, $expected);
  442. Plugin::load('TestPlugin');
  443. $result = $this->Html->css('TestPlugin.style', array('plugin' => false));
  444. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  445. $this->assertTags($result, $expected);
  446. Plugin::unload('TestPlugin');
  447. $result = $this->Html->css('my.css.library');
  448. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  449. $this->assertTags($result, $expected);
  450. $result = $this->Html->css('screen.css?1234');
  451. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  452. $this->assertTags($result, $expected);
  453. $result = $this->Html->css('screen.css?with=param&other=param');
  454. $expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
  455. $this->assertTags($result, $expected);
  456. $result = $this->Html->css('http://whatever.com/screen.css?1234');
  457. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  458. $this->assertTags($result, $expected);
  459. Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
  460. $result = $this->Html->css('cake.generic');
  461. $expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
  462. $this->assertTags($result, $expected);
  463. $result = $this->Html->css('//example.com/css/cake.generic.css');
  464. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  465. $this->assertTags($result, $expected);
  466. $result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
  467. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  468. $this->assertTags($result[0], $expected);
  469. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  470. $this->assertTags($result[1], $expected);
  471. $this->assertEquals(2, count($result));
  472. $this->View->expects($this->at(0))
  473. ->method('append')
  474. ->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
  475. $this->View->expects($this->at(1))
  476. ->method('append')
  477. ->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
  478. $result = $this->Html->css('css_in_head', array('block' => true));
  479. $this->assertNull($result);
  480. $result = $this->Html->css('more_css_in_head', array('block' => true));
  481. $this->assertNull($result);
  482. $result = $this->Html->css('screen', array('rel' => 'import'));
  483. $expected = array(
  484. '<style',
  485. 'preg:/@import url\(.*css\/screen\.css\);/',
  486. '/style'
  487. );
  488. $this->assertTags($result, $expected);
  489. }
  490. /**
  491. * testCssWithFullBase method
  492. *
  493. * @return void
  494. */
  495. public function testCssWithFullBase() {
  496. Configure::write('Asset.filter.css', false);
  497. $here = $this->Html->url('/', true);
  498. $result = $this->Html->css('screen', array('fullBase' => true));
  499. $expected = array(
  500. 'link' => array('rel' => 'stylesheet', 'href' => $here . 'css/screen.css')
  501. );
  502. $this->assertTags($result, $expected);
  503. }
  504. /**
  505. * testPluginCssLink method
  506. *
  507. * @return void
  508. */
  509. public function testPluginCssLink() {
  510. Plugin::load('TestPlugin');
  511. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  512. $expected = array(
  513. 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/')
  514. );
  515. $this->assertTags($result, $expected);
  516. $result = $this->Html->css('TestPlugin.test_plugin_asset.css');
  517. $this->assertTags($result, $expected);
  518. $result = $this->Html->css('TestPlugin.my.css.library');
  519. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  520. $this->assertTags($result, $expected);
  521. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  522. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  523. $this->assertTags($result, $expected);
  524. $result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
  525. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  526. $this->assertTags($result[0], $expected);
  527. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  528. $this->assertTags($result[1], $expected);
  529. $this->assertEquals(2, count($result));
  530. Plugin::unload('TestPlugin');
  531. }
  532. /**
  533. * test use of css() and timestamping
  534. *
  535. * @return void
  536. */
  537. public function testCssTimestamping() {
  538. Configure::write('debug', true);
  539. Configure::write('Asset.timestamp', true);
  540. $expected = array(
  541. 'link' => array('rel' => 'stylesheet', 'href' => '')
  542. );
  543. $result = $this->Html->css('cake.generic');
  544. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  545. $this->assertTags($result, $expected);
  546. Configure::write('debug', false);
  547. $result = $this->Html->css('cake.generic');
  548. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  549. $this->assertTags($result, $expected);
  550. Configure::write('Asset.timestamp', 'force');
  551. $result = $this->Html->css('cake.generic');
  552. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  553. $this->assertTags($result, $expected);
  554. $this->Html->request->webroot = '/testing/';
  555. $result = $this->Html->css('cake.generic');
  556. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  557. $this->assertTags($result, $expected);
  558. $this->Html->request->webroot = '/testing/longer/';
  559. $result = $this->Html->css('cake.generic');
  560. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  561. $this->assertTags($result, $expected);
  562. }
  563. /**
  564. * test use of css() and timestamping with plugin syntax
  565. *
  566. * @return void
  567. */
  568. public function testPluginCssTimestamping() {
  569. Plugin::load('TestPlugin');
  570. Configure::write('debug', true);
  571. Configure::write('Asset.timestamp', true);
  572. $expected = array(
  573. 'link' => array('rel' => 'stylesheet', 'href' => '')
  574. );
  575. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  576. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  577. $this->assertTags($result, $expected);
  578. Configure::write('debug', false);
  579. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  580. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  581. $this->assertTags($result, $expected);
  582. Configure::write('Asset.timestamp', 'force');
  583. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  584. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  585. $this->assertTags($result, $expected);
  586. $this->Html->request->webroot = '/testing/';
  587. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  588. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  589. $this->assertTags($result, $expected);
  590. $this->Html->request->webroot = '/testing/longer/';
  591. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  592. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  593. $this->assertTags($result, $expected);
  594. Plugin::unload('TestPlugin');
  595. }
  596. /**
  597. * test timestamp enforcement for script tags.
  598. *
  599. * @return void
  600. */
  601. public function testScriptTimestamping() {
  602. $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
  603. Configure::write('debug', true);
  604. Configure::write('Asset.timestamp', true);
  605. touch(WWW_ROOT . 'js/__cake_js_test.js');
  606. $timestamp = substr(strtotime('now'), 0, 8);
  607. $result = $this->Html->script('__cake_js_test', array('once' => false));
  608. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  609. Configure::write('debug', false);
  610. Configure::write('Asset.timestamp', 'force');
  611. $result = $this->Html->script('__cake_js_test', array('once' => false));
  612. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  613. unlink(WWW_ROOT . 'js/__cake_js_test.js');
  614. Configure::write('Asset.timestamp', false);
  615. }
  616. /**
  617. * test timestamp enforcement for script tags with plugin syntax.
  618. *
  619. * @return void
  620. */
  621. public function testPluginScriptTimestamping() {
  622. Plugin::load('TestPlugin');
  623. $pluginPath = App::pluginPath('TestPlugin');
  624. $pluginJsPath = $pluginPath . 'webroot/js';
  625. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  626. Configure::write('debug', true);
  627. Configure::write('Asset.timestamp', true);
  628. touch($pluginJsPath . DS . '__cake_js_test.js');
  629. $timestamp = substr(strtotime('now'), 0, 8);
  630. $result = $this->Html->script('TestPlugin.__cake_js_test', array('once' => false));
  631. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  632. Configure::write('debug', false);
  633. Configure::write('Asset.timestamp', 'force');
  634. $result = $this->Html->script('TestPlugin.__cake_js_test', array('once' => false));
  635. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  636. unlink($pluginJsPath . DS . '__cake_js_test.js');
  637. Configure::write('Asset.timestamp', false);
  638. Plugin::unload('TestPlugin');
  639. }
  640. /**
  641. * test that scripts added with uses() are only ever included once.
  642. * test script tag generation
  643. *
  644. * @return void
  645. */
  646. public function testScript() {
  647. $result = $this->Html->script('foo');
  648. $expected = array(
  649. 'script' => array('src' => 'js/foo.js')
  650. );
  651. $this->assertTags($result, $expected);
  652. $result = $this->Html->script(array('foobar', 'bar'));
  653. $expected = array(
  654. array('script' => array('src' => 'js/foobar.js')),
  655. '/script',
  656. array('script' => array('src' => 'js/bar.js')),
  657. '/script',
  658. );
  659. $this->assertTags($result, $expected);
  660. $result = $this->Html->script('jquery-1.3');
  661. $expected = array(
  662. 'script' => array('src' => 'js/jquery-1.3.js')
  663. );
  664. $this->assertTags($result, $expected);
  665. $result = $this->Html->script('test.json');
  666. $expected = array(
  667. 'script' => array('src' => 'js/test.json.js')
  668. );
  669. $this->assertTags($result, $expected);
  670. $result = $this->Html->script('http://example.com/test.json');
  671. $expected = array(
  672. 'script' => array('src' => 'http://example.com/test.json')
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
  676. $expected = array(
  677. 'script' => array('src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
  678. );
  679. $this->assertTags($result, $expected);
  680. $result = $this->Html->script('test.json.js?foo=bar');
  681. $expected = array(
  682. 'script' => array('src' => 'js/test.json.js?foo=bar')
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Html->script('test.json.js?foo=bar&other=test');
  686. $expected = array(
  687. 'script' => array('src' => 'js/test.json.js?foo=bar&amp;other=test')
  688. );
  689. $this->assertTags($result, $expected);
  690. $result = $this->Html->script('foo2', array('pathPrefix' => '/my/custom/path/'));
  691. $expected = array(
  692. 'script' => array('src' => '/my/custom/path/foo2.js')
  693. );
  694. $this->assertTags($result, $expected);
  695. $result = $this->Html->script('foo3', array('pathPrefix' => 'http://cakephp.org/assets/js/'));
  696. $expected = array(
  697. 'script' => array('src' => 'http://cakephp.org/assets/js/foo3.js')
  698. );
  699. $this->assertTags($result, $expected);
  700. $previousConfig = Configure::read('App.jsBaseUrl');
  701. Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
  702. $result = $this->Html->script('foo4');
  703. $expected = array(
  704. 'script' => array('src' => '//cdn.cakephp.org/js/foo4.js')
  705. );
  706. $this->assertTags($result, $expected);
  707. Configure::write('App.jsBaseUrl', $previousConfig);
  708. $result = $this->Html->script('foo');
  709. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  710. $result = $this->Html->script(array('foo', 'bar', 'baz'));
  711. $this->assertNotRegExp('/foo.js/', $result);
  712. $result = $this->Html->script('foo', array('once' => false));
  713. $this->assertNotNull($result);
  714. $result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  715. $expected = array(
  716. 'script' => array('src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * test that plugin scripts added with uses() are only ever included once.
  722. * test script tag generation with plugin syntax
  723. *
  724. * @return void
  725. */
  726. public function testPluginScript() {
  727. Plugin::load('TestPlugin');
  728. $result = $this->Html->script('TestPlugin.foo');
  729. $expected = array(
  730. 'script' => array('src' => 'test_plugin/js/foo.js')
  731. );
  732. $this->assertTags($result, $expected);
  733. $result = $this->Html->script(array('TestPlugin.foobar', 'TestPlugin.bar'));
  734. $expected = array(
  735. array('script' => array('src' => 'test_plugin/js/foobar.js')),
  736. '/script',
  737. array('script' => array('src' => 'test_plugin/js/bar.js')),
  738. '/script',
  739. );
  740. $this->assertTags($result, $expected);
  741. $result = $this->Html->script('TestPlugin.jquery-1.3');
  742. $expected = array(
  743. 'script' => array('src' => 'test_plugin/js/jquery-1.3.js')
  744. );
  745. $this->assertTags($result, $expected);
  746. $result = $this->Html->script('TestPlugin.test.json');
  747. $expected = array(
  748. 'script' => array('src' => 'test_plugin/js/test.json.js')
  749. );
  750. $this->assertTags($result, $expected);
  751. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  752. $expected = array(
  753. 'script' => array('src' => 'test_plugin/jquery-1.3.2.js?someparam=foo')
  754. );
  755. $this->assertTags($result, $expected);
  756. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  757. $expected = array(
  758. 'script' => array('src' => 'test_plugin/js/test.json.js?foo=bar')
  759. );
  760. $this->assertTags($result, $expected);
  761. $result = $this->Html->script('TestPlugin.foo');
  762. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  763. $result = $this->Html->script(array('TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz'));
  764. $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
  765. $result = $this->Html->script('TestPlugin.foo', array('once' => false));
  766. $this->assertNotNull($result);
  767. $result = $this->Html->script('TestPlugin.jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  768. $expected = array(
  769. 'script' => array('src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  770. );
  771. $this->assertTags($result, $expected);
  772. Plugin::unload('TestPlugin');
  773. }
  774. /**
  775. * test that script() works with blocks.
  776. *
  777. * @return void
  778. */
  779. public function testScriptWithBlocks() {
  780. $this->View->expects($this->at(0))
  781. ->method('append')
  782. ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
  783. $this->View->expects($this->at(1))
  784. ->method('append')
  785. ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
  786. $result = $this->Html->script('script_in_head', array('block' => true));
  787. $this->assertNull($result);
  788. $result = $this->Html->script('second_script', array('block' => 'headScripts'));
  789. $this->assertNull($result);
  790. }
  791. /**
  792. * testScriptWithFullBase method
  793. *
  794. * @return void
  795. */
  796. public function testScriptWithFullBase() {
  797. $here = $this->Html->url('/', true);
  798. $result = $this->Html->script('foo', array('fullBase' => true));
  799. $expected = array(
  800. 'script' => array('src' => $here . 'js/foo.js')
  801. );
  802. $this->assertTags($result, $expected);
  803. $result = $this->Html->script(array('foobar', 'bar'), array('fullBase' => true));
  804. $expected = array(
  805. array('script' => array('src' => $here . 'js/foobar.js')),
  806. '/script',
  807. array('script' => array('src' => $here . 'js/bar.js')),
  808. '/script',
  809. );
  810. $this->assertTags($result, $expected);
  811. }
  812. /**
  813. * test a script file in the webroot/theme dir.
  814. *
  815. * @return void
  816. */
  817. public function testScriptInTheme() {
  818. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  819. $themeExists = is_dir(WWW_ROOT . 'theme');
  820. $testfile = WWW_ROOT . 'theme/test_theme/js/__test_js.js';
  821. new File($testfile, true);
  822. $this->Html->request->webroot = '/';
  823. $this->Html->theme = 'test_theme';
  824. $result = $this->Html->script('__test_js.js');
  825. $expected = array(
  826. 'script' => array('src' => '/theme/test_theme/js/__test_js.js')
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * test Script block generation
  832. *
  833. * @return void
  834. */
  835. public function testScriptBlock() {
  836. $result = $this->Html->scriptBlock('window.foo = 2;');
  837. $expected = array(
  838. '<script',
  839. $this->cDataStart,
  840. 'window.foo = 2;',
  841. $this->cDataEnd,
  842. '/script',
  843. );
  844. $this->assertTags($result, $expected);
  845. $result = $this->Html->scriptBlock('window.foo = 2;', array('type' => 'text/x-handlebars-template'));
  846. $expected = array(
  847. 'script' => array('type' => 'text/x-handlebars-template'),
  848. $this->cDataStart,
  849. 'window.foo = 2;',
  850. $this->cDataEnd,
  851. '/script',
  852. );
  853. $this->assertTags($result, $expected);
  854. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
  855. $expected = array(
  856. '<script',
  857. 'window.foo = 2;',
  858. '/script',
  859. );
  860. $this->assertTags($result, $expected);
  861. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
  862. $expected = array(
  863. '<script',
  864. $this->cDataStart,
  865. 'window.foo = 2;',
  866. $this->cDataEnd,
  867. '/script',
  868. );
  869. $this->assertTags($result, $expected);
  870. $this->View->expects($this->at(0))
  871. ->method('append')
  872. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  873. $this->View->expects($this->at(1))
  874. ->method('append')
  875. ->with('scriptTop', $this->stringContains('alert('));
  876. $result = $this->Html->scriptBlock('window.foo = 2;', array('block' => true));
  877. $this->assertNull($result);
  878. $result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
  879. $this->assertNull($result);
  880. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
  881. $expected = array(
  882. 'script' => array('encoding' => 'utf-8'),
  883. 'window.foo = 2;',
  884. '/script',
  885. );
  886. $this->assertTags($result, $expected);
  887. }
  888. /**
  889. * test script tag output buffering when using scriptStart() and scriptEnd();
  890. *
  891. * @return void
  892. */
  893. public function testScriptStartAndScriptEnd() {
  894. $result = $this->Html->scriptStart(array('safe' => true));
  895. $this->assertNull($result);
  896. echo 'this is some javascript';
  897. $result = $this->Html->scriptEnd();
  898. $expected = array(
  899. '<script',
  900. $this->cDataStart,
  901. 'this is some javascript',
  902. $this->cDataEnd,
  903. '/script'
  904. );
  905. $this->assertTags($result, $expected);
  906. $result = $this->Html->scriptStart(array('safe' => false));
  907. $this->assertNull($result);
  908. echo 'this is some javascript';
  909. $result = $this->Html->scriptEnd();
  910. $expected = array(
  911. '<script',
  912. 'this is some javascript',
  913. '/script'
  914. );
  915. $this->assertTags($result, $expected);
  916. $result = $this->Html->scriptStart(array('safe' => true, 'type' => 'text/x-handlebars-template'));
  917. $this->assertNull($result);
  918. echo 'this is some template';
  919. $result = $this->Html->scriptEnd();
  920. $expected = array(
  921. 'script' => array('type' => 'text/x-handlebars-template'),
  922. $this->cDataStart,
  923. 'this is some template',
  924. $this->cDataEnd,
  925. '/script'
  926. );
  927. $this->assertTags($result, $expected);
  928. $this->View->expects($this->once())
  929. ->method('append');
  930. $result = $this->Html->scriptStart(array('safe' => false, 'block' => true));
  931. $this->assertNull($result);
  932. echo 'this is some javascript';
  933. $result = $this->Html->scriptEnd();
  934. $this->assertNull($result);
  935. }
  936. /**
  937. * testCharsetTag method
  938. *
  939. * @return void
  940. */
  941. public function testCharsetTag() {
  942. Configure::write('App.encoding', null);
  943. $result = $this->Html->charset();
  944. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')));
  945. Configure::write('App.encoding', 'ISO-8859-1');
  946. $result = $this->Html->charset();
  947. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1')));
  948. $result = $this->Html->charset('UTF-7');
  949. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7')));
  950. }
  951. /**
  952. * testGetCrumb and addCrumb method
  953. *
  954. * @return void
  955. */
  956. public function testBreadcrumb() {
  957. $this->assertNull($this->Html->getCrumbs());
  958. $this->Html->addCrumb('First', '#first');
  959. $this->Html->addCrumb('Second', '#second');
  960. $this->Html->addCrumb('Third', '#third');
  961. $result = $this->Html->getCrumbs();
  962. $expected = array(
  963. array('a' => array('href' => '#first')),
  964. 'First',
  965. '/a',
  966. '&raquo;',
  967. array('a' => array('href' => '#second')),
  968. 'Second',
  969. '/a',
  970. '&raquo;',
  971. array('a' => array('href' => '#third')),
  972. 'Third',
  973. '/a',
  974. );
  975. $this->assertTags($result, $expected);
  976. $result = $this->Html->getCrumbs(' &gt; ');
  977. $expected = array(
  978. array('a' => array('href' => '#first')),
  979. 'First',
  980. '/a',
  981. ' &gt; ',
  982. array('a' => array('href' => '#second')),
  983. 'Second',
  984. '/a',
  985. ' &gt; ',
  986. array('a' => array('href' => '#third')),
  987. 'Third',
  988. '/a',
  989. );
  990. $this->assertTags($result, $expected);
  991. $this->Html->addCrumb('Fourth', null);
  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. '&raquo;',
  1006. 'Fourth'
  1007. );
  1008. $this->assertTags($result, $expected);
  1009. $result = $this->Html->getCrumbs('-', 'Start');
  1010. $expected = array(
  1011. array('a' => array('href' => '/')),
  1012. 'Start',
  1013. '/a',
  1014. '-',
  1015. array('a' => array('href' => '#first')),
  1016. 'First',
  1017. '/a',
  1018. '-',
  1019. array('a' => array('href' => '#second')),
  1020. 'Second',
  1021. '/a',
  1022. '-',
  1023. array('a' => array('href' => '#third')),
  1024. 'Third',
  1025. '/a',
  1026. '-',
  1027. 'Fourth'
  1028. );
  1029. $this->assertTags($result, $expected);
  1030. }
  1031. /**
  1032. * Test the array form of $startText
  1033. *
  1034. * @return void
  1035. */
  1036. public function testGetCrumbFirstLink() {
  1037. $result = $this->Html->getCrumbList(array(), 'Home');
  1038. $this->assertTags(
  1039. $result,
  1040. array(
  1041. '<ul',
  1042. array('li' => array('class' => 'first')),
  1043. array('a' => array('href' => '/')), 'Home', '/a',
  1044. '/li',
  1045. '/ul'
  1046. )
  1047. );
  1048. $this->Html->addCrumb('First', '#first');
  1049. $this->Html->addCrumb('Second', '#second');
  1050. $result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  1051. $expected = array(
  1052. array('a' => array('href' => '/home')),
  1053. 'img' => array('src' => '/home.png'),
  1054. '/a',
  1055. ' - ',
  1056. array('a' => array('href' => '#first')),
  1057. 'First',
  1058. '/a',
  1059. ' - ',
  1060. array('a' => array('href' => '#second')),
  1061. 'Second',
  1062. '/a',
  1063. );
  1064. $this->assertTags($result, $expected);
  1065. }
  1066. /**
  1067. * testNestedList method
  1068. *
  1069. * @return void
  1070. */
  1071. public function testNestedList() {
  1072. $list = array(
  1073. 'Item 1',
  1074. 'Item 2' => array(
  1075. 'Item 2.1'
  1076. ),
  1077. 'Item 3',
  1078. 'Item 4' => array(
  1079. 'Item 4.1',
  1080. 'Item 4.2',
  1081. 'Item 4.3' => array(
  1082. 'Item 4.3.1',
  1083. 'Item 4.3.2'
  1084. )
  1085. ),
  1086. 'Item 5' => array(
  1087. 'Item 5.1',
  1088. 'Item 5.2'
  1089. )
  1090. );
  1091. $result = $this->Html->nestedList($list);
  1092. $expected = array(
  1093. '<ul',
  1094. '<li', 'Item 1', '/li',
  1095. '<li', 'Item 2',
  1096. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1097. '/li',
  1098. '<li', 'Item 3', '/li',
  1099. '<li', 'Item 4',
  1100. '<ul',
  1101. '<li', 'Item 4.1', '/li',
  1102. '<li', 'Item 4.2', '/li',
  1103. '<li', 'Item 4.3',
  1104. '<ul',
  1105. '<li', 'Item 4.3.1', '/li',
  1106. '<li', 'Item 4.3.2', '/li',
  1107. '/ul',
  1108. '/li',
  1109. '/ul',
  1110. '/li',
  1111. '<li', 'Item 5',
  1112. '<ul',
  1113. '<li', 'Item 5.1', '/li',
  1114. '<li', 'Item 5.2', '/li',
  1115. '/ul',
  1116. '/li',
  1117. '/ul'
  1118. );
  1119. $this->assertTags($result, $expected);
  1120. $result = $this->Html->nestedList($list);
  1121. $this->assertTags($result, $expected);
  1122. $result = $this->Html->nestedList($list, array('tag' => 'ol'));
  1123. $expected = array(
  1124. '<ol',
  1125. '<li', 'Item 1', '/li',
  1126. '<li', 'Item 2',
  1127. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1128. '/li',
  1129. '<li', 'Item 3', '/li',
  1130. '<li', 'Item 4',
  1131. '<ol',
  1132. '<li', 'Item 4.1', '/li',
  1133. '<li', 'Item 4.2', '/li',
  1134. '<li', 'Item 4.3',
  1135. '<ol',
  1136. '<li', 'Item 4.3.1', '/li',
  1137. '<li', 'Item 4.3.2', '/li',
  1138. '/ol',
  1139. '/li',
  1140. '/ol',
  1141. '/li',
  1142. '<li', 'Item 5',
  1143. '<ol',
  1144. '<li', 'Item 5.1', '/li',
  1145. '<li', 'Item 5.2', '/li',
  1146. '/ol',
  1147. '/li',
  1148. '/ol'
  1149. );
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Html->nestedList($list, array('tag' => 'ol'));
  1152. $this->assertTags($result, $expected);
  1153. $result = $this->Html->nestedList($list, array('class' => 'list'));
  1154. $expected = array(
  1155. array('ul' => array('class' => 'list')),
  1156. '<li', 'Item 1', '/li',
  1157. '<li', 'Item 2',
  1158. array('ul' => array('class' => 'list')), '<li', 'Item 2.1', '/li', '/ul',
  1159. '/li',
  1160. '<li', 'Item 3', '/li',
  1161. '<li', 'Item 4',
  1162. array('ul' => array('class' => 'list')),
  1163. '<li', 'Item 4.1', '/li',
  1164. '<li', 'Item 4.2', '/li',
  1165. '<li', 'Item 4.3',
  1166. array('ul' => array('class' => 'list')),
  1167. '<li', 'Item 4.3.1', '/li',
  1168. '<li', 'Item 4.3.2', '/li',
  1169. '/ul',
  1170. '/li',
  1171. '/ul',
  1172. '/li',
  1173. '<li', 'Item 5',
  1174. array('ul' => array('class' => 'list')),
  1175. '<li', 'Item 5.1', '/li',
  1176. '<li', 'Item 5.2', '/li',
  1177. '/ul',
  1178. '/li',
  1179. '/ul'
  1180. );
  1181. $this->assertTags($result, $expected);
  1182. $result = $this->Html->nestedList($list, array(), array('class' => 'item'));
  1183. $expected = array(
  1184. '<ul',
  1185. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1186. array('li' => array('class' => 'item')), 'Item 2',
  1187. '<ul', array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1188. '/li',
  1189. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1190. array('li' => array('class' => 'item')), 'Item 4',
  1191. '<ul',
  1192. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1193. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1194. array('li' => array('class' => 'item')), 'Item 4.3',
  1195. '<ul',
  1196. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1197. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1198. '/ul',
  1199. '/li',
  1200. '/ul',
  1201. '/li',
  1202. array('li' => array('class' => 'item')), 'Item 5',
  1203. '<ul',
  1204. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1205. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1206. '/ul',
  1207. '/li',
  1208. '/ul'
  1209. );
  1210. $this->assertTags($result, $expected);
  1211. $result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
  1212. $expected = array(
  1213. '<ul',
  1214. array('li' => array('class' => 'odd')), 'Item 1', '/li',
  1215. array('li' => array('class' => 'even')), 'Item 2',
  1216. '<ul', array('li' => array('class' => 'odd')), 'Item 2.1', '/li', '/ul',
  1217. '/li',
  1218. array('li' => array('class' => 'odd')), 'Item 3', '/li',
  1219. array('li' => array('class' => 'even')), 'Item 4',
  1220. '<ul',
  1221. array('li' => array('class' => 'odd')), 'Item 4.1', '/li',
  1222. array('li' => array('class' => 'even')), 'Item 4.2', '/li',
  1223. array('li' => array('class' => 'odd')), 'Item 4.3',
  1224. '<ul',
  1225. array('li' => array('class' => 'odd')), 'Item 4.3.1', '/li',
  1226. array('li' => array('class' => 'even')), 'Item 4.3.2', '/li',
  1227. '/ul',
  1228. '/li',
  1229. '/ul',
  1230. '/li',
  1231. array('li' => array('class' => 'odd')), 'Item 5',
  1232. '<ul',
  1233. array('li' => array('class' => 'odd')), 'Item 5.1', '/li',
  1234. array('li' => array('class' => 'even')), 'Item 5.2', '/li',
  1235. '/ul',
  1236. '/li',
  1237. '/ul'
  1238. );
  1239. $this->assertTags($result, $expected);
  1240. $result = $this->Html->nestedList($list, array('class' => 'list'), array('class' => 'item'));
  1241. $expected = array(
  1242. array('ul' => array('class' => 'list')),
  1243. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1244. array('li' => array('class' => 'item')), 'Item 2',
  1245. array('ul' => array('class' => 'list')), array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1246. '/li',
  1247. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1248. array('li' => array('class' => 'item')), 'Item 4',
  1249. array('ul' => array('class' => 'list')),
  1250. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1251. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1252. array('li' => array('class' => 'item')), 'Item 4.3',
  1253. array('ul' => array('class' => 'list')),
  1254. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1255. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1256. '/ul',
  1257. '/li',
  1258. '/ul',
  1259. '/li',
  1260. array('li' => array('class' => 'item')), 'Item 5',
  1261. array('ul' => array('class' => 'list')),
  1262. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1263. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1264. '/ul',
  1265. '/li',
  1266. '/ul'
  1267. );
  1268. $this->assertTags($result, $expected);
  1269. }
  1270. /**
  1271. * testMeta method
  1272. *
  1273. * @return void
  1274. */
  1275. public function testMeta() {
  1276. Router::connect('/:controller', array('action' => 'index'));
  1277. $result = $this->Html->meta('this is an rss feed', array('controller' => 'posts', 'ext' => 'rss'));
  1278. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1279. $result = $this->Html->meta('rss', array('controller' => 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed'));
  1280. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1281. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'));
  1282. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1283. $result = $this->Html->meta('non-existing');
  1284. $this->assertTags($result, array('<meta'));
  1285. $result = $this->Html->meta('non-existing', '/posts.xpp');
  1286. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'non-existing')));
  1287. $result = $this->Html->meta('non-existing', '/posts.xpp', array('type' => 'atom'));
  1288. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing')));
  1289. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'), array('link' => '/articles.rss'));
  1290. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1291. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1292. $this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
  1293. $result = $this->Html->meta('description', 'this is the meta description');
  1294. $this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));
  1295. $result = $this->Html->meta('robots', 'ALL');
  1296. $this->assertTags($result, array('meta' => array('name' => 'robots', 'content' => 'ALL')));
  1297. }
  1298. /**
  1299. * Test generating favicon's with meta()
  1300. *
  1301. * @return void
  1302. */
  1303. public function testMetaIcon() {
  1304. $result = $this->Html->meta('icon', 'favicon.ico');
  1305. $expected = array(
  1306. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1307. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1308. );
  1309. $this->assertTags($result, $expected);
  1310. $result = $this->Html->meta('icon');
  1311. $expected = array(
  1312. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1313. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1314. );
  1315. $this->assertTags($result, $expected);
  1316. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1317. $url = '/favicon.png?one=two&amp;three=four';
  1318. $expected = array(
  1319. 'link' => array(
  1320. 'href' => $url,
  1321. 'type' => 'image/x-icon',
  1322. 'rel' => 'icon'
  1323. ),
  1324. array(
  1325. 'link' => array(
  1326. 'href' => $url,
  1327. 'type' => 'image/x-icon',
  1328. 'rel' => 'shortcut icon'
  1329. )
  1330. )
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. $this->Html->request->webroot = '/testing/';
  1334. $result = $this->Html->meta('icon');
  1335. $expected = array(
  1336. 'link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1337. array('link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1338. );
  1339. $this->assertTags($result, $expected);
  1340. }
  1341. /**
  1342. * Test the inline and block options for meta()
  1343. *
  1344. * @return void
  1345. */
  1346. public function testMetaWithBlocks() {
  1347. $this->View->expects($this->at(0))
  1348. ->method('append')
  1349. ->with('meta', $this->stringContains('robots'));
  1350. $this->View->expects($this->at(1))
  1351. ->method('append')
  1352. ->with('metaTags', $this->stringContains('favicon.ico'));
  1353. $result = $this->Html->meta('robots', 'ALL', array('block' => true));
  1354. $this->assertNull($result);
  1355. $result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));
  1356. $this->assertNull($result);
  1357. }
  1358. /**
  1359. * testTableHeaders method
  1360. *
  1361. * @return void
  1362. */
  1363. public function testTableHeaders() {
  1364. $result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
  1365. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1366. $this->assertTags($result, $expected);
  1367. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight')), 'Date'));
  1368. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1369. $this->assertTags($result, $expected);
  1370. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight', 'width' => '120px')), 'Date'));
  1371. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1372. $this->assertTags($result, $expected);
  1373. $result = $this->Html->tableHeaders(array('ID', array('Name' => array()), 'Date'));
  1374. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1375. $this->assertTags($result, $expected);
  1376. }
  1377. /**
  1378. * testTableCells method
  1379. *
  1380. * @return void
  1381. */
  1382. public function testTableCells() {
  1383. $tr = array(
  1384. 'td content 1',
  1385. array('td content 2', array("width" => "100px")),
  1386. array('td content 3', array('width' => '100px'))
  1387. );
  1388. $result = $this->Html->tableCells($tr);
  1389. $expected = array(
  1390. '<tr',
  1391. '<td', 'td content 1', '/td',
  1392. array('td' => array('width' => '100px')), 'td content 2', '/td',
  1393. array('td' => array('width' => 'preg:/100px/')), 'td content 3', '/td',
  1394. '/tr'
  1395. );
  1396. $this->assertTags($result, $expected);
  1397. $tr = array('td content 1', 'td content 2', 'td content 3');
  1398. $result = $this->Html->tableCells($tr, null, null, true);
  1399. $expected = array(
  1400. '<tr',
  1401. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1402. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1403. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1404. '/tr'
  1405. );
  1406. $this->assertTags($result, $expected);
  1407. $tr = array('td content 1', 'td content 2', 'td content 3');
  1408. $result = $this->Html->tableCells($tr, true);
  1409. $expected = array(
  1410. '<tr',
  1411. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1412. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1413. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1414. '/tr'
  1415. );
  1416. $this->assertTags($result, $expected);
  1417. $tr = array(
  1418. array('td content 1', 'td content 2', 'td content 3'),
  1419. array('td content 1', 'td content 2', 'td content 3'),
  1420. array('td content 1', 'td content 2', 'td content 3')
  1421. );
  1422. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1423. $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>";
  1424. $this->assertEquals($expected, $result);
  1425. $tr = array(
  1426. array('td content 1', 'td content 2', 'td content 3'),
  1427. array('td content 1', 'td content 2', 'td content 3'),
  1428. array('td content 1', 'td content 2', 'td content 3'),
  1429. array('td content 1', 'td content 2', 'td content 3')
  1430. );
  1431. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1432. $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>";
  1433. $this->assertEquals($expected, $result);
  1434. $tr = array(
  1435. array('td content 1', 'td content 2', 'td content 3'),
  1436. array('td content 1', 'td content 2', 'td content 3'),
  1437. array('td content 1', 'td content 2', 'td content 3')
  1438. );
  1439. $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1440. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'), false, false);
  1441. $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>";
  1442. $this->assertEquals($expected, $result);
  1443. }
  1444. /**
  1445. * testTag method
  1446. *
  1447. * @return void
  1448. */
  1449. public function testTag() {
  1450. $result = $this->Html->tag('div');
  1451. $this->assertTags($result, '<div');
  1452. $result = $this->Html->tag('div', 'text');
  1453. $this->assertTags($result, '<div', 'text', '/div');
  1454. $result = $this->Html->tag('div', '<text>', array('class' => 'class-name', 'escape' => true));
  1455. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1456. $result = $this->Html->tag(false, '<em>stuff</em>');
  1457. $this->assertEquals('<em>stuff</em>', $result);
  1458. $result = $this->Html->tag(null, '<em>stuff</em>');
  1459. $this->assertEquals('<em>stuff</em>', $result);
  1460. $result = $this->Html->tag('', '<em>stuff</em>');
  1461. $this->assertEquals('<em>stuff</em>', $result);
  1462. }
  1463. /**
  1464. * testDiv method
  1465. *
  1466. * @return void
  1467. */
  1468. public function testDiv() {
  1469. $result = $this->Html->div('class-name');
  1470. $this->assertTags($result, array('div' => array('class' => 'class-name')));
  1471. $result = $this->Html->div('class-name', 'text');
  1472. $this->assertTags($result, array('div' => array('class' => 'class-name'), 'text', '/div'));
  1473. $result = $this->Html->div('class-name', '<text>', array('escape' => true));
  1474. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1475. }
  1476. /**
  1477. * testPara method
  1478. *
  1479. * @return void
  1480. */
  1481. public function testPara() {
  1482. $result = $this->Html->para('class-name', '');
  1483. $this->assertTags($result, array('p' => array('class' => 'class-name')));
  1484. $result = $this->Html->para('class-name', 'text');
  1485. $this->assertTags($result, array('p' => array('class' => 'class-name'), 'text', '/p'));
  1486. $result = $this->Html->para('class-name', '<text>', array('escape' => true));
  1487. $this->assertTags($result, array('p' => array('class' => 'class-name'), '&lt;text&gt;', '/p'));
  1488. }
  1489. /**
  1490. * testMedia method
  1491. *
  1492. * @return void
  1493. */
  1494. public function testMedia() {
  1495. $result = $this->Html->media('video.webm');
  1496. $expected = array('video' => array('src' => 'files/video.webm'), '/video');
  1497. $this->assertTags($result, $expected);
  1498. $result = $this->Html->media('video.webm', array(
  1499. 'text' => 'Your browser does not support the HTML5 Video element.'
  1500. ));
  1501. $expected = array('video' => array('src' => 'files/video.webm'), 'Your browser does not support the HTML5 Video element.', '/video');
  1502. $this->assertTags($result, $expected);
  1503. $result = $this->Html->media('video.webm', array('autoload', 'muted' => 'muted'));
  1504. $expected = array(
  1505. 'video' => array(
  1506. 'src' => 'files/video.webm',
  1507. 'autoload' => 'autoload',
  1508. 'muted' => 'muted'
  1509. ),
  1510. '/video'
  1511. );
  1512. $this->assertTags($result, $expected);
  1513. $result = $this->Html->media(
  1514. array('video.webm', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  1515. array('pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.')
  1516. );
  1517. $expected = array(
  1518. 'video' => array('poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'),
  1519. array('source' => array('src' => 'videos/video.webm', 'type' => 'video/webm')),
  1520. array('source' => array('src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;')),
  1521. 'Your browser does not support the HTML5 Video element.',
  1522. '/video'
  1523. );
  1524. $this->assertTags($result, $expected);
  1525. $result = $this->Html->media('video.ogv', array('tag' => 'video'));
  1526. $expected = array('video' => array('src' => 'files/video.ogv'), '/video');
  1527. $this->assertTags($result, $expected);
  1528. $result = $this->Html->media('audio.mp3');
  1529. $expected = array('audio' => array('src' => 'files/audio.mp3'), '/audio');
  1530. $this->assertTags($result, $expected);
  1531. $result = $this->Html->media(
  1532. array(array('src' => 'video.mov', 'type' => 'video/mp4'), 'video.webm')
  1533. );
  1534. $expected = array(
  1535. '<video',
  1536. array('source' => array('src' => 'files/video.mov', 'type' => 'video/mp4')),
  1537. array('source' => array('src' => 'files/video.webm', 'type' => 'video/webm')),
  1538. '/video'
  1539. );
  1540. $this->assertTags($result, $expected);
  1541. $result = $this->Html->media(null, array('src' => 'video.webm'));
  1542. $expected = array(
  1543. 'video' => array('src' => 'files/video.webm'),
  1544. '/video'
  1545. );
  1546. $this->assertTags($result, $expected);
  1547. }
  1548. /**
  1549. * testCrumbList method
  1550. *
  1551. * @return void
  1552. */
  1553. public function testCrumbList() {
  1554. $this->assertNull($this->Html->getCrumbList());
  1555. $this->Html->addCrumb('Home', '/', array('class' => 'home'));
  1556. $this->Html->addCrumb('Some page', '/some_page');
  1557. $this->Html->addCrumb('Another page');
  1558. $result = $this->Html->getCrumbList(
  1559. array('class' => 'breadcrumbs')
  1560. );
  1561. $this->assertTags(
  1562. $result,
  1563. array(
  1564. array('ul' => array('class' => 'breadcrumbs')),
  1565. array('li' => array('class' => 'first')),
  1566. array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
  1567. '/li',
  1568. '<li',
  1569. array('a' => array('href' => '/some_page')), 'Some page', '/a',
  1570. '/li',
  1571. array('li' => array('class' => 'last')),
  1572. 'Another page',
  1573. '/li',
  1574. '/ul'
  1575. )
  1576. );
  1577. }
  1578. /**
  1579. * Test getCrumbList startText
  1580. *
  1581. * @return void
  1582. */
  1583. public function testCrumbListFirstLink() {
  1584. $this->Html->addCrumb('First', '#first');
  1585. $this->Html->addCrumb('Second', '#second');
  1586. $result = $this->Html->getCrumbList(array(), 'Home');
  1587. $this->assertTags(
  1588. $result,
  1589. array(
  1590. '<ul',
  1591. array('li' => array('class' => 'first')),
  1592. array('a' => array('href' => '/')), 'Home', '/a',
  1593. '/li',
  1594. '<li',
  1595. array('a' => array('href' => '#first')), 'First', '/a',
  1596. '/li',
  1597. array('li' => array('class' => 'last')),
  1598. array('a' => array('href' => '#second')), 'Second', '/a',
  1599. '/li',
  1600. '/ul'
  1601. )
  1602. );
  1603. $result = $this->Html->getCrumbList(array(), array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  1604. $this->assertTags(
  1605. $result,
  1606. array(
  1607. '<ul',
  1608. array('li' => array('class' => 'first')),
  1609. array('a' => array('href' => '/home')), 'img' => array('src' => '/home.png'), '/a',
  1610. '/li',
  1611. '<li',
  1612. array('a' => array('href' => '#first')), 'First', '/a',
  1613. '/li',
  1614. array('li' => array('class' => 'last')),
  1615. array('a' => array('href' => '#second')), 'Second', '/a',
  1616. '/li',
  1617. '/ul'
  1618. )
  1619. );
  1620. }
  1621. /**
  1622. * test getCrumbList() in Twitter Bootstrap style.
  1623. *
  1624. * @return void
  1625. */
  1626. public function testCrumbListBootstrapStyle() {
  1627. $this->Html->addCrumb('Home', '/', array('class' => 'home'));
  1628. $this->Html->addCrumb('Library', '/lib');
  1629. $this->Html->addCrumb('Data');
  1630. $result = $this->Html->getCrumbList(array(
  1631. 'class' => 'breadcrumb',
  1632. 'separator' => '<span class="divider">-</span>',
  1633. 'firstClass' => false,
  1634. 'lastClass' => 'active'
  1635. ));
  1636. $this->assertTags(
  1637. $result,
  1638. array(
  1639. array('ul' => array('class' => 'breadcrumb')),
  1640. '<li',
  1641. array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
  1642. array('span' => array('class' => 'divider')), '-', '/span',
  1643. '/li',
  1644. '<li',
  1645. array('a' => array('href' => '/lib')), 'Library', '/a',
  1646. array('span' => array('class' => 'divider')), '-', '/span',
  1647. '/li',
  1648. array('li' => array('class' => 'active')), 'Data', '/li',
  1649. '/ul'
  1650. )
  1651. );
  1652. }
  1653. /**
  1654. * Test GetCrumbList using style of Zurb Foundation.
  1655. *
  1656. * @return void
  1657. */
  1658. public function testCrumbListZurbStyle() {
  1659. $this->Html->addCrumb('Home', '#');
  1660. $this->Html->addCrumb('Features', '#');
  1661. $this->Html->addCrumb('Gene Splicing', '#');
  1662. $this->Html->addCrumb('Home', '#');
  1663. $result = $this->Html->getCrumbList(
  1664. array('class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current')
  1665. );
  1666. $this->assertTags(
  1667. $result,
  1668. array(
  1669. array('ul' => array('class' => 'breadcrumbs')),
  1670. '<li',
  1671. array('a' => array('href' => '#')), 'Home', '/a',
  1672. '/li',
  1673. '<li',
  1674. array('a' => array('href' => '#')), 'Features', '/a',
  1675. '/li',
  1676. '<li',
  1677. array('a' => array('href' => '#')), 'Gene Splicing', '/a',
  1678. '/li',
  1679. array('li' => array('class' => 'current')),
  1680. array('a' => array('href' => '#')), 'Home', '/a',
  1681. '/li',
  1682. '/ul'
  1683. ), true
  1684. );
  1685. }
  1686. }