HtmlHelperTest.php 75 KB

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