forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTraitTest.php
More file actions
359 lines (320 loc) · 10.8 KB
/
Copy pathBaseTraitTest.php
File metadata and controls
359 lines (320 loc) · 10.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Test\TestCase\Controller\Traits;
use Authentication\Authenticator\Result;
use Authentication\Controller\Component\AuthenticationComponent;
use Authentication\Identifier\IdentifierCollection;
use Authentication\Identifier\PasswordIdentifier;
use Authentication\Identity;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Mailer\Email;
use Cake\Mailer\TransportFactory;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use CakeDC\Auth\Authentication\AuthenticationService;
use CakeDC\Users\Model\Entity\User;
use PHPUnit_Framework_MockObject_RuntimeException;
/**
* Class BaseTraitTest
*
* @package CakeDC\Users\Test\TestCase\Controller\Traits
*/
abstract class BaseTraitTest extends TestCase
{
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'plugin.CakeDC/Users.Users',
];
/**
* Classname of the trait we are about to test
*
* @var string
*/
public $traitClassName = '';
public $traitMockMethods = [];
public $mockDefaultEmail = false;
public $successLoginRedirect = '/home';
public $logoutRedirect = '/login?fromlogout=1';
public $loginAction = '/login-page';
/**
* @var MockObject
*/
public $Trait;
/**
* @var Table
*/
public $table;
/**
* SetUp and create Trait
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->loadPlugins(['CakeDC/Users' => ['routes' => true]]);
$traitMockMethods = array_unique(array_merge(['getUsersTable'], $this->traitMockMethods));
$this->table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users');
try {
$this->Trait = $this->getMockBuilder($this->traitClassName)
->setMethods($traitMockMethods)
->getMock();
$this->Trait->expects($this->any())
->method('getUsersTable')
->will($this->returnValue($this->table));
} catch (PHPUnit_Framework_MockObject_RuntimeException $ex) {
debug($ex);
$this->fail('Unit tests extending BaseTraitTest should declare the trait class name in the $traitClassName variable before calling setUp()');
}
if ($this->mockDefaultEmail) {
TransportFactory::setConfig('test', [
'className' => 'Debug',
]);
$this->configEmail = Email::getConfig('default');
Email::drop('default');
Email::setConfig('default', [
'transport' => 'test',
'from' => 'cakedc@example.com',
]);
}
}
/**
* tearDown
*
* @return void
*/
public function tearDown(): void
{
unset($this->table, $this->Trait);
if ($this->mockDefaultEmail) {
Email::drop('default');
TransportFactory::drop('test');
//Email::setConfig('default', $this->setConfigEmail);
}
parent::tearDown();
}
/**
* Mock session and mock session attributes
*
* @return \Cake\Http\Session
*/
protected function _mockSession($attributes)
{
$session = new \Cake\Http\Session();
foreach ($attributes as $field => $value) {
$session->write($field, $value);
}
$this->Trait
->getRequest()
->expects($this->any())
->method('getSession')
->willReturn($session);
return $session;
}
/**
* mock request for GET
*
* @return void
*/
protected function _mockRequestGet($withSession = false)
{
$methods = ['is', 'referer', 'getData'];
if ($withSession) {
$methods[] = 'getSession';
}
$request = $this->getMockBuilder('Cake\Http\ServerRequest')
->setMethods($methods)
->getMock();
$request->expects($this->any())
->method('is')
->with('post')
->will($this->returnValue(false));
$this->Trait->setRequest($request);
}
/**
* mock Flash Component
*
* @return void
*/
protected function _mockFlash()
{
$this->Trait->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['error', 'success'])
->disableOriginalConstructor()
->getMock();
}
/**
* mock Request for POST, is and allow methods
*
* @param mixed $with used in with
* @return void
*/
protected function _mockRequestPost($with = 'post')
{
$request = $this->getMockBuilder('Cake\Http\ServerRequest')
->setMethods(['is', 'getData', 'allow'])
->getMock();
$request->expects($this->any())
->method('is')
->with($with)
->will($this->returnValue(true));
$this->Trait->setRequest($request);
}
/**
* Mock Auth and retur user id 1
*
* @return void
*/
protected function _mockAuthLoggedIn($user = [])
{
$user += [
'id' => '00000000-0000-0000-0000-000000000001',
'password' => '12345',
];
$this->_mockAuthentication($user);
}
/**
* Mock the Authentication service
*
* @param array $user
* @param array $failures
* @param \Authentication\Identifier\IdentifierCollection $identifiers custom identifiers collection
* @return void
*/
protected function _mockAuthentication($user = null, $failures = [], $identifiers = null)
{
if ($identifiers === null) {
$passwordIdentifier = $this->getMockBuilder(PasswordIdentifier::class)
->setMethods(['needsPasswordRehash'])
->getMock();
$passwordIdentifier->expects($this->any())
->method('needsPasswordRehash')
->willReturn(false);
$identifiers = new IdentifierCollection([]);
$identifiers->set('Password', $passwordIdentifier);
}
$config = [
'identifiers' => [
'Authentication.Password',
],
'authenticators' => [
'Authentication.Session',
'Authentication.Form',
],
];
$authentication = $this->getMockBuilder(AuthenticationService::class)->setConstructorArgs([$config])->setMethods([
'getResult',
'getFailures',
'identifiers',
])->getMock();
if ($user) {
$user = new User($user);
$identity = new Identity($user);
$result = new Result($user, Result::SUCCESS);
$this->Trait->setRequest($this->Trait->getRequest()->withAttribute('identity', $identity));
} else {
$result = new Result($user, Result::FAILURE_CREDENTIALS_MISSING);
}
$authentication->expects($this->any())
->method('getResult')
->will($this->returnValue($result));
$authentication->expects($this->any())
->method('getFailures')
->will($this->returnValue($failures));
$authentication->expects($this->any())
->method('identifiers')
->will($this->returnValue($identifiers));
$this->Trait->setRequest($this->Trait->getRequest()->withAttribute('authentication', $authentication));
$controller = new Controller($this->Trait->getRequest());
$registry = new ComponentRegistry($controller);
$this->Trait->Authentication = new AuthenticationComponent($registry, [
'loginRedirect' => $this->successLoginRedirect,
'logoutRedirect' => $this->logoutRedirect,
'loginAction' => $this->loginAction,
]);
}
/**
* Mock the Authentication service with a Password Rehash being required.
*
* @param array $user
* @param array $failures
* @return void
*/
protected function _mockAuthenticationWithPasswordRehash($user = null, $failures = [])
{
$config = [
'identifiers' => [
'Authentication.Password',
],
'authenticators' => [
'Authentication.Session',
'Authentication.Form',
],
];
$authentication = $this->getMockBuilder(AuthenticationService::class)->setConstructorArgs([$config])->setMethods([
'getResult',
'getFailures',
'identifiers',
])->getMock();
$authentication->expects($this->any())
->method('identifiers')
->willReturn($identifiers);
if ($user) {
$user = is_object($user) ? $user : new User($user);
$identity = new Identity($user);
$result = new Result($user, Result::SUCCESS);
$this->Trait->setRequest($this->Trait->getRequest()->withAttribute('identity', $identity));
} else {
$result = new Result($user, Result::FAILURE_CREDENTIALS_MISSING);
}
$authentication->expects($this->any())
->method('getResult')
->will($this->returnValue($result));
$authentication->expects($this->any())
->method('getFailures')
->will($this->returnValue($failures));
$this->Trait->setRequest($this->Trait->getRequest()->withAttribute('authentication', $authentication));
//$controller = new Controller($this->Trait->getRequest());
$registry = new ComponentRegistry($this->Trait);
$this->Trait->Authentication = new AuthenticationComponent($registry, [
'loginRedirect' => $this->successLoginRedirect,
'logoutRedirect' => $this->logoutRedirect,
'loginAction' => $this->loginAction,
]);
}
/**
* mock utility
*
* @param Event $event event
* @param array $result array of data
* @return void
*/
protected function _mockDispatchEvent(?Event $event = null, $result = [])
{
if (is_null($event)) {
$event = new Event('cool-name-here');
}
if (!empty($result)) {
$event->setResult(new Entity($result));
}
$this->Trait->expects($this->any())
->method('dispatchEvent')
->will($this->returnValue($event));
}
}