HtmlHelperTest.php 62 KB

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