PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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