HelperTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html 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. * Settings for this helper.
  144. *
  145. * @var array
  146. */
  147. public $settings = array(
  148. 'key1' => 'val1',
  149. 'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'val2.2')
  150. );
  151. /**
  152. * Helpers for this helper.
  153. *
  154. * @var array
  155. */
  156. public $helpers = array('Html', 'TestPlugin.OtherHelper');
  157. /**
  158. * expose a method as public
  159. *
  160. * @param string $options
  161. * @param string $exclude
  162. * @param string $insertBefore
  163. * @param string $insertAfter
  164. * @return void
  165. */
  166. public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  167. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  168. }
  169. }
  170. /**
  171. * HelperTest class
  172. *
  173. * @package Cake.Test.Case.View
  174. */
  175. class HelperTest extends CakeTestCase {
  176. /**
  177. * setUp method
  178. *
  179. * @return void
  180. */
  181. public function setUp() {
  182. parent::setUp();
  183. ClassRegistry::flush();
  184. Router::reload();
  185. $null = null;
  186. $this->View = new View($null);
  187. $this->Helper = new Helper($this->View);
  188. $this->Helper->request = new CakeRequest(null, false);
  189. ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
  190. ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
  191. ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
  192. App::build(array(
  193. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  194. ));
  195. }
  196. /**
  197. * tearDown method
  198. *
  199. * @return void
  200. */
  201. public function tearDown() {
  202. parent::tearDown();
  203. Configure::delete('Asset');
  204. CakePlugin::unload();
  205. unset($this->Helper, $this->View);
  206. }
  207. /**
  208. * Provider for setEntity test.
  209. *
  210. * @return array
  211. */
  212. public static function entityProvider() {
  213. return array(
  214. array(
  215. 'HelperTestPost.id',
  216. array('HelperTestPost', 'id'),
  217. 'HelperTestPost',
  218. 'id'
  219. ),
  220. array(
  221. 'HelperTestComment.body',
  222. array('HelperTestComment', 'body'),
  223. 'HelperTestComment',
  224. 'body'
  225. ),
  226. array(
  227. 'HelperTest.1.Comment.body',
  228. array('HelperTest', '1', 'Comment', 'body'),
  229. 'Comment',
  230. 'body'
  231. ),
  232. array(
  233. 'HelperTestComment.BigField',
  234. array('HelperTestComment', 'BigField'),
  235. 'HelperTestComment',
  236. 'BigField'
  237. ),
  238. array(
  239. 'HelperTestComment.min',
  240. array('HelperTestComment', 'min'),
  241. 'HelperTestComment',
  242. 'min'
  243. )
  244. );
  245. }
  246. /**
  247. * Test settings merging
  248. *
  249. * @return void
  250. */
  251. public function testSettingsMerging() {
  252. $Helper = new TestHelper($this->View, array(
  253. 'key3' => 'val3',
  254. 'key2' => array('key2.2' => 'newval')
  255. ));
  256. $expected = array(
  257. 'key1' => 'val1',
  258. 'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'newval'),
  259. 'key3' => 'val3'
  260. );
  261. $this->assertEquals($expected, $Helper->settings);
  262. }
  263. /**
  264. * Test setting an entity and retrieving the entity, model and field.
  265. *
  266. * @dataProvider entityProvider
  267. * @return void
  268. */
  269. public function testSetEntity($entity, $expected, $modelKey, $fieldKey) {
  270. $this->Helper->setEntity($entity);
  271. $this->assertEquals($expected, $this->Helper->entity());
  272. $this->assertEquals($modelKey, $this->Helper->model());
  273. $this->assertEquals($fieldKey, $this->Helper->field());
  274. }
  275. /**
  276. * test setEntity with setting a scope.
  277. *
  278. * @return
  279. */
  280. public function testSetEntityScoped() {
  281. $this->Helper->setEntity('HelperTestPost', true);
  282. $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
  283. $this->Helper->setEntity('id');
  284. $expected = array('HelperTestPost', 'id');
  285. $this->assertEquals($expected, $this->Helper->entity());
  286. $this->Helper->setEntity('HelperTestComment.body');
  287. $expected = array('HelperTestComment', 'body');
  288. $this->assertEquals($expected, $this->Helper->entity());
  289. $this->Helper->setEntity('body');
  290. $expected = array('HelperTestPost', 'body');
  291. $this->assertEquals($expected, $this->Helper->entity());
  292. $this->Helper->setEntity('2.body');
  293. $expected = array('HelperTestPost', '2', 'body');
  294. $this->assertEquals($expected, $this->Helper->entity());
  295. $this->Helper->setEntity('Something.else');
  296. $expected = array('Something', 'else');
  297. $this->assertEquals($expected, $this->Helper->entity());
  298. $this->Helper->setEntity('HelperTestComment.5.id');
  299. $expected = array('HelperTestComment', 5, 'id');
  300. $this->assertEquals($expected, $this->Helper->entity());
  301. $this->Helper->setEntity('HelperTestComment.id.time');
  302. $expected = array('HelperTestComment', 'id', 'time');
  303. $this->assertEquals($expected, $this->Helper->entity());
  304. $this->Helper->setEntity('HelperTestComment.created.year');
  305. $expected = array('HelperTestComment', 'created', 'year');
  306. $this->assertEquals($expected, $this->Helper->entity());
  307. $this->Helper->setEntity(null);
  308. $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
  309. $expected = array('ModelThatDoesntExist', 'field_that_doesnt_exist');
  310. $this->assertEquals($expected, $this->Helper->entity());
  311. }
  312. /**
  313. * Test that setEntity() and model()/field() work with associated models.
  314. *
  315. * @return void
  316. */
  317. public function testSetEntityAssociated() {
  318. $this->Helper->setEntity('HelperTestPost', true);
  319. $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.title');
  320. $expected = array('HelperTestPost', '1', 'HelperTestComment', '1', 'title');
  321. $this->assertEquals($expected, $this->Helper->entity());
  322. $this->assertEquals('HelperTestComment', $this->Helper->model());
  323. }
  324. /**
  325. * Test creating saveMany() compatible entities
  326. *
  327. * @return void
  328. */
  329. public function testSetEntitySaveMany() {
  330. $this->Helper->setEntity('HelperTestPost', true);
  331. $this->Helper->setEntity('0.HelperTestPost.id');
  332. $expected = array('0', 'HelperTestPost', 'id');
  333. $this->assertEquals($expected, $this->Helper->entity());
  334. }
  335. /**
  336. * Test that setEntity doesn't make CamelCase fields that are not associations an
  337. * associated model.
  338. *
  339. * @return void
  340. */
  341. public function testSetEntityAssociatedCamelCaseField() {
  342. $this->Helper->fieldset = array(
  343. 'HelperTestComment' => array(
  344. 'fields' => array('BigField' => array('type' => 'integer'))
  345. )
  346. );
  347. $this->Helper->setEntity('HelperTestComment', true);
  348. $this->Helper->setEntity('HelperTestComment.BigField');
  349. $this->assertEquals('HelperTestComment', $this->Helper->model());
  350. $this->assertEquals('BigField', $this->Helper->field());
  351. }
  352. /**
  353. * Test that multiple fields work when they are camelcase and in fieldset
  354. *
  355. * @return void
  356. */
  357. public function testSetEntityAssociatedCamelCaseFieldHabtmMultiple() {
  358. $this->Helper->fieldset = array(
  359. 'HelperTestComment' => array(
  360. 'fields' => array('Tag' => array('type' => 'multiple'))
  361. )
  362. );
  363. $this->Helper->setEntity('HelperTestComment', true);
  364. $this->Helper->setEntity('Tag');
  365. $this->assertEquals('Tag', $this->Helper->model());
  366. $this->assertEquals('Tag', $this->Helper->field());
  367. $this->assertEquals(array('Tag', 'Tag'), $this->Helper->entity());
  368. }
  369. /**
  370. * Test that habtm associations can have property fields created.
  371. *
  372. * @return void
  373. */
  374. public function testSetEntityHabtmPropertyFieldNames() {
  375. $this->Helper->fieldset = array(
  376. 'HelperTestComment' => array(
  377. 'fields' => array('Tag' => array('type' => 'multiple'))
  378. )
  379. );
  380. $this->Helper->setEntity('HelperTestComment', true);
  381. $this->Helper->setEntity('Tag.name');
  382. $this->assertEquals('Tag', $this->Helper->model());
  383. $this->assertEquals('name', $this->Helper->field());
  384. $this->assertEquals(array('Tag', 'name'), $this->Helper->entity());
  385. }
  386. /**
  387. * test that 'view' doesn't break things.
  388. *
  389. * @return void
  390. */
  391. public function testSetEntityWithView() {
  392. $this->assertNull($this->Helper->setEntity('Allow.view.group_id'));
  393. $this->assertNull($this->Helper->setEntity('Allow.view'));
  394. $this->assertNull($this->Helper->setEntity('View.view'));
  395. }
  396. /**
  397. * test getting values from Helper
  398. *
  399. * @return void
  400. */
  401. public function testValue() {
  402. $this->Helper->request->data = array('fullname' => 'This is me');
  403. $this->Helper->setEntity('fullname');
  404. $result = $this->Helper->value('fullname');
  405. $this->assertEquals('This is me', $result);
  406. $this->Helper->request->data = array(
  407. 'Post' => array('name' => 'First Post')
  408. );
  409. $this->Helper->setEntity('Post.name');
  410. $result = $this->Helper->value('Post.name');
  411. $this->assertEquals('First Post', $result);
  412. $this->Helper->request->data = array(
  413. 'Post' => array(2 => array('name' => 'First Post'))
  414. );
  415. $this->Helper->setEntity('Post.2.name');
  416. $result = $this->Helper->value('Post.2.name');
  417. $this->assertEquals('First Post', $result);
  418. $this->Helper->request->data = array(
  419. 'Post' => array(
  420. 2 => array('created' => array('year' => '2008'))
  421. )
  422. );
  423. $this->Helper->setEntity('Post.2.created');
  424. $result = $this->Helper->value('Post.2.created');
  425. $this->assertEquals(array('year' => '2008'), $result);
  426. $this->Helper->request->data = array(
  427. 'Post' => array(
  428. 2 => array('created' => array('year' => '2008'))
  429. )
  430. );
  431. $this->Helper->setEntity('Post.2.created.year');
  432. $result = $this->Helper->value('Post.2.created.year');
  433. $this->assertEquals('2008', $result);
  434. }
  435. /**
  436. * Test default values with value()
  437. *
  438. * @return void
  439. */
  440. public function testValueWithDefault() {
  441. $this->Helper->request->data = array('zero' => 0);
  442. $this->Helper->setEntity('zero');
  443. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  444. $this->assertEquals(array('value' => 0), $result);
  445. $this->Helper->request->data = array('zero' => '0');
  446. $result = $this->Helper->value(array('default' => 'something'), 'zero');
  447. $this->assertEquals(array('value' => '0'), $result);
  448. $this->Helper->setEntity('inexistent');
  449. $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
  450. $this->assertEquals(array('value' => 'something'), $result);
  451. }
  452. /**
  453. * Test habtm data fetching and ensure no pollution happens.
  454. *
  455. * @return void
  456. */
  457. public function testValueHabtmKeys() {
  458. $this->Helper->request->data = array(
  459. 'HelperTestTag' => array('HelperTestTag' => '')
  460. );
  461. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  462. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  463. $this->assertEquals('', $result);
  464. $this->Helper->request->data = array(
  465. 'HelperTestTag' => array(
  466. 'HelperTestTag' => array(2, 3, 4)
  467. )
  468. );
  469. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  470. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  471. $this->assertEquals(array(2, 3, 4), $result);
  472. $this->Helper->request->data = array(
  473. 'HelperTestTag' => array(
  474. array('id' => 3),
  475. array('id' => 5)
  476. )
  477. );
  478. $this->Helper->setEntity('HelperTestTag.HelperTestTag');
  479. $result = $this->Helper->value('HelperTestTag.HelperTestTag');
  480. $this->assertEquals(array(3 => 3, 5 => 5), $result);
  481. $this->Helper->request->data = array(
  482. 'HelperTestTag' => array(
  483. 'body' => '',
  484. 'title' => 'winning'
  485. ),
  486. );
  487. $this->Helper->setEntity('HelperTestTag.body');
  488. $result = $this->Helper->value('HelperTestTag.body');
  489. $this->assertEquals('', $result);
  490. }
  491. /**
  492. * Ensure HTML escaping of url params. So link addresses are valid and not exploited
  493. *
  494. * @return void
  495. */
  496. public function testUrlConversion() {
  497. $result = $this->Helper->url('/controller/action/1');
  498. $this->assertEquals('/controller/action/1', $result);
  499. $result = $this->Helper->url('/controller/action/1?one=1&two=2');
  500. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  501. $result = $this->Helper->url(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
  502. $this->assertEquals("/posts/index/page:1%22%20onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  503. $result = $this->Helper->url('/controller/action/1/param:this+one+more');
  504. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  505. $result = $this->Helper->url('/controller/action/1/param:this%20one%20more');
  506. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  507. $result = $this->Helper->url('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  508. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  509. $result = $this->Helper->url(array(
  510. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  511. ));
  512. $this->assertEquals("/posts/index/param:%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  513. $result = $this->Helper->url(array(
  514. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  515. '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
  516. ));
  517. $this->assertEquals("/posts/index/page:1?one=value&amp;two=value&amp;three=purple", $result);
  518. }
  519. /**
  520. * test assetTimestamp application
  521. *
  522. * @return void
  523. */
  524. public function testAssetTimestamp() {
  525. Configure::write('Foo.bar', 'test');
  526. Configure::write('Asset.timestamp', false);
  527. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  528. $this->assertEquals(CSS_URL . 'cake.generic.css', $result);
  529. Configure::write('Asset.timestamp', true);
  530. Configure::write('debug', 0);
  531. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  532. $this->assertEquals(CSS_URL . 'cake.generic.css', $result);
  533. Configure::write('Asset.timestamp', true);
  534. Configure::write('debug', 2);
  535. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  536. $this->assertRegExp('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  537. Configure::write('Asset.timestamp', 'force');
  538. Configure::write('debug', 0);
  539. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
  540. $this->assertRegExp('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  541. $result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
  542. $this->assertEquals(CSS_URL . 'cake.generic.css?someparam', $result);
  543. $this->Helper->request->webroot = '/some/dir/';
  544. $result = $this->Helper->assetTimestamp('/some/dir/' . CSS_URL . 'cake.generic.css');
  545. $this->assertRegExp('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  546. }
  547. /**
  548. * test assetUrl application
  549. *
  550. * @return void
  551. */
  552. public function testAssetUrl() {
  553. $this->Helper->webroot = '';
  554. $result = $this->Helper->assetUrl(array(
  555. 'controller' => 'js',
  556. 'action' => 'post',
  557. 'ext' => 'js'
  558. ),
  559. array('fullBase' => true)
  560. );
  561. $this->assertEquals(FULL_BASE_URL . '/js/post.js', $result);
  562. $result = $this->Helper->assetUrl('foo.jpg', array('pathPrefix' => 'img/'));
  563. $this->assertEquals('img/foo.jpg', $result);
  564. $result = $this->Helper->assetUrl('foo.jpg', array('fullBase' => true));
  565. $this->assertEquals(FULL_BASE_URL . '/foo.jpg', $result);
  566. $result = $this->Helper->assetUrl('style', array('ext' => '.css'));
  567. $this->assertEquals('style.css', $result);
  568. $result = $this->Helper->assetUrl('dir/sub dir/my image', array('ext' => '.jpg'));
  569. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  570. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  571. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  572. }
  573. /**
  574. * Test assetUrl with plugins.
  575. *
  576. * @return void
  577. */
  578. public function testAssetUrlPlugin() {
  579. $this->Helper->webroot = '';
  580. CakePlugin::load('TestPlugin');
  581. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css'));
  582. $this->assertEquals('test_plugin/style.css', $result);
  583. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css', 'plugin' => false));
  584. $this->assertEquals('TestPlugin.style.css', $result);
  585. CakePlugin::unload('TestPlugin');
  586. }
  587. /**
  588. * test assetUrl and Asset.timestamp = force
  589. *
  590. * @return void
  591. */
  592. public function testAssetUrlTimestampForce() {
  593. $this->Helper->webroot = '';
  594. Configure::write('Asset.timestamp', 'force');
  595. $result = $this->Helper->assetUrl('cake.generic.css', array('pathPrefix' => CSS_URL));
  596. $this->assertRegExp('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  597. }
  598. /**
  599. * test assetTimestamp with plugins and themes
  600. *
  601. * @return void
  602. */
  603. public function testAssetTimestampPluginsAndThemes() {
  604. Configure::write('Asset.timestamp', 'force');
  605. App::build(array(
  606. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  607. ));
  608. CakePlugin::load(array('TestPlugin'));
  609. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  610. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  611. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  612. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  613. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/theme.js');
  614. $this->assertRegExp('#/theme/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  615. $result = $this->Helper->assetTimestamp('/theme/test_theme/js/non_existant.js');
  616. $this->assertRegExp('#/theme/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  617. }
  618. /**
  619. * testFieldsWithSameName method
  620. *
  621. * @return void
  622. */
  623. public function testFieldsWithSameName() {
  624. $this->Helper->setEntity('HelperTestTag', true);
  625. $this->Helper->setEntity('HelperTestTag.id');
  626. $expected = array('HelperTestTag', 'id');
  627. $this->assertEquals($expected, $this->Helper->entity());
  628. $this->Helper->setEntity('My.id');
  629. $expected = array('My', 'id');
  630. $this->assertEquals($expected, $this->Helper->entity());
  631. $this->Helper->setEntity('MyOther.id');
  632. $expected = array('MyOther', 'id');
  633. $this->assertEquals($expected, $this->Helper->entity());
  634. }
  635. /**
  636. * testFieldSameAsModel method
  637. *
  638. * @return void
  639. */
  640. public function testFieldSameAsModel() {
  641. $this->Helper->setEntity('HelperTestTag', true);
  642. $this->Helper->setEntity('helper_test_post');
  643. $expected = array('HelperTestTag', 'helper_test_post');
  644. $this->assertEquals($expected, $this->Helper->entity());
  645. $this->Helper->setEntity('HelperTestTag');
  646. $expected = array('HelperTestTag', 'HelperTestTag');
  647. $this->assertEquals($expected, $this->Helper->entity());
  648. }
  649. /**
  650. * testFieldSuffixForDate method
  651. *
  652. * @return void
  653. */
  654. public function testFieldSuffixForDate() {
  655. $this->Helper->setEntity('HelperTestPost', true);
  656. $expected = array('HelperTestPost');
  657. $this->assertEquals($expected, $this->Helper->entity());
  658. foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
  659. $this->Helper->setEntity('date.' . $d);
  660. $expected = array('HelperTestPost', 'date', $d);
  661. $this->assertEquals($expected, $this->Helper->entity());
  662. }
  663. }
  664. /**
  665. * testMulitDimensionValue method
  666. *
  667. * @return void
  668. */
  669. public function testMultiDimensionValue() {
  670. $this->Helper->data = array();
  671. for ($i = 0; $i < 2; $i++) {
  672. $this->Helper->request->data['Model'][$i] = 'what';
  673. $result[] = $this->Helper->value("Model.{$i}");
  674. $this->Helper->request->data['Model'][$i] = array();
  675. for ($j = 0; $j < 2; $j++) {
  676. $this->Helper->request->data['Model'][$i][$j] = 'how';
  677. $result[] = $this->Helper->value("Model.{$i}.{$j}");
  678. }
  679. }
  680. $expected = array('what', 'how', 'how', 'what', 'how', 'how');
  681. $this->assertEquals($expected, $result);
  682. $this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
  683. $result = $this->Helper->value('HelperTestComment.5.id');
  684. $this->assertEquals('ok', $result);
  685. $this->Helper->setEntity('HelperTestPost', true);
  686. $this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
  687. $result = $this->Helper->value('5.created.month');
  688. $this->assertEquals(10, $result);
  689. $this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
  690. $result = $this->Helper->value('HelperTestPost.0.id');
  691. $this->assertEquals(100, $result);
  692. }
  693. /**
  694. * testClean method
  695. *
  696. * @return void
  697. */
  698. public function testClean() {
  699. $result = $this->Helper->clean(array());
  700. $this->assertEquals(null, $result);
  701. $result = $this->Helper->clean(array('<script>with something</script>', '<applet>something else</applet>'));
  702. $this->assertEquals(array('with something', 'something else'), $result);
  703. $result = $this->Helper->clean('<script>with something</script>');
  704. $this->assertEquals('with something', $result);
  705. $result = $this->Helper->clean('<script type="text/javascript">alert("ruined");</script>');
  706. $this->assertNotRegExp('#</*script#', $result);
  707. $result = $this->Helper->clean("<script \ntype=\"text/javascript\">\n\talert('ruined');\n\n\t\t</script>");
  708. $this->assertNotRegExp('#</*script#', $result);
  709. $result = $this->Helper->clean('<body/onload=do(/something/)>');
  710. $this->assertEquals('<body/>', $result);
  711. $result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
  712. $this->assertEquals('&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;', $result);
  713. }
  714. /**
  715. * testMultiDimensionalField method
  716. *
  717. * @return void
  718. */
  719. public function testMultiDimensionalField() {
  720. $this->Helper->setEntity('HelperTestPost', true);
  721. $entity = 'HelperTestPost.2.HelperTestComment.1.title';
  722. $this->Helper->setEntity($entity);
  723. $expected = array(
  724. 'HelperTestPost', '2', 'HelperTestComment', '1', 'title'
  725. );
  726. $this->assertEquals($expected, $this->Helper->entity());
  727. $entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created';
  728. $this->Helper->setEntity($entity);
  729. $expected = array(
  730. 'HelperTestPost', '1', 'HelperTestComment', '1',
  731. 'HelperTestTag', '1', 'created'
  732. );
  733. $this->assertEquals($expected, $this->Helper->entity());
  734. $entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
  735. $expected = array(
  736. 'HelperTestPost', '0', 'HelperTestComment', '1',
  737. 'HelperTestTag', '1', 'fake'
  738. );
  739. $this->Helper->setEntity($entity);
  740. $entity = '1.HelperTestComment.1.HelperTestTag.created.year';
  741. $this->Helper->setEntity($entity);
  742. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
  743. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
  744. $this->assertEquals('My Title', $result);
  745. $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
  746. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  747. $this->assertEquals(2008, $result);
  748. $this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
  749. $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
  750. $this->assertEquals(2008, $result);
  751. $this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
  752. $result = $this->Helper->value('title');
  753. $this->assertEquals('My Title', $result);
  754. $this->Helper->request->data['My']['title'] = 'My Title';
  755. $result = $this->Helper->value('My.title');
  756. $this->assertEquals('My Title', $result);
  757. }
  758. public function testWebrootPaths() {
  759. $this->Helper->request->webroot = '/';
  760. $result = $this->Helper->webroot('/img/cake.power.gif');
  761. $expected = '/img/cake.power.gif';
  762. $this->assertEquals($expected, $result);
  763. $this->Helper->theme = 'test_theme';
  764. App::build(array(
  765. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  766. ));
  767. $result = $this->Helper->webroot('/img/cake.power.gif');
  768. $expected = '/theme/test_theme/img/cake.power.gif';
  769. $this->assertEquals($expected, $result);
  770. $result = $this->Helper->webroot('/img/test.jpg');
  771. $expected = '/theme/test_theme/img/test.jpg';
  772. $this->assertEquals($expected, $result);
  773. $webRoot = Configure::read('App.www_root');
  774. Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
  775. $result = $this->Helper->webroot('/img/cake.power.gif');
  776. $expected = '/theme/test_theme/img/cake.power.gif';
  777. $this->assertEquals($expected, $result);
  778. $result = $this->Helper->webroot('/img/test.jpg');
  779. $expected = '/theme/test_theme/img/test.jpg';
  780. $this->assertEquals($expected, $result);
  781. $result = $this->Helper->webroot('/img/cake.icon.gif');
  782. $expected = '/img/cake.icon.gif';
  783. $this->assertEquals($expected, $result);
  784. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  785. $expected = '/img/cake.icon.gif?some=param';
  786. $this->assertEquals($expected, $result);
  787. Configure::write('App.www_root', $webRoot);
  788. }
  789. /**
  790. * test lazy loading helpers is seamless
  791. *
  792. * @return void
  793. */
  794. public function testLazyLoadingHelpers() {
  795. App::build(array(
  796. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  797. ));
  798. CakePlugin::load(array('TestPlugin'));
  799. $Helper = new TestHelper($this->View);
  800. $this->assertInstanceOf('OtherHelperHelper', $Helper->OtherHelper);
  801. $this->assertInstanceOf('HtmlHelper', $Helper->Html);
  802. App::build();
  803. }
  804. /**
  805. * test that a helpers Helper is not 'attached' to the collection
  806. *
  807. * @return void
  808. */
  809. public function testThatHelperHelpersAreNotAttached() {
  810. $Helper = new TestHelper($this->View);
  811. $Helper->OtherHelper;
  812. $result = $this->View->Helpers->enabled();
  813. $expected = array();
  814. $this->assertEquals($expected, $result, 'Helper helpers were attached to the collection.');
  815. }
  816. /**
  817. * test that the lazy loader doesn't duplicate objects on each access.
  818. *
  819. * @return void
  820. */
  821. public function testLazyLoadingUsesReferences() {
  822. $Helper = new TestHelper($this->View);
  823. $resultA = $Helper->Html;
  824. $resultB = $Helper->Html;
  825. $resultA->testprop = 1;
  826. $this->assertEquals($resultA->testprop, $resultB->testprop);
  827. }
  828. }