HelperTest.php 29 KB

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