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

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

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 2301 lines | 1630 code | 320 blank | 351 comment | 4 complexity | 8947050650bf07d9c06f802c402be43c MD5 | raw file

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

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

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