UrlHelperTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. */
  27. class UrlHelperTest extends TestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. Router::reload();
  36. $this->View = new View();
  37. $this->Helper = new UrlHelper($this->View);
  38. $this->Helper->request = new Request();
  39. Configure::write('App.namespace', 'TestApp');
  40. Plugin::load(['TestTheme']);
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown() {
  48. parent::tearDown();
  49. Configure::delete('Asset');
  50. Plugin::unload();
  51. unset($this->Helper, $this->View);
  52. }
  53. /**
  54. * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
  55. *
  56. * @return void
  57. */
  58. public function testUrlConversion() {
  59. Router::connect('/:controller/:action/*');
  60. $result = $this->Helper->build('/controller/action/1');
  61. $this->assertEquals('/controller/action/1', $result);
  62. $result = $this->Helper->build('/controller/action/1?one=1&two=2');
  63. $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
  64. $result = $this->Helper->build(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
  65. $this->assertEquals("/posts/index?page=1%22+onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
  66. $result = $this->Helper->build('/controller/action/1/param:this+one+more');
  67. $this->assertEquals('/controller/action/1/param:this+one+more', $result);
  68. $result = $this->Helper->build('/controller/action/1/param:this%20one%20more');
  69. $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
  70. $result = $this->Helper->build('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
  71. $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
  72. $result = $this->Helper->build(array(
  73. 'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
  74. ));
  75. $this->assertEquals("/posts/index?param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
  76. $result = $this->Helper->build(array(
  77. 'controller' => 'posts', 'action' => 'index', 'page' => '1',
  78. '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
  79. ));
  80. $this->assertEquals("/posts/index?page=1&amp;one=value&amp;two=value&amp;three=purple", $result);
  81. }
  82. /**
  83. * test assetTimestamp application
  84. *
  85. * @return void
  86. */
  87. public function testAssetTimestamp() {
  88. Configure::write('Foo.bar', 'test');
  89. Configure::write('Asset.timestamp', false);
  90. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  91. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  92. Configure::write('Asset.timestamp', true);
  93. Configure::write('debug', false);
  94. $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
  95. $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
  96. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  97. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
  98. Configure::write('Asset.timestamp', true);
  99. Configure::write('debug', true);
  100. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  101. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  102. Configure::write('Asset.timestamp', 'force');
  103. Configure::write('debug', false);
  104. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  105. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  106. $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
  107. $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
  108. $this->Helper->request->webroot = '/some/dir/';
  109. $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
  110. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  111. }
  112. /**
  113. * test assetUrl application
  114. *
  115. * @return void
  116. */
  117. public function testAssetUrl() {
  118. Router::connect('/:controller/:action/*');
  119. $this->Helper->webroot = '';
  120. $result = $this->Helper->assetUrl(array(
  121. 'controller' => 'js',
  122. 'action' => 'post',
  123. '_ext' => 'js'
  124. ),
  125. array('fullBase' => true)
  126. );
  127. $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
  128. $result = $this->Helper->assetUrl('foo.jpg', array('pathPrefix' => 'img/'));
  129. $this->assertEquals('img/foo.jpg', $result);
  130. $result = $this->Helper->assetUrl('foo.jpg', array('fullBase' => true));
  131. $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
  132. $result = $this->Helper->assetUrl('style', array('ext' => '.css'));
  133. $this->assertEquals('style.css', $result);
  134. $result = $this->Helper->assetUrl('dir/sub dir/my image', array('ext' => '.jpg'));
  135. $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
  136. $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
  137. $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
  138. $result = $this->Helper->assetUrl('dir/big+tall/image', array('ext' => '.jpg'));
  139. $this->assertEquals('dir/big%2Btall/image.jpg', $result);
  140. }
  141. /**
  142. * Test assetUrl with no rewriting.
  143. *
  144. * @return void
  145. */
  146. public function testAssetUrlNoRewrite() {
  147. $this->Helper->request->addPaths(array(
  148. 'base' => '/cake_dev/index.php',
  149. 'webroot' => '/cake_dev/app/webroot/',
  150. 'here' => '/cake_dev/index.php/tasks',
  151. ));
  152. $result = $this->Helper->assetUrl('img/cake.icon.png', array('fullBase' => true));
  153. $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
  154. $this->assertEquals($expected, $result);
  155. }
  156. /**
  157. * Test assetUrl with plugins.
  158. *
  159. * @return void
  160. */
  161. public function testAssetUrlPlugin() {
  162. $this->Helper->webroot = '';
  163. Plugin::load('TestPlugin');
  164. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css'));
  165. $this->assertEquals('test_plugin/style.css', $result);
  166. $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css', 'plugin' => false));
  167. $this->assertEquals('TestPlugin.style.css', $result);
  168. Plugin::unload('TestPlugin');
  169. }
  170. /**
  171. * test assetUrl and Asset.timestamp = force
  172. *
  173. * @return void
  174. */
  175. public function testAssetUrlTimestampForce() {
  176. $this->Helper->webroot = '';
  177. Configure::write('Asset.timestamp', 'force');
  178. $result = $this->Helper->assetUrl('cake.generic.css', array('pathPrefix' => Configure::read('App.cssBaseUrl')));
  179. $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
  180. }
  181. /**
  182. * test assetTimestamp with plugins and themes
  183. *
  184. * @return void
  185. */
  186. public function testAssetTimestampPluginsAndThemes() {
  187. Configure::write('Asset.timestamp', 'force');
  188. Plugin::load(array('TestPlugin'));
  189. $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
  190. $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
  191. $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
  192. $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
  193. $result = $this->Helper->assetTimestamp('/test_theme/js/theme.js');
  194. $this->assertRegExp('#/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
  195. $result = $this->Helper->assetTimestamp('/test_theme/js/non_existant.js');
  196. $this->assertRegExp('#/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
  197. }
  198. /**
  199. * Test generating paths with webroot().
  200. *
  201. * @return void
  202. */
  203. public function testWebrootPaths() {
  204. $this->Helper->request->webroot = '/';
  205. $result = $this->Helper->webroot('/img/cake.power.gif');
  206. $expected = '/img/cake.power.gif';
  207. $this->assertEquals($expected, $result);
  208. $this->Helper->theme = 'TestTheme';
  209. $result = $this->Helper->webroot('/img/cake.power.gif');
  210. $expected = '/test_theme/img/cake.power.gif';
  211. $this->assertEquals($expected, $result);
  212. $result = $this->Helper->webroot('/img/test.jpg');
  213. $expected = '/test_theme/img/test.jpg';
  214. $this->assertEquals($expected, $result);
  215. $webRoot = Configure::read('App.www_root');
  216. Configure::write('App.www_root', TEST_APP . 'TestApp/webroot/');
  217. $result = $this->Helper->webroot('/img/cake.power.gif');
  218. $expected = '/test_theme/img/cake.power.gif';
  219. $this->assertEquals($expected, $result);
  220. $result = $this->Helper->webroot('/img/test.jpg');
  221. $expected = '/test_theme/img/test.jpg';
  222. $this->assertEquals($expected, $result);
  223. $result = $this->Helper->webroot('/img/cake.icon.gif');
  224. $expected = '/img/cake.icon.gif';
  225. $this->assertEquals($expected, $result);
  226. $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
  227. $expected = '/img/cake.icon.gif?some=param';
  228. $this->assertEquals($expected, $result);
  229. Configure::write('App.www_root', $webRoot);
  230. }
  231. }