HelperTest.php 28 KB

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