UrlHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Network\Request;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\View\Helper\UrlHelper;
  22. use Cake\View\View;
  23. /**
  24. * UrlHelperTest class
  25. */
  26. class UrlHelperTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Router::reload();
  37. $this->View = new View();
  38. $this->Helper = new UrlHelper($this->View);
  39. $this->Helper->request = new Request();
  40. Configure::write('App.namespace', 'TestApp');
  41. Plugin::load(['TestTheme']);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. Configure::delete('Asset');
  52. Plugin::unload();
  53. unset($this->Helper, $this->View);
  54. }
  55. /**
  56. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  57. *
  58. * @return void
  59. */
  60. public function testUrlConversion()
  61. {
  62. Router::connect('/:controller/:action/*');
  63. $result = $this->Helper->build('/controller/action/1');
  64. $this->assertEquals('/controller/action/1', $result);
  65. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  66. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  67. $result = $this->Helper->build(['controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"']);
  68. $this->assertEquals("/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  69. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  70. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  71. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  72. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  73. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  74. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  75. $result = $this->Helper->build([
  76. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  77. ]);
  78. $this->assertEquals("/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  79. $result = $this->Helper->build([
  80. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  81. '?' => ['one' => 'value', 'two' => 'value', 'three' => 'purple']
  82. ]);
  83. $this->assertEquals("/posts/index?one=value&amp;two=value&amp;three=purple&amp;page=1", $result);
  84. }
  85. /**
  86. * @return void
  87. */
  88. public function testUrlConversionUnescaped()
  89. {
  90. $result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
  91. $this->assertEquals('/controller/action/1?one=1&two=2', $result);
  92. $result = $this->Helper->build([
  93. 'controller' => 'posts',
  94. 'action' => 'view',
  95. 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24',
  96. '?' => [
  97. 'k' => 'v',
  98. '1' => '2'
  99. ]
  100. ], ['escape' => false]);
  101. $this->assertEquals("/posts/view?k=v&1=2&param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  102. }
  103. /**
  104. * test assetTimestamp application
  105. *
  106. * @return void
  107. */
  108. public function testAssetTimestamp()
  109. {
  110. Configure::write('Foo.bar', 'test');
  111. Configure::write('Asset.timestamp', false);
  112. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  113. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  114. Configure::write('Asset.timestamp', true);
  115. Configure::write('debug', false);
  116. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  117. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  118. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  119. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  120. Configure::write('Asset.timestamp', true);
  121. Configure::write('debug', true);
  122. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  123. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  124. Configure::write('Asset.timestamp', 'force');
  125. Configure::write('debug', false);
  126. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  127. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  128. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  129. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  130. $this->Helper->request->webroot = '/some/dir/';
  131. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  132. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  133. }
  134. /**
  135. * test assetUrl application
  136. *
  137. * @return void
  138. */
  139. public function testAssetUrl()
  140. {
  141. Router::connect('/:controller/:action/*');
  142. $this->Helper->webroot = '';
  143. $result = $this->Helper->assetUrl(
  144. [
  145. 'controller' => 'js',
  146. 'action' => 'post',
  147. '_ext' => 'js'
  148. ],
  149. ['fullBase' => true]
  150. );
  151. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  152. $result = $this->Helper->assetUrl('foo.jpg', ['pathPrefix' => 'img/']);
  153. $this->assertEquals('img/foo.jpg', $result);
  154. $result = $this->Helper->assetUrl('foo.jpg', ['fullBase' => true]);
  155. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  156. $result = $this->Helper->assetUrl('style', ['ext' => '.css']);
  157. $this->assertEquals('style.css', $result);
  158. $result = $this->Helper->assetUrl('dir/sub dir/my image', ['ext' => '.jpg']);
  159. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  160. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  161. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  162. $result = $this->Helper->assetUrl('dir/big+tall/image', ['ext' => '.jpg']);
  163. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  164. }
  165. /**
  166. * Test assetUrl with no rewriting.
  167. *
  168. * @return void
  169. */
  170. public function testAssetUrlNoRewrite()
  171. {
  172. $this->Helper->request->addPaths([
  173. 'base' => '/cake_dev/index.php',
  174. 'webroot' => '/cake_dev/app/webroot/',
  175. 'here' => '/cake_dev/index.php/tasks',
  176. ]);
  177. $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
  178. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  179. $this->assertEquals($expected, $result);
  180. }
  181. /**
  182. * Test assetUrl with plugins.
  183. *
  184. * @return void
  185. */
  186. public function testAssetUrlPlugin()
  187. {
  188. $this->Helper->webroot = '';
  189. Plugin::load('TestPlugin');
  190. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
  191. $this->assertEquals('test_plugin/style.css', $result);
  192. $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css', 'plugin' => false]);
  193. $this->assertEquals('TestPlugin.style.css', $result);
  194. Plugin::unload('TestPlugin');
  195. }
  196. /**
  197. * test assetUrl and Asset.timestamp = force
  198. *
  199. * @return void
  200. */
  201. public function testAssetUrlTimestampForce()
  202. {
  203. $this->Helper->webroot = '';
  204. Configure::write('Asset.timestamp', 'force');
  205. $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
  206. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  207. }
  208. /**
  209. * test assetTimestamp with plugins and themes
  210. *
  211. * @return void
  212. */
  213. public function testAssetTimestampPluginsAndThemes()
  214. {
  215. Configure::write('Asset.timestamp', 'force');
  216. Plugin::load(['TestPlugin']);
  217. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  218. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  219. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  220. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  221. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  222. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  223. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  224. $this->assertRegExp('#/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  225. }
  226. /**
  227. * test script()
  228. *
  229. * @return void
  230. */
  231. public function testScript()
  232. {
  233. Router::connect('/:controller/:action/*');
  234. $this->Helper->webroot = '';
  235. $result = $this->Helper->script(
  236. [
  237. 'controller' => 'js',
  238. 'action' => 'post',
  239. '_ext' => 'js'
  240. ],
  241. ['fullBase' => true]
  242. );
  243. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  244. }
  245. /**
  246. * test image()
  247. *
  248. * @return void
  249. */
  250. public function testImage()
  251. {
  252. $result = $this->Helper->image('foo.jpg');
  253. $this->assertEquals('img/foo.jpg', $result);
  254. $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
  255. $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
  256. $result = $this->Helper->image('dir/sub dir/my image.jpg');
  257. $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
  258. $result = $this->Helper->image('foo.jpg?one=two&three=four');
  259. $this->assertEquals('img/foo.jpg?one=two&amp;three=four', $result);
  260. $result = $this->Helper->image('dir/big+tall/image.jpg');
  261. $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
  262. $result = $this->Helper->image('cid:foo.jpg');
  263. $this->assertEquals('cid:foo.jpg', $result);
  264. $result = $this->Helper->image('CID:foo.jpg');
  265. $this->assertEquals('CID:foo.jpg', $result);
  266. }
  267. /**
  268. * test css
  269. *
  270. * @return void
  271. */
  272. public function testCss()
  273. {
  274. $result = $this->Helper->css('style');
  275. $this->assertEquals('css/style.css', $result);
  276. }
  277. /**
  278. * Test generating paths with webroot().
  279. *
  280. * @return void
  281. */
  282. public function testWebrootPaths()
  283. {
  284. $this->Helper->request->webroot = '/';
  285. $result = $this->Helper->webroot('/img/cake.power.gif');
  286. $expected = '/img/cake.power.gif';
  287. $this->assertEquals($expected, $result);
  288. $this->Helper->theme = 'TestTheme';
  289. $result = $this->Helper->webroot('/img/cake.power.gif');
  290. $expected = '/test_theme/img/cake.power.gif';
  291. $this->assertEquals($expected, $result);
  292. $result = $this->Helper->webroot('/img/test.jpg');
  293. $expected = '/test_theme/img/test.jpg';
  294. $this->assertEquals($expected, $result);
  295. $webRoot = Configure::read('App.wwwRoot');
  296. Configure::write('App.wwwRoot', TEST_APP . 'TestApp/webroot/');
  297. $result = $this->Helper->webroot('/img/cake.power.gif');
  298. $expected = '/test_theme/img/cake.power.gif';
  299. $this->assertEquals($expected, $result);
  300. $result = $this->Helper->webroot('/img/test.jpg');
  301. $expected = '/test_theme/img/test.jpg';
  302. $this->assertEquals($expected, $result);
  303. $result = $this->Helper->webroot('/img/cake.icon.gif');
  304. $expected = '/img/cake.icon.gif';
  305. $this->assertEquals($expected, $result);
  306. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  307. $expected = '/img/cake.icon.gif?some=param';
  308. $this->assertEquals($expected, $result);
  309. Configure::write('App.wwwRoot', $webRoot);
  310. }
  311. }