HtmlHelperTest.php 61 KB

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