HtmlHelperTest.php 63 KB

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