HtmlHelperTest.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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 1.2.0
  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'));
  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', false);
  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', true);
  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(array('display' => 'none', 'margin' => '10px'));
  422. $expected = 'display:none; margin:10px;';
  423. $this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
  424. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
  425. $lines = explode("\n", $result);
  426. $this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
  427. $this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
  428. }
  429. /**
  430. * testCssLink method
  431. *
  432. * @return void
  433. */
  434. public function testCssLink() {
  435. $result = $this->Html->css('screen');
  436. $expected = array(
  437. 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/')
  438. );
  439. $this->assertTags($result, $expected);
  440. $result = $this->Html->css('screen.css');
  441. $this->assertTags($result, $expected);
  442. Plugin::load('TestPlugin');
  443. $result = $this->Html->css('TestPlugin.style', array('plugin' => false));
  444. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  445. $this->assertTags($result, $expected);
  446. Plugin::unload('TestPlugin');
  447. $result = $this->Html->css('my.css.library');
  448. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  449. $this->assertTags($result, $expected);
  450. $result = $this->Html->css('screen.css?1234');
  451. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  452. $this->assertTags($result, $expected);
  453. $result = $this->Html->css('screen.css?with=param&other=param');
  454. $expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
  455. $this->assertTags($result, $expected);
  456. $result = $this->Html->css('http://whatever.com/screen.css?1234');
  457. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  458. $this->assertTags($result, $expected);
  459. Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
  460. $result = $this->Html->css('cake.generic');
  461. $expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
  462. $this->assertTags($result, $expected);
  463. $result = $this->Html->css('//example.com/css/cake.generic.css');
  464. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  465. $this->assertTags($result, $expected);
  466. $result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
  467. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  468. $this->assertTags($result[0], $expected);
  469. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  470. $this->assertTags($result[1], $expected);
  471. $this->assertEquals(2, count($result));
  472. $this->View->expects($this->at(0))
  473. ->method('append')
  474. ->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
  475. $this->View->expects($this->at(1))
  476. ->method('append')
  477. ->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
  478. $result = $this->Html->css('css_in_head', array('block' => true));
  479. $this->assertNull($result);
  480. $result = $this->Html->css('more_css_in_head', array('block' => true));
  481. $this->assertNull($result);
  482. $result = $this->Html->css('screen', array('rel' => 'import'));
  483. $expected = array(
  484. '<style',
  485. 'preg:/@import url\(.*css\/screen\.css\);/',
  486. '/style'
  487. );
  488. $this->assertTags($result, $expected);
  489. }
  490. /**
  491. * testCssWithFullBase method
  492. *
  493. * @return void
  494. */
  495. public function testCssWithFullBase() {
  496. Configure::write('Asset.filter.css', false);
  497. $here = $this->Html->url('/', true);
  498. $result = $this->Html->css('screen', array('fullBase' => true));
  499. $expected = array(
  500. 'link' => array('rel' => 'stylesheet', 'href' => $here . 'css/screen.css')
  501. );
  502. $this->assertTags($result, $expected);
  503. }
  504. /**
  505. * testPluginCssLink method
  506. *
  507. * @return void
  508. */
  509. public function testPluginCssLink() {
  510. Plugin::load('TestPlugin');
  511. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  512. $expected = array(
  513. 'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/')
  514. );
  515. $this->assertTags($result, $expected);
  516. $result = $this->Html->css('TestPlugin.test_plugin_asset.css');
  517. $this->assertTags($result, $expected);
  518. $result = $this->Html->css('TestPlugin.my.css.library');
  519. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  520. $this->assertTags($result, $expected);
  521. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  522. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  523. $this->assertTags($result, $expected);
  524. $result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
  525. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  526. $this->assertTags($result[0], $expected);
  527. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  528. $this->assertTags($result[1], $expected);
  529. $this->assertEquals(2, count($result));
  530. Plugin::unload('TestPlugin');
  531. }
  532. /**
  533. * test use of css() and timestamping
  534. *
  535. * @return void
  536. */
  537. public function testCssTimestamping() {
  538. Configure::write('debug', true);
  539. Configure::write('Asset.timestamp', true);
  540. $expected = array(
  541. 'link' => array('rel' => 'stylesheet', 'href' => '')
  542. );
  543. $result = $this->Html->css('cake.generic');
  544. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  545. $this->assertTags($result, $expected);
  546. Configure::write('debug', false);
  547. $result = $this->Html->css('cake.generic');
  548. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  549. $this->assertTags($result, $expected);
  550. Configure::write('Asset.timestamp', 'force');
  551. $result = $this->Html->css('cake.generic');
  552. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  553. $this->assertTags($result, $expected);
  554. $this->Html->request->webroot = '/testing/';
  555. $result = $this->Html->css('cake.generic');
  556. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  557. $this->assertTags($result, $expected);
  558. $this->Html->request->webroot = '/testing/longer/';
  559. $result = $this->Html->css('cake.generic');
  560. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  561. $this->assertTags($result, $expected);
  562. }
  563. /**
  564. * test use of css() and timestamping with plugin syntax
  565. *
  566. * @return void
  567. */
  568. public function testPluginCssTimestamping() {
  569. Plugin::load('TestPlugin');
  570. Configure::write('debug', true);
  571. Configure::write('Asset.timestamp', true);
  572. $expected = array(
  573. 'link' => array('rel' => 'stylesheet', 'href' => '')
  574. );
  575. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  576. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  577. $this->assertTags($result, $expected);
  578. Configure::write('debug', false);
  579. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  580. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  581. $this->assertTags($result, $expected);
  582. Configure::write('Asset.timestamp', 'force');
  583. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  584. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  585. $this->assertTags($result, $expected);
  586. $this->Html->request->webroot = '/testing/';
  587. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  588. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  589. $this->assertTags($result, $expected);
  590. $this->Html->request->webroot = '/testing/longer/';
  591. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  592. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  593. $this->assertTags($result, $expected);
  594. Plugin::unload('TestPlugin');
  595. }
  596. /**
  597. * test timestamp enforcement for script tags.
  598. *
  599. * @return void
  600. */
  601. public function testScriptTimestamping() {
  602. $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
  603. Configure::write('debug', true);
  604. Configure::write('Asset.timestamp', true);
  605. touch(WWW_ROOT . 'js/__cake_js_test.js');
  606. $timestamp = substr(strtotime('now'), 0, 8);
  607. $result = $this->Html->script('__cake_js_test', array('once' => false));
  608. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  609. Configure::write('debug', false);
  610. Configure::write('Asset.timestamp', 'force');
  611. $result = $this->Html->script('__cake_js_test', array('once' => false));
  612. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  613. unlink(WWW_ROOT . 'js/__cake_js_test.js');
  614. Configure::write('Asset.timestamp', false);
  615. }
  616. /**
  617. * test timestamp enforcement for script tags with plugin syntax.
  618. *
  619. * @return void
  620. */
  621. public function testPluginScriptTimestamping() {
  622. Plugin::load('TestPlugin');
  623. $pluginPath = App::pluginPath('TestPlugin');
  624. $pluginJsPath = $pluginPath . 'webroot/js';
  625. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  626. Configure::write('debug', true);
  627. Configure::write('Asset.timestamp', true);
  628. touch($pluginJsPath . DS . '__cake_js_test.js');
  629. $timestamp = substr(strtotime('now'), 0, 8);
  630. $result = $this->Html->script('TestPlugin.__cake_js_test', array('once' => false));
  631. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  632. Configure::write('debug', false);
  633. Configure::write('Asset.timestamp', 'force');
  634. $result = $this->Html->script('TestPlugin.__cake_js_test', array('once' => false));
  635. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  636. unlink($pluginJsPath . DS . '__cake_js_test.js');
  637. Configure::write('Asset.timestamp', false);
  638. Plugin::unload('TestPlugin');
  639. }
  640. /**
  641. * test that scripts added with uses() are only ever included once.
  642. * test script tag generation
  643. *
  644. * @return void
  645. */
  646. public function testScript() {
  647. $result = $this->Html->script('foo');
  648. $expected = array(
  649. 'script' => array('src' => 'js/foo.js')
  650. );
  651. $this->assertTags($result, $expected);
  652. $result = $this->Html->script(array('foobar', 'bar'));
  653. $expected = array(
  654. array('script' => array('src' => 'js/foobar.js')),
  655. '/script',
  656. array('script' => array('src' => 'js/bar.js')),
  657. '/script',
  658. );
  659. $this->assertTags($result, $expected);
  660. $result = $this->Html->script('jquery-1.3');
  661. $expected = array(
  662. 'script' => array('src' => 'js/jquery-1.3.js')
  663. );
  664. $this->assertTags($result, $expected);
  665. $result = $this->Html->script('test.json');
  666. $expected = array(
  667. 'script' => array('src' => 'js/test.json.js')
  668. );
  669. $this->assertTags($result, $expected);
  670. $result = $this->Html->script('http://example.com/test.json');
  671. $expected = array(
  672. 'script' => array('src' => 'http://example.com/test.json')
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
  676. $expected = array(
  677. 'script' => array('src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
  678. );
  679. $this->assertTags($result, $expected);
  680. $result = $this->Html->script('test.json.js?foo=bar');
  681. $expected = array(
  682. 'script' => array('src' => 'js/test.json.js?foo=bar')
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Html->script('test.json.js?foo=bar&other=test');
  686. $expected = array(
  687. 'script' => array('src' => 'js/test.json.js?foo=bar&amp;other=test')
  688. );
  689. $this->assertTags($result, $expected);
  690. $result = $this->Html->script('foo2', array('pathPrefix' => '/my/custom/path/'));
  691. $expected = array(
  692. 'script' => array('src' => '/my/custom/path/foo2.js')
  693. );
  694. $this->assertTags($result, $expected);
  695. $result = $this->Html->script('foo3', array('pathPrefix' => 'http://cakephp.org/assets/js/'));
  696. $expected = array(
  697. 'script' => array('src' => 'http://cakephp.org/assets/js/foo3.js')
  698. );
  699. $this->assertTags($result, $expected);
  700. $previousConfig = Configure::read('App.jsBaseUrl');
  701. Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
  702. $result = $this->Html->script('foo4');
  703. $expected = array(
  704. 'script' => array('src' => '//cdn.cakephp.org/js/foo4.js')
  705. );
  706. $this->assertTags($result, $expected);
  707. Configure::write('App.jsBaseUrl', $previousConfig);
  708. $result = $this->Html->script('foo');
  709. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  710. $result = $this->Html->script(array('foo', 'bar', 'baz'));
  711. $this->assertNotRegExp('/foo.js/', $result);
  712. $result = $this->Html->script('foo', array('once' => false));
  713. $this->assertNotNull($result);
  714. $result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  715. $expected = array(
  716. 'script' => array('src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * test that plugin scripts added with uses() are only ever included once.
  722. * test script tag generation with plugin syntax
  723. *
  724. * @return void
  725. */
  726. public function testPluginScript() {
  727. Plugin::load('TestPlugin');
  728. $result = $this->Html->script('TestPlugin.foo');
  729. $expected = array(
  730. 'script' => array('src' => 'test_plugin/js/foo.js')
  731. );
  732. $this->assertTags($result, $expected);
  733. $result = $this->Html->script(array('TestPlugin.foobar', 'TestPlugin.bar'));
  734. $expected = array(
  735. array('script' => array('src' => 'test_plugin/js/foobar.js')),
  736. '/script',
  737. array('script' => array('src' => 'test_plugin/js/bar.js')),
  738. '/script',
  739. );
  740. $this->assertTags($result, $expected);
  741. $result = $this->Html->script('TestPlugin.jquery-1.3');
  742. $expected = array(
  743. 'script' => array('src' => 'test_plugin/js/jquery-1.3.js')
  744. );
  745. $this->assertTags($result, $expected);
  746. $result = $this->Html->script('TestPlugin.test.json');
  747. $expected = array(
  748. 'script' => array('src' => 'test_plugin/js/test.json.js')
  749. );
  750. $this->assertTags($result, $expected);
  751. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  752. $expected = array(
  753. 'script' => array('src' => 'test_plugin/jquery-1.3.2.js?someparam=foo')
  754. );
  755. $this->assertTags($result, $expected);
  756. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  757. $expected = array(
  758. 'script' => array('src' => 'test_plugin/js/test.json.js?foo=bar')
  759. );
  760. $this->assertTags($result, $expected);
  761. $result = $this->Html->script('TestPlugin.foo');
  762. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  763. $result = $this->Html->script(array('TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz'));
  764. $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
  765. $result = $this->Html->script('TestPlugin.foo', array('once' => false));
  766. $this->assertNotNull($result);
  767. $result = $this->Html->script('TestPlugin.jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  768. $expected = array(
  769. 'script' => array('src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  770. );
  771. $this->assertTags($result, $expected);
  772. Plugin::unload('TestPlugin');
  773. }
  774. /**
  775. * test that script() works with blocks.
  776. *
  777. * @return void
  778. */
  779. public function testScriptWithBlocks() {
  780. $this->View->expects($this->at(0))
  781. ->method('append')
  782. ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
  783. $this->View->expects($this->at(1))
  784. ->method('append')
  785. ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
  786. $result = $this->Html->script('script_in_head', array('block' => true));
  787. $this->assertNull($result);
  788. $result = $this->Html->script('second_script', array('block' => 'headScripts'));
  789. $this->assertNull($result);
  790. }
  791. /**
  792. * testScriptWithFullBase method
  793. *
  794. * @return void
  795. */
  796. public function testScriptWithFullBase() {
  797. $here = $this->Html->url('/', true);
  798. $result = $this->Html->script('foo', array('fullBase' => true));
  799. $expected = array(
  800. 'script' => array('src' => $here . 'js/foo.js')
  801. );
  802. $this->assertTags($result, $expected);
  803. $result = $this->Html->script(array('foobar', 'bar'), array('fullBase' => true));
  804. $expected = array(
  805. array('script' => array('src' => $here . 'js/foobar.js')),
  806. '/script',
  807. array('script' => array('src' => $here . 'js/bar.js')),
  808. '/script',
  809. );
  810. $this->assertTags($result, $expected);
  811. }
  812. /**
  813. * test a script file in the webroot/theme dir.
  814. *
  815. * @return void
  816. */
  817. public function testScriptInTheme() {
  818. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  819. $themeExists = is_dir(WWW_ROOT . 'theme');
  820. $testfile = WWW_ROOT . 'theme/test_theme/js/__test_js.js';
  821. new File($testfile, true);
  822. $this->Html->request->webroot = '/';
  823. $this->Html->theme = 'test_theme';
  824. $result = $this->Html->script('__test_js.js');
  825. $expected = array(
  826. 'script' => array('src' => '/theme/test_theme/js/__test_js.js')
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * test Script block generation
  832. *
  833. * @return void
  834. */
  835. public function testScriptBlock() {
  836. $result = $this->Html->scriptBlock('window.foo = 2;');
  837. $expected = array(
  838. '<script',
  839. $this->cDataStart,
  840. 'window.foo = 2;',
  841. $this->cDataEnd,
  842. '/script',
  843. );
  844. $this->assertTags($result, $expected);
  845. $result = $this->Html->scriptBlock('window.foo = 2;', array('type' => 'text/x-handlebars-template'));
  846. $expected = array(
  847. 'script' => array('type' => 'text/x-handlebars-template'),
  848. $this->cDataStart,
  849. 'window.foo = 2;',
  850. $this->cDataEnd,
  851. '/script',
  852. );
  853. $this->assertTags($result, $expected);
  854. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
  855. $expected = array(
  856. '<script',
  857. 'window.foo = 2;',
  858. '/script',
  859. );
  860. $this->assertTags($result, $expected);
  861. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
  862. $expected = array(
  863. '<script',
  864. $this->cDataStart,
  865. 'window.foo = 2;',
  866. $this->cDataEnd,
  867. '/script',
  868. );
  869. $this->assertTags($result, $expected);
  870. $this->View->expects($this->at(0))
  871. ->method('append')
  872. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  873. $this->View->expects($this->at(1))
  874. ->method('append')
  875. ->with('scriptTop', $this->stringContains('alert('));
  876. $result = $this->Html->scriptBlock('window.foo = 2;', array('block' => true));
  877. $this->assertNull($result);
  878. $result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
  879. $this->assertNull($result);
  880. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
  881. $expected = array(
  882. 'script' => array('encoding' => 'utf-8'),
  883. 'window.foo = 2;',
  884. '/script',
  885. );
  886. $this->assertTags($result, $expected);
  887. }
  888. /**
  889. * test script tag output buffering when using scriptStart() and scriptEnd();
  890. *
  891. * @return void
  892. */
  893. public function testScriptStartAndScriptEnd() {
  894. $result = $this->Html->scriptStart(array('safe' => true));
  895. $this->assertNull($result);
  896. echo 'this is some javascript';
  897. $result = $this->Html->scriptEnd();
  898. $expected = array(
  899. '<script',
  900. $this->cDataStart,
  901. 'this is some javascript',
  902. $this->cDataEnd,
  903. '/script'
  904. );
  905. $this->assertTags($result, $expected);
  906. $result = $this->Html->scriptStart(array('safe' => false));
  907. $this->assertNull($result);
  908. echo 'this is some javascript';
  909. $result = $this->Html->scriptEnd();
  910. $expected = array(
  911. '<script',
  912. 'this is some javascript',
  913. '/script'
  914. );
  915. $this->assertTags($result, $expected);
  916. $result = $this->Html->scriptStart(array('safe' => true, 'type' => 'text/x-handlebars-template'));
  917. $this->assertNull($result);
  918. echo 'this is some template';
  919. $result = $this->Html->scriptEnd();
  920. $expected = array(
  921. 'script' => array('type' => 'text/x-handlebars-template'),
  922. $this->cDataStart,
  923. 'this is some template',
  924. $this->cDataEnd,
  925. '/script'
  926. );
  927. $this->assertTags($result, $expected);
  928. $this->View->expects($this->once())
  929. ->method('append');
  930. $result = $this->Html->scriptStart(array('safe' => false, 'block' => true));
  931. $this->assertNull($result);
  932. echo 'this is some javascript';
  933. $result = $this->Html->scriptEnd();
  934. $this->assertNull($result);
  935. }
  936. /**
  937. * testCharsetTag method
  938. *
  939. * @return void
  940. */
  941. public function testCharsetTag() {
  942. Configure::write('App.encoding', null);
  943. $result = $this->Html->charset();
  944. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')));
  945. Configure::write('App.encoding', 'ISO-8859-1');
  946. $result = $this->Html->charset();
  947. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1')));
  948. $result = $this->Html->charset('UTF-7');
  949. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7')));
  950. }
  951. /**
  952. * testGetCrumb and addCrumb method
  953. *
  954. * @return void
  955. */
  956. public function testBreadcrumb() {
  957. $this->assertNull($this->Html->getCrumbs());
  958. $this->Html->addCrumb('First', '#first');
  959. $this->Html->addCrumb('Second', '#second');
  960. $this->Html->addCrumb('Third', '#third');
  961. $result = $this->Html->getCrumbs();
  962. $expected = array(
  963. array('a' => array('href' => '#first')),
  964. 'First',
  965. '/a',
  966. '&raquo;',
  967. array('a' => array('href' => '#second')),
  968. 'Second',
  969. '/a',
  970. '&raquo;',
  971. array('a' => array('href' => '#third')),
  972. 'Third',
  973. '/a',
  974. );
  975. $this->assertTags($result, $expected);
  976. $result = $this->Html->getCrumbs(' &gt; ');
  977. $expected = array(
  978. array('a' => array('href' => '#first')),
  979. 'First',
  980. '/a',
  981. ' &gt; ',
  982. array('a' => array('href' => '#second')),
  983. 'Second',
  984. '/a',
  985. ' &gt; ',
  986. array('a' => array('href' => '#third')),
  987. 'Third',
  988. '/a',
  989. );
  990. $this->assertTags($result, $expected);
  991. $this->Html->addCrumb('Fourth', null);
  992. $result = $this->Html->getCrumbs();
  993. $expected = array(
  994. array('a' => array('href' => '#first')),
  995. 'First',
  996. '/a',
  997. '&raquo;',
  998. array('a' => array('href' => '#second')),
  999. 'Second',
  1000. '/a',
  1001. '&raquo;',
  1002. array('a' => array('href' => '#third')),
  1003. 'Third',
  1004. '/a',
  1005. '&raquo;',
  1006. 'Fourth'
  1007. );
  1008. $this->assertTags($result, $expected);
  1009. $result = $this->Html->getCrumbs('-', 'Start');
  1010. $expected = array(
  1011. array('a' => array('href' => '/')),
  1012. 'Start',
  1013. '/a',
  1014. '-',
  1015. array('a' => array('href' => '#first')),
  1016. 'First',
  1017. '/a',
  1018. '-',
  1019. array('a' => array('href' => '#second')),
  1020. 'Second',
  1021. '/a',
  1022. '-',
  1023. array('a' => array('href' => '#third')),
  1024. 'Third',
  1025. '/a',
  1026. '-',
  1027. 'Fourth'
  1028. );
  1029. $this->assertTags($result, $expected);
  1030. }
  1031. /**
  1032. * Test the array form of $startText
  1033. *
  1034. * @return void
  1035. */
  1036. public function testGetCrumbFirstLink() {
  1037. $result = $this->Html->getCrumbList(array(), 'Home');
  1038. $this->assertTags(
  1039. $result,
  1040. array(
  1041. '<ul',
  1042. array('li' => array('class' => 'first')),
  1043. array('a' => array('href' => '/')), 'Home', '/a',
  1044. '/li',
  1045. '/ul'
  1046. )
  1047. );
  1048. $this->Html->addCrumb('First', '#first');
  1049. $this->Html->addCrumb('Second', '#second');
  1050. $result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  1051. $expected = array(
  1052. array('a' => array('href' => '/home')),
  1053. 'img' => array('src' => '/home.png'),
  1054. '/a',
  1055. ' - ',
  1056. array('a' => array('href' => '#first')),
  1057. 'First',
  1058. '/a',
  1059. ' - ',
  1060. array('a' => array('href' => '#second')),
  1061. 'Second',
  1062. '/a',
  1063. );
  1064. $this->assertTags($result, $expected);
  1065. }
  1066. /**
  1067. * testNestedList method
  1068. *
  1069. * @return void
  1070. */
  1071. public function testNestedList() {
  1072. $list = array(
  1073. 'Item 1',
  1074. 'Item 2' => array(
  1075. 'Item 2.1'
  1076. ),
  1077. 'Item 3',
  1078. 'Item 4' => array(
  1079. 'Item 4.1',
  1080. 'Item 4.2',
  1081. 'Item 4.3' => array(
  1082. 'Item 4.3.1',
  1083. 'Item 4.3.2'
  1084. )
  1085. ),
  1086. 'Item 5' => array(
  1087. 'Item 5.1',
  1088. 'Item 5.2'
  1089. )
  1090. );
  1091. $result = $this->Html->nestedList($list);
  1092. $expected = array(
  1093. '<ul',
  1094. '<li', 'Item 1', '/li',
  1095. '<li', 'Item 2',
  1096. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1097. '/li',
  1098. '<li', 'Item 3', '/li',
  1099. '<li', 'Item 4',
  1100. '<ul',
  1101. '<li', 'Item 4.1', '/li',
  1102. '<li', 'Item 4.2', '/li',
  1103. '<li', 'Item 4.3',
  1104. '<ul',
  1105. '<li', 'Item 4.3.1', '/li',
  1106. '<li', 'Item 4.3.2', '/li',
  1107. '/ul',
  1108. '/li',
  1109. '/ul',
  1110. '/li',
  1111. '<li', 'Item 5',
  1112. '<ul',
  1113. '<li', 'Item 5.1', '/li',
  1114. '<li', 'Item 5.2', '/li',
  1115. '/ul',
  1116. '/li',
  1117. '/ul'
  1118. );
  1119. $this->assertTags($result, $expected);
  1120. $result = $this->Html->nestedList($list, null);
  1121. $this->assertTags($result, $expected);
  1122. $result = $this->Html->nestedList($list, array(), array(), 'ol');
  1123. $expected = array(
  1124. '<ol',
  1125. '<li', 'Item 1', '/li',
  1126. '<li', 'Item 2',
  1127. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1128. '/li',
  1129. '<li', 'Item 3', '/li',
  1130. '<li', 'Item 4',
  1131. '<ol',
  1132. '<li', 'Item 4.1', '/li',
  1133. '<li', 'Item 4.2', '/li',
  1134. '<li', 'Item 4.3',
  1135. '<ol',
  1136. '<li', 'Item 4.3.1', '/li',
  1137. '<li', 'Item 4.3.2', '/li',
  1138. '/ol',
  1139. '/li',
  1140. '/ol',
  1141. '/li',
  1142. '<li', 'Item 5',
  1143. '<ol',
  1144. '<li', 'Item 5.1', '/li',
  1145. '<li', 'Item 5.2', '/li',
  1146. '/ol',
  1147. '/li',
  1148. '/ol'
  1149. );
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Html->nestedList($list, 'ol');
  1152. $this->assertTags($result, $expected);
  1153. $result = $this->Html->nestedList($list, array('class' => 'list'));
  1154. $expected = array(
  1155. array('ul' => array('class' => 'list')),
  1156. '<li', 'Item 1', '/li',
  1157. '<li', 'Item 2',
  1158. array('ul' => array('class' => 'list')), '<li', 'Item 2.1', '/li', '/ul',
  1159. '/li',
  1160. '<li', 'Item 3', '/li',
  1161. '<li', 'Item 4',
  1162. array('ul' => array('class' => 'list')),
  1163. '<li', 'Item 4.1', '/li',
  1164. '<li', 'Item 4.2', '/li',
  1165. '<li', 'Item 4.3',
  1166. array('ul' => array('class' => 'list')),
  1167. '<li', 'Item 4.3.1', '/li',
  1168. '<li', 'Item 4.3.2', '/li',
  1169. '/ul',
  1170. '/li',
  1171. '/ul',
  1172. '/li',
  1173. '<li', 'Item 5',
  1174. array('ul' => array('class' => 'list')),
  1175. '<li', 'Item 5.1', '/li',
  1176. '<li', 'Item 5.2', '/li',
  1177. '/ul',
  1178. '/li',
  1179. '/ul'
  1180. );
  1181. $this->assertTags($result, $expected);
  1182. $result = $this->Html->nestedList($list, array(), array('class' => 'item'));
  1183. $expected = array(
  1184. '<ul',
  1185. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1186. array('li' => array('class' => 'item')), 'Item 2',
  1187. '<ul', array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1188. '/li',
  1189. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1190. array('li' => array('class' => 'item')), 'Item 4',
  1191. '<ul',
  1192. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1193. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1194. array('li' => array('class' => 'item')), 'Item 4.3',
  1195. '<ul',
  1196. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1197. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1198. '/ul',
  1199. '/li',
  1200. '/ul',
  1201. '/li',
  1202. array('li' => array('class' => 'item')), 'Item 5',
  1203. '<ul',
  1204. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1205. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1206. '/ul',
  1207. '/li',
  1208. '/ul'
  1209. );
  1210. $this->assertTags($result, $expected);
  1211. $result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
  1212. $expected = array(
  1213. '<ul',
  1214. array('li' => array('class' => 'odd')), 'Item 1', '/li',
  1215. array('li' => array('class' => 'even')), 'Item 2',
  1216. '<ul', array('li' => array('class' => 'odd')), 'Item 2.1', '/li', '/ul',
  1217. '/li',
  1218. array('li' => array('class' => 'odd')), 'Item 3', '/li',
  1219. array('li' => array('class' => 'even')), 'Item 4',
  1220. '<ul',
  1221. array('li' => array('class' => 'odd')), 'Item 4.1', '/li',
  1222. array('li' => array('class' => 'even')), 'Item 4.2', '/li',
  1223. array('li' => array('class' => 'odd')), 'Item 4.3',
  1224. '<ul',
  1225. array('li' => array('class' => 'odd')), 'Item 4.3.1', '/li',
  1226. array('li' => array('class' => 'even')), 'Item 4.3.2', '/li',
  1227. '/ul',
  1228. '/li',
  1229. '/ul',
  1230. '/li',
  1231. array('li' => array('class' => 'odd')), 'Item 5',
  1232. '<ul',
  1233. array('li' => array('class' => 'odd')), 'Item 5.1', '/li',
  1234. array('li' => array('class' => 'even')), 'Item 5.2', '/li',
  1235. '/ul',
  1236. '/li',
  1237. '/ul'
  1238. );
  1239. $this->assertTags($result, $expected);
  1240. $result = $this->Html->nestedList($list, array('class' => 'list'), array('class' => 'item'));
  1241. $expected = array(
  1242. array('ul' => array('class' => 'list')),
  1243. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1244. array('li' => array('class' => 'item')), 'Item 2',
  1245. array('ul' => array('class' => 'list')), array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1246. '/li',
  1247. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1248. array('li' => array('class' => 'item')), 'Item 4',
  1249. array('ul' => array('class' => 'list')),
  1250. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1251. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1252. array('li' => array('class' => 'item')), 'Item 4.3',
  1253. array('ul' => array('class' => 'list')),
  1254. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1255. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1256. '/ul',
  1257. '/li',
  1258. '/ul',
  1259. '/li',
  1260. array('li' => array('class' => 'item')), 'Item 5',
  1261. array('ul' => array('class' => 'list')),
  1262. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1263. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1264. '/ul',
  1265. '/li',
  1266. '/ul'
  1267. );
  1268. $this->assertTags($result, $expected);
  1269. }
  1270. /**
  1271. * testMeta method
  1272. *
  1273. * @return void
  1274. */
  1275. public function testMeta() {
  1276. Router::connect('/:controller', array('action' => 'index'));
  1277. $result = $this->Html->meta('this is an rss feed', array('controller' => 'posts', 'ext' => 'rss'));
  1278. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1279. $result = $this->Html->meta('rss', array('controller' => 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed'));
  1280. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1281. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'));
  1282. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1283. $result = $this->Html->meta('non-existing');
  1284. $this->assertTags($result, array('<meta'));
  1285. $result = $this->Html->meta('non-existing', '/posts.xpp');
  1286. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'non-existing')));
  1287. $result = $this->Html->meta('non-existing', '/posts.xpp', array('type' => 'atom'));
  1288. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing')));
  1289. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'), array('link' => '/articles.rss'));
  1290. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1291. $result = $this->Html->meta(array('link' => 'favicon.ico', 'rel' => 'icon'));
  1292. $expected = array(
  1293. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'icon'),
  1294. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'shortcut icon'))
  1295. );
  1296. $this->assertTags($result, $expected);
  1297. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1298. $this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
  1299. $result = $this->Html->meta('description', 'this is the meta description');
  1300. $this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));
  1301. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'));
  1302. $this->assertTags($result, array('meta' => array('name' => 'ROBOTS', 'content' => 'ALL')));
  1303. }
  1304. /**
  1305. * Test generating favicon's with meta()
  1306. *
  1307. * @return void
  1308. */
  1309. public function testMetaIcon() {
  1310. $result = $this->Html->meta('icon', 'favicon.ico');
  1311. $expected = array(
  1312. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1313. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1314. );
  1315. $this->assertTags($result, $expected);
  1316. $result = $this->Html->meta('icon');
  1317. $expected = array(
  1318. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1319. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1320. );
  1321. $this->assertTags($result, $expected);
  1322. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1323. $url = '/favicon.png?one=two&amp;three=four';
  1324. $expected = array(
  1325. 'link' => array(
  1326. 'href' => $url,
  1327. 'type' => 'image/x-icon',
  1328. 'rel' => 'icon'
  1329. ),
  1330. array(
  1331. 'link' => array(
  1332. 'href' => $url,
  1333. 'type' => 'image/x-icon',
  1334. 'rel' => 'shortcut icon'
  1335. )
  1336. )
  1337. );
  1338. $this->assertTags($result, $expected);
  1339. $this->Html->request->webroot = '/testing/';
  1340. $result = $this->Html->meta('icon');
  1341. $expected = array(
  1342. 'link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1343. array('link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1344. );
  1345. $this->assertTags($result, $expected);
  1346. }
  1347. /**
  1348. * Test the inline and block options for meta()
  1349. *
  1350. * @return void
  1351. */
  1352. public function testMetaWithBlocks() {
  1353. $this->View->expects($this->at(0))
  1354. ->method('append')
  1355. ->with('meta', $this->stringContains('ROBOTS'));
  1356. $this->View->expects($this->at(1))
  1357. ->method('append')
  1358. ->with('metaTags', $this->stringContains('favicon.ico'));
  1359. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('block' => true));
  1360. $this->assertNull($result);
  1361. $result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));
  1362. $this->assertNull($result);
  1363. }
  1364. /**
  1365. * testTableHeaders method
  1366. *
  1367. * @return void
  1368. */
  1369. public function testTableHeaders() {
  1370. $result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
  1371. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1372. $this->assertTags($result, $expected);
  1373. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight')), 'Date'));
  1374. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1375. $this->assertTags($result, $expected);
  1376. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight', 'width' => '120px')), 'Date'));
  1377. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1378. $this->assertTags($result, $expected);
  1379. $result = $this->Html->tableHeaders(array('ID', array('Name' => array()), 'Date'));
  1380. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1381. $this->assertTags($result, $expected);
  1382. }
  1383. /**
  1384. * testTableCells method
  1385. *
  1386. * @return void
  1387. */
  1388. public function testTableCells() {
  1389. $tr = array(
  1390. 'td content 1',
  1391. array('td content 2', array("width" => "100px")),
  1392. array('td content 3', array('width' => '100px'))
  1393. );
  1394. $result = $this->Html->tableCells($tr);
  1395. $expected = array(
  1396. '<tr',
  1397. '<td', 'td content 1', '/td',
  1398. array('td' => array('width' => '100px')), 'td content 2', '/td',
  1399. array('td' => array('width' => 'preg:/100px/')), 'td content 3', '/td',
  1400. '/tr'
  1401. );
  1402. $this->assertTags($result, $expected);
  1403. $tr = array('td content 1', 'td content 2', 'td content 3');
  1404. $result = $this->Html->tableCells($tr, null, null, true);
  1405. $expected = array(
  1406. '<tr',
  1407. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1408. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1409. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1410. '/tr'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $tr = array('td content 1', 'td content 2', 'td content 3');
  1414. $result = $this->Html->tableCells($tr, true);
  1415. $expected = array(
  1416. '<tr',
  1417. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1418. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1419. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1420. '/tr'
  1421. );
  1422. $this->assertTags($result, $expected);
  1423. $tr = array(
  1424. array('td content 1', 'td content 2', 'td content 3'),
  1425. array('td content 1', 'td content 2', 'td content 3'),
  1426. array('td content 1', 'td content 2', 'td content 3')
  1427. );
  1428. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1429. $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>";
  1430. $this->assertEquals($expected, $result);
  1431. $tr = array(
  1432. array('td content 1', 'td content 2', 'td content 3'),
  1433. array('td content 1', 'td content 2', 'td content 3'),
  1434. array('td content 1', 'td content 2', 'td content 3'),
  1435. array('td content 1', 'td content 2', 'td content 3')
  1436. );
  1437. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1438. $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>";
  1439. $this->assertEquals($expected, $result);
  1440. $tr = array(
  1441. array('td content 1', 'td content 2', 'td content 3'),
  1442. array('td content 1', 'td content 2', 'td content 3'),
  1443. array('td content 1', 'td content 2', 'td content 3')
  1444. );
  1445. $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1446. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'), false, false);
  1447. $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>";
  1448. $this->assertEquals($expected, $result);
  1449. }
  1450. /**
  1451. * testTag method
  1452. *
  1453. * @return void
  1454. */
  1455. public function testTag() {
  1456. $result = $this->Html->tag('div');
  1457. $this->assertTags($result, '<div');
  1458. $result = $this->Html->tag('div', 'text');
  1459. $this->assertTags($result, '<div', 'text', '/div');
  1460. $result = $this->Html->tag('div', '<text>', array('class' => 'class-name', 'escape' => true));
  1461. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1462. $result = $this->Html->tag(false, '<em>stuff</em>');
  1463. $this->assertEquals('<em>stuff</em>', $result);
  1464. $result = $this->Html->tag(null, '<em>stuff</em>');
  1465. $this->assertEquals('<em>stuff</em>', $result);
  1466. $result = $this->Html->tag('', '<em>stuff</em>');
  1467. $this->assertEquals('<em>stuff</em>', $result);
  1468. }
  1469. /**
  1470. * testDiv method
  1471. *
  1472. * @return void
  1473. */
  1474. public function testDiv() {
  1475. $result = $this->Html->div('class-name');
  1476. $this->assertTags($result, array('div' => array('class' => 'class-name')));
  1477. $result = $this->Html->div('class-name', 'text');
  1478. $this->assertTags($result, array('div' => array('class' => 'class-name'), 'text', '/div'));
  1479. $result = $this->Html->div('class-name', '<text>', array('escape' => true));
  1480. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1481. }
  1482. /**
  1483. * testPara method
  1484. *
  1485. * @return void
  1486. */
  1487. public function testPara() {
  1488. $result = $this->Html->para('class-name', '');
  1489. $this->assertTags($result, array('p' => array('class' => 'class-name')));
  1490. $result = $this->Html->para('class-name', 'text');
  1491. $this->assertTags($result, array('p' => array('class' => 'class-name'), 'text', '/p'));
  1492. $result = $this->Html->para('class-name', '<text>', array('escape' => true));
  1493. $this->assertTags($result, array('p' => array('class' => 'class-name'), '&lt;text&gt;', '/p'));
  1494. }
  1495. /**
  1496. * testMedia method
  1497. *
  1498. * @return void
  1499. */
  1500. public function testMedia() {
  1501. $result = $this->Html->media('video.webm');
  1502. $expected = array('video' => array('src' => 'files/video.webm'), '/video');
  1503. $this->assertTags($result, $expected);
  1504. $result = $this->Html->media('video.webm', array(
  1505. 'text' => 'Your browser does not support the HTML5 Video element.'
  1506. ));
  1507. $expected = array('video' => array('src' => 'files/video.webm'), 'Your browser does not support the HTML5 Video element.', '/video');
  1508. $this->assertTags($result, $expected);
  1509. $result = $this->Html->media('video.webm', array('autoload', 'muted' => 'muted'));
  1510. $expected = array(
  1511. 'video' => array(
  1512. 'src' => 'files/video.webm',
  1513. 'autoload' => 'autoload',
  1514. 'muted' => 'muted'
  1515. ),
  1516. '/video'
  1517. );
  1518. $this->assertTags($result, $expected);
  1519. $result = $this->Html->media(
  1520. array('video.webm', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  1521. array('pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.')
  1522. );
  1523. $expected = array(
  1524. 'video' => array('poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'),
  1525. array('source' => array('src' => 'videos/video.webm', 'type' => 'video/webm')),
  1526. array('source' => array('src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;')),
  1527. 'Your browser does not support the HTML5 Video element.',
  1528. '/video'
  1529. );
  1530. $this->assertTags($result, $expected);
  1531. $result = $this->Html->media('video.ogv', array('tag' => 'video'));
  1532. $expected = array('video' => array('src' => 'files/video.ogv'), '/video');
  1533. $this->assertTags($result, $expected);
  1534. $result = $this->Html->media('audio.mp3');
  1535. $expected = array('audio' => array('src' => 'files/audio.mp3'), '/audio');
  1536. $this->assertTags($result, $expected);
  1537. $result = $this->Html->media(
  1538. array(array('src' => 'video.mov', 'type' => 'video/mp4'), 'video.webm')
  1539. );
  1540. $expected = array(
  1541. '<video',
  1542. array('source' => array('src' => 'files/video.mov', 'type' => 'video/mp4')),
  1543. array('source' => array('src' => 'files/video.webm', 'type' => 'video/webm')),
  1544. '/video'
  1545. );
  1546. $this->assertTags($result, $expected);
  1547. $result = $this->Html->media(null, array('src' => 'video.webm'));
  1548. $expected = array(
  1549. 'video' => array('src' => 'files/video.webm'),
  1550. '/video'
  1551. );
  1552. $this->assertTags($result, $expected);
  1553. }
  1554. /**
  1555. * testCrumbList method
  1556. *
  1557. * @return void
  1558. */
  1559. public function testCrumbList() {
  1560. $this->assertNull($this->Html->getCrumbList());
  1561. $this->Html->addCrumb('Home', '/', array('class' => 'home'));
  1562. $this->Html->addCrumb('Some page', '/some_page');
  1563. $this->Html->addCrumb('Another page');
  1564. $result = $this->Html->getCrumbList(
  1565. array('class' => 'breadcrumbs')
  1566. );
  1567. $this->assertTags(
  1568. $result,
  1569. array(
  1570. array('ul' => array('class' => 'breadcrumbs')),
  1571. array('li' => array('class' => 'first')),
  1572. array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
  1573. '/li',
  1574. '<li',
  1575. array('a' => array('href' => '/some_page')), 'Some page', '/a',
  1576. '/li',
  1577. array('li' => array('class' => 'last')),
  1578. 'Another page',
  1579. '/li',
  1580. '/ul'
  1581. )
  1582. );
  1583. }
  1584. /**
  1585. * Test getCrumbList startText
  1586. *
  1587. * @return void
  1588. */
  1589. public function testCrumbListFirstLink() {
  1590. $this->Html->addCrumb('First', '#first');
  1591. $this->Html->addCrumb('Second', '#second');
  1592. $result = $this->Html->getCrumbList(array(), 'Home');
  1593. $this->assertTags(
  1594. $result,
  1595. array(
  1596. '<ul',
  1597. array('li' => array('class' => 'first')),
  1598. array('a' => array('href' => '/')), 'Home', '/a',
  1599. '/li',
  1600. '<li',
  1601. array('a' => array('href' => '#first')), 'First', '/a',
  1602. '/li',
  1603. array('li' => array('class' => 'last')),
  1604. array('a' => array('href' => '#second')), 'Second', '/a',
  1605. '/li',
  1606. '/ul'
  1607. )
  1608. );
  1609. $result = $this->Html->getCrumbList(array(), array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  1610. $this->assertTags(
  1611. $result,
  1612. array(
  1613. '<ul',
  1614. array('li' => array('class' => 'first')),
  1615. array('a' => array('href' => '/home')), 'img' => array('src' => '/home.png'), '/a',
  1616. '/li',
  1617. '<li',
  1618. array('a' => array('href' => '#first')), 'First', '/a',
  1619. '/li',
  1620. array('li' => array('class' => 'last')),
  1621. array('a' => array('href' => '#second')), 'Second', '/a',
  1622. '/li',
  1623. '/ul'
  1624. )
  1625. );
  1626. }
  1627. /**
  1628. * test getCrumbList() in Twitter Bootstrap style.
  1629. *
  1630. * @return void
  1631. */
  1632. public function testCrumbListBootstrapStyle() {
  1633. $this->Html->addCrumb('Home', '/', array('class' => 'home'));
  1634. $this->Html->addCrumb('Library', '/lib');
  1635. $this->Html->addCrumb('Data');
  1636. $result = $this->Html->getCrumbList(array(
  1637. 'class' => 'breadcrumb',
  1638. 'separator' => '<span class="divider">-</span>',
  1639. 'firstClass' => false,
  1640. 'lastClass' => 'active'
  1641. ));
  1642. $this->assertTags(
  1643. $result,
  1644. array(
  1645. array('ul' => array('class' => 'breadcrumb')),
  1646. '<li',
  1647. array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
  1648. array('span' => array('class' => 'divider')), '-', '/span',
  1649. '/li',
  1650. '<li',
  1651. array('a' => array('href' => '/lib')), 'Library', '/a',
  1652. array('span' => array('class' => 'divider')), '-', '/span',
  1653. '/li',
  1654. array('li' => array('class' => 'active')), 'Data', '/li',
  1655. '/ul'
  1656. )
  1657. );
  1658. }
  1659. /**
  1660. * Test GetCrumbList using style of Zurb Foundation.
  1661. *
  1662. * @return void
  1663. */
  1664. public function testCrumbListZurbStyle() {
  1665. $this->Html->addCrumb('Home', '#');
  1666. $this->Html->addCrumb('Features', '#');
  1667. $this->Html->addCrumb('Gene Splicing', '#');
  1668. $this->Html->addCrumb('Home', '#');
  1669. $result = $this->Html->getCrumbList(
  1670. array('class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current')
  1671. );
  1672. $this->assertTags(
  1673. $result,
  1674. array(
  1675. array('ul' => array('class' => 'breadcrumbs')),
  1676. '<li',
  1677. array('a' => array('href' => '#')), 'Home', '/a',
  1678. '/li',
  1679. '<li',
  1680. array('a' => array('href' => '#')), 'Features', '/a',
  1681. '/li',
  1682. '<li',
  1683. array('a' => array('href' => '#')), 'Gene Splicing', '/a',
  1684. '/li',
  1685. array('li' => array('class' => 'current')),
  1686. array('a' => array('href' => '#')), 'Home', '/a',
  1687. '/li',
  1688. '/ul'
  1689. ), true
  1690. );
  1691. }
  1692. }