HelperTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 habtm associations can have property fields created.
  313. *
  314. * @return void
  315. */
  316. public function testSetEntityHabtmPropertyFieldNames() {
  317. $this->Helper->fieldset = array(
  318. 'HelperTestComment' => array(
  319. 'fields' => array('Tag' => array('type' => 'multiple'))
  320. )
  321. );
  322. $this->Helper->setEntity('HelperTestComment', true);
  323. $this->Helper->setEntity('Tag.name');
  324. $this->assertEquals('Tag', $this->Helper->model());
  325. $this->assertEquals('name', $this->Helper->field());
  326. $this->assertEquals(array('Tag', 'name'), $this->Helper->entity());
  327. }
  328. /**
  329. * test that 'view' doesn't break things.
  330. *
  331. * @return void
  332. */
  333. public function testSetEntityWithView() {
  334. $this->assertNull($this->Helper->setEntity('Allow.view.group_id'));
  335. $this->assertNull($this->Helper->setEntity('Allow.view'));
  336. $this->assertNull($this->Helper->setEntity('View.view'));
  337. }
  338. /**
  339. * test getting values from Helper
  340. *
  341. * @return void
  342. */
  343. public function testValue() {
  344. $this->Helper->request->data = array('fullname' => 'This is me');
  345. $this->Helper->setEntity('fullname');
  346. $result = $this->Helper->value('fullname');
  347. $this->assertEqual($result, 'This is me');
  348. $this->Helper->request->data = array(
  349. 'Post' => array('name' => 'First Post')
  350. );
  351. $this->Helper->setEntity('Post.name');
  352. $result = $this->Helper->value('Post.name');
  353. $this->assertEqual($result, 'First Post');
  354. $this->Helper->request->data = array(
  355. 'Post' => array(2 => array('name' => 'First Post'))
  356. );
  357. $this->Helper->setEntity('Post.2.name');
  358. $result = $this->Helper->value('Post.2.name');
  359. $this->assertEqual($result, 'First Post');
  360. $this->Helper->request->data = array(
  361. 'Post' => array(
  362. 2 => array('created' => array('year' => '2008'))
  363. )
  364. );
  365. $this->Helper->setEntity('Post.2.created');
  366. $result = $this->Helper->value('Post.2.created');
  367. $this->assertEqual($result, array('year' => '2008'));
  368. $this->Helper->request->data = array(
  369. 'Post' => array(
  370. 2 => array('created' => array('year' => '2008'))
  371. )
  372. );
  373. $this->Helper->setEntity('Post.2.created.year');
  374. $result = $this->Helper->value('Post.2.created.year');
  375. $this->assertEqual($result, '2008');
  376. }
  377. /**
  378. * Test default values with value()
  379. *
  380. * @return void
  381. */
  382. function testValueWithDefault() {
  383. $this->Helper->request->data = array('zero' => 0);
  384. $this->Helper->setEntity('zero');
  385. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  386. $this->assertEqual($result, array('value' => 0));
  387. $this->Helper->request->data = array('zero' => '0');
  388. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  389. $this->assertEqual($result, array('value' => '0'));
  390. $this->Helper->setEntity('inexistent');
  391. $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
  392. $this->assertEqual($result, array('value' => 'something'));
  393. }
  394. /**
  395. * Test habtm data fetching and ensure no pollution happens.
  396. *
  397. * @return void
  398. */
  399. function testValueHabtmKeys() {
  400. $this->Helper->request->data = array(
  401. 'HelperTestTag' => array('HelperTestTag' => '')
  402. );
  403. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  404. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  405. $this->assertEqual($result, '');
  406. $this->Helper->request->data = array(
  407. 'HelperTestTag' => array(
  408. 'HelperTestTag' => array(2, 3, 4)
  409. )
  410. );
  411. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  412. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  413. $this->assertEqual($result, array(2, 3, 4));
  414. $this->Helper->request->data = array(
  415. 'HelperTestTag' => array(
  416. array('id' => 3),
  417. array('id' => 5)
  418. )
  419. );
  420. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  421. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  422. $this->assertEqual($result, array(3 => 3, 5 => 5));
  423. $this->Helper->request->data = array(
  424. 'HelperTestTag' => array(
  425. 'body' => '',
  426. 'title' => 'winning'
  427. ),
  428. );
  429. $this->Helper->setEntity('HelperTestTag.body');
  430. $result = $this->Helper->value('HelperTestTag.body');
  431. $this->assertEqual($result, '');
  432. }
  433. /**
  434. * Ensure HTML escaping of url params. So link addresses are valid and not exploited
  435. *
  436. * @return void
  437. */
  438. public function testUrlConversion() {
  439. $result = $this->Helper->url('/controller/action/1');
  440. $this->assertEqual($result, '/controller/action/1');
  441. $result = $this->Helper->url('/controller/action/1?one=1&two=2');
  442. $this->assertEqual($result, '/controller/action/1?one=1&amp;two=2');
  443. $result = $this->Helper->url(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
  444. $this->assertEqual($result, "/posts/index/page:1&quot; onclick=&quot;alert(&#039;XSS&#039;);&quot;");
  445. $result = $this->Helper->url('/controller/action/1/param:this+one+more');
  446. $this->assertEqual($result, '/controller/action/1/param:this+one+more');
  447. $result = $this->Helper->url('/controller/action/1/param:this%20one%20more');
  448. $this->assertEqual($result, '/controller/action/1/param:this%20one%20more');
  449. $result = $this->Helper->url('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  450. $this->assertEqual($result, '/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  451. $result = $this->Helper->url(array(
  452. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  453. ));
  454. $this->assertEqual($result, "/posts/index/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24");
  455. $result = $this->Helper->url(array(
  456. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  457. '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
  458. ));
  459. $this->assertEqual($result, "/posts/index/page:1?one=value&amp;two=value&amp;three=purple");
  460. }
  461. /**
  462. * test assetTimestamp application
  463. *
  464. * @return void
  465. */
  466. public function testAssetTimestamp() {
  467. $_timestamp = Configure::read('Asset.timestamp');
  468. $_debug = Configure::read('debug');
  469. Configure::write('Asset.timestamp', false);
  470. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  471. $this->assertEqual($result, CSS_URL . 'cake.generic.css');
  472. Configure::write('Asset.timestamp', true);
  473. Configure::write('debug', 0);
  474. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  475. $this->assertEqual($result, CSS_URL . 'cake.generic.css');
  476. Configure::write('Asset.timestamp', true);
  477. Configure::write('debug', 2);
  478. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  479. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  480. Configure::write('Asset.timestamp', 'force');
  481. Configure::write('debug', 0);
  482. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  483. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  484. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
  485. $this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
  486. $this->Helper->request->webroot = '/some/dir/';
  487. $result = $this->Helper->assetTimestamp('/some/dir/' . CSS_URL . 'cake.generic.css');
  488. $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  489. Configure::write('debug', $_debug);
  490. Configure::write('Asset.timestamp', $_timestamp);
  491. }
  492. /**
  493. * test assetTimestamp with plugins and themes
  494. *
  495. * @return void
  496. */
  497. public function testAssetTimestampPluginsAndThemes() {
  498. $_timestamp = Configure::read('Asset.timestamp');
  499. Configure::write('Asset.timestamp', 'force');
  500. App::build(array(
  501. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  502. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  503. ));
  504. CakePlugin::loadAll();
  505. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  506. $this->assertPattern('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  507. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  508. $this->assertPattern('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  509. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/theme.js');
  510. $this->assertPattern('#/theme/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  511. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/non_existant.js');
  512. $this->assertPattern('#/theme/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  513. App::build();
  514. Configure::write('Asset.timestamp', $_timestamp);
  515. }
  516. /**
  517. * testFieldsWithSameName method
  518. *
  519. * @return void
  520. */
  521. public function testFieldsWithSameName() {
  522. $this->Helper->setEntity('HelperTestTag', true);
  523. $this->Helper->setEntity('HelperTestTag.id');
  524. $expected = array('HelperTestTag', 'id');
  525. $this->assertEquals($expected, $this->Helper->entity());
  526. $this->Helper->setEntity('My.id');
  527. $expected = array('My', 'id');
  528. $this->assertEquals($expected, $this->Helper->entity());
  529. $this->Helper->setEntity('MyOther.id');
  530. $expected = array('MyOther', 'id');
  531. $this->assertEquals($expected, $this->Helper->entity());
  532. }
  533. /**
  534. * testFieldSameAsModel method
  535. *
  536. * @return void
  537. */
  538. public function testFieldSameAsModel() {
  539. $this->Helper->setEntity('HelperTestTag', true);
  540. $this->Helper->setEntity('helper_test_post');
  541. $expected = array('HelperTestTag', 'helper_test_post');
  542. $this->assertEquals($expected, $this->Helper->entity());
  543. $this->Helper->setEntity('HelperTestTag');
  544. $expected = array('HelperTestTag', 'HelperTestTag');
  545. $this->assertEquals($expected, $this->Helper->entity());
  546. }
  547. /**
  548. * testFieldSuffixForDate method
  549. *
  550. * @return void
  551. */
  552. public function testFieldSuffixForDate() {
  553. $this->Helper->setEntity('HelperTestPost', true);
  554. $expected = array('HelperTestPost');
  555. $this->assertEquals($expected, $this->Helper->entity());
  556. foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
  557. $this->Helper->setEntity('date.' . $d);
  558. $expected = array('HelperTestPost', 'date', $d);
  559. $this->assertEquals($expected, $this->Helper->entity());
  560. }
  561. }
  562. /**
  563. * testMulitDimensionValue method
  564. *
  565. * @return void
  566. */
  567. public function testMultiDimensionValue() {
  568. $this->Helper->data = array();
  569. for ($i = 0; $i < 2; $i++) {
  570. $this->Helper->request->data['Model'][$i] = 'what';
  571. $result[] = $this->Helper->value("Model.{$i}");
  572. $this->Helper->request->data['Model'][$i] = array();
  573. for ($j = 0; $j < 2; $j++) {
  574. $this->Helper->request->data['Model'][$i][$j] = 'how';
  575. $result[] = $this->Helper->value("Model.{$i}.{$j}");
  576. }
  577. }
  578. $expected = array('what', 'how', 'how', 'what', 'how', 'how');
  579. $this->assertEqual($expected, $result);
  580. $this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
  581. $result = $this->Helper->value('HelperTestComment.5.id');
  582. $this->assertEqual($result, 'ok');
  583. $this->Helper->setEntity('HelperTestPost', true);
  584. $this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
  585. $result = $this->Helper->value('5.created.month');
  586. $this->assertEqual($result, 10);
  587. $this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
  588. $result = $this->Helper->value('HelperTestPost.0.id');
  589. $this->assertEqual($result, 100);
  590. }
  591. /**
  592. * testClean method
  593. *
  594. * @return void
  595. */
  596. public function testClean() {
  597. $result = $this->Helper->clean(array());
  598. $this->assertEqual($result, null);
  599. $result = $this->Helper->clean(array('<script>with something</script>', '<applet>something else</applet>'));
  600. $this->assertEqual($result, array('with something', 'something else'));
  601. $result = $this->Helper->clean('<script>with something</script>');
  602. $this->assertEqual($result, 'with something');
  603. $result = $this->Helper->clean('<script type="text/javascript">alert("ruined");</script>');
  604. $this->assertNoPattern('#</*script#', $result);
  605. $result = $this->Helper->clean("<script \ntype=\"text/javascript\">\n\talert('ruined');\n\n\t\t</script>");
  606. $this->assertNoPattern('#</*script#', $result);
  607. $result = $this->Helper->clean('<body/onload=do(/something/)>');
  608. $this->assertEqual($result, '<body/>');
  609. $result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
  610. $this->assertEqual($result, '&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;');
  611. }
  612. /**
  613. * testMultiDimensionalField method
  614. *
  615. * @return void
  616. */
  617. public function testMultiDimensionalField() {
  618. $this->Helper->setEntity('HelperTestPost', true);
  619. $entity = 'HelperTestPost.2.HelperTestComment.1.title';
  620. $this->Helper->setEntity($entity);
  621. $expected = array(
  622. 'HelperTestPost', '2', 'HelperTestComment', '1', 'title'
  623. );
  624. $this->assertEquals($expected, $this->Helper->entity());
  625. $entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created';
  626. $this->Helper->setEntity($entity);
  627. $expected = array(
  628. 'HelperTestPost', '1', 'HelperTestComment', '1',
  629. 'HelperTestTag', '1', 'created'
  630. );
  631. $this->assertEquals($expected, $this->Helper->entity());
  632. $entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
  633. $expected = array(
  634. 'HelperTestPost', '0', 'HelperTestComment', '1',
  635. 'HelperTestTag', '1', 'fake'
  636. );
  637. $this->Helper->setEntity($entity);
  638. $entity = '1.HelperTestComment.1.HelperTestTag.created.year';
  639. $this->Helper->setEntity($entity);
  640. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
  641. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
  642. $this->assertEquals('My Title', $result);
  643. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
  644. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  645. $this->assertEquals(2008, $result);
  646. $this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
  647. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  648. $this->assertEquals(2008, $result);
  649. $this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
  650. $result = $this->Helper->value('title');
  651. $this->assertEquals('My Title', $result);
  652. $this->Helper->request->data['My']['title'] = 'My Title';
  653. $result = $this->Helper->value('My.title');
  654. $this->assertEqual($result,'My Title');
  655. }
  656. public function testWebrootPaths() {
  657. $this->Helper->request->webroot = '/';
  658. $result = $this->Helper->webroot('/img/cake.power.gif');
  659. $expected = '/img/cake.power.gif';
  660. $this->assertEqual($expected, $result);
  661. $this->Helper->theme = 'test_theme';
  662. App::build(array(
  663. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  664. ));
  665. $result = $this->Helper->webroot('/img/cake.power.gif');
  666. $expected = '/theme/test_theme/img/cake.power.gif';
  667. $this->assertEqual($expected, $result);
  668. $result = $this->Helper->webroot('/img/test.jpg');
  669. $expected = '/theme/test_theme/img/test.jpg';
  670. $this->assertEqual($expected, $result);
  671. $webRoot = Configure::read('App.www_root');
  672. Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
  673. $result = $this->Helper->webroot('/img/cake.power.gif');
  674. $expected = '/theme/test_theme/img/cake.power.gif';
  675. $this->assertEqual($expected, $result);
  676. $result = $this->Helper->webroot('/img/test.jpg');
  677. $expected = '/theme/test_theme/img/test.jpg';
  678. $this->assertEqual($expected, $result);
  679. $result = $this->Helper->webroot('/img/cake.icon.gif');
  680. $expected = '/img/cake.icon.gif';
  681. $this->assertEqual($expected, $result);
  682. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  683. $expected = '/img/cake.icon.gif?some=param';
  684. $this->assertEqual($expected, $result);
  685. Configure::write('App.www_root', $webRoot);
  686. }
  687. /**
  688. * test lazy loading helpers is seamless
  689. *
  690. * @return void
  691. */
  692. public function testLazyLoadingHelpers() {
  693. App::build(array(
  694. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  695. ));
  696. CakePlugin::loadAll();
  697. $Helper = new TestHelper($this->View);
  698. $this->assertInstanceOf('OtherHelperHelper', $Helper->OtherHelper);
  699. $this->assertInstanceOf('HtmlHelper', $Helper->Html);
  700. App::build();
  701. }
  702. /**
  703. * test that a helpers Helper is not 'attached' to the collection
  704. *
  705. * @return void
  706. */
  707. public function testThatHelperHelpersAreNotAttached() {
  708. $Helper = new TestHelper($this->View);
  709. $Helper->OtherHelper;
  710. $result = $this->View->Helpers->enabled();
  711. $expected = array();
  712. $this->assertEquals($expected, $result, 'Helper helpers were attached to the collection.');
  713. }
  714. /**
  715. * test that the lazy loader doesn't duplicate objects on each access.
  716. *
  717. * @return void
  718. */
  719. public function testLazyLoadingUsesReferences() {
  720. $Helper = new TestHelper($this->View);
  721. $result1 = $Helper->Html;
  722. $result2 = $Helper->Html;
  723. $result1->testprop = 1;
  724. $this->assertEquals($result1->testprop, $result2->testprop);
  725. }
  726. }