HtmlHelperTest.php 75 KB

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