HtmlHelperTest.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Filesystem\File;
  21. use Cake\Filesystem\Folder;
  22. use Cake\Network\Request;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\ClassRegistry;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\Helper\HtmlHelper;
  28. use Cake\View\View;
  29. /**
  30. * HtmlHelperTest class
  31. *
  32. */
  33. class HtmlHelperTest extends TestCase
  34. {
  35. /**
  36. * Regexp for CDATA start block
  37. *
  38. * @var string
  39. */
  40. public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  41. /**
  42. * Regexp for CDATA end block
  43. *
  44. * @var string
  45. */
  46. public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  47. /**
  48. * html property
  49. *
  50. * @var object
  51. */
  52. public $Html = null;
  53. /**
  54. * setUp method
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. parent::setUp();
  61. $controller = $this->getMock('Cake\Controller\Controller', ['redirect']);
  62. $this->View = $this->getMock('Cake\View\View', ['append']);
  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. }
  297. /**
  298. * Test image() with query strings.
  299. *
  300. * @return void
  301. */
  302. public function testImageQueryString()
  303. {
  304. $result = $this->Html->image('test.gif?one=two&three=four');
  305. $expected = ['img' => ['src' => 'img/test.gif?one=two&amp;three=four', 'alt' => '']];
  306. $this->assertHtml($expected, $result);
  307. $result = $this->Html->image([
  308. 'controller' => 'images',
  309. 'action' => 'display',
  310. 'test',
  311. '?' => ['one' => 'two', 'three' => 'four']
  312. ]);
  313. $expected = ['img' => ['src' => '/images/display/test?one=two&amp;three=four', 'alt' => '']];
  314. $this->assertHtml($expected, $result);
  315. }
  316. /**
  317. * Test that image works with pathPrefix.
  318. *
  319. * @return void
  320. */
  321. public function testImagePathPrefix()
  322. {
  323. $result = $this->Html->image('test.gif', ['pathPrefix' => '/my/custom/path/']);
  324. $expected = ['img' => ['src' => '/my/custom/path/test.gif', 'alt' => '']];
  325. $this->assertHtml($expected, $result);
  326. $result = $this->Html->image('test.gif', ['pathPrefix' => 'http://cakephp.org/assets/img/']);
  327. $expected = ['img' => ['src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '']];
  328. $this->assertHtml($expected, $result);
  329. $result = $this->Html->image('test.gif', ['pathPrefix' => '//cakephp.org/assets/img/']);
  330. $expected = ['img' => ['src' => '//cakephp.org/assets/img/test.gif', 'alt' => '']];
  331. $this->assertHtml($expected, $result);
  332. $previousConfig = Configure::read('App.imageBaseUrl');
  333. Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
  334. $result = $this->Html->image('test.gif');
  335. $expected = ['img' => ['src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '']];
  336. $this->assertHtml($expected, $result);
  337. Configure::write('App.imageBaseUrl', $previousConfig);
  338. }
  339. /**
  340. * Test that image() works with fullBase and a webroot not equal to /
  341. *
  342. * @return void
  343. */
  344. public function testImageWithFullBase()
  345. {
  346. $result = $this->Html->image('test.gif', ['fullBase' => true]);
  347. $here = $this->Html->Url->build('/', true);
  348. $expected = ['img' => ['src' => $here . 'img/test.gif', 'alt' => '']];
  349. $this->assertHtml($expected, $result);
  350. $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
  351. $here = $this->Html->Url->build('/', true);
  352. $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
  353. $this->assertHtml($expected, $result);
  354. $request = $this->Html->request;
  355. $request->webroot = '/myproject/';
  356. $request->base = '/myproject';
  357. Router::pushRequest($request);
  358. $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
  359. $here = $this->Html->Url->build('/', true);
  360. $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
  361. $this->assertHtml($expected, $result);
  362. }
  363. /**
  364. * test image() with Asset.timestamp
  365. *
  366. * @return void
  367. */
  368. public function testImageWithTimestampping()
  369. {
  370. Configure::write('Asset.timestamp', 'force');
  371. $this->Html->request->webroot = '/';
  372. $result = $this->Html->image('cake.icon.png');
  373. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  374. $this->assertHtml($expected, $result);
  375. Configure::write('debug', false);
  376. Configure::write('Asset.timestamp', 'force');
  377. $result = $this->Html->image('cake.icon.png');
  378. $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
  379. $this->assertHtml($expected, $result);
  380. $this->Html->request->webroot = '/testing/longer/';
  381. $result = $this->Html->image('cake.icon.png');
  382. $expected = [
  383. 'img' => ['src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '']
  384. ];
  385. $this->assertHtml($expected, $result);
  386. }
  387. /**
  388. * Tests creation of an image tag using a theme and asset timestamping
  389. *
  390. * @return void
  391. */
  392. public function testImageTagWithTheme()
  393. {
  394. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  395. $testfile = WWW_ROOT . 'test_theme/img/__cake_test_image.gif';
  396. $File = new File($testfile, true);
  397. Configure::write('Asset.timestamp', true);
  398. Configure::write('debug', true);
  399. $this->Html->Url->request->webroot = '/';
  400. $this->Html->Url->theme = 'TestTheme';
  401. $result = $this->Html->image('__cake_test_image.gif');
  402. $expected = [
  403. 'img' => [
  404. 'src' => 'preg:/\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  405. 'alt' => ''
  406. ]];
  407. $this->assertHtml($expected, $result);
  408. $this->Html->Url->request->webroot = '/testing/';
  409. $result = $this->Html->image('__cake_test_image.gif');
  410. $expected = [
  411. 'img' => [
  412. 'src' => 'preg:/\/testing\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  413. 'alt' => ''
  414. ]];
  415. $this->assertHtml($expected, $result);
  416. $File->delete();
  417. }
  418. /**
  419. * test theme assets in main webroot path
  420. *
  421. * @return void
  422. */
  423. public function testThemeAssetsInMainWebrootPath()
  424. {
  425. $webRoot = Configure::read('App.wwwRoot');
  426. Configure::write('App.wwwRoot', TEST_APP . 'webroot/');
  427. $this->Html->Url->theme = 'TestTheme';
  428. $result = $this->Html->css('webroot_test');
  429. $expected = [
  430. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/webroot_test\.css/']
  431. ];
  432. $this->assertHtml($expected, $result);
  433. $this->Html->theme = 'TestTheme';
  434. $result = $this->Html->css('theme_webroot');
  435. $expected = [
  436. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/theme_webroot\.css/']
  437. ];
  438. $this->assertHtml($expected, $result);
  439. }
  440. /**
  441. * testStyle method
  442. *
  443. * @return void
  444. */
  445. public function testStyle()
  446. {
  447. $result = $this->Html->style(['display' => 'none', 'margin' => '10px']);
  448. $expected = 'display:none; margin:10px;';
  449. $this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
  450. $result = $this->Html->style(['display' => 'none', 'margin' => '10px'], false);
  451. $lines = explode("\n", $result);
  452. $this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
  453. $this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
  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. $this->cDataStart,
  919. 'window.foo = 2;',
  920. $this->cDataEnd,
  921. '/script',
  922. ];
  923. $this->assertHtml($expected, $result);
  924. $result = $this->Html->scriptBlock('window.foo = 2;', ['type' => 'text/x-handlebars-template']);
  925. $expected = [
  926. 'script' => ['type' => 'text/x-handlebars-template'],
  927. $this->cDataStart,
  928. 'window.foo = 2;',
  929. $this->cDataEnd,
  930. '/script',
  931. ];
  932. $this->assertHtml($expected, $result);
  933. $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => false]);
  934. $expected = [
  935. '<script',
  936. 'window.foo = 2;',
  937. '/script',
  938. ];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => true]);
  941. $expected = [
  942. '<script',
  943. $this->cDataStart,
  944. 'window.foo = 2;',
  945. $this->cDataEnd,
  946. '/script',
  947. ];
  948. $this->assertHtml($expected, $result);
  949. $this->View->expects($this->at(0))
  950. ->method('append')
  951. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  952. $this->View->expects($this->at(1))
  953. ->method('append')
  954. ->with('scriptTop', $this->stringContains('alert('));
  955. $result = $this->Html->scriptBlock('window.foo = 2;', ['block' => true]);
  956. $this->assertNull($result);
  957. $result = $this->Html->scriptBlock('alert("hi")', ['block' => 'scriptTop']);
  958. $this->assertNull($result);
  959. $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => false, 'encoding' => 'utf-8']);
  960. $expected = [
  961. 'script' => ['encoding' => 'utf-8'],
  962. 'window.foo = 2;',
  963. '/script',
  964. ];
  965. $this->assertHtml($expected, $result);
  966. }
  967. /**
  968. * test script tag output buffering when using scriptStart() and scriptEnd();
  969. *
  970. * @return void
  971. */
  972. public function testScriptStartAndScriptEnd()
  973. {
  974. $result = $this->Html->scriptStart(['safe' => true]);
  975. $this->assertNull($result);
  976. echo 'this is some javascript';
  977. $result = $this->Html->scriptEnd();
  978. $expected = [
  979. '<script',
  980. $this->cDataStart,
  981. 'this is some javascript',
  982. $this->cDataEnd,
  983. '/script'
  984. ];
  985. $this->assertHtml($expected, $result);
  986. $result = $this->Html->scriptStart(['safe' => false]);
  987. $this->assertNull($result);
  988. echo 'this is some javascript';
  989. $result = $this->Html->scriptEnd();
  990. $expected = [
  991. '<script',
  992. 'this is some javascript',
  993. '/script'
  994. ];
  995. $this->assertHtml($expected, $result);
  996. $result = $this->Html->scriptStart(['safe' => true, 'type' => 'text/x-handlebars-template']);
  997. $this->assertNull($result);
  998. echo 'this is some template';
  999. $result = $this->Html->scriptEnd();
  1000. $expected = [
  1001. 'script' => ['type' => 'text/x-handlebars-template'],
  1002. $this->cDataStart,
  1003. 'this is some template',
  1004. $this->cDataEnd,
  1005. '/script'
  1006. ];
  1007. $this->assertHtml($expected, $result);
  1008. $this->View->expects($this->once())
  1009. ->method('append');
  1010. $result = $this->Html->scriptStart(['safe' => false, 'block' => true]);
  1011. $this->assertNull($result);
  1012. echo 'this is some javascript';
  1013. $result = $this->Html->scriptEnd();
  1014. $this->assertNull($result);
  1015. }
  1016. /**
  1017. * testCharsetTag method
  1018. *
  1019. * @return void
  1020. */
  1021. public function testCharsetTag()
  1022. {
  1023. Configure::write('App.encoding', null);
  1024. $result = $this->Html->charset();
  1025. $expected = ['meta' => ['http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8']];
  1026. $this->assertHtml($expected, $result);
  1027. Configure::write('App.encoding', 'ISO-8859-1');
  1028. $result = $this->Html->charset();
  1029. $expected = ['meta' => ['http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1']];
  1030. $this->assertHtml($expected, $result);
  1031. $result = $this->Html->charset('UTF-7');
  1032. $expected = ['meta' => ['http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7']];
  1033. $this->assertHtml($expected, $result);
  1034. }
  1035. /**
  1036. * testGetCrumb and addCrumb method
  1037. *
  1038. * @return void
  1039. */
  1040. public function testBreadcrumb()
  1041. {
  1042. $this->assertNull($this->Html->getCrumbs());
  1043. $this->Html->addCrumb('First', '#first');
  1044. $this->Html->addCrumb('Second', '#second');
  1045. $this->Html->addCrumb('Third', '#third');
  1046. $result = $this->Html->getCrumbs();
  1047. $expected = [
  1048. ['a' => ['href' => '#first']],
  1049. 'First',
  1050. '/a',
  1051. '&raquo;',
  1052. ['a' => ['href' => '#second']],
  1053. 'Second',
  1054. '/a',
  1055. '&raquo;',
  1056. ['a' => ['href' => '#third']],
  1057. 'Third',
  1058. '/a',
  1059. ];
  1060. $this->assertHtml($expected, $result);
  1061. $result = $this->Html->getCrumbs(' &gt; ');
  1062. $expected = [
  1063. ['a' => ['href' => '#first']],
  1064. 'First',
  1065. '/a',
  1066. ' &gt; ',
  1067. ['a' => ['href' => '#second']],
  1068. 'Second',
  1069. '/a',
  1070. ' &gt; ',
  1071. ['a' => ['href' => '#third']],
  1072. 'Third',
  1073. '/a',
  1074. ];
  1075. $this->assertHtml($expected, $result);
  1076. $this->Html->addCrumb('Fourth', null);
  1077. $result = $this->Html->getCrumbs();
  1078. $expected = [
  1079. ['a' => ['href' => '#first']],
  1080. 'First',
  1081. '/a',
  1082. '&raquo;',
  1083. ['a' => ['href' => '#second']],
  1084. 'Second',
  1085. '/a',
  1086. '&raquo;',
  1087. ['a' => ['href' => '#third']],
  1088. 'Third',
  1089. '/a',
  1090. '&raquo;',
  1091. 'Fourth'
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. $result = $this->Html->getCrumbs('-', 'Start');
  1095. $expected = [
  1096. ['a' => ['href' => '/']],
  1097. 'Start',
  1098. '/a',
  1099. '-',
  1100. ['a' => ['href' => '#first']],
  1101. 'First',
  1102. '/a',
  1103. '-',
  1104. ['a' => ['href' => '#second']],
  1105. 'Second',
  1106. '/a',
  1107. '-',
  1108. ['a' => ['href' => '#third']],
  1109. 'Third',
  1110. '/a',
  1111. '-',
  1112. 'Fourth'
  1113. ];
  1114. $this->assertHtml($expected, $result);
  1115. }
  1116. /**
  1117. * Test the array form of $startText
  1118. *
  1119. * @return void
  1120. */
  1121. public function testGetCrumbFirstLink()
  1122. {
  1123. $result = $this->Html->getCrumbList([], 'Home');
  1124. $expected = [
  1125. '<ul',
  1126. ['li' => ['class' => 'first']],
  1127. ['a' => ['href' => '/']], 'Home', '/a',
  1128. '/li',
  1129. '/ul'
  1130. ];
  1131. $this->assertHtml($expected, $result);
  1132. $this->Html->addCrumb('First', '#first');
  1133. $this->Html->addCrumb('Second', '#second');
  1134. $result = $this->Html->getCrumbs(' - ', ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
  1135. $expected = [
  1136. ['a' => ['href' => '/home']],
  1137. 'img' => ['src' => '/home.png'],
  1138. '/a',
  1139. ' - ',
  1140. ['a' => ['href' => '#first']],
  1141. 'First',
  1142. '/a',
  1143. ' - ',
  1144. ['a' => ['href' => '#second']],
  1145. 'Second',
  1146. '/a',
  1147. ];
  1148. $this->assertHtml($expected, $result);
  1149. }
  1150. /**
  1151. * testNestedList method
  1152. *
  1153. * @return void
  1154. */
  1155. public function testNestedList()
  1156. {
  1157. $list = [
  1158. 'Item 1',
  1159. 'Item 2' => [
  1160. 'Item 2.1'
  1161. ],
  1162. 'Item 3',
  1163. 'Item 4' => [
  1164. 'Item 4.1',
  1165. 'Item 4.2',
  1166. 'Item 4.3' => [
  1167. 'Item 4.3.1',
  1168. 'Item 4.3.2'
  1169. ]
  1170. ],
  1171. 'Item 5' => [
  1172. 'Item 5.1',
  1173. 'Item 5.2'
  1174. ]
  1175. ];
  1176. $result = $this->Html->nestedList($list);
  1177. $expected = [
  1178. '<ul',
  1179. '<li', 'Item 1', '/li',
  1180. '<li', 'Item 2',
  1181. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1182. '/li',
  1183. '<li', 'Item 3', '/li',
  1184. '<li', 'Item 4',
  1185. '<ul',
  1186. '<li', 'Item 4.1', '/li',
  1187. '<li', 'Item 4.2', '/li',
  1188. '<li', 'Item 4.3',
  1189. '<ul',
  1190. '<li', 'Item 4.3.1', '/li',
  1191. '<li', 'Item 4.3.2', '/li',
  1192. '/ul',
  1193. '/li',
  1194. '/ul',
  1195. '/li',
  1196. '<li', 'Item 5',
  1197. '<ul',
  1198. '<li', 'Item 5.1', '/li',
  1199. '<li', 'Item 5.2', '/li',
  1200. '/ul',
  1201. '/li',
  1202. '/ul'
  1203. ];
  1204. $this->assertHtml($expected, $result);
  1205. $result = $this->Html->nestedList($list);
  1206. $this->assertHtml($expected, $result);
  1207. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1208. $expected = [
  1209. '<ol',
  1210. '<li', 'Item 1', '/li',
  1211. '<li', 'Item 2',
  1212. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1213. '/li',
  1214. '<li', 'Item 3', '/li',
  1215. '<li', 'Item 4',
  1216. '<ol',
  1217. '<li', 'Item 4.1', '/li',
  1218. '<li', 'Item 4.2', '/li',
  1219. '<li', 'Item 4.3',
  1220. '<ol',
  1221. '<li', 'Item 4.3.1', '/li',
  1222. '<li', 'Item 4.3.2', '/li',
  1223. '/ol',
  1224. '/li',
  1225. '/ol',
  1226. '/li',
  1227. '<li', 'Item 5',
  1228. '<ol',
  1229. '<li', 'Item 5.1', '/li',
  1230. '<li', 'Item 5.2', '/li',
  1231. '/ol',
  1232. '/li',
  1233. '/ol'
  1234. ];
  1235. $this->assertHtml($expected, $result);
  1236. $result = $this->Html->nestedList($list, ['tag' => 'ol']);
  1237. $this->assertHtml($expected, $result);
  1238. $result = $this->Html->nestedList($list, ['class' => 'list']);
  1239. $expected = [
  1240. ['ul' => ['class' => 'list']],
  1241. '<li', 'Item 1', '/li',
  1242. '<li', 'Item 2',
  1243. ['ul' => ['class' => 'list']], '<li', 'Item 2.1', '/li', '/ul',
  1244. '/li',
  1245. '<li', 'Item 3', '/li',
  1246. '<li', 'Item 4',
  1247. ['ul' => ['class' => 'list']],
  1248. '<li', 'Item 4.1', '/li',
  1249. '<li', 'Item 4.2', '/li',
  1250. '<li', 'Item 4.3',
  1251. ['ul' => ['class' => 'list']],
  1252. '<li', 'Item 4.3.1', '/li',
  1253. '<li', 'Item 4.3.2', '/li',
  1254. '/ul',
  1255. '/li',
  1256. '/ul',
  1257. '/li',
  1258. '<li', 'Item 5',
  1259. ['ul' => ['class' => 'list']],
  1260. '<li', 'Item 5.1', '/li',
  1261. '<li', 'Item 5.2', '/li',
  1262. '/ul',
  1263. '/li',
  1264. '/ul'
  1265. ];
  1266. $this->assertHtml($expected, $result);
  1267. $result = $this->Html->nestedList($list, [], ['class' => 'item']);
  1268. $expected = [
  1269. '<ul',
  1270. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1271. ['li' => ['class' => 'item']], 'Item 2',
  1272. '<ul', ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1273. '/li',
  1274. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1275. ['li' => ['class' => 'item']], 'Item 4',
  1276. '<ul',
  1277. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1278. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1279. ['li' => ['class' => 'item']], 'Item 4.3',
  1280. '<ul',
  1281. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1282. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1283. '/ul',
  1284. '/li',
  1285. '/ul',
  1286. '/li',
  1287. ['li' => ['class' => 'item']], 'Item 5',
  1288. '<ul',
  1289. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1290. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1291. '/ul',
  1292. '/li',
  1293. '/ul'
  1294. ];
  1295. $this->assertHtml($expected, $result);
  1296. $result = $this->Html->nestedList($list, [], ['even' => 'even', 'odd' => 'odd']);
  1297. $expected = [
  1298. '<ul',
  1299. ['li' => ['class' => 'odd']], 'Item 1', '/li',
  1300. ['li' => ['class' => 'even']], 'Item 2',
  1301. '<ul', ['li' => ['class' => 'odd']], 'Item 2.1', '/li', '/ul',
  1302. '/li',
  1303. ['li' => ['class' => 'odd']], 'Item 3', '/li',
  1304. ['li' => ['class' => 'even']], 'Item 4',
  1305. '<ul',
  1306. ['li' => ['class' => 'odd']], 'Item 4.1', '/li',
  1307. ['li' => ['class' => 'even']], 'Item 4.2', '/li',
  1308. ['li' => ['class' => 'odd']], 'Item 4.3',
  1309. '<ul',
  1310. ['li' => ['class' => 'odd']], 'Item 4.3.1', '/li',
  1311. ['li' => ['class' => 'even']], 'Item 4.3.2', '/li',
  1312. '/ul',
  1313. '/li',
  1314. '/ul',
  1315. '/li',
  1316. ['li' => ['class' => 'odd']], 'Item 5',
  1317. '<ul',
  1318. ['li' => ['class' => 'odd']], 'Item 5.1', '/li',
  1319. ['li' => ['class' => 'even']], 'Item 5.2', '/li',
  1320. '/ul',
  1321. '/li',
  1322. '/ul'
  1323. ];
  1324. $this->assertHtml($expected, $result);
  1325. $result = $this->Html->nestedList($list, ['class' => 'list'], ['class' => 'item']);
  1326. $expected = [
  1327. ['ul' => ['class' => 'list']],
  1328. ['li' => ['class' => 'item']], 'Item 1', '/li',
  1329. ['li' => ['class' => 'item']], 'Item 2',
  1330. ['ul' => ['class' => 'list']], ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
  1331. '/li',
  1332. ['li' => ['class' => 'item']], 'Item 3', '/li',
  1333. ['li' => ['class' => 'item']], 'Item 4',
  1334. ['ul' => ['class' => 'list']],
  1335. ['li' => ['class' => 'item']], 'Item 4.1', '/li',
  1336. ['li' => ['class' => 'item']], 'Item 4.2', '/li',
  1337. ['li' => ['class' => 'item']], 'Item 4.3',
  1338. ['ul' => ['class' => 'list']],
  1339. ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
  1340. ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
  1341. '/ul',
  1342. '/li',
  1343. '/ul',
  1344. '/li',
  1345. ['li' => ['class' => 'item']], 'Item 5',
  1346. ['ul' => ['class' => 'list']],
  1347. ['li' => ['class' => 'item']], 'Item 5.1', '/li',
  1348. ['li' => ['class' => 'item']], 'Item 5.2', '/li',
  1349. '/ul',
  1350. '/li',
  1351. '/ul'
  1352. ];
  1353. $this->assertHtml($expected, $result);
  1354. }
  1355. /**
  1356. * testMeta method
  1357. *
  1358. * @return void
  1359. */
  1360. public function testMeta()
  1361. {
  1362. Router::connect('/:controller', ['action' => 'index']);
  1363. $result = $this->Html->meta('this is an rss feed', ['controller' => 'posts', '_ext' => 'rss']);
  1364. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1365. $this->assertHtml($expected, $result);
  1366. $result = $this->Html->meta('rss', ['controller' => 'posts', '_ext' => 'rss'], ['title' => 'this is an rss feed']);
  1367. $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
  1368. $this->assertHtml($expected, $result);
  1369. $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml']);
  1370. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1371. $this->assertHtml($expected, $result);
  1372. $result = $this->Html->meta('non-existing');
  1373. $expected = ['<meta'];
  1374. $this->assertHtml($expected, $result);
  1375. $result = $this->Html->meta('non-existing', 'some content');
  1376. $expected = ['meta' => ['name' => 'non-existing', 'content' => 'some content']];
  1377. $this->assertHtml($expected, $result);
  1378. $result = $this->Html->meta('non-existing', '/posts.xpp', ['type' => 'atom']);
  1379. $expected = ['link' => ['href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing']];
  1380. $this->assertHtml($expected, $result);
  1381. $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml'], ['link' => '/articles.rss']);
  1382. $expected = ['link' => ['href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom']];
  1383. $this->assertHtml($expected, $result);
  1384. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1385. $expected = ['meta' => ['name' => 'keywords', 'content' => 'these, are, some, meta, keywords']];
  1386. $this->assertHtml($expected, $result);
  1387. $result = $this->Html->meta('description', 'this is the meta description');
  1388. $expected = ['meta' => ['name' => 'description', 'content' => 'this is the meta description']];
  1389. $this->assertHtml($expected, $result);
  1390. $result = $this->Html->meta('robots', 'ALL');
  1391. $expected = ['meta' => ['name' => 'robots', 'content' => 'ALL']];
  1392. $this->assertHtml($expected, $result);
  1393. $result = $this->Html->meta('viewport', 'width=device-width');
  1394. $expected = [
  1395. 'meta' => ['name' => 'viewport', 'content' => 'width=device-width']
  1396. ];
  1397. $this->assertHtml($expected, $result);
  1398. $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
  1399. $expected = [
  1400. 'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP']
  1401. ];
  1402. $this->assertHtml($expected, $result);
  1403. }
  1404. /**
  1405. * Test generating favicon's with meta()
  1406. *
  1407. * @return void
  1408. */
  1409. public function testMetaIcon()
  1410. {
  1411. $result = $this->Html->meta('icon', 'favicon.ico');
  1412. $expected = [
  1413. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1414. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1415. ];
  1416. $this->assertHtml($expected, $result);
  1417. $result = $this->Html->meta('icon');
  1418. $expected = [
  1419. 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1420. ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1421. ];
  1422. $this->assertHtml($expected, $result);
  1423. $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
  1424. $url = '/favicon.png?one=two&amp;three=four';
  1425. $expected = [
  1426. 'link' => [
  1427. 'href' => $url,
  1428. 'type' => 'image/x-icon',
  1429. 'rel' => 'icon'
  1430. ],
  1431. [
  1432. 'link' => [
  1433. 'href' => $url,
  1434. 'type' => 'image/x-icon',
  1435. 'rel' => 'shortcut icon'
  1436. ]
  1437. ]
  1438. ];
  1439. $this->assertHtml($expected, $result);
  1440. $this->Html->request->webroot = '/testing/';
  1441. $result = $this->Html->meta('icon');
  1442. $expected = [
  1443. 'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1444. ['link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1445. ];
  1446. $this->assertHtml($expected, $result);
  1447. }
  1448. /**
  1449. * Test generating favicon's with meta() with theme
  1450. *
  1451. * @return void
  1452. */
  1453. public function testMetaIconWithTheme()
  1454. {
  1455. $this->Html->Url->theme = 'TestTheme';
  1456. $result = $this->Html->meta('icon', 'favicon.ico');
  1457. $expected = [
  1458. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1459. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1460. ];
  1461. $this->assertHtml($expected, $result);
  1462. $result = $this->Html->meta('icon');
  1463. $expected = [
  1464. 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1465. ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1466. ];
  1467. $this->assertHtml($expected, $result);
  1468. $this->Html->request->webroot = '/testing/';
  1469. $result = $this->Html->meta('icon');
  1470. $expected = [
  1471. 'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
  1472. ['link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']]
  1473. ];
  1474. $this->assertHtml($expected, $result);
  1475. }
  1476. /**
  1477. * Test the inline and block options for meta()
  1478. *
  1479. * @return void
  1480. */
  1481. public function testMetaWithBlocks()
  1482. {
  1483. $this->View->expects($this->at(0))
  1484. ->method('append')
  1485. ->with('meta', $this->stringContains('robots'));
  1486. $this->View->expects($this->at(1))
  1487. ->method('append')
  1488. ->with('metaTags', $this->stringContains('favicon.ico'));
  1489. $result = $this->Html->meta('robots', 'ALL', ['block' => true]);
  1490. $this->assertNull($result);
  1491. $result = $this->Html->meta('icon', 'favicon.ico', ['block' => 'metaTags']);
  1492. $this->assertNull($result);
  1493. }
  1494. /**
  1495. * testTableHeaders method
  1496. *
  1497. * @return void
  1498. */
  1499. public function testTableHeaders()
  1500. {
  1501. $result = $this->Html->tableHeaders(['ID', 'Name', 'Date']);
  1502. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1503. $this->assertHtml($expected, $result);
  1504. $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight']], 'Date']);
  1505. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1506. $this->assertHtml($expected, $result);
  1507. $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
  1508. $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1509. $this->assertHtml($expected, $result);
  1510. $result = $this->Html->tableHeaders(['ID', ['Name' => []], 'Date']);
  1511. $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
  1512. $this->assertHtml($expected, $result);
  1513. }
  1514. /**
  1515. * testTableCells method
  1516. *
  1517. * @return void
  1518. */
  1519. public function testTableCells()
  1520. {
  1521. $tr = [
  1522. 'td content 1',
  1523. ['td content 2', ["width" => "100px"]],
  1524. ['td content 3', ['width' => '100px']]
  1525. ];
  1526. $result = $this->Html->tableCells($tr);
  1527. $expected = [
  1528. '<tr',
  1529. '<td', 'td content 1', '/td',
  1530. ['td' => ['width' => '100px']], 'td content 2', '/td',
  1531. ['td' => ['width' => 'preg:/100px/']], 'td content 3', '/td',
  1532. '/tr'
  1533. ];
  1534. $this->assertHtml($expected, $result);
  1535. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1536. $result = $this->Html->tableCells($tr, null, null, true);
  1537. $expected = [
  1538. '<tr',
  1539. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1540. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1541. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1542. '/tr'
  1543. ];
  1544. $this->assertHtml($expected, $result);
  1545. $tr = ['td content 1', 'td content 2', 'td content 3'];
  1546. $result = $this->Html->tableCells($tr, true);
  1547. $expected = [
  1548. '<tr',
  1549. ['td' => ['class' => 'column-1']], 'td content 1', '/td',
  1550. ['td' => ['class' => 'column-2']], 'td content 2', '/td',
  1551. ['td' => ['class' => 'column-3']], 'td content 3', '/td',
  1552. '/tr'
  1553. ];
  1554. $this->assertHtml($expected, $result);
  1555. $tr = [
  1556. ['td content 1', 'td content 2', 'td content 3'],
  1557. ['td content 1', 'td content 2', 'td content 3'],
  1558. ['td content 1', 'td content 2', 'td content 3']
  1559. ];
  1560. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1561. $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>";
  1562. $this->assertEquals($expected, $result);
  1563. $tr = [
  1564. ['td content 1', 'td content 2', 'td content 3'],
  1565. ['td content 1', 'td content 2', 'td content 3'],
  1566. ['td content 1', 'td content 2', 'td content 3'],
  1567. ['td content 1', 'td content 2', 'td content 3']
  1568. ];
  1569. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1570. $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>";
  1571. $this->assertEquals($expected, $result);
  1572. $tr = [
  1573. ['td content 1', 'td content 2', 'td content 3'],
  1574. ['td content 1', 'td content 2', 'td content 3'],
  1575. ['td content 1', 'td content 2', 'td content 3']
  1576. ];
  1577. $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
  1578. $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
  1579. $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>";
  1580. $this->assertEquals($expected, $result);
  1581. $tr = [
  1582. 'td content 1',
  1583. 'td content 2',
  1584. ['td content 3', ['class' => 'foo']]
  1585. ];
  1586. $result = $this->Html->tableCells($tr, null, null, 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' => 'foo column-3']], 'td content 3', '/td',
  1592. '/tr'
  1593. ];
  1594. $this->assertHtml($expected, $result);
  1595. }
  1596. /**
  1597. * testTag method
  1598. *
  1599. * @return void
  1600. */
  1601. public function testTag()
  1602. {
  1603. $result = $this->Html->tag('div');
  1604. $this->assertHtml('<div', $result);
  1605. $result = $this->Html->tag('div', 'text');
  1606. $this->assertHtml(['<div', 'text', '/div'], $result);
  1607. $result = $this->Html->tag('div', '<text>', ['class' => 'class-name', 'escape' => true]);
  1608. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1609. $this->assertHtml($expected, $result);
  1610. $result = $this->Html->tag(false, '<em>stuff</em>');
  1611. $this->assertEquals('<em>stuff</em>', $result);
  1612. $result = $this->Html->tag(null, '<em>stuff</em>');
  1613. $this->assertEquals('<em>stuff</em>', $result);
  1614. $result = $this->Html->tag('', '<em>stuff</em>');
  1615. $this->assertEquals('<em>stuff</em>', $result);
  1616. }
  1617. /**
  1618. * testDiv method
  1619. *
  1620. * @return void
  1621. */
  1622. public function testDiv()
  1623. {
  1624. $result = $this->Html->div('class-name');
  1625. $expected = ['div' => ['class' => 'class-name']];
  1626. $this->assertHtml($expected, $result);
  1627. $result = $this->Html->div('class-name', 'text');
  1628. $expected = ['div' => ['class' => 'class-name'], 'text', '/div'];
  1629. $this->assertHtml($expected, $result);
  1630. $result = $this->Html->div('class-name', '<text>', ['escape' => true]);
  1631. $expected = ['div' => ['class' => 'class-name'], '&lt;text&gt;', '/div'];
  1632. $this->assertHtml($expected, $result);
  1633. }
  1634. /**
  1635. * testPara method
  1636. *
  1637. * @return void
  1638. */
  1639. public function testPara()
  1640. {
  1641. $result = $this->Html->para('class-name', '');
  1642. $expected = ['p' => ['class' => 'class-name']];
  1643. $this->assertHtml($expected, $result);
  1644. $result = $this->Html->para('class-name', 'text');
  1645. $expected = ['p' => ['class' => 'class-name'], 'text', '/p'];
  1646. $this->assertHtml($expected, $result);
  1647. $result = $this->Html->para('class-name', '<text>', ['escape' => true]);
  1648. $expected = ['p' => ['class' => 'class-name'], '&lt;text&gt;', '/p'];
  1649. $this->assertHtml($expected, $result);
  1650. }
  1651. /**
  1652. * testMedia method
  1653. *
  1654. * @return void
  1655. */
  1656. public function testMedia()
  1657. {
  1658. $result = $this->Html->media('video.webm');
  1659. $expected = ['video' => ['src' => 'files/video.webm'], '/video'];
  1660. $this->assertHtml($expected, $result);
  1661. $result = $this->Html->media('video.webm', [
  1662. 'text' => 'Your browser does not support the HTML5 Video element.'
  1663. ]);
  1664. $expected = ['video' => ['src' => 'files/video.webm'], 'Your browser does not support the HTML5 Video element.', '/video'];
  1665. $this->assertHtml($expected, $result);
  1666. $result = $this->Html->media('video.webm', ['autoload', 'muted' => 'muted']);
  1667. $expected = [
  1668. 'video' => [
  1669. 'src' => 'files/video.webm',
  1670. 'autoload' => 'autoload',
  1671. 'muted' => 'muted'
  1672. ],
  1673. '/video'
  1674. ];
  1675. $this->assertHtml($expected, $result);
  1676. $result = $this->Html->media(
  1677. ['video.webm', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
  1678. ['pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.']
  1679. );
  1680. $expected = [
  1681. 'video' => ['poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'],
  1682. ['source' => ['src' => 'videos/video.webm', 'type' => 'video/webm']],
  1683. ['source' => ['src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;']],
  1684. 'Your browser does not support the HTML5 Video element.',
  1685. '/video'
  1686. ];
  1687. $this->assertHtml($expected, $result);
  1688. $result = $this->Html->media('video.ogv', ['tag' => 'video']);
  1689. $expected = ['video' => ['src' => 'files/video.ogv'], '/video'];
  1690. $this->assertHtml($expected, $result);
  1691. $result = $this->Html->media('audio.mp3');
  1692. $expected = ['audio' => ['src' => 'files/audio.mp3'], '/audio'];
  1693. $this->assertHtml($expected, $result);
  1694. $result = $this->Html->media(
  1695. [['src' => 'video.mov', 'type' => 'video/mp4'], 'video.webm']
  1696. );
  1697. $expected = [
  1698. '<video',
  1699. ['source' => ['src' => 'files/video.mov', 'type' => 'video/mp4']],
  1700. ['source' => ['src' => 'files/video.webm', 'type' => 'video/webm']],
  1701. '/video'
  1702. ];
  1703. $this->assertHtml($expected, $result);
  1704. $result = $this->Html->media(null, ['src' => 'video.webm']);
  1705. $expected = [
  1706. 'video' => ['src' => 'files/video.webm'],
  1707. '/video'
  1708. ];
  1709. $this->assertHtml($expected, $result);
  1710. }
  1711. /**
  1712. * testCrumbList method
  1713. *
  1714. * @return void
  1715. */
  1716. public function testCrumbList()
  1717. {
  1718. $this->assertNull($this->Html->getCrumbList());
  1719. $this->Html->addCrumb('Home', '/', ['class' => 'home']);
  1720. $this->Html->addCrumb('Some page', '/some_page');
  1721. $this->Html->addCrumb('Another page');
  1722. $result = $this->Html->getCrumbList(
  1723. ['class' => 'breadcrumbs']
  1724. );
  1725. $expected = [
  1726. ['ul' => ['class' => 'breadcrumbs']],
  1727. ['li' => ['class' => 'first']],
  1728. ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
  1729. '/li',
  1730. '<li',
  1731. ['a' => ['href' => '/some_page']], 'Some page', '/a',
  1732. '/li',
  1733. ['li' => ['class' => 'last']],
  1734. 'Another page',
  1735. '/li',
  1736. '/ul'
  1737. ];
  1738. $this->assertHtml($expected, $result);
  1739. }
  1740. /**
  1741. * Test getCrumbList startText
  1742. *
  1743. * @return void
  1744. */
  1745. public function testCrumbListFirstLink()
  1746. {
  1747. $this->Html->addCrumb('First', '#first');
  1748. $this->Html->addCrumb('Second', '#second');
  1749. $result = $this->Html->getCrumbList([], 'Home');
  1750. $expected = [
  1751. '<ul',
  1752. ['li' => ['class' => 'first']],
  1753. ['a' => ['href' => '/']], 'Home', '/a',
  1754. '/li',
  1755. '<li',
  1756. ['a' => ['href' => '#first']], 'First', '/a',
  1757. '/li',
  1758. ['li' => ['class' => 'last']],
  1759. ['a' => ['href' => '#second']], 'Second', '/a',
  1760. '/li',
  1761. '/ul'
  1762. ];
  1763. $this->assertHtml($expected, $result);
  1764. $result = $this->Html->getCrumbList([], ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
  1765. $expected = [
  1766. '<ul',
  1767. ['li' => ['class' => 'first']],
  1768. ['a' => ['href' => '/home']], 'img' => ['src' => '/home.png'], '/a',
  1769. '/li',
  1770. '<li',
  1771. ['a' => ['href' => '#first']], 'First', '/a',
  1772. '/li',
  1773. ['li' => ['class' => 'last']],
  1774. ['a' => ['href' => '#second']], 'Second', '/a',
  1775. '/li',
  1776. '/ul'
  1777. ];
  1778. $this->assertHtml($expected, $result);
  1779. }
  1780. /**
  1781. * test getCrumbList() in Twitter Bootstrap style.
  1782. *
  1783. * @return void
  1784. */
  1785. public function testCrumbListBootstrapStyle()
  1786. {
  1787. $this->Html->addCrumb('Home', '/', ['class' => 'home']);
  1788. $this->Html->addCrumb('Library', '/lib');
  1789. $this->Html->addCrumb('Data');
  1790. $result = $this->Html->getCrumbList([
  1791. 'class' => 'breadcrumb',
  1792. 'separator' => '<span class="divider">-</span>',
  1793. 'firstClass' => false,
  1794. 'lastClass' => 'active'
  1795. ]);
  1796. $expected = [
  1797. ['ul' => ['class' => 'breadcrumb']],
  1798. '<li',
  1799. ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
  1800. ['span' => ['class' => 'divider']], '-', '/span',
  1801. '/li',
  1802. '<li',
  1803. ['a' => ['href' => '/lib']], 'Library', '/a',
  1804. ['span' => ['class' => 'divider']], '-', '/span',
  1805. '/li',
  1806. ['li' => ['class' => 'active']], 'Data', '/li',
  1807. '/ul'
  1808. ];
  1809. $this->assertHtml($expected, $result);
  1810. }
  1811. /**
  1812. * Test GetCrumbList using style of Zurb Foundation.
  1813. *
  1814. * @return void
  1815. */
  1816. public function testCrumbListZurbStyle()
  1817. {
  1818. $this->Html->addCrumb('Home', '#');
  1819. $this->Html->addCrumb('Features', '#');
  1820. $this->Html->addCrumb('Gene Splicing', '#');
  1821. $this->Html->addCrumb('Home', '#');
  1822. $result = $this->Html->getCrumbList(
  1823. ['class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current']
  1824. );
  1825. $expected = [
  1826. ['ul' => ['class' => 'breadcrumbs']],
  1827. '<li',
  1828. ['a' => ['href' => '#']], 'Home', '/a',
  1829. '/li',
  1830. '<li',
  1831. ['a' => ['href' => '#']], 'Features', '/a',
  1832. '/li',
  1833. '<li',
  1834. ['a' => ['href' => '#']], 'Gene Splicing', '/a',
  1835. '/li',
  1836. ['li' => ['class' => 'current']],
  1837. ['a' => ['href' => '#']], 'Home', '/a',
  1838. '/li',
  1839. '/ul'
  1840. ];
  1841. $this->assertHtml($expected, $result, true);
  1842. }
  1843. /**
  1844. * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
  1845. *
  1846. * @return void
  1847. */
  1848. public function testCssAndScriptWithSameName()
  1849. {
  1850. $result = $this->Html->css('foo');
  1851. $expected = [
  1852. 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/']
  1853. ];
  1854. $this->assertHtml($expected, $result);
  1855. $result = $this->Html->script('foo');
  1856. $expected = [
  1857. 'script' => ['src' => 'js/foo.js']
  1858. ];
  1859. $this->assertHtml($expected, $result);
  1860. }
  1861. }