HtmlHelperTest.php 63 KB

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