12
12
*/
13
13
class CommonComponentTest extends TestCase {
14
14
15
- //public $fixtures = array('core.sessions', 'plugin.tools.tools_users', 'plugin.tools.roles');
16
-
15
+ /**
16
+ * @return void
17
+ */
17
18
public function setUp () {
18
19
parent ::setUp ();
19
20
20
21
Configure::write ('App.namespace ' , 'TestApp ' );
22
+ Configure::write ('App.fullBaseUrl ' , 'http://localhost ' );
21
23
22
- $ this ->Controller = new CommonComponentTestController (new Request ('/test ' ));
24
+ $ this ->request = new Request ('/my_controller/foo ' );
25
+ $ this ->request ->params ['controller ' ] = 'MyController ' ;
26
+ $ this ->request ->params ['action ' ] = 'foo ' ;
27
+ $ this ->Controller = new CommonComponentTestController ($ this ->request );
23
28
$ this ->Controller ->startupProcess ();
24
29
}
25
30
31
+ /**
32
+ * @return void
33
+ */
26
34
public function tearDown () {
27
35
parent ::tearDown ();
28
36
@@ -31,8 +39,6 @@ public function tearDown() {
31
39
}
32
40
33
41
/**
34
- * CommonComponentTest::testLoadComponent()
35
- *
36
42
* @return void
37
43
*/
38
44
public function testLoadComponent () {
@@ -70,8 +76,6 @@ public function testLoadComponent() {
70
76
}
71
77
72
78
/**
73
- * CommonComponentTest::testGetParams()
74
- *
75
79
* @return void
76
80
*/
77
81
public function testGetParams () {
@@ -83,8 +87,6 @@ public function testGetParams() {
83
87
}
84
88
85
89
/**
86
- * CommonComponentTest::testGetDefaultUrlParams()
87
- *
88
90
* @return void
89
91
*/
90
92
public function testGetDefaultUrlParams () {
@@ -106,8 +108,6 @@ public function testCurrentUrl() {
106
108
}
107
109
108
110
/**
109
- * CommonComponentTest::testIsForeignReferer()
110
- *
111
111
* @return void
112
112
*/
113
113
public function testIsForeignReferer () {
@@ -125,39 +125,69 @@ public function testIsForeignReferer() {
125
125
}
126
126
127
127
/**
128
- * CommonComponentTest::testAutoRedirect()
129
- *
130
128
* @return void
131
129
*/
132
130
public function testPostRedirect () {
133
131
$ is = $ this ->Controller ->Common ->postRedirect (['action ' => 'foo ' ]);
134
132
$ is = $ this ->Controller ->response ->header ();
135
- $ this ->assertSame ('/foo ' , $ is ['Location ' ]);
133
+ $ this ->assertSame ('http://localhost /foo ' , $ is ['Location ' ]);
136
134
$ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
137
135
}
138
136
139
137
/**
140
- * CommonComponentTest::testAutoRedirect()
141
- *
142
138
* @return void
143
139
*/
144
140
public function testAutoRedirect () {
145
141
$ is = $ this ->Controller ->Common ->autoRedirect (['action ' => 'foo ' ]);
146
142
$ is = $ this ->Controller ->response ->header ();
147
- $ this ->assertSame ('/foo ' , $ is ['Location ' ]);
148
- $ this ->assertSame (200 , $ this ->Controller ->response ->statusCode ());
143
+ $ this ->assertSame ('http://localhost /foo ' , $ is ['Location ' ]);
144
+ $ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
149
145
}
150
146
151
147
/**
152
- * CommonComponentTest::testAutoRedirect()
153
- *
154
148
* @return void
155
149
*/
156
150
public function testAutoRedirectReferer () {
151
+ $ this ->request ->env ('HTTP_REFERER ' , 'http://localhost/my_controller/some-referer-action ' );
152
+
157
153
$ is = $ this ->Controller ->Common ->autoRedirect (['action ' => 'foo ' ], true );
158
154
$ is = $ this ->Controller ->response ->header ();
159
- $ this ->assertSame ('/foo ' , $ is ['Location ' ]);
160
- $ this ->assertSame (200 , $ this ->Controller ->response ->statusCode ());
155
+ $ this ->assertSame ('http://localhost/my_controller/some-referer-action ' , $ is ['Location ' ]);
156
+ $ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
157
+ }
158
+
159
+ /**
160
+ * @return void
161
+ */
162
+ public function testAutoPostRedirect () {
163
+ $ is = $ this ->Controller ->Common ->autoPostRedirect (['action ' => 'foo ' ], true );
164
+ $ is = $ this ->Controller ->response ->header ();
165
+ $ this ->assertSame ('http://localhost/foo ' , $ is ['Location ' ]);
166
+ $ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
167
+ }
168
+
169
+ /**
170
+ * @return void
171
+ */
172
+ public function testAutoPostRedirectReferer () {
173
+ $ this ->request ->env ('HTTP_REFERER ' , 'http://localhost/my_controller/allowed ' );
174
+
175
+ $ is = $ this ->Controller ->Common ->autoPostRedirect (['controller ' => 'MyController ' , 'action ' => 'foo ' ], true );
176
+ $ is = $ this ->Controller ->response ->header ();
177
+ $ this ->assertSame ('http://localhost/my_controller/allowed ' , $ is ['Location ' ]);
178
+ $ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
179
+ }
180
+
181
+ /**
182
+ * @return void
183
+ */
184
+ public function testAutoPostRedirectRefererNotWhitelisted () {
185
+ $ this ->request ->env ('HTTP_REFERER ' , 'http://localhost/my_controller/wrong ' );
186
+
187
+ $ is = $ this ->Controller ->Common ->autoPostRedirect (['controller ' => 'MyController ' , 'action ' => 'foo ' ], true );
188
+ $ is = $ this ->Controller ->response ->header ();
189
+ $ this ->assertSame ('http://localhost/my_controller/foo ' , $ is ['Location ' ]);
190
+ $ this ->assertSame (302 , $ this ->Controller ->response ->statusCode ());
161
191
}
162
192
163
193
}
@@ -167,9 +197,16 @@ public function testAutoRedirectReferer() {
167
197
*/
168
198
class CommonComponentTestController extends Controller {
169
199
200
+ public $ name = 'MyController ' ;
201
+
170
202
/**
171
203
* @var array
172
204
*/
173
205
public $ components = ['Tools.Common ' ];
174
206
207
+ /**
208
+ * @var array
209
+ */
210
+ public $ autoRedirectActions = ['allowed ' ];
211
+
175
212
}
0 commit comments