PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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