HtmlHelperTest.php 71 KB

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