PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cakeX/tests/cases/libs/view/helper.test.php

https://github.com/v2ninad/nm_cake
PHP | 886 lines | 522 code | 130 blank | 234 comment | 2 complexity | 32828472b217f20358cb907fd1d09c31 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::import('Core', array('View', 'Helper', 'Router'));
  20. /**
  21. * HelperTestPost class
  22. *
  23. * @package cake.tests.cases.libs.view
  24. */
  25. class HelperTestPost extends Model {
  26. /**
  27. * useTable property
  28. *
  29. * @var bool false
  30. * @access public
  31. */
  32. public $useTable = false;
  33. /**
  34. * schema method
  35. *
  36. * @access public
  37. * @return void
  38. */
  39. function schema($field = false) {
  40. $this->_schema = array(
  41. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  42. 'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  43. 'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
  44. 'number' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  45. 'date' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  46. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  47. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  48. );
  49. return $this->_schema;
  50. }
  51. /**
  52. * hasAndBelongsToMany property
  53. *
  54. * @var array
  55. * @access public
  56. */
  57. public $hasAndBelongsToMany = array('HelperTestTag'=> array('with' => 'HelperTestPostsTag'));
  58. }
  59. /**
  60. * HelperTestComment class
  61. *
  62. * @package cake.tests.cases.libs.view
  63. */
  64. class HelperTestComment extends Model {
  65. /**
  66. * useTable property
  67. *
  68. * @var bool false
  69. * @access public
  70. */
  71. public $useTable = false;
  72. /**
  73. * schema method
  74. *
  75. * @access public
  76. * @return void
  77. */
  78. function schema($field = false) {
  79. $this->_schema = array(
  80. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  81. 'author_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  82. 'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  83. 'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
  84. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  85. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  86. );
  87. return $this->_schema;
  88. }
  89. }
  90. /**
  91. * HelperTestTag class
  92. *
  93. * @package cake.tests.cases.libs.view
  94. */
  95. class HelperTestTag extends Model {
  96. /**
  97. * useTable property
  98. *
  99. * @var bool false
  100. * @access public
  101. */
  102. public $useTable = false;
  103. /**
  104. * schema method
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. function schema($field = false) {
  110. $this->_schema = array(
  111. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  112. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  113. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  114. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  115. );
  116. return $this->_schema;
  117. }
  118. }
  119. /**
  120. * HelperTestPostsTag class
  121. *
  122. * @package cake.tests.cases.libs.view
  123. */
  124. class HelperTestPostsTag extends Model {
  125. /**
  126. * useTable property
  127. *
  128. * @var bool false
  129. * @access public
  130. */
  131. public $useTable = false;
  132. /**
  133. * schema method
  134. *
  135. * @access public
  136. * @return void
  137. */
  138. function schema($field = false) {
  139. $this->_schema = array(
  140. 'helper_test_post_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  141. 'helper_test_tag_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  142. );
  143. return $this->_schema;
  144. }
  145. }
  146. class TestHelper extends Helper {
  147. /**
  148. * helpers for this helper.
  149. *
  150. * @var string
  151. */
  152. var $helpers = array('Html', 'TestPlugin.OtherHelper');
  153. /**
  154. * expose a method as public
  155. *
  156. * @param string $options
  157. * @param string $exclude
  158. * @param string $insertBefore
  159. * @param string $insertAfter
  160. * @return void
  161. */
  162. function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  163. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  164. }
  165. }
  166. /**
  167. * Html5TestHelper class
  168. *
  169. * @package cake.tests.cases.libs.view
  170. */
  171. class Html5TestHelper extends TestHelper {
  172. /**
  173. * Minimized
  174. *
  175. * @var array
  176. */
  177. protected $_minimizedAttributes = array('require', 'checked');
  178. /**
  179. * Allow compact use in HTML
  180. *
  181. * @var string
  182. */
  183. protected $_minimizedAttributeFormat = '%s';
  184. /**
  185. * Test to attribute format
  186. *
  187. * @var string
  188. */
  189. protected $_attributeFormat = 'data-%s="%s"';
  190. }
  191. /**
  192. * HelperTest class
  193. *
  194. * @package cake.tests.cases.libs.view
  195. */
  196. class HelperTest extends CakeTestCase {
  197. /**
  198. * setUp method
  199. *
  200. * @access public
  201. * @return void
  202. */
  203. function setUp() {
  204. ClassRegistry::flush();
  205. Router::reload();
  206. $null = null;
  207. $this->View = new View($null);
  208. $this->Helper = new Helper($this->View);
  209. $this->Helper->request = new CakeRequest(null, false);
  210. ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
  211. ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
  212. ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
  213. }
  214. /**
  215. * tearDown method
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function tearDown() {
  221. unset($this->Helper, $this->View);
  222. ClassRegistry::flush();
  223. }
  224. /**
  225. * testFormFieldNameParsing method
  226. *
  227. * @access public
  228. * @return void
  229. */
  230. function testSetEntity() {
  231. // PHP4 reference hack
  232. ClassRegistry::removeObject('view');
  233. ClassRegistry::addObject('view', $this->View);
  234. $this->Helper->setEntity('HelperTestPost.id');
  235. $this->assertFalse($this->View->modelScope);
  236. $this->assertEqual($this->View->model, 'HelperTestPost');
  237. $this->assertEqual($this->View->field, 'id');
  238. $this->assertEqual($this->View->modelId, null);
  239. $this->assertEqual($this->View->association, null);
  240. $this->Helper->setEntity('HelperTestComment.body');
  241. $this->assertFalse($this->View->modelScope);
  242. $this->assertEqual($this->View->model, 'HelperTestComment');
  243. $this->assertEqual($this->View->field, 'body');
  244. $this->assertEqual($this->View->modelId, null);
  245. $this->assertEqual($this->View->association, null);
  246. $this->assertEqual($this->View->fieldSuffix, null);
  247. $this->Helper->setEntity('HelperTestPost', true);
  248. $this->assertTrue($this->View->modelScope);
  249. $this->assertEqual($this->View->model, 'HelperTestPost');
  250. $this->assertEqual($this->View->field, null);
  251. $this->assertEqual($this->View->modelId, null);
  252. $this->assertEqual($this->View->association, null);
  253. $this->assertEqual($this->View->fieldSuffix, null);
  254. $this->Helper->setEntity('_Token.fields');
  255. $this->assertTrue($this->View->modelScope);
  256. $this->assertEqual($this->View->model, 'HelperTestPost');
  257. $this->assertEqual($this->View->field, 'fields');
  258. $this->assertEqual($this->View->modelId, null);
  259. $this->assertEqual($this->View->association, '_Token');
  260. $this->assertEqual($this->View->fieldSuffix, null);
  261. $this->Helper->setEntity('id');
  262. $this->assertTrue($this->View->modelScope);
  263. $this->assertEqual($this->View->model, 'HelperTestPost');
  264. $this->assertEqual($this->View->field, 'id');
  265. $this->assertEqual($this->View->modelId, null);
  266. $this->assertEqual($this->View->association, null);
  267. $this->assertEqual($this->View->fieldSuffix, null);
  268. $this->Helper->setEntity('HelperTestComment.body');
  269. $this->assertTrue($this->View->modelScope);
  270. $this->assertEqual($this->View->model, 'HelperTestPost');
  271. $this->assertEqual($this->View->field, 'body');
  272. $this->assertEqual($this->View->modelId, null);
  273. $this->assertEqual($this->View->association, 'HelperTestComment');
  274. $this->assertEqual($this->View->fieldSuffix, null);
  275. $this->Helper->setEntity('body');
  276. $this->assertTrue($this->View->modelScope);
  277. $this->assertEqual($this->View->model, 'HelperTestPost');
  278. $this->assertEqual($this->View->field, 'body');
  279. $this->assertEqual($this->View->modelId, null);
  280. $this->assertEqual($this->View->association, null);
  281. $this->assertEqual($this->View->fieldSuffix, null);
  282. $this->Helper->setEntity('Something.else');
  283. $this->assertTrue($this->View->modelScope);
  284. $this->assertEqual($this->View->model, 'HelperTestPost');
  285. $this->assertEqual($this->View->field, 'else');
  286. $this->assertEqual($this->View->modelId, false);
  287. $this->assertEqual($this->View->association, 'Something');
  288. $this->assertEqual($this->View->fieldSuffix, '');
  289. $this->Helper->setEntity('5.id');
  290. $this->assertTrue($this->View->modelScope);
  291. $this->assertEqual($this->View->model, 'HelperTestPost');
  292. $this->assertEqual($this->View->field, 'id');
  293. $this->assertEqual($this->View->modelId, '5');
  294. $this->assertEqual($this->View->association, null);
  295. $this->assertEqual($this->View->fieldSuffix, null);
  296. $this->assertEqual($this->View->entity(), array('HelperTestPost', 5, 'id'));
  297. $this->Helper->setEntity('0.id');
  298. $this->assertTrue($this->View->modelScope);
  299. $this->assertEqual($this->View->model, 'HelperTestPost');
  300. $this->assertEqual($this->View->field, 'id');
  301. $this->assertEqual($this->View->modelId, '0');
  302. $this->assertEqual($this->View->association, null);
  303. $this->assertEqual($this->View->fieldSuffix, null);
  304. $this->assertEqual($this->View->entity(), array('HelperTestPost', 0, 'id'));
  305. $this->Helper->setEntity('5.created.month');
  306. $this->assertTrue($this->View->modelScope);
  307. $this->assertEqual($this->View->model, 'HelperTestPost');
  308. $this->assertEqual($this->View->field, 'created');
  309. $this->assertEqual($this->View->modelId, '5');
  310. $this->assertEqual($this->View->association, null);
  311. $this->assertEqual($this->View->fieldSuffix, 'month');
  312. $this->Helper->setEntity('HelperTestComment.5.id');
  313. $this->assertTrue($this->View->modelScope);
  314. $this->assertEqual($this->View->model, 'HelperTestPost');
  315. $this->assertEqual($this->View->field, 'id');
  316. $this->assertEqual($this->View->modelId, '5');
  317. $this->assertEqual($this->View->association, 'HelperTestComment');
  318. $this->assertEqual($this->View->fieldSuffix, null);
  319. $this->Helper->setEntity('HelperTestComment.id.time');
  320. $this->assertTrue($this->View->modelScope);
  321. $this->assertEqual($this->View->model, 'HelperTestPost');
  322. $this->assertEqual($this->View->field, 'id');
  323. $this->assertEqual($this->View->modelId, null);
  324. $this->assertEqual($this->View->association, 'HelperTestComment');
  325. $this->assertEqual($this->View->fieldSuffix, 'time');
  326. $this->Helper->setEntity('HelperTestTag');
  327. $this->assertTrue($this->View->modelScope);
  328. $this->assertEqual($this->View->model, 'HelperTestPost');
  329. $this->assertEqual($this->View->field, 'HelperTestTag');
  330. $this->assertEqual($this->View->modelId, '');
  331. $this->assertEqual($this->View->association, 'HelperTestTag');
  332. $this->assertEqual($this->View->fieldSuffix, '');
  333. $this->Helper->setEntity(null);
  334. $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
  335. $this->assertFalse($this->View->modelScope);
  336. $this->assertEqual($this->View->model, 'ModelThatDoesntExist');
  337. $this->assertEqual($this->View->field, 'field_that_doesnt_exist');
  338. $this->assertEqual($this->View->modelId, null);
  339. $this->assertEqual($this->View->association, null);
  340. $this->assertEqual($this->View->fieldSuffix, null);
  341. }
  342. /**
  343. * test that 'view' doesn't break things.
  344. *
  345. * @return void
  346. */
  347. function testSetEntityWithView() {
  348. $this->assertNull($this->Helper->setEntity('Allow.view.group_id'));
  349. $this->assertNull($this->Helper->setEntity('Allow.view'));
  350. $this->assertNull($this->Helper->setEntity('View.view'));
  351. }
  352. /**
  353. * test getting values from Helper
  354. *
  355. * @return void
  356. */
  357. function testValue() {
  358. $this->Helper->request->data = array('fullname' => 'This is me');
  359. $this->Helper->setEntity('fullname');
  360. $result = $this->Helper->value('fullname');
  361. $this->assertEqual($result, 'This is me');
  362. $this->Helper->request->data = array('Post' => array('name' => 'First Post'));
  363. $this->Helper->setEntity('Post.name');
  364. $result = $this->Helper->value('Post.name');
  365. $this->assertEqual($result, 'First Post');
  366. $this->Helper->request->data = array('Post' => array(2 => array('name' => 'First Post')));
  367. $this->Helper->setEntity('Post.2.name');
  368. $result = $this->Helper->value('Post.2.name');
  369. $this->assertEqual($result, 'First Post');
  370. $this->Helper->request->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
  371. $this->Helper->setEntity('Post.2.created');
  372. $result = $this->Helper->value('Post.2.created');
  373. $this->assertEqual($result, array('year' => '2008'));
  374. $this->Helper->request->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
  375. $this->Helper->setEntity('Post.2.created.year');
  376. $result = $this->Helper->value('Post.2.created.year');
  377. $this->assertEqual($result, '2008');
  378. $this->Helper->request->data = array('HelperTestTag' => array('HelperTestTag' => ''));
  379. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  380. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  381. $this->assertEqual($result, '');
  382. $this->Helper->request->data = array('HelperTestTag' => array('HelperTestTag' => array(2, 3, 4)));
  383. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  384. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  385. $this->assertEqual($result, array(2, 3, 4));
  386. $this->Helper->request->data = array(
  387. 'HelperTestTag' => array(
  388. array('id' => 3),
  389. array('id' => 5)
  390. )
  391. );
  392. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  393. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  394. $this->assertEqual($result, array(3 => 3, 5 => 5));
  395. $this->Helper->request->data = array('zero' => 0);
  396. $this->Helper->setEntity('zero');
  397. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  398. $this->assertEqual($result, array('value' => 0));
  399. $this->Helper->request->data = array('zero' => '0');
  400. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  401. $this->assertEqual($result, array('value' => '0'));
  402. $this->Helper->setEntity('inexistent');
  403. $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
  404. $this->assertEqual($result, array('value' => 'something'));
  405. }
  406. /**
  407. * Ensure HTML escaping of url params. So link addresses are valid and not exploited
  408. *
  409. * @return void
  410. */
  411. function testUrlConversion() {
  412. $result = $this->Helper->url('/controller/action/1');
  413. $this->assertEqual($result, '/controller/action/1');
  414. $result = $this->Helper->url('/controller/action/1?one=1&two=2');
  415. $this->assertEqual($result, '/controller/action/1?one=1&amp;two=2');
  416. $result = $this->Helper->url(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
  417. $this->assertEqual($result, "/posts/index/page:1&quot; onclick=&quot;alert(&#039;XSS&#039;);&quot;");
  418. $result = $this->Helper->url('/controller/action/1/param:this+one+more');
  419. $this->assertEqual($result, '/controller/action/1/param:this+one+more');
  420. $result = $this->Helper->url('/controller/action/1/param:this%20one%20more');
  421. $this->assertEqual($result, '/controller/action/1/param:this%20one%20more');
  422. $result = $this->Helper->url('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  423. $this->assertEqual($result, '/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  424. $result = $this->Helper->url(array(
  425. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  426. ));
  427. $this->assertEqual($result, "/posts/index/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24");
  428. $result = $this->Helper->url(array(
  429. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  430. '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
  431. ));
  432. $this->assertEqual($result, "/posts/index/page:1?one=value&amp;two=value&amp;three=purple");
  433. }
  434. /**
  435. * test assetTimestamp application
  436. *
  437. * @return void
  438. */
  439. function testAssetTimestamp() {
  440. $_timestamp = Configure::read('Asset.timestamp');
  441. $_debug = Configure::read('debug');
  442. Configure::write('Asset.timestamp', false);
  443. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  444. $this->assertEqual($result, CSS_URL . 'cake.generic.css');
  445. Configure::write('Asset.timestamp', true);
  446. Configure::write('debug', 0);
  447. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  448. $this->assertEqual($result, CSS_URL . 'cake.generic.css');
  449. Configure::write('Asset.timestamp', true);
  450. Configure::write('debug', 2);
  451. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  452. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  453. Configure::write('Asset.timestamp', 'force');
  454. Configure::write('debug', 0);
  455. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  456. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  457. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
  458. $this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
  459. $this->Helper->request->webroot = '/some/dir/';
  460. $result = $this->Helper->assetTimestamp('/some/dir/' . CSS_URL . 'cake.generic.css');
  461. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  462. Configure::write('debug', $_debug);
  463. Configure::write('Asset.timestamp', $_timestamp);
  464. }
  465. /**
  466. * test assetTimestamp with plugins and themes
  467. *
  468. * @return void
  469. */
  470. function testAssetTimestampPluginsAndThemes() {
  471. $_timestamp = Configure::read('Asset.timestamp');
  472. Configure::write('Asset.timestamp', 'force');
  473. App::build(array(
  474. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  475. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
  476. ));
  477. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  478. $this->assertPattern('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  479. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  480. $this->assertPattern('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  481. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/theme.js');
  482. $this->assertPattern('#/theme/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  483. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/non_existant.js');
  484. $this->assertPattern('#/theme/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  485. App::build();
  486. Configure::write('Asset.timestamp', $_timestamp);
  487. }
  488. /**
  489. * testFieldsWithSameName method
  490. *
  491. * @access public
  492. * @return void
  493. */
  494. function testFieldsWithSameName() {
  495. // PHP4 reference hack
  496. ClassRegistry::removeObject('view');
  497. ClassRegistry::addObject('view', $this->View);
  498. $this->Helper->setEntity('HelperTestTag', true);
  499. $this->Helper->setEntity('HelperTestTag.id');
  500. $this->assertEqual($this->View->model, 'HelperTestTag');
  501. $this->assertEqual($this->View->field, 'id');
  502. $this->assertEqual($this->View->modelId, null);
  503. $this->assertEqual($this->View->association, null);
  504. $this->assertEqual($this->View->fieldSuffix, null);
  505. $this->Helper->setEntity('My.id');
  506. $this->assertEqual($this->View->model, 'HelperTestTag');
  507. $this->assertEqual($this->View->field, 'id');
  508. $this->assertEqual($this->View->modelId, null);
  509. $this->assertEqual($this->View->association, 'My');
  510. $this->assertEqual($this->View->fieldSuffix, null);
  511. $this->Helper->setEntity('MyOther.id');
  512. $this->assertEqual($this->View->model, 'HelperTestTag');
  513. $this->assertEqual($this->View->field, 'id');
  514. $this->assertEqual($this->View->modelId, null);
  515. $this->assertEqual($this->View->association, 'MyOther');
  516. $this->assertEqual($this->View->fieldSuffix, null);
  517. }
  518. /**
  519. * testFieldSameAsModel method
  520. *
  521. * @access public
  522. * @return void
  523. */
  524. function testFieldSameAsModel() {
  525. // PHP4 reference hack
  526. ClassRegistry::removeObject('view');
  527. ClassRegistry::addObject('view', $this->View);
  528. $this->Helper->setEntity('HelperTestTag', true);
  529. $this->Helper->setEntity('helper_test_post');
  530. $this->assertEqual($this->View->model, 'HelperTestTag');
  531. $this->assertEqual($this->View->field, 'helper_test_post');
  532. $this->assertEqual($this->View->modelId, null);
  533. $this->assertEqual($this->View->association, null);
  534. $this->assertEqual($this->View->fieldSuffix, null);
  535. $this->Helper->setEntity('HelperTestTag');
  536. $this->assertEqual($this->View->model, 'HelperTestTag');
  537. $this->assertEqual($this->View->field, 'HelperTestTag');
  538. $this->assertEqual($this->View->modelId, null);
  539. $this->assertEqual($this->View->association, null);
  540. $this->assertEqual($this->View->fieldSuffix, null);
  541. $this->assertEqual($this->View->entityPath, 'HelperTestTag');
  542. }
  543. /**
  544. * testFieldSuffixForDate method
  545. *
  546. * @access public
  547. * @return void
  548. */
  549. function testFieldSuffixForDate() {
  550. // PHP4 reference hack
  551. ClassRegistry::removeObject('view');
  552. ClassRegistry::addObject('view', $this->View);
  553. $this->Helper->setEntity('HelperTestPost', true);
  554. $this->assertEqual($this->View->model, 'HelperTestPost');
  555. $this->assertEqual($this->View->field, null);
  556. $this->assertEqual($this->View->modelId, null);
  557. $this->assertEqual($this->View->association, null);
  558. $this->assertEqual($this->View->fieldSuffix, null);
  559. $this->Helper->setEntity('date.month');
  560. $this->assertEqual($this->View->model, 'HelperTestPost');
  561. $this->assertEqual($this->View->field, 'date');
  562. $this->assertEqual($this->View->modelId, null);
  563. $this->assertEqual($this->View->association, null);
  564. $this->assertEqual($this->View->fieldSuffix, 'month');
  565. }
  566. /**
  567. * testMulitDimensionValue method
  568. *
  569. * @access public
  570. * @return void
  571. */
  572. function testMulitDimensionValue() {
  573. $this->Helper->data = array();
  574. for ($i = 0; $i < 2; $i++) {
  575. $this->Helper->request->data['Model'][$i] = 'what';
  576. $result[] = $this->Helper->value("Model.{$i}");
  577. $this->Helper->request->data['Model'][$i] = array();
  578. for ($j = 0; $j < 2; $j++) {
  579. $this->Helper->request->data['Model'][$i][$j] = 'how';
  580. $result[] = $this->Helper->value("Model.{$i}.{$j}");
  581. }
  582. }
  583. $expected = array('what', 'how', 'how', 'what', 'how', 'how');
  584. $this->assertEqual($result, $expected);
  585. $this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
  586. $result = $this->Helper->value('HelperTestComment.5.id');
  587. $this->assertEqual($result, 'ok');
  588. $this->Helper->setEntity('HelperTestPost', true);
  589. $this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
  590. $result = $this->Helper->value('5.created.month');
  591. $this->assertEqual($result, 10);
  592. $this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
  593. $result = $this->Helper->value('0.id');
  594. $this->assertEqual($result, 100);
  595. }
  596. /**
  597. * testClean method
  598. *
  599. * @access public
  600. * @return void
  601. */
  602. function testClean() {
  603. $result = $this->Helper->clean(array());
  604. $this->assertEqual($result, null);
  605. $result = $this->Helper->clean(array('<script>with something</script>', '<applet>something else</applet>'));
  606. $this->assertEqual($result, array('with something', 'something else'));
  607. $result = $this->Helper->clean('<script>with something</script>');
  608. $this->assertEqual($result, 'with something');
  609. $result = $this->Helper->clean('<script type="text/javascript">alert("ruined");</script>');
  610. $this->assertNoPattern('#</*script#', $result);
  611. $result = $this->Helper->clean("<script \ntype=\"text/javascript\">\n\talert('ruined');\n\n\t\t</script>");
  612. $this->assertNoPattern('#</*script#', $result);
  613. $result = $this->Helper->clean('<body/onload=do(/something/)>');
  614. $this->assertEqual($result, '<body/>');
  615. $result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
  616. $this->assertEqual($result, '&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;');
  617. }
  618. /**
  619. * testMultiDimensionalField method
  620. *
  621. * @access public
  622. * @return void
  623. */
  624. function testMultiDimensionalField() {
  625. // PHP4 reference hack
  626. ClassRegistry::removeObject('view');
  627. ClassRegistry::addObject('view', $this->View);
  628. $this->Helper->setEntity('HelperTestPost', true);
  629. $this->Helper->setEntity('HelperTestPost.2.HelperTestComment.1.title');
  630. $this->assertEqual($this->View->model, 'HelperTestPost');
  631. $this->assertEqual($this->View->association, 'HelperTestComment');
  632. $this->assertEqual($this->View->modelId,2);
  633. $this->assertEqual($this->View->field, 'title');
  634. $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created');
  635. $this->assertEqual($this->View->field,'created');
  636. $this->assertEqual($this->View->association,'HelperTestTag');
  637. $this->assertEqual($this->View->modelId,1);
  638. $this->Helper->setEntity('HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake');
  639. $this->assertEqual($this->View->model,'HelperTestPost');
  640. $this->assertEqual($this->View->association,'HelperTestTag');
  641. $this->assertEqual($this->View->field,null);
  642. $this->Helper->setEntity('1.HelperTestComment.1.HelperTestTag.created.year');
  643. $this->assertEqual($this->View->model,'HelperTestPost');
  644. $this->assertEqual($this->View->association,'HelperTestTag');
  645. $this->assertEqual($this->View->field,'created');
  646. $this->assertEqual($this->View->modelId,1);
  647. $this->assertEqual($this->View->fieldSuffix,'year');
  648. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
  649. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
  650. $this->assertEqual($result,'My Title');
  651. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
  652. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  653. $this->assertEqual($result,2008);
  654. $this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
  655. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  656. $this->assertEqual($result,2008);
  657. $this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
  658. $result = $this->Helper->value('title');
  659. $this->assertEqual($result,'My Title');
  660. $this->Helper->request->data['My']['title'] = 'My Title';
  661. $result = $this->Helper->value('My.title');
  662. $this->assertEqual($result,'My Title');
  663. }
  664. function testWebrootPaths() {
  665. $this->Helper->request->webroot = '/';
  666. $result = $this->Helper->webroot('/img/cake.power.gif');
  667. $expected = '/img/cake.power.gif';
  668. $this->assertEqual($result, $expected);
  669. $this->Helper->theme = 'test_theme';
  670. App::build(array(
  671. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  672. ));
  673. $result = $this->Helper->webroot('/img/cake.power.gif');
  674. $expected = '/theme/test_theme/img/cake.power.gif';
  675. $this->assertEqual($result, $expected);
  676. $result = $this->Helper->webroot('/img/test.jpg');
  677. $expected = '/theme/test_theme/img/test.jpg';
  678. $this->assertEqual($result, $expected);
  679. $webRoot = Configure::read('App.www_root');
  680. Configure::write('App.www_root', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'webroot' . DS);
  681. $result = $this->Helper->webroot('/img/cake.power.gif');
  682. $expected = '/theme/test_theme/img/cake.power.gif';
  683. $this->assertEqual($result, $expected);
  684. $result = $this->Helper->webroot('/img/test.jpg');
  685. $expected = '/theme/test_theme/img/test.jpg';
  686. $this->assertEqual($result, $expected);
  687. $result = $this->Helper->webroot('/img/cake.icon.gif');
  688. $expected = '/img/cake.icon.gif';
  689. $this->assertEqual($result, $expected);
  690. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  691. $expected = '/img/cake.icon.gif?some=param';
  692. $this->assertEqual($result, $expected);
  693. Configure::write('App.www_root', $webRoot);
  694. }
  695. /**
  696. * test parsing attributes.
  697. *
  698. * @return void
  699. */
  700. function testParseAttributeCompact() {
  701. $helper = new TestHelper($this->View);
  702. $compact = array('compact', 'checked', 'declare', 'readonly', 'disabled',
  703. 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
  704. foreach ($compact as $attribute) {
  705. foreach (array('true', true, 1, '1', $attribute) as $value) {
  706. $attrs = array($attribute => $value);
  707. $expected = ' ' . $attribute . '="' . $attribute . '"';
  708. $this->assertEqual($helper->parseAttributes($attrs), $expected, '%s Failed on ' . $value);
  709. }
  710. }
  711. $this->assertEqual($helper->parseAttributes(array('compact')), ' compact="compact"');
  712. $helper = new Html5TestHelper($this->View);
  713. $expected = ' require';
  714. $this->assertEqual($helper->parseAttributes(array('require')), $expected);
  715. $this->assertEqual($helper->parseAttributes(array('require' => true)), $expected);
  716. $this->assertEqual($helper->parseAttributes(array('require' => false)), '');
  717. }
  718. /**
  719. * test lazy loading helpers is seamless
  720. *
  721. * @return void
  722. */
  723. function testLazyLoadingHelpers() {
  724. App::build(array(
  725. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  726. ));
  727. $Helper = new TestHelper($this->View);
  728. $this->assertInstanceOf('OtherHelperHelper', $Helper->OtherHelper);
  729. $this->assertInstanceOf('HtmlHelper', $Helper->Html);
  730. App::build();
  731. }
  732. /**
  733. * test that a helpers Helper is not 'attached' to the collection
  734. *
  735. * @return void
  736. */
  737. function testThatHelperHelpersAreNotAttached() {
  738. $Helper = new TestHelper($this->View);
  739. $Helper->OtherHelper;
  740. $result = $this->View->Helpers->enabled();
  741. $expected = array();
  742. $this->assertEquals($expected, $result, 'Helper helpers were attached to the collection.');
  743. }
  744. /**
  745. * test that the lazy loader doesn't duplicate objects on each access.
  746. *
  747. * @return void
  748. */
  749. function testLazyLoadingUsesReferences() {
  750. $Helper = new TestHelper($this->View);
  751. $result1 = $Helper->Html;
  752. $result2 = $Helper->Html;
  753. $result1->testprop = 1;
  754. $this->assertEquals($result1->testprop, $result2->testprop);
  755. }
  756. }