HtmlHelperTest.php 64 KB

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