HtmlHelperTest.php 68 KB

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