HtmlHelperTest.php 62 KB

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