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