HelperTest.php 29 KB

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