HelperTest.php 15 KB

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