PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/Routing/Filter/AssetFilterTest.php

https://github.com/ceeram/cakephp
PHP | 254 lines | 166 code | 25 blank | 63 comment | 0 complexity | f2509e7fb87b1d28dd1e6efc50646003 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 Open Group Test Suite License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @since 2.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Routing\Filter;
  15. use Cake\Core\App;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Event\Event;
  19. use Cake\Network\Request;
  20. use Cake\Network\Response;
  21. use Cake\Routing\Filter\AssetFilter;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Asset filter test case.
  25. */
  26. class AssetFilterTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Plugin::load(['TestTheme']);
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown()
  44. {
  45. parent::tearDown();
  46. Plugin::unload();
  47. }
  48. /**
  49. * Tests that $response->checkNotModified() is called and bypasses
  50. * file dispatching
  51. *
  52. * @return void
  53. * @triggers DispatcherTest $this, compact('request', 'response')
  54. * @triggers DispatcherTest $this, compact('request', 'response')
  55. */
  56. public function testNotModified()
  57. {
  58. $filter = new AssetFilter();
  59. $time = filemtime(Plugin::path('TestTheme') . 'webroot/img/cake.power.gif');
  60. $time = new \DateTime('@' . $time);
  61. $response = $this->getMock('Cake\Network\Response', ['send', 'checkNotModified']);
  62. $request = new Request('test_theme/img/cake.power.gif');
  63. $response->expects($this->once())->method('checkNotModified')
  64. ->with($request)
  65. ->will($this->returnValue(true));
  66. $event = new Event('DispatcherTest', $this, compact('request', 'response'));
  67. ob_start();
  68. $this->assertSame($response, $filter->beforeDispatch($event));
  69. ob_end_clean();
  70. $this->assertEquals(200, $response->statusCode());
  71. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  72. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'checkNotModified']);
  73. $request = new Request('test_theme/img/cake.power.gif');
  74. $response->expects($this->once())->method('checkNotModified')
  75. ->with($request)
  76. ->will($this->returnValue(true));
  77. $response->expects($this->never())->method('send');
  78. $event = new Event('DispatcherTest', $this, compact('request', 'response'));
  79. $this->assertSame($response, $filter->beforeDispatch($event));
  80. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  81. }
  82. /**
  83. * Test that no exceptions are thrown for //index.php type URLs.
  84. *
  85. * @return void
  86. * @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
  87. */
  88. public function test404OnDoubleSlash()
  89. {
  90. $filter = new AssetFilter();
  91. $response = $this->getMock('Response', ['_sendHeader']);
  92. $request = new Request('//index.php');
  93. $event = new Event('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  94. $this->assertNull($filter->beforeDispatch($event));
  95. $this->assertFalse($event->isStopped());
  96. }
  97. /**
  98. * Test that 404's are returned when .. is in the URL
  99. *
  100. * @return voi
  101. * @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
  102. * @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
  103. */
  104. public function test404OnDoubleDot()
  105. {
  106. $filter = new AssetFilter();
  107. $response = $this->getMock('Response', ['_sendHeader']);
  108. $request = new Request('test_theme/../webroot/css/test_asset.css');
  109. $event = new Event('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  110. $this->assertNull($filter->beforeDispatch($event));
  111. $this->assertFalse($event->isStopped());
  112. $request = new Request('test_theme/%3e./webroot/css/test_asset.css');
  113. $event = new Event('Dispatcher.beforeRequest', $this, compact('request', 'response'));
  114. $this->assertNull($filter->beforeDispatch($event));
  115. $this->assertFalse($event->isStopped());
  116. }
  117. /**
  118. * Data provider for asset filter
  119. *
  120. * - theme assets.
  121. * - plugin assets.
  122. * - plugin assets in sub directories.
  123. * - unknown plugin assets.
  124. *
  125. * @return array
  126. */
  127. public static function assetProvider()
  128. {
  129. return [
  130. [
  131. 'test_theme/flash/theme_test.swf',
  132. 'Plugin/TestTheme/webroot/flash/theme_test.swf'
  133. ],
  134. [
  135. 'test_theme/pdfs/theme_test.pdf',
  136. 'Plugin/TestTheme/webroot/pdfs/theme_test.pdf'
  137. ],
  138. [
  139. 'test_theme/img/test.jpg',
  140. 'Plugin/TestTheme/webroot/img/test.jpg'
  141. ],
  142. [
  143. 'test_theme/css/test_asset.css',
  144. 'Plugin/TestTheme/webroot/css/test_asset.css'
  145. ],
  146. [
  147. 'test_theme/js/theme.js',
  148. 'Plugin/TestTheme/webroot/js/theme.js'
  149. ],
  150. [
  151. 'test_theme/js/one/theme_one.js',
  152. 'Plugin/TestTheme/webroot/js/one/theme_one.js'
  153. ],
  154. [
  155. 'test_theme/space%20image.text',
  156. 'Plugin/TestTheme/webroot/space image.text'
  157. ],
  158. [
  159. 'test_plugin/root.js',
  160. 'Plugin/TestPlugin/webroot/root.js'
  161. ],
  162. [
  163. 'test_plugin/flash/plugin_test.swf',
  164. 'Plugin/TestPlugin/webroot/flash/plugin_test.swf'
  165. ],
  166. [
  167. 'test_plugin/pdfs/plugin_test.pdf',
  168. 'Plugin/TestPlugin/webroot/pdfs/plugin_test.pdf'
  169. ],
  170. [
  171. 'test_plugin/js/test_plugin/test.js',
  172. 'Plugin/TestPlugin/webroot/js/test_plugin/test.js'
  173. ],
  174. [
  175. 'test_plugin/css/test_plugin_asset.css',
  176. 'Plugin/TestPlugin/webroot/css/test_plugin_asset.css'
  177. ],
  178. [
  179. 'test_plugin/img/cake.icon.gif',
  180. 'Plugin/TestPlugin/webroot/img/cake.icon.gif'
  181. ],
  182. [
  183. 'plugin_js/js/plugin_js.js',
  184. 'Plugin/PluginJs/webroot/js/plugin_js.js'
  185. ],
  186. [
  187. 'plugin_js/js/one/plugin_one.js',
  188. 'Plugin/PluginJs/webroot/js/one/plugin_one.js'
  189. ],
  190. [
  191. 'test_plugin/css/unknown.extension',
  192. 'Plugin/TestPlugin/webroot/css/unknown.extension'
  193. ],
  194. [
  195. 'test_plugin/css/theme_one.htc',
  196. 'Plugin/TestPlugin/webroot/css/theme_one.htc'
  197. ],
  198. [
  199. 'company/test_plugin_three/css/company.css',
  200. 'Plugin/Company/TestPluginThree/webroot/css/company.css'
  201. ],
  202. ];
  203. }
  204. /**
  205. * Test assets
  206. *
  207. * @dataProvider assetProvider
  208. * @return void
  209. * @triggers Dispatcher.beforeDispatch $this, compact('request', 'response')
  210. */
  211. public function testAsset($url, $file)
  212. {
  213. Plugin::load(['Company/TestPluginThree', 'TestPlugin', 'PluginJs']);
  214. $filter = new AssetFilter();
  215. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  216. $request = new Request($url);
  217. $event = new Event('Dispatcher.beforeDispatch', $this, compact('request', 'response'));
  218. ob_start();
  219. $filter->beforeDispatch($event);
  220. $result = ob_get_contents();
  221. ob_end_clean();
  222. $path = TEST_APP . str_replace('/', DS, $file);
  223. $file = file_get_contents($path);
  224. $this->assertEquals($file, $result);
  225. $expected = filesize($path);
  226. $headers = $response->header();
  227. $this->assertEquals($expected, $headers['Content-Length']);
  228. }
  229. }