HelperTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 1.2.0
  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. protected $_defaultConfig = 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. $this->View = new View();
  160. $this->Helper = new Helper($this->View);
  161. $this->Helper->request = new Request();
  162. Configure::write('App.namespace', 'TestApp');
  163. Plugin::load(['TestTheme']);
  164. }
  165. /**
  166. * tearDown method
  167. *
  168. * @return void
  169. */
  170. public function tearDown() {
  171. parent::tearDown();
  172. Configure::delete('Asset');
  173. Plugin::unload();
  174. unset($this->Helper, $this->View);
  175. }
  176. /**
  177. * Test settings merging
  178. *
  179. * @return void
  180. */
  181. public function testSettingsMerging() {
  182. $Helper = new TestHelper($this->View, array(
  183. 'key3' => 'val3',
  184. 'key2' => array('key2.2' => 'newval')
  185. ));
  186. $expected = array(
  187. 'key1' => 'val1',
  188. 'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'newval'),
  189. 'key3' => 'val3'
  190. );
  191. $this->assertEquals($expected, $Helper->config());
  192. }
  193. /**
  194. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  195. *
  196. * @return void
  197. */
  198. public function testUrlConversion() {
  199. Router::connect('/:controller/:action/*');
  200. $result = $this->Helper->url('/controller/action/1');
  201. $this->assertEquals('/controller/action/1', $result);
  202. $result = $this->Helper->url('/controller/action/1?one=1&two=2');
  203. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  204. $result = $this->Helper->url(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
  205. $this->assertEquals("/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  206. $result = $this->Helper->url('/controller/action/1/param:this+one+more');
  207. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  208. $result = $this->Helper->url('/controller/action/1/param:this%20one%20more');
  209. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  210. $result = $this->Helper->url('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  211. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  212. $result = $this->Helper->url(array(
  213. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  214. ));
  215. $this->assertEquals("/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  216. $result = $this->Helper->url(array(
  217. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  218. '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
  219. ));
  220. $this->assertEquals("/posts/index?page=1&amp;one=value&amp;two=value&amp;three=purple", $result);
  221. }
  222. /**
  223. * test assetTimestamp application
  224. *
  225. * @return void
  226. */
  227. public function testAssetTimestamp() {
  228. Configure::write('Foo.bar', 'test');
  229. Configure::write('Asset.timestamp', false);
  230. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  231. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  232. Configure::write('Asset.timestamp', true);
  233. Configure::write('debug', false);
  234. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  235. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  236. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  237. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  238. Configure::write('Asset.timestamp', true);
  239. Configure::write('debug', true);
  240. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  241. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  242. Configure::write('Asset.timestamp', 'force');
  243. Configure::write('debug', false);
  244. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  245. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  246. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  247. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  248. $this->Helper->request->webroot = '/some/dir/';
  249. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  250. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  251. }
  252. /**
  253. * test assetUrl application
  254. *
  255. * @return void
  256. */
  257. public function testAssetUrl() {
  258. Router::connect('/:controller/:action/*');
  259. $this->Helper->webroot = '';
  260. $result = $this->Helper->assetUrl(array(
  261. 'controller' => 'js',
  262. 'action' => 'post',
  263. '_ext' => 'js'
  264. ),
  265. array('fullBase' => true)
  266. );
  267. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  268. $result = $this->Helper->assetUrl('foo.jpg', array('pathPrefix' => 'img/'));
  269. $this->assertEquals('img/foo.jpg', $result);
  270. $result = $this->Helper->assetUrl('foo.jpg', array('fullBase' => true));
  271. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  272. $result = $this->Helper->assetUrl('style', array('ext' => '.css'));
  273. $this->assertEquals('style.css', $result);
  274. $result = $this->Helper->assetUrl('dir/sub dir/my image', array('ext' => '.jpg'));
  275. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  276. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  277. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  278. $result = $this->Helper->assetUrl('dir/big+tall/image', array('ext' => '.jpg'));
  279. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  280. }
  281. /**
  282. * Test assetUrl with no rewriting.
  283. *
  284. * @return void
  285. */
  286. public function testAssetUrlNoRewrite() {
  287. $this->Helper->request->addPaths(array(
  288. 'base' => '/cake_dev/index.php',
  289. 'webroot' => '/cake_dev/app/webroot/',
  290. 'here' => '/cake_dev/index.php/tasks',
  291. ));
  292. $result = $this->Helper->assetUrl('img/cake.icon.png', array('fullBase' => true));
  293. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  294. $this->assertEquals($expected, $result);
  295. }
  296. /**
  297. * Test assetUrl with plugins.
  298. *
  299. * @return void
  300. */
  301. public function testAssetUrlPlugin() {
  302. $this->Helper->webroot = '';
  303. Plugin::load('TestPlugin');
  304. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css'));
  305. $this->assertEquals('test_plugin/style.css', $result);
  306. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css', 'plugin' => false));
  307. $this->assertEquals('TestPlugin.style.css', $result);
  308. Plugin::unload('TestPlugin');
  309. }
  310. /**
  311. * test assetUrl and Asset.timestamp = force
  312. *
  313. * @return void
  314. */
  315. public function testAssetUrlTimestampForce() {
  316. $this->Helper->webroot = '';
  317. Configure::write('Asset.timestamp', 'force');
  318. $result = $this->Helper->assetUrl('cake.generic.css', array('pathPrefix' => Configure::read('App.cssBaseUrl')));
  319. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  320. }
  321. /**
  322. * test assetTimestamp with plugins and themes
  323. *
  324. * @return void
  325. */
  326. public function testAssetTimestampPluginsAndThemes() {
  327. Configure::write('Asset.timestamp', 'force');
  328. Plugin::load(array('TestPlugin'));
  329. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  330. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  331. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  332. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  333. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  334. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  335. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  336. $this->assertRegExp('#/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  337. }
  338. /**
  339. * Test generating paths with webroot().
  340. *
  341. * @return void
  342. */
  343. public function testWebrootPaths() {
  344. $this->Helper->request->webroot = '/';
  345. $result = $this->Helper->webroot('/img/cake.power.gif');
  346. $expected = '/img/cake.power.gif';
  347. $this->assertEquals($expected, $result);
  348. $this->Helper->theme = 'TestTheme';
  349. $result = $this->Helper->webroot('/img/cake.power.gif');
  350. $expected = '/test_theme/img/cake.power.gif';
  351. $this->assertEquals($expected, $result);
  352. $result = $this->Helper->webroot('/img/test.jpg');
  353. $expected = '/test_theme/img/test.jpg';
  354. $this->assertEquals($expected, $result);
  355. $webRoot = Configure::read('App.www_root');
  356. Configure::write('App.www_root', TEST_APP . 'TestApp/webroot/');
  357. $result = $this->Helper->webroot('/img/cake.power.gif');
  358. $expected = '/test_theme/img/cake.power.gif';
  359. $this->assertEquals($expected, $result);
  360. $result = $this->Helper->webroot('/img/test.jpg');
  361. $expected = '/test_theme/img/test.jpg';
  362. $this->assertEquals($expected, $result);
  363. $result = $this->Helper->webroot('/img/cake.icon.gif');
  364. $expected = '/img/cake.icon.gif';
  365. $this->assertEquals($expected, $result);
  366. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  367. $expected = '/img/cake.icon.gif?some=param';
  368. $this->assertEquals($expected, $result);
  369. Configure::write('App.www_root', $webRoot);
  370. }
  371. /**
  372. * test lazy loading helpers is seamless
  373. *
  374. * @return void
  375. */
  376. public function testLazyLoadingHelpers() {
  377. Plugin::load(array('TestPlugin'));
  378. $Helper = new TestHelper($this->View);
  379. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  380. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  381. }
  382. /**
  383. * test that a helpers Helper is not 'attached' to the collection
  384. *
  385. * @return void
  386. */
  387. public function testThatHelperHelpersAreNotAttached() {
  388. Plugin::loadAll();
  389. $events = $this->getMock('\Cake\Event\EventManager');
  390. $this->View->eventManager($events);
  391. $events->expects($this->never())
  392. ->method('attach');
  393. $Helper = new TestHelper($this->View);
  394. $Helper->OtherHelper;
  395. }
  396. /**
  397. * test that the lazy loader doesn't duplicate objects on each access.
  398. *
  399. * @return void
  400. */
  401. public function testLazyLoadingUsesReferences() {
  402. $Helper = new TestHelper($this->View);
  403. $resultA = $Helper->Html;
  404. $resultB = $Helper->Html;
  405. $resultA->testprop = 1;
  406. $this->assertEquals($resultA->testprop, $resultB->testprop);
  407. }
  408. }