Skip to content

Commit b68dee9

Browse files
committed
fix deprecations
1 parent 6b8117f commit b68dee9

File tree

10 files changed

+48
-50
lines changed

10 files changed

+48
-50
lines changed

src/Controller/Component/LoginComponent.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,20 @@ protected function handlePasswordRehash($service, $user, \Cake\Http\ServerReques
205205

206206
// new way to define identifiers, inside the authenticators
207207
$authenticatorNames = (array)Configure::read('Auth.PasswordRehash.authenticators');
208+
if ($authenticatorNames === []) {
209+
return;
210+
}
211+
212+
$authenticationProvider = $service->getAuthenticationProvider();
213+
if (!$authenticationProvider || !method_exists($authenticationProvider, 'getIdentifier')) {
214+
return;
215+
}
216+
217+
/** @var \Authentication\Identifier\IdentifierCollection $identifierCollection */
218+
$identifierCollection = $authenticationProvider->getIdentifier();
208219
foreach ($authenticatorNames as $authenticatorName => $identifierName) {
209-
if (!$service->authenticators()->has($authenticatorName)) {
210-
Log::warning("Error saving user id $user->id password after rehashing: authenticator $authenticatorName not found. Check your Auth.PasswordRehash.authenticators configuration.");
211-
continue;
212-
}
213-
/**
214-
* @var \Authentication\Identifier\IdentifierCollection $identifierCollection
215-
*/
216-
$identifierCollection = $service->authenticators()->get($authenticatorName)->getIdentifier();
217220
if (!$identifierCollection->has($identifierName)) {
218-
Log::warning("Error saving user id $user->id password after rehashing: identifier $identifierName not found. Check your Auth.PasswordRehash.authenticators configuration.");
221+
Log::warning("Error saving user id $user->id password after rehashing: identifier $identifierName not found for authenticator $authenticatorName. Check your Auth.PasswordRehash.authenticators configuration.");
219222
continue;
220223
}
221224

src/Controller/Traits/LinkSocialTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public function callbackLinkSocial($alias = null)
7272
$userId = $identity['id'] ?? null;
7373
$user = $this->getUsersTable()->get($userId);
7474

75-
$this->getUsersTable()->linkSocialAccount($user, $data);
75+
/** @var \CakeDC\Users\Model\Behavior\LinkSocialBehavior $linkSocial */
76+
$linkSocial = $this->getUsersTable()->getBehavior('LinkSocial');
77+
$linkSocial->linkSocialAccount($user, $data);
7678

7779
if ($user->getErrors()) {
7880
$this->Flash->error($message);

src/Model/Behavior/OneTimeLoginLinkBehavior.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class OneTimeLoginLinkBehavior extends Behavior
2525
public function sendLoginLink(string $name): void
2626
{
2727
$table = $this->table();
28-
$user = $table->find('byUsernameOrEmail', ['username' => $name])->first();
28+
$user = $table->find('byUsernameOrEmail', username: $name)->first();
2929
if ($user === null) {
3030
throw new RecordNotFoundException(__('Username not found.'));
3131
}
@@ -65,7 +65,7 @@ public function loginWithToken(string $token): ?EntityInterface
6565
{
6666
$lifeTime = Configure::read('Auth.OneTimeLogin.tokenLifeTime', 600);
6767
$user = $this->table()
68-
->find('byOneTimeToken', ['token' => $token])
68+
->find('byOneTimeToken', token: $token)
6969
->first();
7070

7171
if ($user && ($user->login_token_date >= DateTime::now()->subSeconds($lifeTime))) {
@@ -89,7 +89,7 @@ public function loginWithToken(string $token): ?EntityInterface
8989
*/
9090
public function requestTokenSend(string $username): void
9191
{
92-
$user = $this->table()->find('byUsernameOrEmail', ['username' => $username])->first();
92+
$user = $this->table()->find('byUsernameOrEmail', username: $username)->first();
9393
if ($user) {
9494
$this->table()->updateAll([
9595
'token_send_requested' => true,
@@ -103,12 +103,11 @@ public function requestTokenSend(string $username): void
103103
* Find by username or email.
104104
*
105105
* @param \Cake\ORM\Query $query The query builder.
106-
* @param array $options Options.
106+
* @param string|null $username Username or email.
107107
* @return \Cake\ORM\Query
108108
*/
109-
public function findByUsernameOrEmail(Query $query, array $options = []): Query
109+
public function findByUsernameOrEmail(Query $query, ?string $username = null): Query
110110
{
111-
$username = $options['username'] ?? null;
112111
if (empty($username)) {
113112
throw new OutOfBoundsException('Missing username');
114113
}
@@ -125,12 +124,11 @@ public function findByUsernameOrEmail(Query $query, array $options = []): Query
125124
* Find by token
126125
*
127126
* @param \Cake\ORM\Query $query
128-
* @param array $options
127+
* @param string|null $token
129128
* @return \Cake\ORM\Query
130129
*/
131-
public function findByOneTimeToken(Query $query, array $options = []): Query
130+
public function findByOneTimeToken(Query $query, ?string $token = null): Query
132131
{
133-
$token = $options['token'] ?? null;
134132
if (empty($token)) {
135133
throw new OutOfBoundsException('Missing token');
136134
}

tests/TestCase/Controller/Traits/BaseTrait.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Authentication\Authenticator\Result;
1717
use Authentication\Controller\Component\AuthenticationComponent;
1818
use Authentication\Identifier\IdentifierCollection;
19-
use Authentication\Identifier\PasswordIdentifier;
2019
use Authentication\Identity;
2120
use Cake\Controller\ComponentRegistry;
2221
use Cake\Controller\Controller;
@@ -239,14 +238,7 @@ protected function _mockAuthLoggedIn($user = [])
239238
protected function _mockAuthentication($user = null, $failures = [], $identifiers = null)
240239
{
241240
if ($identifiers === null) {
242-
$passwordIdentifier = $this->getMockBuilder(PasswordIdentifier::class)
243-
->onlyMethods(['needsPasswordRehash'])
244-
->getMock();
245-
$passwordIdentifier->expects($this->any())
246-
->method('needsPasswordRehash')
247-
->willReturn(false);
248241
$identifiers = new IdentifierCollection([]);
249-
$identifiers->set('Password', $passwordIdentifier);
250242
}
251243

252244
$config = [

tests/TestCase/Controller/Traits/LinkSocialTraitTest.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function setUp(): void
5858
parent::setUp();
5959
$request = new ServerRequest();
6060
$this->Trait = $this->getMockBuilder('CakeDC\Users\Controller\UsersController')
61-
->onlyMethods(['dispatchEvent', 'redirect', 'set'])
61+
->onlyMethods(['dispatchEvent', 'redirect', 'set', 'getUsersTable'])
6262
->setConstructorArgs([new ServerRequest()])
6363
->getMock();
6464

@@ -142,11 +142,11 @@ public function testLinkSocialHappy()
142142

143143
$this->Provider->expects($this->any())
144144
->method('getState')
145-
->will($this->returnValue('_NEW_STATE_'));
145+
->willReturn('_NEW_STATE_');
146146

147147
$this->Provider->expects($this->any())
148148
->method('getAuthorizationUrl')
149-
->will($this->returnValue('http://facebook.com/redirect/url'));
149+
->willReturn('http://facebook.com/redirect/url');
150150

151151
$this->Trait->Flash->expects($this->never())
152152
->method('error');
@@ -157,7 +157,7 @@ public function testLinkSocialHappy()
157157
$this->Trait->expects($this->once())
158158
->method('redirect')
159159
->with($this->equalTo('http://facebook.com/redirect/url'))
160-
->will($this->returnValue(new Response()));
160+
->willReturn(new Response());
161161

162162
$this->Trait->linkSocial('facebook');
163163
}
@@ -226,14 +226,14 @@ public function testCallbackLinkSocialHappy()
226226
$this->equalTo('authorization_code'),
227227
$this->equalTo(['code' => 'ZPO9972j3092304230']),
228228
)
229-
->will($this->returnValue($Token));
229+
->willReturn($Token);
230230

231231
$this->Provider->expects($this->any())
232232
->method('getResourceOwner')
233233
->with(
234234
$this->equalTo($Token),
235235
)
236-
->will($this->returnValue($user));
236+
->willReturn($user);
237237

238238
$this->Trait = $this->getMockBuilder('CakeDC\Users\Controller\UsersController')
239239
->onlyMethods(['dispatchEvent', 'redirect', 'set', 'getUsersTable', 'log'])
@@ -258,7 +258,7 @@ public function testCallbackLinkSocialHappy()
258258

259259
$this->Trait->expects($this->any())
260260
->method('getUsersTable')
261-
->will($this->returnValue($Table));
261+
->willReturn($Table);
262262

263263
$this->_mockAuthLoggedIn();
264264
$this->_mockDispatchEvent(new Event('event'));
@@ -311,19 +311,26 @@ public function testCallbackLinkSocialWithValidationErrors()
311311
]);
312312
$Table = $this->getMockBuilder(\CakeDC\Users\Model\Table\UsersTable::class)
313313
->setConstructorArgs([['alias' => 'Users', 'connection' => \Cake\Datasource\ConnectionManager::get('test')]])
314-
->onlyMethods(['newEntity', 'get'])
315-
->addMethods(['linkSocialAccount'])
314+
->onlyMethods(['newEntity', 'get', 'getBehavior'])
315+
->getMock();
316+
$behavior = $this->getMockBuilder(\CakeDC\Users\Model\Behavior\LinkSocialBehavior::class)
317+
->disableOriginalConstructor()
318+
->onlyMethods(['linkSocialAccount'])
316319
->getMock();
317320

318321
$Table->setAlias('Users');
319322

320323
$Table->expects($this->once())
321324
->method('get')
322-
->will($this->returnValue($user));
325+
->willReturn($user);
323326

324327
$Table->expects($this->once())
328+
->method('getBehavior')
329+
->with('LinkSocial')
330+
->willReturn($behavior);
331+
$behavior->expects($this->once())
325332
->method('linkSocialAccount')
326-
->will($this->returnValue($user));
333+
->willReturn($user);
327334

328335
$Token = new \League\OAuth2\Client\Token\AccessToken([
329336
'access_token' => 'test-token',
@@ -377,14 +384,14 @@ public function testCallbackLinkSocialWithValidationErrors()
377384
$this->equalTo('authorization_code'),
378385
$this->equalTo(['code' => 'ZPO9972j3092304230']),
379386
)
380-
->will($this->returnValue($Token));
387+
->willReturn($Token);
381388

382389
$this->Provider->expects($this->any())
383390
->method('getResourceOwner')
384391
->with(
385392
$this->equalTo($Token),
386393
)
387-
->will($this->returnValue($user));
394+
->willReturn($user);
388395

389396
$this->Trait = $this->getMockBuilder('CakeDC\Users\Controller\UsersController')
390397
->onlyMethods(['dispatchEvent', 'redirect', 'set', 'getUsersTable', 'log'])
@@ -393,7 +400,7 @@ public function testCallbackLinkSocialWithValidationErrors()
393400

394401
$this->Trait->expects($this->any())
395402
->method('getUsersTable')
396-
->will($this->returnValue($Table));
403+
->willReturn($Table);
397404

398405
$this->Trait->setRequest(ServerRequestFactory::fromGlobals());
399406
$this->Trait->getRequest()->getSession()->write('oauth2state', '__TEST_STATE__');

tests/TestCase/Controller/Traits/ReCaptchaTraitTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public function testGetRecaptchaInstance()
110110
Configure::write('Users.reCaptcha.secret', 'secret');
111111
$trait = $this->getMockBuilder('CakeDC\Users\Controller\Traits\ReCaptchaTrait')->getMockForTrait();
112112
$method = new ReflectionMethod(get_class($trait), '_getReCaptchaInstance');
113-
$method->setAccessible(true);
114113
$method->invokeArgs($trait, []);
115114
$this->assertNotEmpty($method->invoke($trait));
116115
}
@@ -119,7 +118,6 @@ public function testGetRecaptchaInstanceNull()
119118
{
120119
$trait = $this->getMockBuilder('CakeDC\Users\Controller\Traits\ReCaptchaTrait')->getMockForTrait();
121120
$method = new ReflectionMethod(get_class($trait), '_getReCaptchaInstance');
122-
$method->setAccessible(true);
123121
$method->invokeArgs($trait, []);
124122
$this->assertNull($method->invoke($trait));
125123
}

tests/TestCase/Mailer/UsersMailerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ public function invokeMethod(&$object, $methodName, $parameters = [])
176176
{
177177
$reflection = new \ReflectionClass(get_class($object));
178178
$method = $reflection->getMethod($methodName);
179-
$method->setAccessible(true);
180179

181180
return $method->invokeArgs($object, $parameters);
182181
}

tests/TestCase/Model/Behavior/LinkSocialBehaviorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public static function providerFacebookLinkSocialAccountAccountExists()
304304

305305
return [
306306
'provider' => [
307-
'data' => [
307+
[
308308
'id' => 'reference-2-1',
309309
'username' => null,
310310
'full_name' => 'Full name',
@@ -332,8 +332,8 @@ public static function providerFacebookLinkSocialAccountAccountExists()
332332
'link' => 'facebook-link-15579',
333333
'provider' => 'Facebook',
334334
],
335-
'user' => '00000000-0000-0000-0000-000000000001',
336-
'result' => [
335+
'00000000-0000-0000-0000-000000000001',
336+
[
337337
'id' => '00000000-0000-0000-0000-000000000003',
338338
'provider' => 'Facebook',
339339
'username' => null,

tests/TestCase/Model/Behavior/OneTimeLoginLinkBehaviorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function testRequestTokenSend(): void
193193
public function testFindByUsernameOrEmailMissingUsername(): void
194194
{
195195
$this->expectException(OutOfBoundsException::class);
196-
$this->table->find('byUsernameOrEmail', [])->first();
196+
$this->table->find('byUsernameOrEmail')->first();
197197
}
198198

199199
/**
@@ -204,6 +204,6 @@ public function testFindByUsernameOrEmailMissingUsername(): void
204204
public function testFindByOneTimeTokenMissingToken(): void
205205
{
206206
$this->expectException(OutOfBoundsException::class);
207-
$this->table->find('byOneTimeToken', [])->first();
207+
$this->table->find('byOneTimeToken')->first();
208208
}
209209
}

tests/TestCase/Model/Behavior/PasswordBehaviorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ protected function _executeGetUser($reference)
292292
$realBehavior = $this->table->getBehavior('Password');
293293

294294
$method = new ReflectionMethod(get_class($realBehavior), '_getUser');
295-
$method->setAccessible(true);
296295

297296
return $method->invoke($realBehavior, $reference);
298297
}

0 commit comments

Comments
 (0)