HelperTest.php 30 KB

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