PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php

https://gitlab.com/manuperazafa/elsartenbackend
PHP | 219 lines | 132 code | 31 blank | 56 comment | 0 complexity | 7d0b030ec1ce99be299abad2c7beebbf MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  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/view/1196/Testing CakePHP(tm) Tests
  12. * @package Cake.Test.Case.Routing.Filter
  13. * @since CakePHP(tm) v 2.2
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('AssetDispatcher', 'Routing/Filter');
  17. App::uses('CakeEvent', 'Event');
  18. App::uses('CakeResponse', 'Network');
  19. /**
  20. * Class AssetDispatcherTest
  21. *
  22. * @package Cake.Test.Case.Routing.Filter
  23. */
  24. class AssetDispatcherTest extends CakeTestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. parent::tearDown();
  32. Configure::write('Dispatcher.filters', array());
  33. }
  34. /**
  35. * test that asset filters work for theme and plugin assets
  36. *
  37. * @return void
  38. */
  39. public function testAssetFilterForThemeAndPlugins() {
  40. $filter = new AssetDispatcher();
  41. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  42. Configure::write('Asset.filter', array(
  43. 'js' => '',
  44. 'css' => ''
  45. ));
  46. App::build(array(
  47. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  48. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  49. ), App::RESET);
  50. $request = new CakeRequest('theme/test_theme/ccss/cake.generic.css');
  51. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  52. $this->assertSame($response, $filter->beforeDispatch($event));
  53. $this->assertTrue($event->isStopped());
  54. $request = new CakeRequest('theme/test_theme/cjs/debug_kit.js');
  55. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  56. $this->assertSame($response, $filter->beforeDispatch($event));
  57. $this->assertTrue($event->isStopped());
  58. $request = new CakeRequest('test_plugin/ccss/cake.generic.css');
  59. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  60. $this->assertSame($response, $filter->beforeDispatch($event));
  61. $this->assertTrue($event->isStopped());
  62. $request = new CakeRequest('test_plugin/cjs/debug_kit.js');
  63. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  64. $this->assertSame($response, $filter->beforeDispatch($event));
  65. $this->assertTrue($event->isStopped());
  66. $request = new CakeRequest('css/ccss/debug_kit.css');
  67. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  68. $this->assertNull($filter->beforeDispatch($event));
  69. $this->assertFalse($event->isStopped());
  70. $request = new CakeRequest('js/cjs/debug_kit.js');
  71. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  72. $this->assertNull($filter->beforeDispatch($event));
  73. $this->assertFalse($event->isStopped());
  74. }
  75. /**
  76. * AssetDispatcher should not 404 extensions that could be handled
  77. * by Routing.
  78. *
  79. * @return void
  80. */
  81. public function testNoHandleRoutedExtension() {
  82. $filter = new AssetDispatcher();
  83. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  84. Configure::write('Asset.filter', array(
  85. 'js' => '',
  86. 'css' => ''
  87. ));
  88. App::build(array(
  89. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  90. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  91. ), App::RESET);
  92. Router::parseExtensions('json');
  93. Router::connect('/test_plugin/api/v1/:action', array('controller' => 'api'));
  94. CakePlugin::load('TestPlugin');
  95. $request = new CakeRequest('test_plugin/api/v1/forwarding.json');
  96. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  97. $this->assertNull($filter->beforeDispatch($event));
  98. $this->assertFalse($event->isStopped(), 'Events for routed extensions should not be stopped');
  99. }
  100. /**
  101. * Tests that $response->checkNotModified() is called and bypasses
  102. * file dispatching
  103. *
  104. * @return void
  105. */
  106. public function testNotModified() {
  107. $filter = new AssetDispatcher();
  108. Configure::write('Asset.filter', array(
  109. 'js' => '',
  110. 'css' => ''
  111. ));
  112. App::build(array(
  113. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  114. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  115. ));
  116. $time = filemtime(App::themePath('TestTheme') . 'webroot' . DS . 'img' . DS . 'cake.power.gif');
  117. $time = new DateTime('@' . $time);
  118. $response = $this->getMock('CakeResponse', array('send', 'checkNotModified'));
  119. $request = new CakeRequest('theme/test_theme/img/cake.power.gif');
  120. $response->expects($this->once())->method('checkNotModified')
  121. ->with($request)
  122. ->will($this->returnValue(true));
  123. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  124. ob_start();
  125. $this->assertSame($response, $filter->beforeDispatch($event));
  126. ob_end_clean();
  127. $this->assertEquals(200, $response->statusCode());
  128. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  129. $response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified'));
  130. $request = new CakeRequest('theme/test_theme/img/cake.power.gif');
  131. $response->expects($this->once())->method('checkNotModified')
  132. ->with($request)
  133. ->will($this->returnValue(true));
  134. $response->expects($this->never())->method('send');
  135. $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
  136. $this->assertSame($response, $filter->beforeDispatch($event));
  137. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  138. }
  139. /**
  140. * Test that no exceptions are thrown for //index.php type URLs.
  141. *
  142. * @return void
  143. */
  144. public function test404OnDoubleSlash() {
  145. $filter = new AssetDispatcher();
  146. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  147. $request = new CakeRequest('//index.php');
  148. $event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  149. $this->assertNull($filter->beforeDispatch($event));
  150. $this->assertFalse($event->isStopped());
  151. }
  152. /**
  153. * Test that attempts to traverse directories are prevented.
  154. *
  155. * @return void
  156. */
  157. public function test404OnDoubleDot() {
  158. App::build(array(
  159. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  160. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  161. ), App::RESET);
  162. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  163. $request = new CakeRequest('theme/test_theme/../../../../../../VERSION.txt');
  164. $event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  165. $response->expects($this->never())->method('send');
  166. $filter = new AssetDispatcher();
  167. $this->assertNull($filter->beforeDispatch($event));
  168. $this->assertFalse($event->isStopped());
  169. }
  170. /**
  171. * Test that attempts to traverse directories with urlencoded paths fail.
  172. *
  173. * @return void
  174. */
  175. public function test404OnDoubleDotEncoded() {
  176. App::build(array(
  177. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  178. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  179. ), App::RESET);
  180. $response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
  181. $request = new CakeRequest('theme/test_theme/%2e./%2e./%2e./%2e./%2e./%2e./VERSION.txt');
  182. $event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  183. $response->expects($this->never())->method('send');
  184. $filter = new AssetDispatcher();
  185. $this->assertNull($filter->beforeDispatch($event));
  186. $this->assertFalse($event->isStopped());
  187. }
  188. }