HtmlHelperTest.php 69 KB

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