PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php

https://bitbucket.org/praveen_excell/opshop
PHP | 1910 lines | 1341 code | 273 blank | 296 comment | 4 complexity | 2d9dbac783d5ea709c7a1af9f83e85da MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * HtmlHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('Helper', 'View');
  21. App::uses('AppHelper', 'View/Helper');
  22. App::uses('HtmlHelper', 'View/Helper');
  23. App::uses('FormHelper', 'View/Helper');
  24. App::uses('ClassRegistry', 'Utility');
  25. App::uses('Folder', 'Utility');
  26. if (!defined('FULL_BASE_URL')) {
  27. define('FULL_BASE_URL', 'http://cakephp.org');
  28. }
  29. /**
  30. * TheHtmlTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class TheHtmlTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'TheTest'
  39. */
  40. public $name = 'TheTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. class TestHtmlHelper extends HtmlHelper {
  49. /**
  50. * expose a method as public
  51. *
  52. * @param string $options
  53. * @param string $exclude
  54. * @param string $insertBefore
  55. * @param string $insertAfter
  56. * @return void
  57. */
  58. public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  59. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  60. }
  61. /**
  62. * Get a protected attribute value
  63. *
  64. * @param string $attribute
  65. * @return mixed
  66. */
  67. public function getAttribute($attribute) {
  68. if (!isset($this->{$attribute})) {
  69. return null;
  70. }
  71. return $this->{$attribute};
  72. }
  73. }
  74. /**
  75. * Html5TestHelper class
  76. *
  77. * @package Cake.Test.Case.View.Helper
  78. */
  79. class Html5TestHelper extends TestHtmlHelper {
  80. /**
  81. * Minimized
  82. *
  83. * @var array
  84. */
  85. protected $_minimizedAttributes = array('require', 'checked');
  86. /**
  87. * Allow compact use in HTML
  88. *
  89. * @var string
  90. */
  91. protected $_minimizedAttributeFormat = '%s';
  92. /**
  93. * Test to attribute format
  94. *
  95. * @var string
  96. */
  97. protected $_attributeFormat = 'data-%s="%s"';
  98. }
  99. /**
  100. * HtmlHelperTest class
  101. *
  102. * @package Cake.Test.Case.View.Helper
  103. */
  104. class HtmlHelperTest extends CakeTestCase {
  105. /**
  106. * Regexp for CDATA start block
  107. *
  108. * @var string
  109. */
  110. public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  111. /**
  112. * Regexp for CDATA end block
  113. *
  114. * @var string
  115. */
  116. public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  117. /**
  118. * html property
  119. *
  120. * @var object
  121. */
  122. public $Html = null;
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp() {
  129. parent::setUp();
  130. $this->View = $this->getMock('View', array('append'), array(new TheHtmlTestController()));
  131. $this->Html = new TestHtmlHelper($this->View);
  132. $this->Html->request = new CakeRequest(null, false);
  133. $this->Html->request->webroot = '';
  134. App::build(array(
  135. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  136. ));
  137. Configure::write('Asset.timestamp', false);
  138. }
  139. /**
  140. * tearDown method
  141. *
  142. * @return void
  143. */
  144. public function tearDown() {
  145. parent::tearDown();
  146. unset($this->Html, $this->View);
  147. }
  148. /**
  149. * testDocType method
  150. *
  151. * @return void
  152. */
  153. public function testDocType() {
  154. $result = $this->Html->docType();
  155. $expected = '<!DOCTYPE html>';
  156. $this->assertEquals($expected, $result);
  157. $result = $this->Html->docType('html4-strict');
  158. $expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
  159. $this->assertEquals($expected, $result);
  160. $this->assertNull($this->Html->docType('non-existing-doctype'));
  161. }
  162. /**
  163. * testLink method
  164. *
  165. * @return void
  166. */
  167. public function testLink() {
  168. Router::connect('/:controller/:action/*');
  169. $this->Html->request->webroot = '';
  170. $result = $this->Html->link('/home');
  171. $expected = array('a' => array('href' => '/home'), 'preg:/\/home/', '/a');
  172. $this->assertTags($result, $expected);
  173. $result = $this->Html->link(array('action' => 'login', '<[You]>'));
  174. $expected = array(
  175. 'a' => array('href' => '/login/%3C%5BYou%5D%3E'),
  176. 'preg:/\/login\/&lt;\[You\]&gt;/',
  177. '/a'
  178. );
  179. $this->assertTags($result, $expected);
  180. Router::reload();
  181. $result = $this->Html->link('Posts', array('controller' => 'posts', 'action' => 'index', 'full_base' => true));
  182. $expected = array('a' => array('href' => FULL_BASE_URL . '/posts'), 'Posts', '/a');
  183. $this->assertTags($result, $expected);
  184. $result = $this->Html->link('Home', '/home', array('confirm' => 'Are you sure you want to do this?'));
  185. $expected = array(
  186. 'a' => array('href' => '/home', 'onclick' => 'return confirm(&#039;Are you sure you want to do this?&#039;);'),
  187. 'Home',
  188. '/a'
  189. );
  190. $this->assertTags($result, $expected);
  191. $result = $this->Html->link('Home', '/home', array('default' => false));
  192. $expected = array(
  193. 'a' => array('href' => '/home', 'onclick' => 'event.returnValue = false; return false;'),
  194. 'Home',
  195. '/a'
  196. );
  197. $this->assertTags($result, $expected);
  198. $result = $this->Html->link('Home', '/home', array('default' => false, 'onclick' => 'someFunction();'));
  199. $expected = array(
  200. 'a' => array('href' => '/home', 'onclick' => 'someFunction(); event.returnValue = false; return false;'),
  201. 'Home',
  202. '/a'
  203. );
  204. $this->assertTags($result, $expected);
  205. $result = $this->Html->link('Next >', '#');
  206. $expected = array(
  207. 'a' => array('href' => '#'),
  208. 'Next &gt;',
  209. '/a'
  210. );
  211. $this->assertTags($result, $expected);
  212. $result = $this->Html->link('Next >', '#', array('escape' => true));
  213. $expected = array(
  214. 'a' => array('href' => '#'),
  215. 'Next &gt;',
  216. '/a'
  217. );
  218. $this->assertTags($result, $expected);
  219. $result = $this->Html->link('Next >', '#', array('escape' => 'utf-8'));
  220. $expected = array(
  221. 'a' => array('href' => '#'),
  222. 'Next &gt;',
  223. '/a'
  224. );
  225. $this->assertTags($result, $expected);
  226. $result = $this->Html->link('Next >', '#', array('escape' => false));
  227. $expected = array(
  228. 'a' => array('href' => '#'),
  229. 'Next >',
  230. '/a'
  231. );
  232. $this->assertTags($result, $expected);
  233. $result = $this->Html->link('Next >', '#', array(
  234. 'title' => 'to escape &#8230; or not escape?',
  235. 'escape' => false
  236. ));
  237. $expected = array(
  238. 'a' => array('href' => '#', 'title' => 'to escape &#8230; or not escape?'),
  239. 'Next >',
  240. '/a'
  241. );
  242. $this->assertTags($result, $expected);
  243. $result = $this->Html->link('Next >', '#', array(
  244. 'title' => 'to escape &#8230; or not escape?',
  245. 'escape' => true
  246. ));
  247. $expected = array(
  248. 'a' => array('href' => '#', 'title' => 'to escape &amp;#8230; or not escape?'),
  249. 'Next &gt;',
  250. '/a'
  251. );
  252. $this->assertTags($result, $expected);
  253. $result = $this->Html->link('Original size', array(
  254. 'controller' => 'images', 'action' => 'view', 3, '?' => array('height' => 100, 'width' => 200)
  255. ));
  256. $expected = array(
  257. 'a' => array('href' => '/images/view/3?height=100&amp;width=200'),
  258. 'Original size',
  259. '/a'
  260. );
  261. $this->assertTags($result, $expected);
  262. Configure::write('Asset.timestamp', false);
  263. $result = $this->Html->link($this->Html->image('test.gif'), '#', array('escape' => false));
  264. $expected = array(
  265. 'a' => array('href' => '#'),
  266. 'img' => array('src' => 'img/test.gif', 'alt' => ''),
  267. '/a'
  268. );
  269. $this->assertTags($result, $expected);
  270. $result = $this->Html->image('test.gif', array('url' => '#'));
  271. $expected = array(
  272. 'a' => array('href' => '#'),
  273. 'img' => array('src' => 'img/test.gif', 'alt' => ''),
  274. '/a'
  275. );
  276. $this->assertTags($result, $expected);
  277. $result = $this->Html->link($this->Html->image('../favicon.ico'), '#', array('escape' => false));
  278. $expected = array(
  279. 'a' => array('href' => '#'),
  280. 'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
  281. '/a'
  282. );
  283. $this->assertTags($result, $expected);
  284. $result = $this->Html->image('../favicon.ico', array('url' => '#'));
  285. $expected = array(
  286. 'a' => array('href' => '#'),
  287. 'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
  288. '/a'
  289. );
  290. $this->assertTags($result, $expected);
  291. $result = $this->Html->link('http://www.example.org?param1=value1&param2=value2');
  292. $expected = array('a' => array('href' => 'http://www.example.org?param1=value1&amp;param2=value2'), 'http://www.example.org?param1=value1&amp;param2=value2', '/a');
  293. $this->assertTags($result, $expected);
  294. }
  295. /**
  296. * testImageTag method
  297. *
  298. * @return void
  299. */
  300. public function testImageTag() {
  301. $this->Html->request->webroot = '';
  302. $result = $this->Html->image('test.gif');
  303. $this->assertTags($result, array('img' => array('src' => 'img/test.gif', 'alt' => '')));
  304. $result = $this->Html->image('http://google.com/logo.gif');
  305. $this->assertTags($result, array('img' => array('src' => 'http://google.com/logo.gif', 'alt' => '')));
  306. $result = $this->Html->image(array('controller' => 'test', 'action' => 'view', 1, 'ext' => 'gif'));
  307. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  308. $result = $this->Html->image('/test/view/1.gif');
  309. $this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
  310. $result = $this->Html->image('test.gif?one=two&three=four');
  311. $this->assertTags($result, array('img' => array('src' => 'img/test.gif?one=two&amp;three=four', 'alt' => '')));
  312. }
  313. /**
  314. * Test that image() works with fullBase and a webroot not equal to /
  315. *
  316. * @return void
  317. */
  318. public function testImageWithFullBase() {
  319. $result = $this->Html->image('test.gif', array('fullBase' => true));
  320. $here = $this->Html->url('/', true);
  321. $this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => '')));
  322. $result = $this->Html->image('sub/test.gif', array('fullBase' => true));
  323. $here = $this->Html->url('/', true);
  324. $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
  325. $request = $this->Html->request;
  326. $request->webroot = '/myproject/';
  327. $request->base = '/myproject';
  328. Router::setRequestInfo($request);
  329. $result = $this->Html->image('sub/test.gif', array('fullBase' => true));
  330. $here = $this->Html->url('/', true);
  331. $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
  332. }
  333. /**
  334. * test image() with Asset.timestamp
  335. *
  336. * @return void
  337. */
  338. public function testImageWithTimestampping() {
  339. Configure::write('Asset.timestamp', 'force');
  340. $this->Html->request->webroot = '/';
  341. $result = $this->Html->image('cake.icon.png');
  342. $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
  343. Configure::write('debug', 0);
  344. Configure::write('Asset.timestamp', 'force');
  345. $result = $this->Html->image('cake.icon.png');
  346. $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
  347. $this->Html->request->webroot = '/testing/longer/';
  348. $result = $this->Html->image('cake.icon.png');
  349. $expected = array(
  350. 'img' => array('src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '')
  351. );
  352. $this->assertTags($result, $expected);
  353. }
  354. /**
  355. * Tests creation of an image tag using a theme and asset timestamping
  356. *
  357. * @return void
  358. */
  359. public function testImageTagWithTheme() {
  360. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  361. $themeExists = is_dir(WWW_ROOT . 'theme');
  362. App::uses('File', 'Utility');
  363. $testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'img' . DS . '__cake_test_image.gif';
  364. $File = new File($testfile, true);
  365. App::build(array(
  366. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  367. ));
  368. Configure::write('Asset.timestamp', true);
  369. Configure::write('debug', 1);
  370. $this->Html->request->webroot = '/';
  371. $this->Html->theme = 'test_theme';
  372. $result = $this->Html->image('__cake_test_image.gif');
  373. $this->assertTags($result, array(
  374. 'img' => array(
  375. 'src' => 'preg:/\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  376. 'alt' => ''
  377. )));
  378. $this->Html->request->webroot = '/testing/';
  379. $result = $this->Html->image('__cake_test_image.gif');
  380. $this->assertTags($result, array(
  381. 'img' => array(
  382. 'src' => 'preg:/\/testing\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
  383. 'alt' => ''
  384. )));
  385. $dir = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
  386. $dir->delete();
  387. if (!$themeExists) {
  388. $dir = new Folder(WWW_ROOT . 'theme');
  389. $dir->delete();
  390. }
  391. }
  392. /**
  393. * test theme assets in main webroot path
  394. *
  395. * @return void
  396. */
  397. public function testThemeAssetsInMainWebrootPath() {
  398. App::build(array(
  399. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  400. ));
  401. $webRoot = Configure::read('App.www_root');
  402. Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
  403. $this->Html->theme = 'test_theme';
  404. $result = $this->Html->css('webroot_test');
  405. $expected = array(
  406. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/webroot_test\.css/')
  407. );
  408. $this->assertTags($result, $expected);
  409. $this->Html->theme = 'test_theme';
  410. $result = $this->Html->css('theme_webroot');
  411. $expected = array(
  412. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/theme_webroot\.css/')
  413. );
  414. $this->assertTags($result, $expected);
  415. Configure::write('App.www_root', $webRoot);
  416. }
  417. /**
  418. * testStyle method
  419. *
  420. * @return void
  421. */
  422. public function testStyle() {
  423. $result = $this->Html->style('display: none;');
  424. $this->assertEquals('display: none;', $result);
  425. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'));
  426. $expected = 'display:none; margin:10px;';
  427. $this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
  428. $result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
  429. $lines = explode("\n", $result);
  430. $this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
  431. $this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
  432. }
  433. /**
  434. * testCssLink method
  435. *
  436. * @return void
  437. */
  438. public function testCssLink() {
  439. Configure::write('Asset.filter.css', false);
  440. $result = $this->Html->css('screen');
  441. $expected = array(
  442. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/')
  443. );
  444. $this->assertTags($result, $expected);
  445. $result = $this->Html->css('screen.css');
  446. $this->assertTags($result, $expected);
  447. CakePlugin::load('TestPlugin');
  448. $result = $this->Html->css('TestPlugin.style', null, array('plugin' => false));
  449. $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
  450. $this->assertTags($result, $expected);
  451. CakePlugin::unload('TestPlugin');
  452. $result = $this->Html->css('my.css.library');
  453. $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
  454. $this->assertTags($result, $expected);
  455. $result = $this->Html->css('screen.css?1234');
  456. $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
  457. $this->assertTags($result, $expected);
  458. $result = $this->Html->css('screen.css?with=param&other=param');
  459. $expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
  460. $this->assertTags($result, $expected);
  461. $result = $this->Html->css('http://whatever.com/screen.css?1234');
  462. $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
  463. $this->assertTags($result, $expected);
  464. Configure::write('Asset.filter.css', 'css.php');
  465. $result = $this->Html->css('cake.generic');
  466. $expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css/';
  467. $this->assertTags($result, $expected);
  468. $result = $this->Html->css('//example.com/css/cake.generic.css');
  469. $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
  470. $this->assertTags($result, $expected);
  471. Configure::write('Asset.filter.css', false);
  472. $result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
  473. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  474. $this->assertTags($result[0], $expected);
  475. $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
  476. $this->assertTags($result[1], $expected);
  477. $this->assertEquals(2, count($result));
  478. $this->View->expects($this->at(0))
  479. ->method('append')
  480. ->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
  481. $this->View->expects($this->at(1))
  482. ->method('append')
  483. ->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
  484. $result = $this->Html->css('css_in_head', null, array('inline' => false));
  485. $this->assertNull($result);
  486. $result = $this->Html->css('more_css_in_head', null, array('inline' => false));
  487. $this->assertNull($result);
  488. }
  489. /**
  490. * testPluginCssLink method
  491. *
  492. * @return void
  493. */
  494. public function testPluginCssLink() {
  495. Configure::write('Asset.filter.css', false);
  496. CakePlugin::load('TestPlugin');
  497. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  498. $expected = array(
  499. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/')
  500. );
  501. $this->assertTags($result, $expected);
  502. $result = $this->Html->css('TestPlugin.test_plugin_asset.css');
  503. $this->assertTags($result, $expected);
  504. $result = $this->Html->css('TestPlugin.my.css.library');
  505. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
  506. $this->assertTags($result, $expected);
  507. $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
  508. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
  509. $this->assertTags($result, $expected);
  510. Configure::write('Asset.filter.css', 'css.php');
  511. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  512. $expected['link']['href'] = 'preg:/.*test_plugin\/ccss\/test_plugin_asset\.css/';
  513. $this->assertTags($result, $expected);
  514. Configure::write('Asset.filter.css', false);
  515. $result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
  516. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  517. $this->assertTags($result[0], $expected);
  518. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
  519. $this->assertTags($result[1], $expected);
  520. $this->assertEquals(2, count($result));
  521. CakePlugin::unload('TestPlugin');
  522. }
  523. /**
  524. * test use of css() and timestamping
  525. *
  526. * @return void
  527. */
  528. public function testCssTimestamping() {
  529. Configure::write('debug', 2);
  530. Configure::write('Asset.timestamp', true);
  531. $expected = array(
  532. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
  533. );
  534. $result = $this->Html->css('cake.generic');
  535. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  536. $this->assertTags($result, $expected);
  537. Configure::write('debug', 0);
  538. $result = $this->Html->css('cake.generic');
  539. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
  540. $this->assertTags($result, $expected);
  541. Configure::write('Asset.timestamp', 'force');
  542. $result = $this->Html->css('cake.generic');
  543. $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
  544. $this->assertTags($result, $expected);
  545. $this->Html->request->webroot = '/testing/';
  546. $result = $this->Html->css('cake.generic');
  547. $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
  548. $this->assertTags($result, $expected);
  549. $this->Html->request->webroot = '/testing/longer/';
  550. $result = $this->Html->css('cake.generic');
  551. $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
  552. $this->assertTags($result, $expected);
  553. }
  554. /**
  555. * test use of css() and timestamping with plugin syntax
  556. *
  557. * @return void
  558. */
  559. public function testPluginCssTimestamping() {
  560. CakePlugin::load('TestPlugin');
  561. Configure::write('debug', 2);
  562. Configure::write('Asset.timestamp', true);
  563. $expected = array(
  564. 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
  565. );
  566. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  567. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  568. $this->assertTags($result, $expected);
  569. Configure::write('debug', 0);
  570. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  571. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
  572. $this->assertTags($result, $expected);
  573. Configure::write('Asset.timestamp', 'force');
  574. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  575. $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  576. $this->assertTags($result, $expected);
  577. $this->Html->request->webroot = '/testing/';
  578. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  579. $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  580. $this->assertTags($result, $expected);
  581. $this->Html->request->webroot = '/testing/longer/';
  582. $result = $this->Html->css('TestPlugin.test_plugin_asset');
  583. $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
  584. $this->assertTags($result, $expected);
  585. CakePlugin::unload('TestPlugin');
  586. }
  587. /**
  588. * test timestamp enforcement for script tags.
  589. *
  590. * @return void
  591. */
  592. public function testScriptTimestamping() {
  593. $this->skipIf(!is_writable(JS), 'webroot/js is not Writable, timestamp testing has been skipped.');
  594. Configure::write('debug', 2);
  595. Configure::write('Asset.timestamp', true);
  596. touch(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
  597. $timestamp = substr(strtotime('now'), 0, 8);
  598. $result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
  599. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  600. Configure::write('debug', 0);
  601. Configure::write('Asset.timestamp', 'force');
  602. $result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
  603. $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  604. unlink(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
  605. Configure::write('Asset.timestamp', false);
  606. }
  607. /**
  608. * test timestamp enforcement for script tags with plugin syntax.
  609. *
  610. * @return void
  611. */
  612. public function testPluginScriptTimestamping() {
  613. CakePlugin::load('TestPlugin');
  614. $pluginPath = App::pluginPath('TestPlugin');
  615. $pluginJsPath = $pluginPath . 'webroot/js';
  616. $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
  617. Configure::write('debug', 2);
  618. Configure::write('Asset.timestamp', true);
  619. touch($pluginJsPath . DS . '__cake_js_test.js');
  620. $timestamp = substr(strtotime('now'), 0, 8);
  621. $result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
  622. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  623. Configure::write('debug', 0);
  624. Configure::write('Asset.timestamp', 'force');
  625. $result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
  626. $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
  627. unlink($pluginJsPath . DS . '__cake_js_test.js');
  628. Configure::write('Asset.timestamp', false);
  629. CakePlugin::unload('TestPlugin');
  630. }
  631. /**
  632. * test that scripts added with uses() are only ever included once.
  633. * test script tag generation
  634. *
  635. * @return void
  636. */
  637. public function testScript() {
  638. $result = $this->Html->script('foo');
  639. $expected = array(
  640. 'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
  641. );
  642. $this->assertTags($result, $expected);
  643. $result = $this->Html->script(array('foobar', 'bar'));
  644. $expected = array(
  645. array('script' => array('type' => 'text/javascript', 'src' => 'js/foobar.js')),
  646. '/script',
  647. array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
  648. '/script',
  649. );
  650. $this->assertTags($result, $expected);
  651. $result = $this->Html->script('jquery-1.3');
  652. $expected = array(
  653. 'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
  654. );
  655. $this->assertTags($result, $expected);
  656. $result = $this->Html->script('test.json');
  657. $expected = array(
  658. 'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js')
  659. );
  660. $this->assertTags($result, $expected);
  661. $result = $this->Html->script('http://example.com/test.json');
  662. $expected = array(
  663. 'script' => array('type' => 'text/javascript', 'src' => 'http://example.com/test.json')
  664. );
  665. $this->assertTags($result, $expected);
  666. $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
  667. $expected = array(
  668. 'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
  669. );
  670. $this->assertTags($result, $expected);
  671. $result = $this->Html->script('test.json.js?foo=bar');
  672. $expected = array(
  673. 'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js?foo=bar')
  674. );
  675. $this->assertTags($result, $expected);
  676. $result = $this->Html->script('test.json.js?foo=bar&other=test');
  677. $expected = array(
  678. 'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js?foo=bar&amp;other=test')
  679. );
  680. $this->assertTags($result, $expected);
  681. $result = $this->Html->script('foo');
  682. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  683. $result = $this->Html->script(array('foo', 'bar', 'baz'));
  684. $this->assertNotRegExp('/foo.js/', $result);
  685. $result = $this->Html->script('foo', array('inline' => true, 'once' => false));
  686. $this->assertNotNull($result);
  687. $result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  688. $expected = array(
  689. 'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  690. );
  691. $this->assertTags($result, $expected);
  692. }
  693. /**
  694. * test that plugin scripts added with uses() are only ever included once.
  695. * test script tag generation with plugin syntax
  696. *
  697. * @return void
  698. */
  699. public function testPluginScript() {
  700. CakePlugin::load('TestPlugin');
  701. $result = $this->Html->script('TestPlugin.foo');
  702. $expected = array(
  703. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foo.js')
  704. );
  705. $this->assertTags($result, $expected);
  706. $result = $this->Html->script(array('TestPlugin.foobar', 'TestPlugin.bar'));
  707. $expected = array(
  708. array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foobar.js')),
  709. '/script',
  710. array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/bar.js')),
  711. '/script',
  712. );
  713. $this->assertTags($result, $expected);
  714. $result = $this->Html->script('TestPlugin.jquery-1.3');
  715. $expected = array(
  716. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.js')
  717. );
  718. $this->assertTags($result, $expected);
  719. $result = $this->Html->script('TestPlugin.test.json');
  720. $expected = array(
  721. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js')
  722. );
  723. $this->assertTags($result, $expected);
  724. $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
  725. $expected = array(
  726. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/jquery-1.3.2.js?someparam=foo')
  727. );
  728. $this->assertTags($result, $expected);
  729. $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
  730. $expected = array(
  731. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js?foo=bar')
  732. );
  733. $this->assertTags($result, $expected);
  734. $result = $this->Html->script('TestPlugin.foo');
  735. $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
  736. $result = $this->Html->script(array('TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz'));
  737. $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
  738. $result = $this->Html->script('TestPlugin.foo', array('inline' => true, 'once' => false));
  739. $this->assertNotNull($result);
  740. $result = $this->Html->script('TestPlugin.jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
  741. $expected = array(
  742. 'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
  743. );
  744. $this->assertTags($result, $expected);
  745. CakePlugin::unload('TestPlugin');
  746. }
  747. /**
  748. * test that script() works with blocks.
  749. *
  750. * @return void
  751. */
  752. public function testScriptWithBlocks() {
  753. $this->View->expects($this->at(0))
  754. ->method('append')
  755. ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
  756. $this->View->expects($this->at(1))
  757. ->method('append')
  758. ->with('script', $this->matchesRegularExpression('/bool_false.js/'));
  759. $this->View->expects($this->at(2))
  760. ->method('append')
  761. ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
  762. $result = $this->Html->script('script_in_head', array('inline' => false));
  763. $this->assertNull($result);
  764. $result = $this->Html->script('bool_false', false);
  765. $this->assertNull($result);
  766. $result = $this->Html->script('second_script', array('block' => 'headScripts'));
  767. $this->assertNull($result);
  768. }
  769. /**
  770. * Test that Asset.filter.js works.
  771. *
  772. * @return void
  773. */
  774. public function testScriptAssetFilter() {
  775. Configure::write('Asset.filter.js', 'js.php');
  776. $result = $this->Html->script('jquery-1.3');
  777. $expected = array(
  778. 'script' => array('type' => 'text/javascript', 'src' => 'cjs/jquery-1.3.js')
  779. );
  780. $this->assertTags($result, $expected);
  781. $result = $this->Html->script('//example.com/js/jquery-1.3.js');
  782. $expected = array(
  783. 'script' => array('type' => 'text/javascript', 'src' => '//example.com/js/jquery-1.3.js')
  784. );
  785. $this->assertTags($result, $expected);
  786. }
  787. /**
  788. * test a script file in the webroot/theme dir.
  789. *
  790. * @return void
  791. */
  792. public function testScriptInTheme() {
  793. $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
  794. $themeExists = is_dir(WWW_ROOT . 'theme');
  795. App::uses('File', 'Utility');
  796. $testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'js' . DS . '__test_js.js';
  797. $File = new File($testfile, true);
  798. App::build(array(
  799. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  800. ));
  801. $this->Html->webroot = '/';
  802. $this->Html->theme = 'test_theme';
  803. $result = $this->Html->script('__test_js.js');
  804. $expected = array(
  805. 'script' => array('src' => '/theme/test_theme/js/__test_js.js', 'type' => 'text/javascript')
  806. );
  807. $this->assertTags($result, $expected);
  808. $Folder = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
  809. $Folder->delete();
  810. if (!$themeExists) {
  811. $dir = new Folder(WWW_ROOT . 'theme');
  812. $dir->delete();
  813. }
  814. }
  815. /**
  816. * test Script block generation
  817. *
  818. * @return void
  819. */
  820. public function testScriptBlock() {
  821. $result = $this->Html->scriptBlock('window.foo = 2;');
  822. $expected = array(
  823. 'script' => array('type' => 'text/javascript'),
  824. $this->cDataStart,
  825. 'window.foo = 2;',
  826. $this->cDataEnd,
  827. '/script',
  828. );
  829. $this->assertTags($result, $expected);
  830. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
  831. $expected = array(
  832. 'script' => array('type' => 'text/javascript'),
  833. 'window.foo = 2;',
  834. '/script',
  835. );
  836. $this->assertTags($result, $expected);
  837. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
  838. $expected = array(
  839. 'script' => array('type' => 'text/javascript'),
  840. $this->cDataStart,
  841. 'window.foo = 2;',
  842. $this->cDataEnd,
  843. '/script',
  844. );
  845. $this->assertTags($result, $expected);
  846. $this->View->expects($this->at(0))
  847. ->method('append')
  848. ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
  849. $this->View->expects($this->at(1))
  850. ->method('append')
  851. ->with('scriptTop', $this->stringContains('alert('));
  852. $result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
  853. $this->assertNull($result);
  854. $result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
  855. $this->assertNull($result);
  856. $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
  857. $expected = array(
  858. 'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
  859. 'window.foo = 2;',
  860. '/script',
  861. );
  862. $this->assertTags($result, $expected);
  863. }
  864. /**
  865. * test script tag output buffering when using scriptStart() and scriptEnd();
  866. *
  867. * @return void
  868. */
  869. public function testScriptStartAndScriptEnd() {
  870. $result = $this->Html->scriptStart(array('safe' => true));
  871. $this->assertNull($result);
  872. echo 'this is some javascript';
  873. $result = $this->Html->scriptEnd();
  874. $expected = array(
  875. 'script' => array('type' => 'text/javascript'),
  876. $this->cDataStart,
  877. 'this is some javascript',
  878. $this->cDataEnd,
  879. '/script'
  880. );
  881. $this->assertTags($result, $expected);
  882. $result = $this->Html->scriptStart(array('safe' => false));
  883. $this->assertNull($result);
  884. echo 'this is some javascript';
  885. $result = $this->Html->scriptEnd();
  886. $expected = array(
  887. 'script' => array('type' => 'text/javascript'),
  888. 'this is some javascript',
  889. '/script'
  890. );
  891. $this->assertTags($result, $expected);
  892. $this->View->expects($this->once())
  893. ->method('append');
  894. $result = $this->Html->scriptStart(array('safe' => false, 'inline' => false));
  895. $this->assertNull($result);
  896. echo 'this is some javascript';
  897. $result = $this->Html->scriptEnd();
  898. $this->assertNull($result);
  899. }
  900. /**
  901. * testCharsetTag method
  902. *
  903. * @return void
  904. */
  905. public function testCharsetTag() {
  906. Configure::write('App.encoding', null);
  907. $result = $this->Html->charset();
  908. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')));
  909. Configure::write('App.encoding', 'ISO-8859-1');
  910. $result = $this->Html->charset();
  911. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1')));
  912. $result = $this->Html->charset('UTF-7');
  913. $this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7')));
  914. }
  915. /**
  916. * testGetCrumb and addCrumb method
  917. *
  918. * @return void
  919. */
  920. public function testBreadcrumb() {
  921. $this->assertNull($this->Html->getCrumbs());
  922. $this->Html->addCrumb('First', '#first');
  923. $this->Html->addCrumb('Second', '#second');
  924. $this->Html->addCrumb('Third', '#third');
  925. $result = $this->Html->getCrumbs();
  926. $expected = array(
  927. array('a' => array('href' => '#first')),
  928. 'First',
  929. '/a',
  930. '&raquo;',
  931. array('a' => array('href' => '#second')),
  932. 'Second',
  933. '/a',
  934. '&raquo;',
  935. array('a' => array('href' => '#third')),
  936. 'Third',
  937. '/a',
  938. );
  939. $this->assertTags($result, $expected);
  940. $result = $this->Html->getCrumbs(' &gt; ');
  941. $expected = array(
  942. array('a' => array('href' => '#first')),
  943. 'First',
  944. '/a',
  945. ' &gt; ',
  946. array('a' => array('href' => '#second')),
  947. 'Second',
  948. '/a',
  949. ' &gt; ',
  950. array('a' => array('href' => '#third')),
  951. 'Third',
  952. '/a',
  953. );
  954. $this->assertTags($result, $expected);
  955. $this->Html->addCrumb('Fourth', null);
  956. $result = $this->Html->getCrumbs();
  957. $expected = array(
  958. array('a' => array('href' => '#first')),
  959. 'First',
  960. '/a',
  961. '&raquo;',
  962. array('a' => array('href' => '#second')),
  963. 'Second',
  964. '/a',
  965. '&raquo;',
  966. array('a' => array('href' => '#third')),
  967. 'Third',
  968. '/a',
  969. '&raquo;',
  970. 'Fourth'
  971. );
  972. $this->assertTags($result, $expected);
  973. $result = $this->Html->getCrumbs('-', 'Start');
  974. $expected = array(
  975. array('a' => array('href' => '/')),
  976. 'Start',
  977. '/a',
  978. '-',
  979. array('a' => array('href' => '#first')),
  980. 'First',
  981. '/a',
  982. '-',
  983. array('a' => array('href' => '#second')),
  984. 'Second',
  985. '/a',
  986. '-',
  987. array('a' => array('href' => '#third')),
  988. 'Third',
  989. '/a',
  990. '-',
  991. 'Fourth'
  992. );
  993. $this->assertTags($result, $expected);
  994. }
  995. /**
  996. * Test the array form of $startText
  997. */
  998. public function testGetCrumbFirstLink() {
  999. $result = $this->Html->getCrumbList(null, 'Home');
  1000. $this->assertTags(
  1001. $result,
  1002. array(
  1003. '<ul',
  1004. array('li' => array('class' => 'first')),
  1005. array('a' => array('href' => '/')), 'Home', '/a',
  1006. '/li',
  1007. '/ul'
  1008. )
  1009. );
  1010. $this->Html->addCrumb('First', '#first');
  1011. $this->Html->addCrumb('Second', '#second');
  1012. $result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
  1013. $expected = array(
  1014. array('a' => array('href' => '/home')),
  1015. 'img' => array('src' => '/home.png'),
  1016. '/a',
  1017. ' - ',
  1018. array('a' => array('href' => '#first')),
  1019. 'First',
  1020. '/a',
  1021. ' - ',
  1022. array('a' => array('href' => '#second')),
  1023. 'Second',
  1024. '/a',
  1025. );
  1026. $this->assertTags($result, $expected);
  1027. }
  1028. /**
  1029. * testNestedList method
  1030. *
  1031. * @return void
  1032. */
  1033. public function testNestedList() {
  1034. $list = array(
  1035. 'Item 1',
  1036. 'Item 2' => array(
  1037. 'Item 2.1'
  1038. ),
  1039. 'Item 3',
  1040. 'Item 4' => array(
  1041. 'Item 4.1',
  1042. 'Item 4.2',
  1043. 'Item 4.3' => array(
  1044. 'Item 4.3.1',
  1045. 'Item 4.3.2'
  1046. )
  1047. ),
  1048. 'Item 5' => array(
  1049. 'Item 5.1',
  1050. 'Item 5.2'
  1051. )
  1052. );
  1053. $result = $this->Html->nestedList($list);
  1054. $expected = array(
  1055. '<ul',
  1056. '<li', 'Item 1', '/li',
  1057. '<li', 'Item 2',
  1058. '<ul', '<li', 'Item 2.1', '/li', '/ul',
  1059. '/li',
  1060. '<li', 'Item 3', '/li',
  1061. '<li', 'Item 4',
  1062. '<ul',
  1063. '<li', 'Item 4.1', '/li',
  1064. '<li', 'Item 4.2', '/li',
  1065. '<li', 'Item 4.3',
  1066. '<ul',
  1067. '<li', 'Item 4.3.1', '/li',
  1068. '<li', 'Item 4.3.2', '/li',
  1069. '/ul',
  1070. '/li',
  1071. '/ul',
  1072. '/li',
  1073. '<li', 'Item 5',
  1074. '<ul',
  1075. '<li', 'Item 5.1', '/li',
  1076. '<li', 'Item 5.2', '/li',
  1077. '/ul',
  1078. '/li',
  1079. '/ul'
  1080. );
  1081. $this->assertTags($result, $expected);
  1082. $result = $this->Html->nestedList($list, null);
  1083. $this->assertTags($result, $expected);
  1084. $result = $this->Html->nestedList($list, array(), array(), 'ol');
  1085. $expected = array(
  1086. '<ol',
  1087. '<li', 'Item 1', '/li',
  1088. '<li', 'Item 2',
  1089. '<ol', '<li', 'Item 2.1', '/li', '/ol',
  1090. '/li',
  1091. '<li', 'Item 3', '/li',
  1092. '<li', 'Item 4',
  1093. '<ol',
  1094. '<li', 'Item 4.1', '/li',
  1095. '<li', 'Item 4.2', '/li',
  1096. '<li', 'Item 4.3',
  1097. '<ol',
  1098. '<li', 'Item 4.3.1', '/li',
  1099. '<li', 'Item 4.3.2', '/li',
  1100. '/ol',
  1101. '/li',
  1102. '/ol',
  1103. '/li',
  1104. '<li', 'Item 5',
  1105. '<ol',
  1106. '<li', 'Item 5.1', '/li',
  1107. '<li', 'Item 5.2', '/li',
  1108. '/ol',
  1109. '/li',
  1110. '/ol'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Html->nestedList($list, 'ol');
  1114. $this->assertTags($result, $expected);
  1115. $result = $this->Html->nestedList($list, array('class' => 'list'));
  1116. $expected = array(
  1117. array('ul' => array('class' => 'list')),
  1118. '<li', 'Item 1', '/li',
  1119. '<li', 'Item 2',
  1120. array('ul' => array('class' => 'list')), '<li', 'Item 2.1', '/li', '/ul',
  1121. '/li',
  1122. '<li', 'Item 3', '/li',
  1123. '<li', 'Item 4',
  1124. array('ul' => array('class' => 'list')),
  1125. '<li', 'Item 4.1', '/li',
  1126. '<li', 'Item 4.2', '/li',
  1127. '<li', 'Item 4.3',
  1128. array('ul' => array('class' => 'list')),
  1129. '<li', 'Item 4.3.1', '/li',
  1130. '<li', 'Item 4.3.2', '/li',
  1131. '/ul',
  1132. '/li',
  1133. '/ul',
  1134. '/li',
  1135. '<li', 'Item 5',
  1136. array('ul' => array('class' => 'list')),
  1137. '<li', 'Item 5.1', '/li',
  1138. '<li', 'Item 5.2', '/li',
  1139. '/ul',
  1140. '/li',
  1141. '/ul'
  1142. );
  1143. $this->assertTags($result, $expected);
  1144. $result = $this->Html->nestedList($list, array(), array('class' => 'item'));
  1145. $expected = array(
  1146. '<ul',
  1147. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1148. array('li' => array('class' => 'item')), 'Item 2',
  1149. '<ul', array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1150. '/li',
  1151. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1152. array('li' => array('class' => 'item')), 'Item 4',
  1153. '<ul',
  1154. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1155. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1156. array('li' => array('class' => 'item')), 'Item 4.3',
  1157. '<ul',
  1158. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1159. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1160. '/ul',
  1161. '/li',
  1162. '/ul',
  1163. '/li',
  1164. array('li' => array('class' => 'item')), 'Item 5',
  1165. '<ul',
  1166. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1167. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1168. '/ul',
  1169. '/li',
  1170. '/ul'
  1171. );
  1172. $this->assertTags($result, $expected);
  1173. $result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
  1174. $expected = array(
  1175. '<ul',
  1176. array('li' => array('class' => 'odd')), 'Item 1', '/li',
  1177. array('li' => array('class' => 'even')), 'Item 2',
  1178. '<ul', array('li' => array('class' => 'odd')), 'Item 2.1', '/li', '/ul',
  1179. '/li',
  1180. array('li' => array('class' => 'odd')), 'Item 3', '/li',
  1181. array('li' => array('class' => 'even')), 'Item 4',
  1182. '<ul',
  1183. array('li' => array('class' => 'odd')), 'Item 4.1', '/li',
  1184. array('li' => array('class' => 'even')), 'Item 4.2', '/li',
  1185. array('li' => array('class' => 'odd')), 'Item 4.3',
  1186. '<ul',
  1187. array('li' => array('class' => 'odd')), 'Item 4.3.1', '/li',
  1188. array('li' => array('class' => 'even')), 'Item 4.3.2', '/li',
  1189. '/ul',
  1190. '/li',
  1191. '/ul',
  1192. '/li',
  1193. array('li' => array('class' => 'odd')), 'Item 5',
  1194. '<ul',
  1195. array('li' => array('class' => 'odd')), 'Item 5.1', '/li',
  1196. array('li' => array('class' => 'even')), 'Item 5.2', '/li',
  1197. '/ul',
  1198. '/li',
  1199. '/ul'
  1200. );
  1201. $this->assertTags($result, $expected);
  1202. $result = $this->Html->nestedList($list, array('class' => 'list'), array('class' => 'item'));
  1203. $expected = array(
  1204. array('ul' => array('class' => 'list')),
  1205. array('li' => array('class' => 'item')), 'Item 1', '/li',
  1206. array('li' => array('class' => 'item')), 'Item 2',
  1207. array('ul' => array('class' => 'list')), array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
  1208. '/li',
  1209. array('li' => array('class' => 'item')), 'Item 3', '/li',
  1210. array('li' => array('class' => 'item')), 'Item 4',
  1211. array('ul' => array('class' => 'list')),
  1212. array('li' => array('class' => 'item')), 'Item 4.1', '/li',
  1213. array('li' => array('class' => 'item')), 'Item 4.2', '/li',
  1214. array('li' => array('class' => 'item')), 'Item 4.3',
  1215. array('ul' => array('class' => 'list')),
  1216. array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
  1217. array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
  1218. '/ul',
  1219. '/li',
  1220. '/ul',
  1221. '/li',
  1222. array('li' => array('class' => 'item')), 'Item 5',
  1223. array('ul' => array('class' => 'list')),
  1224. array('li' => array('class' => 'item')), 'Item 5.1', '/li',
  1225. array('li' => array('class' => 'item')), 'Item 5.2', '/li',
  1226. '/ul',
  1227. '/li',
  1228. '/ul'
  1229. );
  1230. $this->assertTags($result, $expected);
  1231. }
  1232. /**
  1233. * testMeta method
  1234. *
  1235. * @return void
  1236. */
  1237. public function testMeta() {
  1238. $result = $this->Html->meta('this is an rss feed', array('controller' => 'posts', 'ext' => 'rss'));
  1239. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1240. $result = $this->Html->meta('rss', array('controller' => 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed'));
  1241. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
  1242. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'));
  1243. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1244. $result = $this->Html->meta('non-existing');
  1245. $this->assertTags($result, array('<meta'));
  1246. $result = $this->Html->meta('non-existing', '/posts.xpp');
  1247. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'non-existing')));
  1248. $result = $this->Html->meta('non-existing', '/posts.xpp', array('type' => 'atom'));
  1249. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing')));
  1250. $result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'), array('link' => '/articles.rss'));
  1251. $this->assertTags($result, array('link' => array('href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom')));
  1252. $result = $this->Html->meta(array('link' => 'favicon.ico', 'rel' => 'icon'));
  1253. $expected = array(
  1254. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'icon'),
  1255. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'shortcut icon'))
  1256. );
  1257. $this->assertTags($result, $expected);
  1258. $result = $this->Html->meta('icon', 'favicon.ico');
  1259. $expected = array(
  1260. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1261. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1262. );
  1263. $this->assertTags($result, $expected);
  1264. $result = $this->Html->meta('icon');
  1265. $expected = array(
  1266. 'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
  1267. array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
  1268. );
  1269. $this->assertTags($result, $expected);
  1270. $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
  1271. $this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
  1272. $this->assertRegExp('/\s+\/>$/', $result);
  1273. $result = $this->Html->meta('description', 'this is the meta description');
  1274. $this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));
  1275. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'));
  1276. $this->assertTags($result, array('meta' => array('name' => 'ROBOTS', 'content' => 'ALL')));
  1277. }
  1278. /**
  1279. * Test the inline and block options for meta()
  1280. */
  1281. public function testMetaWithBlocks() {
  1282. $this->View->expects($this->at(0))
  1283. ->method('append')
  1284. ->with('meta', $this->stringContains('ROBOTS'));
  1285. $this->View->expects($this->at(1))
  1286. ->method('append')
  1287. ->with('metaTags', $this->stringContains('favicon.ico'));
  1288. $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('inline' => false));
  1289. $this->assertNull($result);
  1290. $result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));
  1291. $this->assertNull($result);
  1292. }
  1293. /**
  1294. * testTableHeaders method
  1295. *
  1296. * @return void
  1297. */
  1298. public function testTableHeaders() {
  1299. $result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
  1300. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1301. $this->assertTags($result, $expected);
  1302. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight')), 'Date'));
  1303. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1304. $this->assertTags($result, $expected);
  1305. $result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight', 'width' => '120px')), 'Date'));
  1306. $expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1307. $this->assertTags($result, $expected);
  1308. $result = $this->Html->tableHeaders(array('ID', array('Name' => array()), 'Date'));
  1309. $expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
  1310. $this->assertTags($result, $expected);
  1311. }
  1312. /**
  1313. * testTableCells method
  1314. *
  1315. * @return void
  1316. */
  1317. public function testTableCells() {
  1318. $tr = array(
  1319. 'td content 1',
  1320. array('td content 2', array("width" => "100px")),
  1321. array('td content 3', "width=100px")
  1322. );
  1323. $result = $this->Html->tableCells($tr);
  1324. $expected = array(
  1325. '<tr',
  1326. '<td', 'td content 1', '/td',
  1327. array('td' => array('width' => '100px')), 'td content 2', '/td',
  1328. array('td' => array('width' => 'preg:/100px/')), 'td content 3', '/td',
  1329. '/tr'
  1330. );
  1331. $this->assertTags($result, $expected);
  1332. $tr = array('td content 1', 'td content 2', 'td content 3');
  1333. $result = $this->Html->tableCells($tr, null, null, true);
  1334. $expected = array(
  1335. '<tr',
  1336. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1337. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1338. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1339. '/tr'
  1340. );
  1341. $this->assertTags($result, $expected);
  1342. $tr = array('td content 1', 'td content 2', 'td content 3');
  1343. $result = $this->Html->tableCells($tr, true);
  1344. $expected = array(
  1345. '<tr',
  1346. array('td' => array('class' => 'column-1')), 'td content 1', '/td',
  1347. array('td' => array('class' => 'column-2')), 'td content 2', '/td',
  1348. array('td' => array('class' => 'column-3')), 'td content 3', '/td',
  1349. '/tr'
  1350. );
  1351. $this->assertTags($result, $expected);
  1352. $tr = array(
  1353. array('td content 1', 'td content 2', 'td content 3'),
  1354. array('td content 1', 'td content 2', 'td content 3'),
  1355. array('td content 1', 'td content 2', 'td content 3')
  1356. );
  1357. $result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
  1358. $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=\"ev…

Large files files are truncated, but you can click here to view the full file