HtmlHelperTest.php 61 KB

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