HtmlHelperTest.php 67 KB

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