Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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