HtmlHelperTest.php 64 KB

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