HtmlHelperTest.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. <?php
  2. /**
  3. * HtmlHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing 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. 'plugins' => 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. }
  292. /**
  293. * testImageTag method
  294. *
  295. * @return void
  296. */
  297. public function testImageTag() {
  298. $this->Html->request->webroot = '';
  299. $result = $this->Html->image('test.gif');
  300. $this->assertTags($result, array('img' => array('src' => 'img/test.gif', 'alt' => '')));
  301. $result = $this->Html->image('http://google.com/logo.gif');
  302. $this->assertTags($result, array('img' => array('src' => 'http://google.com/logo.gif', 'alt' => '')));
  303. $result = $this->Html->image(array('controller' => 'test', 'action' => 'view', 1, 'ext' => 'gif'));
  304. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  305. $result = $this->Html->image('/test/view/1.gif');
  306. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  307. $result = $this->Html->image('test.gif', array('fullBase' => true));
  308. $here = $this->Html->url('/', true);
  309. $this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => '')));
  310. $result = $this->Html->image('sub/test.gif', array('fullBase' => true));
  311. $here = $this->Html->url('/', true);
  312. $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
  313. }
  314. /**
  315. * test image() with Asset.timestamp
  316. *
  317. * @return void
  318. */
  319. public function testImageWithTimestampping() {
  320. Configure::write('Asset.timestamp', 'force');
  321. $this->Html->request->webroot = '/';
  322. $result = $this->Html->image('cake.icon.png');
  323. $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
  324. Configure::write('debug', 0);
  325. Configure::write('Asset.timestamp', 'force');
  326. $result = $this->Html->image('cake.icon.png');
  327. $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
  328. $this->Html->request->webroot = '/testing/longer/';
  329. $result = $this->Html->image('cake.icon.png');
  330. $expected = array(
  331. 'img' => array('src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '')
  332. );
  333. $this->assertTags($result, $expected);
  334. }
  335. /**
  336. * Tests creation of an image tag using a theme and asset timestamping
  337. *
  338. * @return void
  339. */
  340. public function testImageTagWithTheme() {
  341. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  342. $themeExists = is_dir(WWW_ROOT . 'theme');
  343. App::uses('File', 'Utility');
  344. $testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'img' . DS . '__cake_test_image.gif';
  345. $File = new File($testfile, true);
  346. App::build(array(
  347. 'views' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  348. ));
  349. Configure::write('Asset.timestamp', true);
  350. Configure::write('debug', 1);
  351. $this->Html->request->webroot = '/';
  352. $this->Html->theme = 'test_theme';
  353. $result = $this->Html->image('__cake_test_image.gif');
  354. $this->assertTags($result, array(
  355. 'img' => array(
  356. 'src' => 'preg:/\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  357. 'alt' => ''
  358. )));
  359. $this->Html->request->webroot = '/testing/';
  360. $result = $this->Html->image('__cake_test_image.gif');
  361. $this->assertTags($result, array(
  362. 'img' => array(
  363. 'src' => 'preg:/\/testing\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  364. 'alt' => ''
  365. )));
  366. $dir = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
  367. $dir->delete();
  368. if (!$themeExists) {
  369. $dir = new Folder(WWW_ROOT . 'theme');
  370. $dir->delete();
  371. }
  372. }
  373. /**
  374. * test theme assets in main webroot path
  375. *
  376. * @return void
  377. */
  378. public function testThemeAssetsInMainWebrootPath() {
  379. App::build(array(
  380. 'views' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  381. ));
  382. $webRoot = Configure::read('App.www_root');
  383. Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
  384. $this->Html->theme = 'test_theme';
  385. $result = $this->Html->css('webroot_test');
  386. $expected = array(
  387. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/webroot_test\.css/')
  388. );
  389. $this->assertTags($result, $expected);
  390. $this->Html->theme = 'test_theme';
  391. $result = $this->Html->css('theme_webroot');
  392. $expected = array(
  393. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/theme_webroot\.css/')
  394. );
  395. $this->assertTags($result, $expected);
  396. Configure::write('App.www_root', $webRoot);
  397. }
  398. /**
  399. * testStyle method
  400. *
  401. * @return void
  402. */
  403. public function testStyle() {
  404. $result = $this->Html->style('display: none;');
  405. $this->assertEquals($result, 'display: none;');
  406. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'));
  407. $expected = 'display:none; margin:10px;';
  408. $this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
  409. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
  410. $lines = explode("\n", $result);
  411. $this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
  412. $this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
  413. }
  414. /**
  415. * testCssLink method
  416. *
  417. * @return void
  418. */
  419. public function testCssLink() {
  420. Configure::write('Asset.filter.css', false);
  421. $result = $this->Html->css('screen');
  422. $expected = array(
  423. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/')
  424. );
  425. $this->assertTags($result, $expected);
  426. $result = $this->Html->css('screen.css');
  427. $this->assertTags($result, $expected);
  428. CakePlugin::load('TestPlugin');
  429. $result = $this->Html->css('TestPlugin.style', null, array('plugin' => false));
  430. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  431. $this->assertTags($result, $expected);
  432. CakePlugin::unload('TestPlugin');
  433. $result = $this->Html->css('my.css.library');
  434. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  435. $this->assertTags($result, $expected);
  436. $result = $this->Html->css('screen.css?1234');
  437. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  438. $this->assertTags($result, $expected);
  439. $result = $this->Html->css('http://whatever.com/screen.css?1234');
  440. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  441. $this->assertTags($result, $expected);
  442. Configure::write('Asset.filter.css', 'css.php');
  443. $result = $this->Html->css('cake.generic');
  444. $expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css/';
  445. $this->assertTags($result, $expected);
  446. $result = $this->Html->css('//example.com/css/cake.generic.css');
  447. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  448. $this->assertTags($result, $expected);
  449. Configure::write('Asset.filter.css', false);
  450. $result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
  451. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  452. $this->assertTags($result[0], $expected);
  453. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  454. $this->assertTags($result[1], $expected);
  455. $this->assertEquals(count($result), 2);
  456. $this->View->expects($this->at(0))
  457. ->method('append')
  458. ->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
  459. $this->View->expects($this->at(1))
  460. ->method('append')
  461. ->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
  462. $result = $this->Html->css('css_in_head', null, array('inline' => false));
  463. $this->assertNull($result);
  464. $result = $this->Html->css('more_css_in_head', null, array('inline' => false));
  465. $this->assertNull($result);
  466. }
  467. /**
  468. * testPluginCssLink method
  469. *
  470. * @return void
  471. */
  472. public function testPluginCssLink() {
  473. Configure::write('Asset.filter.css', false);
  474. CakePlugin::load('TestPlugin');
  475. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  476. $expected = array(
  477. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/')
  478. );
  479. $this->assertTags($result, $expected);
  480. $result = $this->Html->css('TestPlugin.test_plugin_asset.css');
  481. $this->assertTags($result, $expected);
  482. $result = $this->Html->css('TestPlugin.my.css.library');
  483. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  484. $this->assertTags($result, $expected);
  485. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  486. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  487. $this->assertTags($result, $expected);
  488. Configure::write('Asset.filter.css', 'css.php');
  489. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  490. $expected['link']['href'] = 'preg:/.*test_plugin\/ccss\/test_plugin_asset\.css/';
  491. $this->assertTags($result, $expected);
  492. Configure::write('Asset.filter.css', false);
  493. $result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
  494. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  495. $this->assertTags($result[0], $expected);
  496. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  497. $this->assertTags($result[1], $expected);
  498. $this->assertEquals(count($result), 2);
  499. CakePlugin::unload('TestPlugin');
  500. }
  501. /**
  502. * test use of css() and timestamping
  503. *
  504. * @return void
  505. */
  506. public function testCssTimestamping() {
  507. Configure::write('debug', 2);
  508. Configure::write('Asset.timestamp', true);
  509. $expected = array(
  510. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
  511. );
  512. $result = $this->Html->css('cake.generic');
  513. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  514. $this->assertTags($result, $expected);
  515. Configure::write('debug', 0);
  516. $result = $this->Html->css('cake.generic');
  517. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  518. $this->assertTags($result, $expected);
  519. Configure::write('Asset.timestamp', 'force');
  520. $result = $this->Html->css('cake.generic');
  521. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  522. $this->assertTags($result, $expected);
  523. $this->Html->request->webroot = '/testing/';
  524. $result = $this->Html->css('cake.generic');
  525. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  526. $this->assertTags($result, $expected);
  527. $this->Html->request->webroot = '/testing/longer/';
  528. $result = $this->Html->css('cake.generic');
  529. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  530. $this->assertTags($result, $expected);
  531. }
  532. /**
  533. * test use of css() and timestamping with plugin syntax
  534. *
  535. * @return void
  536. */
  537. public function testPluginCssTimestamping() {
  538. CakePlugin::load('TestPlugin');
  539. Configure::write('debug', 2);
  540. Configure::write('Asset.timestamp', true);
  541. $expected = array(
  542. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
  543. );
  544. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  545. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  546. $this->assertTags($result, $expected);
  547. Configure::write('debug', 0);
  548. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  549. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  550. $this->assertTags($result, $expected);
  551. Configure::write('Asset.timestamp', 'force');
  552. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  553. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  554. $this->assertTags($result, $expected);
  555. $this->Html->request->webroot = '/testing/';
  556. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  557. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  558. $this->assertTags($result, $expected);
  559. $this->Html->request->webroot = '/testing/longer/';
  560. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  561. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  562. $this->assertTags($result, $expected);
  563. CakePlugin::unload('TestPlugin');
  564. }
  565. /**
  566. * test timestamp enforcement for script tags.
  567. *
  568. * @return void
  569. */
  570. public function testScriptTimestamping() {
  571. $this->skipIf(!is_writable(JS), 'webroot/js is not Writable, timestamp testing has been skipped.');
  572. Configure::write('debug', 2);
  573. Configure::write('Asset.timestamp', true);
  574. touch(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
  575. $timestamp = substr(strtotime('now'), 0, 8);
  576. $result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
  577. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  578. Configure::write('debug', 0);
  579. Configure::write('Asset.timestamp', 'force');
  580. $result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
  581. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  582. unlink(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
  583. Configure::write('Asset.timestamp', false);
  584. }
  585. /**
  586. * test timestamp enforcement for script tags with plugin syntax.
  587. *
  588. * @return void
  589. */
  590. public function testPluginScriptTimestamping() {
  591. CakePlugin::load('TestPlugin');
  592. $pluginPath = App::pluginPath('TestPlugin');
  593. $pluginJsPath = $pluginPath . 'webroot/js';
  594. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  595. Configure::write('debug', 2);
  596. Configure::write('Asset.timestamp', true);
  597. touch($pluginJsPath . DS . '__cake_js_test.js');
  598. $timestamp = substr(strtotime('now'), 0, 8);
  599. $result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
  600. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  601. Configure::write('debug', 0);
  602. Configure::write('Asset.timestamp', 'force');
  603. $result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
  604. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  605. unlink($pluginJsPath . DS . '__cake_js_test.js');
  606. Configure::write('Asset.timestamp', false);
  607. CakePlugin::unload('TestPlugin');
  608. }
  609. /**
  610. * test that scripts added with uses() are only ever included once.
  611. * test script tag generation
  612. *
  613. * @return void
  614. */
  615. public function testScript() {
  616. $result = $this->Html->script('foo');
  617. $expected = array(
  618. 'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
  619. );
  620. $this->assertTags($result, $expected);
  621. $result = $this->Html->script(array('foobar', 'bar'));
  622. $expected = array(
  623. array('script' => array('type' => 'text/javascript', 'src' => 'js/foobar.js')),
  624. '/script',
  625. array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
  626. '/script',
  627. );
  628. $this->assertTags($result, $expected);
  629. $result = $this->Html->script('jquery-1.3');
  630. $expected = array(
  631. 'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
  632. );
  633. $this->assertTags($result, $expected);
  634. $result = $this->Html->script('test.json');
  635. $expected = array(
  636. 'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js')
  637. );
  638. $this->assertTags($result, $expected);
  639. $result = $this->Html->script('http://example.com/test.json');
  640. $expected = array(
  641. 'script' => array('type' => 'text/javascript', 'src' => 'http://example.com/test.json')
  642. );
  643. $this->assertTags($result, $expected);
  644. $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
  645. $expected = array(
  646. 'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
  647. );
  648. $this->assertTags($result, $expected);
  649. $result = $this->Html->script('test.json.js?foo=bar');
  650. $expected = array(
  651. 'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js?foo=bar')
  652. );
  653. $this->assertTags($result, $expected);
  654. $result = $this->Html->script('foo');
  655. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  656. $result = $this->Html->script(array('foo', 'bar', 'baz'));
  657. $this->assertNotRegExp('/foo.js/', $result);
  658. $result = $this->Html->script('foo', array('inline' => true, 'once' => false));
  659. $this->assertNotNull($result);
  660. $result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  661. $expected = array(
  662. 'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  663. );
  664. $this->assertTags($result, $expected);
  665. }
  666. /**
  667. * test that plugin scripts added with uses() are only ever included once.
  668. * test script tag generation with plugin syntax
  669. *
  670. * @return void
  671. */
  672. public function testPluginScript() {
  673. CakePlugin::load('TestPlugin');
  674. $result = $this->Html->script('TestPlugin.foo');
  675. $expected = array(
  676. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foo.js')
  677. );
  678. $this->assertTags($result, $expected);
  679. $result = $this->Html->script(array('TestPlugin.foobar', 'TestPlugin.bar'));
  680. $expected = array(
  681. array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foobar.js')),
  682. '/script',
  683. array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/bar.js')),
  684. '/script',
  685. );
  686. $this->assertTags($result, $expected);
  687. $result = $this->Html->script('TestPlugin.jquery-1.3');
  688. $expected = array(
  689. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.js')
  690. );
  691. $this->assertTags($result, $expected);
  692. $result = $this->Html->script('TestPlugin.test.json');
  693. $expected = array(
  694. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js')
  695. );
  696. $this->assertTags($result, $expected);
  697. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  698. $expected = array(
  699. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/jquery-1.3.2.js?someparam=foo')
  700. );
  701. $this->assertTags($result, $expected);
  702. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  703. $expected = array(
  704. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js?foo=bar')
  705. );
  706. $this->assertTags($result, $expected);
  707. $result = $this->Html->script('TestPlugin.foo');
  708. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  709. $result = $this->Html->script(array('TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz'));
  710. $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
  711. $result = $this->Html->script('TestPlugin.foo', array('inline' => true, 'once' => false));
  712. $this->assertNotNull($result);
  713. $result = $this->Html->script('TestPlugin.jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  714. $expected = array(
  715. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  716. );
  717. $this->assertTags($result, $expected);
  718. CakePlugin::unload('TestPlugin');
  719. }
  720. /**
  721. * test that script() works with blocks.
  722. *
  723. * @return void
  724. */
  725. public function testScriptWithBlocks() {
  726. $this->View->expects($this->at(0))
  727. ->method('append')
  728. ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
  729. $this->View->expects($this->at(1))
  730. ->method('append')
  731. ->with('script', $this->matchesRegularExpression('/bool_false.js/'));
  732. $this->View->expects($this->at(2))
  733. ->method('append')
  734. ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
  735. $result = $this->Html->script('script_in_head', array('inline' => false));
  736. $this->assertNull($result);
  737. $result = $this->Html->script('bool_false', false);
  738. $this->assertNull($result);
  739. $result = $this->Html->script('second_script', array('block' => 'headScripts'));
  740. $this->assertNull($result);
  741. }
  742. /**
  743. * Test that Asset.filter.js works.
  744. *
  745. * @return void
  746. */
  747. function testScriptAssetFilter() {
  748. Configure::write('Asset.filter.js', 'js.php');
  749. $result = $this->Html->script('jquery-1.3');
  750. $expected = array(
  751. 'script' => array('type' => 'text/javascript', 'src' => 'cjs/jquery-1.3.js')
  752. );
  753. $this->assertTags($result, $expected);
  754. $result = $this->Html->script('//example.com/js/jquery-1.3.js');
  755. $expected = array(
  756. 'script' => array('type' => 'text/javascript', 'src' => '//example.com/js/jquery-1.3.js')
  757. );
  758. $this->assertTags($result, $expected);
  759. }
  760. /**
  761. * test a script file in the webroot/theme dir.
  762. *
  763. * @return void
  764. */
  765. public function testScriptInTheme() {
  766. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  767. $themeExists = is_dir(WWW_ROOT . 'theme');
  768. App::uses('File', 'Utility');
  769. $testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'js' . DS . '__test_js.js';
  770. $File = new File($testfile, true);
  771. App::build(array(
  772. 'views' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  773. ));
  774. $this->Html->webroot = '/';
  775. $this->Html->theme = 'test_theme';
  776. $result = $this->Html->script('__test_js.js');
  777. $expected = array(
  778. 'script' => array('src' => '/theme/test_theme/js/__test_js.js', 'type' => 'text/javascript')
  779. );
  780. $this->assertTags($result, $expected);
  781. $Folder = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
  782. $Folder->delete();
  783. if (!$themeExists) {
  784. $dir = new Folder(WWW_ROOT . 'theme');
  785. $dir->delete();
  786. }
  787. }
  788. /**
  789. * test Script block generation
  790. *
  791. * @return void
  792. */
  793. public function testScriptBlock() {
  794. $result = $this->Html->scriptBlock('window.foo = 2;');
  795. $expected = array(
  796. 'script' => array('type' => 'text/javascript'),
  797. $this->cDataStart,
  798. 'window.foo = 2;',
  799. $this->cDataEnd,
  800. '/script',
  801. );
  802. $this->assertTags($result, $expected);
  803. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
  804. $expected = array(
  805. 'script' => array('type' => 'text/javascript'),
  806. 'window.foo = 2;',
  807. '/script',
  808. );
  809. $this->assertTags($result, $expected);
  810. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
  811. $expected = array(
  812. 'script' => array('type' => 'text/javascript'),
  813. $this->cDataStart,
  814. 'window.foo = 2;',
  815. $this->cDataEnd,
  816. '/script',
  817. );
  818. $this->assertTags($result, $expected);
  819. $this->View->expects($this->at(0))
  820. ->method('append')
  821. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  822. $this->View->expects($this->at(1))
  823. ->method('append')
  824. ->with('scriptTop', $this->stringContains('alert('));
  825. $result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
  826. $this->assertNull($result);
  827. $result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
  828. $this->assertNull($result);
  829. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
  830. $expected = array(
  831. 'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
  832. 'window.foo = 2;',
  833. '/script',
  834. );
  835. $this->assertTags($result, $expected);
  836. }
  837. /**
  838. * test script tag output buffering when using scriptStart() and scriptEnd();
  839. *
  840. * @return void
  841. */
  842. public function testScriptStartAndScriptEnd() {
  843. $result = $this->Html->scriptStart(array('safe' => true));
  844. $this->assertNull($result);
  845. echo 'this is some javascript';
  846. $result = $this->Html->scriptEnd();
  847. $expected = array(
  848. 'script' => array('type' => 'text/javascript'),
  849. $this->cDataStart,
  850. 'this is some javascript',
  851. $this->cDataEnd,
  852. '/script'
  853. );
  854. $this->assertTags($result, $expected);
  855. $result = $this->Html->scriptStart(array('safe' => false));
  856. $this->assertNull($result);
  857. echo 'this is some javascript';
  858. $result = $this->Html->scriptEnd();
  859. $expected = array(
  860. 'script' => array('type' => 'text/javascript'),
  861. 'this is some javascript',
  862. '/script'
  863. );
  864. $this->assertTags($result, $expected);
  865. $this->View->expects($this->once())
  866. ->method('append');
  867. $result = $this->Html->scriptStart(array('safe' => false, 'inline' => false));
  868. $this->assertNull($result);
  869. echo 'this is some javascript';
  870. $result = $this->Html->scriptEnd();
  871. $this->assertNull($result);
  872. }
  873. /**
  874. * testCharsetTag method
  875. *
  876. * @return void
  877. */
  878. public function testCharsetTag() {
  879. Configure::write('App.encoding', null);
  880. $result = $this->Html->charset();
  881. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')));
  882. Configure::write('App.encoding', 'ISO-8859-1');
  883. $result = $this->Html->charset();
  884. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1')));
  885. $result = $this->Html->charset('UTF-7');
  886. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7')));
  887. }
  888. /**
  889. * testGetCrumb and addCrumb method
  890. *
  891. * @return void
  892. */
  893. public function testBreadcrumb() {
  894. $this->assertNull($this->Html->getCrumbs());
  895. $this->Html->addCrumb('First', '#first');
  896. $this->Html->addCrumb('Second', '#second');
  897. $this->Html->addCrumb('Third', '#third');
  898. $result = $this->Html->getCrumbs();
  899. $expected = array(
  900. array('a' => array('href' => '#first')),
  901. 'First',
  902. '/a',
  903. '&raquo;',
  904. array('a' => array('href' => '#second')),
  905. 'Second',
  906. '/a',
  907. '&raquo;',
  908. array('a' => array('href' => '#third')),
  909. 'Third',
  910. '/a',
  911. );
  912. $this->assertTags($result, $expected);
  913. $result = $this->Html->getCrumbs(' &gt; ');
  914. $expected = array(
  915. array('a' => array('href' => '#first')),
  916. 'First',
  917. '/a',
  918. ' &gt; ',
  919. array('a' => array('href' => '#second')),
  920. 'Second',
  921. '/a',
  922. ' &gt; ',
  923. array('a' => array('href' => '#third')),
  924. 'Third',
  925. '/a',
  926. );
  927. $this->assertTags($result, $expected);
  928. $this->Html->addCrumb('Fourth', null);
  929. $result = $this->Html->getCrumbs();
  930. $expected = array(
  931. array('a' => array('href' => '#first')),
  932. 'First',
  933. '/a',
  934. '&raquo;',
  935. array('a' => array('href' => '#second')),
  936. 'Second',
  937. '/a',
  938. '&raquo;',
  939. array('a' => array('href' => '#third')),
  940. 'Third',
  941. '/a',
  942. '&raquo;',
  943. 'Fourth'
  944. );
  945. $this->assertTags($result, $expected);
  946. $result = $this->Html->getCrumbs('-', 'Start');
  947. $expected = array(
  948. array('a' => array('href' => '/')),
  949. 'Start',
  950. '/a',
  951. '-',
  952. array('a' => array('href' => '#first')),
  953. 'First',
  954. '/a',
  955. '-',
  956. array('a' => array('href' => '#second')),
  957. 'Second',
  958. '/a',
  959. '-',
  960. array('a' => array('href' => '#third')),
  961. 'Third',
  962. '/a',
  963. '-',
  964. 'Fourth'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * Test the array form of $startText
  970. */
  971. public function testGetCrumbFirstLink() {
  972. $this->Html->addCrumb('First', '#first');
  973. $this->Html->addCrumb('Second', '#second');
  974. $result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  975. $expected = array(
  976. array('a' => array('href' => '/home')),
  977. 'img' => array('src' => '/home.png'),
  978. '/a',
  979. ' - ',
  980. array('a' => array('href' => '#first')),
  981. 'First',
  982. '/a',
  983. ' - ',
  984. array('a' => array('href' => '#second')),
  985. 'Second',
  986. '/a',
  987. );
  988. $this->assertTags($result, $expected);
  989. }
  990. /**
  991. * testNestedList method
  992. *
  993. * @return void
  994. */
  995. public function testNestedList() {
  996. $list = array(
  997. 'Item 1',
  998. 'Item 2' => array(
  999. 'Item 2.1'
  1000. ),
  1001. 'Item 3',
  1002. 'Item 4' => array(
  1003. 'Item 4.1',
  1004. 'Item 4.2',
  1005. 'Item 4.3' => array(
  1006. 'Item 4.3.1',
  1007. 'Item 4.3.2'
  1008. )
  1009. ),
  1010. 'Item 5' => array(
  1011. 'Item 5.1',
  1012. 'Item 5.2'
  1013. )
  1014. );
  1015. $result = $this->Html->nestedList($list);
  1016. $expected = array(
  1017. '<ul',
  1018. '<li', 'Item 1', '/li',
  1019. '<li', 'Item 2',
  1020. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1021. '/li',
  1022. '<li', 'Item 3', '/li',
  1023. '<li', 'Item 4',
  1024. '<ul',
  1025. '<li', 'Item 4.1', '/li',
  1026. '<li', 'Item 4.2', '/li',
  1027. '<li', 'Item 4.3',
  1028. '<ul',
  1029. '<li', 'Item 4.3.1', '/li',
  1030. '<li', 'Item 4.3.2', '/li',
  1031. '/ul',
  1032. '/li',
  1033. '/ul',
  1034. '/li',
  1035. '<li', 'Item 5',
  1036. '<ul',
  1037. '<li', 'Item 5.1', '/li',
  1038. '<li', 'Item 5.2', '/li',
  1039. '/ul',
  1040. '/li',
  1041. '/ul'
  1042. );
  1043. $this->assertTags($result, $expected);
  1044. $result = $this->Html->nestedList($list, null);
  1045. $expected = array(
  1046. '<ul',
  1047. '<li', 'Item 1', '/li',
  1048. '<li', 'Item 2',
  1049. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1050. '/li',
  1051. '<li', 'Item 3', '/li',
  1052. '<li', 'Item 4',
  1053. '<ul',
  1054. '<li', 'Item 4.1', '/li',
  1055. '<li', 'Item 4.2', '/li',
  1056. '<li', 'Item 4.3',
  1057. '<ul',
  1058. '<li', 'Item 4.3.1', '/li',
  1059. '<li', 'Item 4.3.2', '/li',
  1060. '/ul',
  1061. '/li',
  1062. '/ul',
  1063. '/li',
  1064. '<li', 'Item 5',
  1065. '<ul',
  1066. '<li', 'Item 5.1', '/li',
  1067. '<li', 'Item 5.2', '/li',
  1068. '/ul',
  1069. '/li',
  1070. '/ul'
  1071. );
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Html->nestedList($list, array(), array(), 'ol');
  1074. $expected = array(
  1075. '<ol',
  1076. '<li', 'Item 1', '/li',
  1077. '<li', 'Item 2',
  1078. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1079. '/li',
  1080. '<li', 'Item 3', '/li',
  1081. '<li', 'Item 4',
  1082. '<ol',
  1083. '<li', 'Item 4.1', '/li',
  1084. '<li', 'Item 4.2', '/li',
  1085. '<li', 'Item 4.3',
  1086. '<ol',
  1087. '<li', 'Item 4.3.1', '/li',
  1088. '<li', 'Item 4.3.2', '/li',
  1089. '/ol',
  1090. '/li',
  1091. '/ol',
  1092. '/li',
  1093. '<li', 'Item 5',
  1094. '<ol',
  1095. '<li', 'Item 5.1', '/li',
  1096. '<li', 'Item 5.2', '/li',
  1097. '/ol',
  1098. '/li',
  1099. '/ol'
  1100. );
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Html->nestedList($list, '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, array('class' => 'list'));
  1132. $expected = array(
  1133. array('ul' => array('class' => 'list')),
  1134. '<li', 'Item 1', '/li',
  1135. '<li', 'Item 2',
  1136. array('ul' => array('class' => 'list')), '<li', 'Item 2.1', '/li', '/ul',
  1137. '/li',
  1138. '<li', 'Item 3', '/li',
  1139. '<li', 'Item 4',
  1140. array('ul' => array('class' => 'list')),
  1141. '<li', 'Item 4.1', '/li',
  1142. '<li', 'Item 4.2', '/li',
  1143. '<li', 'Item 4.3',
  1144. array('ul' => array('class' => 'list')),
  1145. '<li', 'Item 4.3.1', '/li',
  1146. '<li', 'Item 4.3.2', '/li',
  1147. '/ul',
  1148. '/li',
  1149. '/ul',
  1150. '/li',
  1151. '<li', 'Item 5',
  1152. array('ul' => array('class' => 'list')),
  1153. '<li', 'Item 5.1', '/li',
  1154. '<li', 'Item 5.2', '/li',
  1155. '/ul',
  1156. '/li',
  1157. '/ul'
  1158. );
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Html->nestedList($list, array(), array('class' => 'item'));
  1161. $expected = array(
  1162. '<ul',
  1163. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1164. array('li' => array('class' => 'item')), 'Item 2',
  1165. '<ul', array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1166. '/li',
  1167. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1168. array('li' => array('class' => 'item')), 'Item 4',
  1169. '<ul',
  1170. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1171. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1172. array('li' => array('class' => 'item')), 'Item 4.3',
  1173. '<ul',
  1174. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1175. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1176. '/ul',
  1177. '/li',
  1178. '/ul',
  1179. '/li',
  1180. array('li' => array('class' => 'item')), 'Item 5',
  1181. '<ul',
  1182. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1183. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1184. '/ul',
  1185. '/li',
  1186. '/ul'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. $result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
  1190. $expected = array(
  1191. '<ul',
  1192. array('li' => array('class' => 'odd')), 'Item 1', '/li',
  1193. array('li' => array('class' => 'even')), 'Item 2',
  1194. '<ul', array('li' => array('class' => 'odd')), 'Item 2.1', '/li', '/ul',
  1195. '/li',
  1196. array('li' => array('class' => 'odd')), 'Item 3', '/li',
  1197. array('li' => array('class' => 'even')), 'Item 4',
  1198. '<ul',
  1199. array('li' => array('class' => 'odd')), 'Item 4.1', '/li',
  1200. array('li' => array('class' => 'even')), 'Item 4.2', '/li',
  1201. array('li' => array('class' => 'odd')), 'Item 4.3',
  1202. '<ul',
  1203. array('li' => array('class' => 'odd')), 'Item 4.3.1', '/li',
  1204. array('li' => array('class' => 'even')), 'Item 4.3.2', '/li',
  1205. '/ul',
  1206. '/li',
  1207. '/ul',
  1208. '/li',
  1209. array('li' => array('class' => 'odd')), 'Item 5',
  1210. '<ul',
  1211. array('li' => array('class' => 'odd')), 'Item 5.1', '/li',
  1212. array('li' => array('class' => 'even')), 'Item 5.2', '/li',
  1213. '/ul',
  1214. '/li',
  1215. '/ul'
  1216. );
  1217. $this->assertTags($result, $expected);
  1218. $result = $this->Html->nestedList($list, array('class' => 'list'), array('class' => 'item'));
  1219. $expected = array(
  1220. array('ul' => array('class' => 'list')),
  1221. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1222. array('li' => array('class' => 'item')), 'Item 2',
  1223. array('ul' => array('class' => 'list')), array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1224. '/li',
  1225. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1226. array('li' => array('class' => 'item')), 'Item 4',
  1227. array('ul' => array('class' => 'list')),
  1228. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1229. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1230. array('li' => array('class' => 'item')), 'Item 4.3',
  1231. array('ul' => array('class' => 'list')),
  1232. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1233. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1234. '/ul',
  1235. '/li',
  1236. '/ul',
  1237. '/li',
  1238. array('li' => array('class' => 'item')), 'Item 5',
  1239. array('ul' => array('class' => 'list')),
  1240. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1241. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1242. '/ul',
  1243. '/li',
  1244. '/ul'
  1245. );
  1246. $this->assertTags($result, $expected);
  1247. }
  1248. /**
  1249. * testMeta method
  1250. *
  1251. * @return void
  1252. */
  1253. public function testMeta() {
  1254. $result = $this->Html->meta('this is an rss feed', array('controller' => 'posts', 'ext' => 'rss'));
  1255. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1256. $result = $this->Html->meta('rss', array('controller' => 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed'));
  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('atom', array('controller' => 'posts', 'ext' => 'xml'));
  1259. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1260. $result = $this->Html->meta('non-existing');
  1261. $this->assertTags($result, array('<meta'));
  1262. $result = $this->Html->meta('non-existing', '/posts.xpp');
  1263. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'non-existing')));
  1264. $result = $this->Html->meta('non-existing', '/posts.xpp', array('type' => 'atom'));
  1265. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing')));
  1266. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'), array('link' => '/articles.rss'));
  1267. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1268. $result = $this->Html->meta(array('link' => 'favicon.ico', 'rel' => 'icon'));
  1269. $expected = array(
  1270. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'icon'),
  1271. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'shortcut icon'))
  1272. );
  1273. $this->assertTags($result, $expected);
  1274. $result = $this->Html->meta('icon', 'favicon.ico');
  1275. $expected = array(
  1276. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1277. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1278. );
  1279. $this->assertTags($result, $expected);
  1280. $result = $this->Html->meta('icon');
  1281. $expected = array(
  1282. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1283. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1284. );
  1285. $this->assertTags($result, $expected);
  1286. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1287. $this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
  1288. $this->assertRegExp('/\s+\/>$/', $result);
  1289. $result = $this->Html->meta('description', 'this is the meta description');
  1290. $this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));
  1291. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'));
  1292. $this->assertTags($result, array('meta' => array('name' => 'ROBOTS', 'content' => 'ALL')));
  1293. }
  1294. /**
  1295. * Test the inline and block options for meta()
  1296. */
  1297. public function testMetaWithBlocks() {
  1298. $this->View->expects($this->at(0))
  1299. ->method('append')
  1300. ->with('meta', $this->stringContains('ROBOTS'));
  1301. $this->View->expects($this->at(1))
  1302. ->method('append')
  1303. ->with('metaTags', $this->stringContains('favicon.ico'));
  1304. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('inline' => false));
  1305. $this->assertNull($result);
  1306. $result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));
  1307. $this->assertNull($result);
  1308. }
  1309. /**
  1310. * testTableHeaders method
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTableHeaders() {
  1315. $result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
  1316. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1317. $this->assertTags($result, $expected);
  1318. }
  1319. /**
  1320. * testTableCells method
  1321. *
  1322. * @return void
  1323. */
  1324. public function testTableCells() {
  1325. $tr = array(
  1326. 'td content 1',
  1327. array('td content 2', array("width" => "100px")),
  1328. array('td content 3', "width=100px")
  1329. );
  1330. $result = $this->Html->tableCells($tr);
  1331. $expected = array(
  1332. '<tr',
  1333. '<td', 'td content 1', '/td',
  1334. array('td' => array('width' => '100px')), 'td content 2', '/td',
  1335. array('td' => array('width' => 'preg:/100px/')), 'td content 3', '/td',
  1336. '/tr'
  1337. );
  1338. $this->assertTags($result, $expected);
  1339. $tr = array('td content 1', 'td content 2', 'td content 3');
  1340. $result = $this->Html->tableCells($tr, null, null, true);
  1341. $expected = array(
  1342. '<tr',
  1343. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1344. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1345. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1346. '/tr'
  1347. );
  1348. $this->assertTags($result, $expected);
  1349. $tr = array('td content 1', 'td content 2', 'td content 3');
  1350. $result = $this->Html->tableCells($tr, true);
  1351. $expected = array(
  1352. '<tr',
  1353. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1354. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1355. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1356. '/tr'
  1357. );
  1358. $this->assertTags($result, $expected);
  1359. $tr = array(
  1360. array('td content 1', 'td content 2', 'td content 3'),
  1361. array('td content 1', 'td content 2', 'td content 3'),
  1362. array('td content 1', 'td content 2', 'td content 3')
  1363. );
  1364. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1365. $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>";
  1366. $this->assertEquals($expected, $result);
  1367. $tr = array(
  1368. array('td content 1', 'td content 2', 'td content 3'),
  1369. array('td content 1', 'td content 2', 'td content 3'),
  1370. array('td content 1', 'td content 2', 'td content 3'),
  1371. array('td content 1', 'td content 2', 'td content 3')
  1372. );
  1373. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1374. $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>";
  1375. $this->assertEquals($expected, $result);
  1376. $tr = array(
  1377. array('td content 1', 'td content 2', 'td content 3'),
  1378. array('td content 1', 'td content 2', 'td content 3'),
  1379. array('td content 1', 'td content 2', 'td content 3')
  1380. );
  1381. $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1382. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'), false, false);
  1383. $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>";
  1384. $this->assertEquals($expected, $result);
  1385. }
  1386. /**
  1387. * testTag method
  1388. *
  1389. * @return void
  1390. */
  1391. public function testTag() {
  1392. $result = $this->Html->tag('div');
  1393. $this->assertTags($result, '<div');
  1394. $result = $this->Html->tag('div', 'text');
  1395. $this->assertTags($result, '<div', 'text', '/div');
  1396. $result = $this->Html->tag('div', '<text>', 'class-name');
  1397. $this->assertTags($result, array('div' => array('class' => 'class-name'), 'preg:/<text>/', '/div'));
  1398. $result = $this->Html->tag('div', '<text>', array('class' => 'class-name', 'escape' => true));
  1399. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1400. }
  1401. /**
  1402. * testUseTag method
  1403. *
  1404. * @return void
  1405. */
  1406. public function testUseTag() {
  1407. $result = $this->Html->useTag('unknowntag');
  1408. $this->assertEquals($result, '');
  1409. $result = $this->Html->useTag('formend');
  1410. $this->assertTags($result, '/form');
  1411. $result = $this->Html->useTag('form', 'url', ' test');
  1412. $this->assertEquals($result, '<form action="url" test>');
  1413. $result = $this->Html->useTag('form', 'example.com', array('test' => 'ok'));
  1414. $this->assertTags($result, array('form' => array('test' => 'ok', 'action' => 'example.com')));
  1415. }
  1416. /**
  1417. * testDiv method
  1418. *
  1419. * @return void
  1420. */
  1421. public function testDiv() {
  1422. $result = $this->Html->div('class-name');
  1423. $this->assertTags($result, array('div' => array('class' => 'class-name')));
  1424. $result = $this->Html->div('class-name', 'text');
  1425. $this->assertTags($result, array('div' => array('class' => 'class-name'), 'text', '/div'));
  1426. $result = $this->Html->div('class-name', '<text>', array('escape' => true));
  1427. $this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
  1428. }
  1429. /**
  1430. * testPara method
  1431. *
  1432. * @return void
  1433. */
  1434. public function testPara() {
  1435. $result = $this->Html->para('class-name', '');
  1436. $this->assertTags($result, array('p' => array('class' => 'class-name')));
  1437. $result = $this->Html->para('class-name', 'text');
  1438. $this->assertTags($result, array('p' => array('class' => 'class-name'), 'text', '/p'));
  1439. $result = $this->Html->para('class-name', '<text>', array('escape' => true));
  1440. $this->assertTags($result, array('p' => array('class' => 'class-name'), '&lt;text&gt;', '/p'));
  1441. }
  1442. /**
  1443. * testMedia method
  1444. *
  1445. * @return void
  1446. */
  1447. public function testMedia() {
  1448. $result = $this->Html->media('video.webm');
  1449. $expected = array('video' => array('src' => 'files/video.webm'), '/video');
  1450. $this->assertTags($result, $expected);
  1451. $result = $this->Html->media('video.webm', array(
  1452. 'text' => 'Your browser does not support the HTML5 Video element.'
  1453. ));
  1454. $expected = array('video' => array('src' => 'files/video.webm'), 'Your browser does not support the HTML5 Video element.', '/video');
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Html->media('video.webm', array('autoload', 'muted' => 'muted'));
  1457. $expected = array(
  1458. 'video' => array(
  1459. 'src' => 'files/video.webm',
  1460. 'autoload' => 'autoload',
  1461. 'muted' => 'muted'
  1462. ),
  1463. '/video'
  1464. );
  1465. $this->assertTags($result, $expected);
  1466. $result = $this->Html->media(
  1467. array('video.webm', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  1468. array('pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.')
  1469. );
  1470. $expected = array(
  1471. 'video' => array('poster' => IMAGES_URL . 'poster.jpg'),
  1472. array('source' => array('src' => 'videos/video.webm', 'type' => 'video/webm')),
  1473. array('source' => array('src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;')),
  1474. 'Your browser does not support the HTML5 Video element.',
  1475. '/video'
  1476. );
  1477. $this->assertTags($result, $expected);
  1478. $result = $this->Html->media('video.ogv', array('type' => 'video'));
  1479. $expected = array('video' => array('src' => 'files/video.ogv'), '/video');
  1480. $this->assertTags($result, $expected);
  1481. $result = $this->Html->media('audio.mp3');
  1482. $expected = array('audio' => array('src' => 'files/audio.mp3'), '/audio');
  1483. $this->assertTags($result, $expected);
  1484. $result = $this->Html->media(
  1485. array(array('src' => 'video.mov', 'type' => 'video/mp4'), 'video.webm')
  1486. );
  1487. $expected = array(
  1488. '<video',
  1489. array('source' => array('src' => 'files/video.mov', 'type' => 'video/mp4')),
  1490. array('source' => array('src' => 'files/video.webm', 'type' => 'video/webm')),
  1491. '/video'
  1492. );
  1493. $this->assertTags($result, $expected);
  1494. }
  1495. /**
  1496. * testCrumbList method
  1497. *
  1498. *
  1499. * @return void
  1500. */
  1501. public function testCrumbList() {
  1502. $this->assertNull($this->Html->getCrumbList());
  1503. $this->Html->addCrumb('Home', '/', array('class' => 'home'));
  1504. $this->Html->addCrumb('Some page', '/some_page');
  1505. $this->Html->addCrumb('Another page');
  1506. $result = $this->Html->getCrumbList(
  1507. array('class' => 'breadcrumbs')
  1508. );
  1509. $this->assertTags(
  1510. $result,
  1511. array(
  1512. array('ul' => array('class' => 'breadcrumbs')),
  1513. array('li' => array('class' => 'first')),
  1514. array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
  1515. '/li',
  1516. '<li',
  1517. array('a' => array('href' => '/some_page')), 'Some page', '/a',
  1518. '/li',
  1519. array('li' => array('class' => 'last')),
  1520. 'Another page',
  1521. '/li',
  1522. '/ul'
  1523. )
  1524. );
  1525. }
  1526. /**
  1527. * testLoadConfig method
  1528. *
  1529. * @return void
  1530. */
  1531. public function testLoadConfig() {
  1532. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config'. DS;
  1533. $result = $this->Html->loadConfig('htmlhelper_tags', $path);
  1534. $expected = array(
  1535. 'tags' => array(
  1536. 'form' => 'start form',
  1537. 'formend' => 'finish form'
  1538. )
  1539. );
  1540. $this->assertEquals($expected, $result);
  1541. $tags = $this->Html->getAttribute('_tags');
  1542. $this->assertEquals($tags['form'], 'start form');
  1543. $this->assertEquals($tags['formend'], 'finish form');
  1544. $this->assertEquals($tags['selectend'], '</select>');
  1545. $result = $this->Html->loadConfig(array('htmlhelper_minimized.ini', 'ini'), $path);
  1546. $expected = array(
  1547. 'minimizedAttributeFormat' => 'format'
  1548. );
  1549. $this->assertEquals($expected, $result);
  1550. $this->assertEquals($this->Html->getAttribute('_minimizedAttributeFormat'), 'format');
  1551. }
  1552. /**
  1553. * testLoadConfigWrongFile method
  1554. *
  1555. * @return void
  1556. * @expectedException ConfigureException
  1557. */
  1558. public function testLoadConfigWrongFile() {
  1559. $result = $this->Html->loadConfig('wrong_file');
  1560. }
  1561. /**
  1562. * testLoadConfigWrongReader method
  1563. *
  1564. * @return void
  1565. * @expectedException ConfigureException
  1566. */
  1567. public function testLoadConfigWrongReader() {
  1568. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config'. DS;
  1569. $result = $this->Html->loadConfig(array('htmlhelper_tags', 'wrong_reader'), $path);
  1570. }
  1571. /**
  1572. * test parsing attributes.
  1573. *
  1574. * @return void
  1575. */
  1576. public function testParseAttributeCompact() {
  1577. $helper = new TestHtmlHelper($this->View);
  1578. $compact = array('compact', 'checked', 'declare', 'readonly', 'disabled',
  1579. 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
  1580. foreach ($compact as $attribute) {
  1581. foreach (array('true', true, 1, '1', $attribute) as $value) {
  1582. $attrs = array($attribute => $value);
  1583. $expected = ' ' . $attribute . '="' . $attribute . '"';
  1584. $this->assertEquals($helper->parseAttributes($attrs), $expected, '%s Failed on ' . $value);
  1585. }
  1586. }
  1587. $this->assertEquals($helper->parseAttributes(array('compact')), ' compact="compact"');
  1588. $attrs = array('class' => array('foo', 'bar'));
  1589. $expected = ' class="foo bar"';
  1590. $this->assertEquals(' class="foo bar"', $helper->parseAttributes($attrs));
  1591. $helper = new Html5TestHelper($this->View);
  1592. $expected = ' require';
  1593. $this->assertEquals($helper->parseAttributes(array('require')), $expected);
  1594. $this->assertEquals($helper->parseAttributes(array('require' => true)), $expected);
  1595. $this->assertEquals($helper->parseAttributes(array('require' => false)), '');
  1596. }
  1597. }