HtmlHelperTest.php 62 KB

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