HelperTest.php 25 KB

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