HelperTest.php 28 KB

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