HtmlHelperTest.php 64 KB

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