5
5
use Cake \Controller \ComponentRegistry ;
6
6
use Cake \Controller \Controller ;
7
7
use Cake \Core \Configure ;
8
+ use Cake \Core \Plugin ;
8
9
use Cake \Event \Event ;
9
10
use Cake \Http \ServerRequest ;
10
11
use Cake \TestSuite \TestCase ;
@@ -26,6 +27,11 @@ class AuthUserComponentTest extends TestCase {
26
27
*/
27
28
protected $ AuthUser ;
28
29
30
+ /**
31
+ * @var \Cake\Controller\Controller
32
+ */
33
+ protected $ controller ;
34
+
29
35
/**
30
36
* @return void
31
37
*/
@@ -34,10 +40,13 @@ public function setUp(): void {
34
40
'allowFilePath ' => ROOT . DS . 'tests ' . DS . 'test_files ' . DS ,
35
41
];
36
42
37
- $ controller = new Controller (new ServerRequest ());
38
- $ componentRegistry = new ComponentRegistry ($ controller );
43
+ $ this -> controller = new Controller (new ServerRequest ());
44
+ $ componentRegistry = new ComponentRegistry ($ this -> controller );
39
45
$ this ->AuthUser = new TestAuthUserComponent ($ componentRegistry );
40
- $ this ->AuthUser ->Auth = $ this ->getMockBuilder (AuthComponent::class)->onlyMethods (['user ' ])->setConstructorArgs ([$ componentRegistry , $ config ])->getMock ();
46
+ $ this ->controller ->loadComponent ('TinyAuth.Auth ' , [
47
+ 'allowFilePath ' => Plugin::path ('TinyAuth ' ) . 'tests ' . DS . 'test_files ' . DS ,
48
+ ]);
49
+ $ this ->controller ->Auth = $ this ->getMockBuilder (AuthComponent::class)->onlyMethods (['user ' ])->setConstructorArgs ([$ componentRegistry , $ config ])->getMock ();
41
50
42
51
Configure::write ('Roles ' , [
43
52
'user ' => 1 ,
@@ -55,7 +64,7 @@ public function testIsAuthorizedValid() {
55
64
'id ' => 1 ,
56
65
'role_id ' => 1 ,
57
66
];
58
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
67
+ $ this ->controller ->Auth ->expects ($ this ->once ())
59
68
->method ('user ' )
60
69
->with (null )
61
70
->will ($ this ->returnValue ($ user ));
@@ -76,7 +85,7 @@ public function testIsAuthorizedInvalid() {
76
85
'id ' => 1 ,
77
86
'role_id ' => 1 ,
78
87
];
79
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
88
+ $ this ->controller ->Auth ->expects ($ this ->once ())
80
89
->method ('user ' )
81
90
->with (null )
82
91
->will ($ this ->returnValue ($ user ));
@@ -95,7 +104,7 @@ public function testIsAuthorizedInvalid() {
95
104
public function testIsAuthorizedNotLoggedIn () {
96
105
$ user = [
97
106
];
98
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
107
+ $ this ->controller ->Auth ->expects ($ this ->once ())
99
108
->method ('user ' )
100
109
->with (null )
101
110
->will ($ this ->returnValue ($ user ));
@@ -180,7 +189,7 @@ public function testEmptyAuthSession() {
180
189
* @return void
181
190
*/
182
191
public function testId () {
183
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
192
+ $ this ->controller ->Auth ->expects ($ this ->once ())
184
193
->method ('user ' )
185
194
->with (null )
186
195
->will ($ this ->returnValue (['id ' => '1 ' ]));
@@ -192,7 +201,7 @@ public function testId() {
192
201
* @return void
193
202
*/
194
203
public function testIsMe () {
195
- $ this ->AuthUser ->Auth ->expects ($ this ->any ())
204
+ $ this ->controller ->Auth ->expects ($ this ->any ())
196
205
->method ('user ' )
197
206
->with (null )
198
207
->will ($ this ->returnValue (['id ' => '1 ' ]));
@@ -209,7 +218,7 @@ public function testIsMe() {
209
218
* @return void
210
219
*/
211
220
public function testUser () {
212
- $ this ->AuthUser ->Auth ->expects ($ this ->any ())
221
+ $ this ->controller ->Auth ->expects ($ this ->any ())
213
222
->method ('user ' )
214
223
->with (null )
215
224
->will ($ this ->returnValue (['id ' => '1 ' , 'username ' => 'foo ' ]));
@@ -225,7 +234,7 @@ public function testUser() {
225
234
public function testRoles () {
226
235
$ this ->AuthUser ->setConfig ('multiRole ' , true );
227
236
228
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
237
+ $ this ->controller ->Auth ->expects ($ this ->once ())
229
238
->method ('user ' )
230
239
->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => ['1 ' , '2 ' ]]));
231
240
@@ -238,7 +247,7 @@ public function testRoles() {
238
247
public function testRolesDeep () {
239
248
$ this ->AuthUser ->setConfig ('multiRole ' , true );
240
249
241
- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
250
+ $ this ->controller ->Auth ->expects ($ this ->once ())
242
251
->method ('user ' )
243
252
->with (null )
244
253
->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
@@ -252,7 +261,7 @@ public function testRolesDeep() {
252
261
public function testHasRole () {
253
262
$ this ->AuthUser ->setConfig ('multiRole ' , true );
254
263
255
- $ this ->AuthUser ->Auth ->expects ($ this ->exactly (3 ))
264
+ $ this ->controller ->Auth ->expects ($ this ->exactly (3 ))
256
265
->method ('user ' )
257
266
->with (null )
258
267
->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
@@ -271,7 +280,7 @@ public function testHasRole() {
271
280
public function testHasRoles () {
272
281
$ this ->AuthUser ->setConfig ('multiRole ' , true );
273
282
274
- $ this ->AuthUser ->Auth ->expects ($ this ->exactly (6 ))
283
+ $ this ->controller ->Auth ->expects ($ this ->exactly (6 ))
275
284
->method ('user ' )
276
285
->with (null )
277
286
->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
0 commit comments