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