Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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
 * Tests that $response->checkNotModified() is called and bypasses
88
 * file dispatching
89
 *
90
 * @return void
91
 */
92
	public function testNotModified() {
93
		$filter = new AssetDispatcher();
94
		Configure::write('Asset.filter', array(
95
			'js' => '',
96
			'css' => ''
97
		));
98
		App::build(array(
99
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
100
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
101
		));
102
		$time = filemtime(App::themePath('TestTheme') . 'webroot' . DS . 'img' . DS . 'cake.power.gif');
103
		$time = new DateTime('@' . $time);
104
 
105
		$response = $this->getMock('CakeResponse', array('send', 'checkNotModified'));
106
		$request = new CakeRequest('theme/test_theme/img/cake.power.gif');
107
 
108
		$response->expects($this->once())->method('checkNotModified')
109
			->with($request)
110
			->will($this->returnValue(true));
111
		$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
112
 
113
		ob_start();
114
		$this->assertSame($response, $filter->beforeDispatch($event));
115
		ob_end_clean();
116
		$this->assertEquals(200, $response->statusCode());
117
		$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
118
 
119
		$response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified'));
120
		$request = new CakeRequest('theme/test_theme/img/cake.power.gif');
121
 
122
		$response->expects($this->once())->method('checkNotModified')
123
			->with($request)
124
			->will($this->returnValue(true));
125
		$response->expects($this->never())->method('send');
126
		$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
127
 
128
		$this->assertSame($response, $filter->beforeDispatch($event));
129
		$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
130
	}
131
 
132
/**
133
 * Test that no exceptions are thrown for //index.php type URLs.
134
 *
135
 * @return void
136
 */
137
	public function test404OnDoubleSlash() {
138
		$filter = new AssetDispatcher();
139
 
140
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
141
		$request = new CakeRequest('//index.php');
142
		$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
143
 
144
		$this->assertNull($filter->beforeDispatch($event));
145
		$this->assertFalse($event->isStopped());
146
	}
147
 
148
/**
149
 * Test that attempts to traverse directories are prevented.
150
 *
151
 * @return void
152
 */
153
	public function test404OnDoubleDot() {
154
		App::build(array(
155
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
156
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
157
		), APP::RESET);
158
 
159
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
160
		$request = new CakeRequest('theme/test_theme/../../../../../../VERSION.txt');
161
		$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
162
 
163
		$response->expects($this->never())->method('send');
164
 
165
		$filter = new AssetDispatcher();
166
		$this->assertNull($filter->beforeDispatch($event));
167
		$this->assertFalse($event->isStopped());
168
	}
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
 
181
		$response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
182
		$request = new CakeRequest('theme/test_theme/%2e./%2e./%2e./%2e./%2e./%2e./VERSION.txt');
183
		$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
184
 
185
		$response->expects($this->never())->method('send');
186
 
187
		$filter = new AssetDispatcher();
188
		$this->assertNull($filter->beforeDispatch($event));
189
		$this->assertFalse($event->isStopped());
190
	}
191
 
192
}