PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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