From b9c288ac60227d394ba47a07339cce83a3ed51f9 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sun, 14 Dec 2025 12:08:08 +0100 Subject: [PATCH] fix deprecations --- .github/workflows/ci.yml | 2 +- .gitignore | 1 + Docs/Documentation/Events.md | 33 ++-- composer.json | 8 +- phpstan-baseline.neon | 146 +++++++----------- phpstan.neon | 15 +- psalm.xml | 8 + src/Command/Logic/CreateUserTrait.php | 5 +- src/Command/UsersPasswordEmailCommand.php | 8 +- src/Controller/Component/LoginComponent.php | 8 +- src/Controller/SocialAccountsController.php | 4 +- src/Controller/Traits/LinkSocialTrait.php | 4 +- src/Controller/Traits/LoginTrait.php | 6 +- .../Traits/PasswordManagementTrait.php | 8 +- src/Controller/Traits/RegisterTrait.php | 10 +- src/Controller/Traits/UserValidationTrait.php | 14 +- .../PasswordLockout/LockoutHandler.php | 4 +- src/Identifier/SocialIdentifier.php | 12 +- src/Model/Behavior/RegisterBehavior.php | 9 +- src/Model/Behavior/SocialBehavior.php | 9 +- src/Model/Entity/User.php | 2 +- .../Table/FailedPasswordAttemptsTable.php | 1 + src/Model/Table/SocialAccountsTable.php | 4 + src/Model/Table/UsersTable.php | 9 ++ src/Traits/RandomStringTrait.php | 2 +- src/{Plugin.php => UsersPlugin.php} | 2 +- .../Model/Behavior/RegisterBehaviorTest.php | 42 +++-- tests/TestCase/Model/Table/UsersTableTest.php | 25 +-- tests/TestCase/PluginTest.php | 14 +- tests/TestCase/View/Helper/UserHelperTest.php | 5 +- tests/bootstrap.php | 2 +- tests/test_app/TestApp/Application.php | 4 +- 32 files changed, 229 insertions(+), 197 deletions(-) rename src/{Plugin.php => UsersPlugin.php} (98%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 01d176768..788f994a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.1', '8.2', '8.3', '8.4'] + php-version: ['8.2', '8.3', '8.4'] db-type: [sqlite, mysql, pgsql] prefer-lowest: [''] diff --git a/.gitignore b/.gitignore index b4faf0fdb..20b9e23c9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ composer.lock .php_cs* /coverage .phpunit.result.cache +.phpunit.cache /config/Migrations/schema-dump-default.lock diff --git a/Docs/Documentation/Events.md b/Docs/Documentation/Events.md index 61fcee79d..19b20c483 100644 --- a/Docs/Documentation/Events.md +++ b/Docs/Documentation/Events.md @@ -25,6 +25,7 @@ logic will be performed if you don't assign an array to result, but if you set it we will use as redirect url. - Create or update file src/Event/UsersListener.php: + ```php 'beforeLogout', + \CakeDC\Users\UsersPlugin::EVENT_BEFORE_LOGOUT => 'beforeLogout', ]; } @@ -75,6 +76,7 @@ object and the controller object. You can also populate the new user entity or stop the register process. - Create or update file src/Event/UsersListener.php: + ```php 'beforeRegister', + \CakeDC\Users\UsersPlugin::EVENT_BEFORE_REGISTER => 'beforeRegister', ]; } @@ -136,6 +138,7 @@ have access to 'location' and 'request' keys in the event object and the controller object. - Create or update file src/Event/UsersListener.php: + ```php 'beforeSocialLoginRedirect', + \CakeDC\Users\UsersPlugin::EVENT_BEFORE_SOCIAL_LOGIN_REDIRECT => 'beforeSocialLoginRedirect', ]; } @@ -183,6 +186,7 @@ the social behavior object. You can also set a new user entity object as result. - Create or update file src/Event/UsersListener.php: + ```php 'beforeSocialLoginRedirect', + \CakeDC\Users\UsersPlugin::EVENT_BEFORE_SOCIAL_LOGIN_REDIRECT => 'beforeSocialLoginRedirect', ]; } @@ -229,6 +233,7 @@ have access to user data. You can also set an array as result to perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterLogin', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_LOGIN => 'afterLogin', ]; } @@ -281,6 +286,7 @@ have access to user data and the controller object. You can also set an array as result to perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterLogout', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_LOGOUT => 'afterLogout', ]; } @@ -332,6 +338,7 @@ set a custom http response as result to render a different content or perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterRegister', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_REGISTER => 'afterRegister', ]; } @@ -383,6 +390,7 @@ you have access to some user data and the controller object. You can also set an array as result to perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterChangePassword', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_CHANGE_PASSWORD => 'afterChangePassword', ]; } @@ -433,6 +441,7 @@ When adding a custom logic to execute after sending the token for user validation you can also set an array as result to perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterResendTokenValidation', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_RESEND_TOKEN_VALIDATION => 'afterResendTokenValidation', ]; } @@ -482,6 +491,7 @@ you have access to some user data and the controller object. You can also set an array as result to perform a custom redirect. - Create or update file src/Event/UsersListener.php: + ```php 'afterEmailTokenValidation', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_EMAIL_TOKEN_VALIDATION => 'afterEmailTokenValidation', ]; } @@ -530,6 +540,7 @@ I want to add custom logic after user email is validated to autologin user This is how you can autologin the user after email is validate: - Create or update file src/Event/UsersListener.php: + ```php 'afterEmailTokenValidation', + \CakeDC\Users\UsersPlugin::EVENT_AFTER_EMAIL_TOKEN_VALIDATION => 'afterEmailTokenValidation', ]; } diff --git a/composer.json b/composer.json index 592ee7445..88bb6b32f 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "prefer-stable": true, "require": { "php": ">=8.1", - "cakephp/cakephp": "^5.0", + "cakephp/cakephp": "5.3.0-RC2", "cakedc/auth": "^10.1", "cakephp/authorization": "^3.0", "cakephp/authentication": "^3.0" @@ -88,9 +88,9 @@ "test": "phpunit --stderr", "stan": "phpstan analyse src/", "psalm": "php vendor/psalm/phar/psalm.phar --show-info=false src/ ", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.9.0 psalm/phar:~5.1.0 && mv composer.backup composer.json", - "stan-rebuild-baseline": "phpstan analyse --configuration phpstan.neon --error-format baselineNeon src/ > phpstan-baseline.neon", - "psalm-rebuild-baseline": "php vendor/psalm/phar/psalm.phar --show-info=false --set-baseline=psalm-baseline.xml src/", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~2.1.0 psalm/phar:~6.14.0 && mv composer.backup composer.json", + "stan-rebuild-baseline": "phpstan analyse --generate-baseline", + "psalm-rebuild-baseline": "php vendor/psalm/phar/psalm.phar --show-info=false --set-baseline=psalm-baseline.xml", "rector": "rector process src/", "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.11.2 && mv composer.backup composer.json", "coverage-test": "phpunit --stderr --coverage-clover=clover.xml" diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 9accfb9f5..5c359f77d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,181 +1,151 @@ parameters: ignoreErrors: - - message: "#^Access to an undefined property Cake\\\\Controller\\\\Controller\\:\\:\\$Authentication\\.$#" + message: '#^Access to an undefined property Cake\\Controller\\Controller\:\:\$Authentication\.$#' + identifier: property.notFound count: 1 path: src/Controller/Component/LoginComponent.php - - message: "#^Call to an undefined method Cake\\\\Controller\\\\Controller\\:\\:getUsersTable\\(\\)\\.$#" + message: '#^Call to an undefined method Cake\\Controller\\Controller\:\:getUsersTable\(\)\.$#' + identifier: method.notFound count: 2 path: src/Controller/Component/LoginComponent.php - - message: "#^Access to an undefined property CakeDC\\\\Users\\\\Controller\\\\UsersController\\:\\:\\$OneTimePasswordAuthenticator\\.$#" - count: 3 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\Controller\\\\Component\\:\\:handleLogin\\(\\)\\.$#" - count: 3 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:changePassword\\(\\)\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:linkSocialAccount\\(\\)\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:register\\(\\)\\.$#" - count: 2 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:resetToken\\(\\)\\.$#" - count: 2 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:validate\\(\\)\\.$#" - count: 2 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:validationCurrentPassword\\(\\)\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:validationPasswordConfirm\\(\\)\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Parameter \\#2 \\$options of method Cake\\\\Controller\\\\Component\\\\FlashComponent\\:\\:error\\(\\) expects array, string given\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Parameter \\#2 \\$options of method Cake\\\\Controller\\\\Component\\\\FlashComponent\\:\\:success\\(\\) expects array, string given\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Parameter \\#2 \\$usersTable of class CakeDC\\\\Users\\\\Webauthn\\\\RegisterAdapter constructor expects CakeDC\\\\Users\\\\Model\\\\Table\\\\UsersTable\\|null, Cake\\\\ORM\\\\Table given\\.$#" - count: 1 - path: src/Controller/UsersController.php - - - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:socialLogin\\(\\)\\.$#" - count: 1 - path: src/Identifier/SocialIdentifier.php - - - - message: "#^Parameter \\#1 \\$request of method CakeDC\\\\Users\\\\Middleware\\\\SocialAuthMiddleware\\:\\:goNext\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#" + message: '#^Parameter \#1 \$request of method CakeDC\\Users\\Middleware\\SocialAuthMiddleware\:\:goNext\(\) expects Cake\\Http\\ServerRequest, Psr\\Http\\Message\\ServerRequestInterface given\.$#' + identifier: argument.type count: 1 path: src/Middleware/SocialAuthMiddleware.php - - message: "#^Parameter \\#2 \\$request of method CakeDC\\\\Users\\\\Utility\\\\UsersUrl\\:\\:checkActionOnRequest\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#" + message: '#^Parameter \#2 \$request of method CakeDC\\Users\\Utility\\UsersUrl\:\:checkActionOnRequest\(\) expects Cake\\Http\\ServerRequest, Psr\\Http\\Message\\ServerRequestInterface given\.$#' + identifier: argument.type count: 1 path: src/Middleware/SocialAuthMiddleware.php - - message: "#^Parameter \\#1 \\$request of method CakeDC\\\\Users\\\\Middleware\\\\SocialAuthMiddleware\\:\\:goNext\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#" + message: '#^Parameter \#1 \$request of method CakeDC\\Users\\Middleware\\SocialAuthMiddleware\:\:goNext\(\) expects Cake\\Http\\ServerRequest, Psr\\Http\\Message\\ServerRequestInterface given\.$#' + identifier: argument.type count: 1 path: src/Middleware/SocialEmailMiddleware.php - - message: "#^Parameter \\#2 \\$request of method CakeDC\\\\Users\\\\Utility\\\\UsersUrl\\:\\:checkActionOnRequest\\(\\) expects Cake\\\\Http\\\\ServerRequest, Psr\\\\Http\\\\Message\\\\ServerRequestInterface given\\.$#" + message: '#^Parameter \#2 \$request of method CakeDC\\Users\\Utility\\UsersUrl\:\:checkActionOnRequest\(\) expects Cake\\Http\\ServerRequest, Psr\\Http\\Message\\ServerRequestInterface given\.$#' + identifier: argument.type count: 1 path: src/Middleware/SocialEmailMiddleware.php - - message: "#^Call to an undefined method Cake\\\\Datasource\\\\EntityInterface\\:\\:updateToken\\(\\)\\.$#" + message: '#^Call to an undefined method Cake\\Datasource\\EntityInterface\:\:updateToken\(\)\.$#' + identifier: method.notFound count: 1 path: src/Model/Behavior/BaseTokenBehavior.php - - message: "#^Access to an undefined property Cake\\\\ORM\\\\Table\\:\\:\\$SocialAccounts\\.$#" + message: '#^Access to an undefined property Cake\\ORM\\Table\:\:\$SocialAccounts\.$#' + identifier: property.notFound count: 5 path: src/Model/Behavior/LinkSocialBehavior.php - - message: "#^Negated boolean expression is always false\\.$#" + message: '#^Negated boolean expression is always false\.$#' + identifier: booleanNot.alwaysFalse count: 1 path: src/Model/Behavior/LinkSocialBehavior.php - - message: "#^Method CakeDC\\\\Users\\\\Model\\\\Behavior\\\\PasswordBehavior\\:\\:resetToken\\(\\) should return string but returns Cake\\\\Datasource\\\\EntityInterface\\|false\\.$#" + message: '#^Method CakeDC\\Users\\Model\\Behavior\\PasswordBehavior\:\:resetToken\(\) should return string but returns Cake\\Datasource\\EntityInterface\|false\.$#' + identifier: return.type count: 1 path: src/Model/Behavior/PasswordBehavior.php - - message: "#^Access to an undefined property Cake\\\\Datasource\\\\EntityInterface\\:\\:\\$activation_date\\.$#" + message: '#^Unable to resolve the template type TEntity in call to method Cake\\ORM\\Table\\:\:save\(\)$#' + identifier: argument.templateType + count: 1 + path: src/Model/Behavior/PasswordBehavior.php + + - + message: '#^Access to an undefined property Cake\\Datasource\\EntityInterface\:\:\$activation_date\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Access to an undefined property Cake\\\\Datasource\\\\EntityInterface\\:\\:\\$active\\.$#" + message: '#^Access to an undefined property Cake\\Datasource\\EntityInterface\:\:\$active\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Access to an undefined property Cake\\\\Datasource\\\\EntityInterface\\:\\:\\$token_expires\\.$#" + message: '#^Access to an undefined property Cake\\Datasource\\EntityInterface\:\:\$token_expires\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Access to an undefined property Cake\\\\Datasource\\\\EntityInterface\\:\\:\\$validated\\.$#" + message: '#^Access to an undefined property Cake\\Datasource\\EntityInterface\:\:\$validated\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Access to an undefined property Cake\\\\ORM\\\\Table\\:\\:\\$isValidateEmail\\.$#" + message: '#^Access to an undefined property Cake\\ORM\\Table\:\:\$isValidateEmail\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Call to an undefined method Cake\\\\ORM\\\\Table\\:\\:validationRegister\\(\\)\\.$#" + message: '#^Call to an undefined method Cake\\ORM\\Table\:\:validationRegister\(\)\.$#' + identifier: method.notFound count: 1 path: src/Model/Behavior/RegisterBehavior.php - - message: "#^Access to an undefined property CakeDC\\\\Users\\\\Model\\\\Entity\\\\SocialAccount\\:\\:\\$active\\.$#" + message: '#^Access to an undefined property CakeDC\\Users\\Model\\Entity\\SocialAccount\:\:\$active\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/SocialAccountBehavior.php - - message: "#^Method CakeDC\\\\Users\\\\Model\\\\Behavior\\\\SocialAccountBehavior\\:\\:resendValidation\\(\\) should return CakeDC\\\\Users\\\\Model\\\\Entity\\\\User but returns array\\.$#" + message: '#^Method CakeDC\\Users\\Model\\Behavior\\SocialAccountBehavior\:\:resendValidation\(\) should return CakeDC\\Users\\Model\\Entity\\User but returns array\.$#' + identifier: return.type count: 1 path: src/Model/Behavior/SocialAccountBehavior.php - - message: "#^Method CakeDC\\\\Users\\\\Model\\\\Behavior\\\\SocialAccountBehavior\\:\\:validateAccount\\(\\) should return CakeDC\\\\Users\\\\Model\\\\Entity\\\\User but returns Cake\\\\Datasource\\\\EntityInterface\\.$#" + message: '#^Method CakeDC\\Users\\Model\\Behavior\\SocialAccountBehavior\:\:validateAccount\(\) should return CakeDC\\Users\\Model\\Entity\\User but returns Cake\\Datasource\\EntityInterface\.$#' + identifier: return.type count: 1 path: src/Model/Behavior/SocialAccountBehavior.php - - message: "#^Access to an undefined property Cake\\\\ORM\\\\Table\\:\\:\\$SocialAccounts\\.$#" + message: '#^Access to an undefined property Cake\\ORM\\Table\:\:\$SocialAccounts\.$#' + identifier: property.notFound count: 4 path: src/Model/Behavior/SocialBehavior.php - - message: "#^Access to an undefined property Cake\\\\ORM\\\\Table\\:\\:\\$isValidateEmail\\.$#" + message: '#^Access to an undefined property Cake\\ORM\\Table\:\:\$isValidateEmail\.$#' + identifier: property.notFound count: 1 path: src/Model/Behavior/SocialBehavior.php - - message: "#^Parameter \\#3 \\$tokenExpiration of method CakeDC\\\\Users\\\\Model\\\\Behavior\\\\BaseTokenBehavior\\:\\:_updateActive\\(\\) expects int, string given\\.$#" + message: '#^Parameter \#3 \$tokenExpiration of method CakeDC\\Users\\Model\\Behavior\\BaseTokenBehavior\:\:_updateActive\(\) expects int, string given\.$#' + identifier: argument.type count: 1 path: src/Model/Behavior/SocialBehavior.php - - message: "#^Method CakeDC\\\\Users\\\\Model\\\\Entity\\\\User\\:\\:_setTos\\(\\) should return bool but returns string\\.$#" + message: '#^Method CakeDC\\Users\\Model\\Entity\\User\:\:_setTos\(\) should return bool but returns string\.$#' + identifier: return.type count: 1 path: src/Model/Entity/User.php + + - + message: '#^PHPDoc tag @property for property CakeDC\\Users\\Model\\Table\\FailedPasswordAttemptsTable\:\:\$Users contains unresolvable type\.$#' + identifier: propertyTag.unresolvableType + count: 1 + path: src/Model/Table/FailedPasswordAttemptsTable.php diff --git a/phpstan.neon b/phpstan.neon index f6dbf5f0b..ba20c2527 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,14 +3,15 @@ includes: parameters: level: 6 - checkMissingIterableValueType: false - checkGenericClassInNonGenericObjectType: false bootstrapFiles: - tests/bootstrap.php - excludes_analyse: + paths: + - ./src + excludePaths: - src/Controller/SocialAccountsController.php - - src/Controller/UsersAccountsController.php - - src/Controller/AppAccountsController.php + - src/Controller/UsersController.php + - src/Controller/AppController.php ignoreErrors: - -services: + - identifier: missingType.generics + - identifier: missingType.iterableValue + - identifier: trait.unused diff --git a/psalm.xml b/psalm.xml index 1de17a837..8d835bd55 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,19 +1,27 @@ + + + + diff --git a/src/Command/Logic/CreateUserTrait.php b/src/Command/Logic/CreateUserTrait.php index a1ad1739d..4137df4f5 100644 --- a/src/Command/Logic/CreateUserTrait.php +++ b/src/Command/Logic/CreateUserTrait.php @@ -71,8 +71,11 @@ protected function _createUser(Arguments $args, ConsoleIo $io, $template) $email = $email ?: $username . '@example.com'; $role = $role ?: $template['role']; + /** @var \CakeDC\Users\Model\Behavior\SocialBehavior $socialBehavior */ + $socialBehavior = $UsersTable->getBehavior('Social'); + $user = [ - 'username' => $UsersTable->generateUniqueUsername($username), + 'username' => $socialBehavior->generateUniqueUsername($username), 'email' => $email, 'password' => $password, 'active' => 1, diff --git a/src/Command/UsersPasswordEmailCommand.php b/src/Command/UsersPasswordEmailCommand.php index 3465094b6..35d27f5c4 100644 --- a/src/Command/UsersPasswordEmailCommand.php +++ b/src/Command/UsersPasswordEmailCommand.php @@ -42,11 +42,11 @@ public function execute(Arguments $args, ConsoleIo $io) if (empty($reference)) { $io->abort(__d('cake_d_c/users', 'Please enter a username or email.')); } - /** - * @var \CakeDC\Users\Model\Table\UsersTable $UsersTable - */ + /** @var \CakeDC\Users\Model\Table\UsersTable $UsersTable */ $UsersTable = $this->getTableLocator()->get('Users'); - $resetUser = $UsersTable->resetToken($reference, [ + /** @var \CakeDC\Users\Model\Behavior\PasswordBehavior $passwordBehavior */ + $passwordBehavior = $UsersTable->getBehavior('Password'); + $resetUser = $passwordBehavior->resetToken($reference, [ 'expiration' => Configure::read('Users.Token.expiration'), 'checkActive' => false, 'sendEmail' => true, diff --git a/src/Controller/Component/LoginComponent.php b/src/Controller/Component/LoginComponent.php index dcc67c802..0807db862 100644 --- a/src/Controller/Component/LoginComponent.php +++ b/src/Controller/Component/LoginComponent.php @@ -20,7 +20,7 @@ use Cake\Log\Log; use CakeDC\Auth\Authentication\AuthenticationService; use CakeDC\Auth\Traits\IsAuthorizedTrait; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; use CakeDC\Users\Utility\UsersUrl; use Laminas\Diactoros\Uri; @@ -64,7 +64,7 @@ public function handleLogin($errorOnlyPost, $redirectFailure) if (!$service) { throw new \UnexpectedValueException('Authentication service not found in this request'); } - $eventBefore = $this->getController()->dispatchEvent(Plugin::EVENT_BEFORE_LOGIN, []); + $eventBefore = $this->getController()->dispatchEvent(UsersPlugin::EVENT_BEFORE_LOGIN, []); if (is_array($eventBefore->getResult())) { return $this->getController()->redirect($eventBefore->getResult()); } @@ -79,7 +79,7 @@ public function handleLogin($errorOnlyPost, $redirectFailure) return $this->afterIdentifyUser($user); } if ($request->is('post') || $errorOnlyPost === false) { - $this->getController()->dispatchEvent(Plugin::EVENT_AFTER_LOGIN_FAILURE, ['result' => $result]); + $this->getController()->dispatchEvent(UsersPlugin::EVENT_AFTER_LOGIN_FAILURE, ['result' => $result]); return $this->handleFailure($redirectFailure); } @@ -153,7 +153,7 @@ public function getErrorMessage(?ResultInterface $result = null) */ protected function afterIdentifyUser($user) { - $event = $this->getController()->dispatchEvent(Plugin::EVENT_AFTER_LOGIN, ['user' => $user]); + $event = $this->getController()->dispatchEvent(UsersPlugin::EVENT_AFTER_LOGIN, ['user' => $user]); if (is_array($event->getResult())) { // in this case we don't checkSafeHost the url as the url params are generated by an event return $this->getController()->redirect($event->getResult()); diff --git a/src/Controller/SocialAccountsController.php b/src/Controller/SocialAccountsController.php index e69a14869..eb77969d5 100644 --- a/src/Controller/SocialAccountsController.php +++ b/src/Controller/SocialAccountsController.php @@ -45,7 +45,7 @@ public function initialize(): void public function validateAccount($provider, $reference, $token) { try { - $result = $this->SocialAccounts->validateAccount($provider, $reference, $token); + $result = $this->SocialAccounts->getBehavior('SocialAccount')->validateAccount($provider, $reference, $token); if ($result) { $this->Flash->success(__d('cake_d_c/users', 'Account validated successfully')); } else { @@ -73,7 +73,7 @@ public function validateAccount($provider, $reference, $token) public function resendValidation($provider, $reference) { try { - $result = $this->SocialAccounts->resendValidation($provider, $reference); + $result = $this->SocialAccounts->getBehavior('SocialAccount')->resendValidation($provider, $reference); if ($result) { $this->Flash->success(__d('cake_d_c/users', 'Email sent successfully')); } else { diff --git a/src/Controller/Traits/LinkSocialTrait.php b/src/Controller/Traits/LinkSocialTrait.php index e32d2d7c6..6fdf8f5af 100644 --- a/src/Controller/Traits/LinkSocialTrait.php +++ b/src/Controller/Traits/LinkSocialTrait.php @@ -15,7 +15,7 @@ use CakeDC\Auth\Social\MapUser; use CakeDC\Auth\Social\Service\ServiceFactory; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; /** * Actions to allow user to link social accounts @@ -36,7 +36,7 @@ public function linkSocial($alias = null) ->createFromProvider($alias) ->getAuthorizationUrl($this->getRequest()); - $this->dispatchEvent(Plugin::EVENT_BEFORE_SOCIAL_LOGIN_REDIRECT, [ + $this->dispatchEvent(UsersPlugin::EVENT_BEFORE_SOCIAL_LOGIN_REDIRECT, [ 'location' => $authUrl, 'request' => $this->request, ]); diff --git a/src/Controller/Traits/LoginTrait.php b/src/Controller/Traits/LoginTrait.php index 88d51044f..88ce7f5ed 100644 --- a/src/Controller/Traits/LoginTrait.php +++ b/src/Controller/Traits/LoginTrait.php @@ -15,7 +15,7 @@ use CakeDC\Auth\Authentication\AuthenticationService; use CakeDC\Users\Loader\LoginComponentLoader; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; /** * Covers the login, logout and social login @@ -64,7 +64,7 @@ public function logout() $user = $this->getRequest()->getAttribute('identity'); $user = $user ?? []; - $eventBefore = $this->dispatchEvent(Plugin::EVENT_BEFORE_LOGOUT, ['user' => $user]); + $eventBefore = $this->dispatchEvent(UsersPlugin::EVENT_BEFORE_LOGOUT, ['user' => $user]); if (is_array($eventBefore->getResult())) { return $this->redirect($eventBefore->getResult()); } @@ -72,7 +72,7 @@ public function logout() $this->getRequest()->getSession()->destroy(); $this->Flash->success(__d('cake_d_c/users', 'You\'ve successfully logged out')); - $eventAfter = $this->dispatchEvent(Plugin::EVENT_AFTER_LOGOUT, ['user' => $user]); + $eventAfter = $this->dispatchEvent(UsersPlugin::EVENT_AFTER_LOGOUT, ['user' => $user]); if (is_array($eventAfter->getResult())) { return $this->redirect($eventAfter->getResult()); } diff --git a/src/Controller/Traits/PasswordManagementTrait.php b/src/Controller/Traits/PasswordManagementTrait.php index 3f712e489..42a01645b 100644 --- a/src/Controller/Traits/PasswordManagementTrait.php +++ b/src/Controller/Traits/PasswordManagementTrait.php @@ -19,7 +19,7 @@ use CakeDC\Users\Exception\UserNotActiveException; use CakeDC\Users\Exception\UserNotFoundException; use CakeDC\Users\Exception\WrongPasswordException; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; use Exception; /** @@ -108,9 +108,9 @@ public function changePassword($id = null) if ($user->getErrors()) { $this->Flash->error(__d('cake_d_c/users', 'Password could not be changed')); } else { - $result = $this->getUsersTable()->changePassword($user); + $result = $this->getUsersTable()->getBehavior('Password')->changePassword($user); if ($result) { - $event = $this->dispatchEvent(Plugin::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $result]); + $event = $this->dispatchEvent(UsersPlugin::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $result]); $eventResult = $event->getResult(); if (!empty($eventResult) && is_array($eventResult)) { return $this->redirect($event->getResult()); @@ -161,7 +161,7 @@ public function requestResetPassword() $reference = $this->getRequest()->getData('reference'); try { - $resetUser = $this->getUsersTable()->resetToken($reference, [ + $resetUser = $this->getUsersTable()->getBehavior('Password')->resetToken($reference, [ 'expiration' => Configure::read('Users.Token.expiration'), 'checkActive' => false, 'sendEmail' => true, diff --git a/src/Controller/Traits/RegisterTrait.php b/src/Controller/Traits/RegisterTrait.php index c6b0a1480..e3f76cc27 100644 --- a/src/Controller/Traits/RegisterTrait.php +++ b/src/Controller/Traits/RegisterTrait.php @@ -17,7 +17,7 @@ use Cake\Datasource\EntityInterface; use Cake\Http\Exception\NotFoundException; use Cake\Http\Response; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; /** * Covers registration features and email token validation @@ -60,7 +60,7 @@ public function register() 'use_tos' => $useTos, ]; $requestData = $this->getRequest()->getData(); - $event = $this->dispatchEvent(Plugin::EVENT_BEFORE_REGISTER, [ + $event = $this->dispatchEvent(UsersPlugin::EVENT_BEFORE_REGISTER, [ 'usersTable' => $usersTable, 'options' => $options, 'userEntity' => $user, @@ -70,7 +70,7 @@ public function register() if ($result instanceof EntityInterface) { $data = $result->toArray(); $data['password'] = $requestData['password'] ?? null; //since password is a hidden property - $userSaved = $usersTable->register($user, $data, $options); + $userSaved = $usersTable->getBehavior('Register')->register($user, $data, $options); $errors = \collection($user->getErrors())->unfold()->toArray(); if ($userSaved) { return $this->_afterRegister($userSaved); @@ -105,7 +105,7 @@ public function register() return; } - $userSaved = $usersTable->register($user, $requestData, $options); + $userSaved = $usersTable->getBehavior('Register')->register($user, $requestData, $options); $errors = \collection($user->getErrors())->unfold()->toArray(); if (!$userSaved && Configure::read('Users.Registration.showVerboseError') && count($errors) > 0) { foreach ($errors as $error) { @@ -152,7 +152,7 @@ protected function _afterRegister(EntityInterface $userSaved) if ($validateEmail) { $message = __d('cake_d_c/users', 'Please validate your account before log in'); } - $event = $this->dispatchEvent(Plugin::EVENT_AFTER_REGISTER, [ + $event = $this->dispatchEvent(UsersPlugin::EVENT_AFTER_REGISTER, [ 'user' => $userSaved, ]); $result = $event->getResult(); diff --git a/src/Controller/Traits/UserValidationTrait.php b/src/Controller/Traits/UserValidationTrait.php index 66cd0f704..cb5c996de 100644 --- a/src/Controller/Traits/UserValidationTrait.php +++ b/src/Controller/Traits/UserValidationTrait.php @@ -17,7 +17,7 @@ use CakeDC\Users\Exception\TokenExpiredException; use CakeDC\Users\Exception\UserAlreadyActiveException; use CakeDC\Users\Exception\UserNotFoundException; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; use CakeDC\Users\Utility\UsersUrl; use Exception; @@ -41,10 +41,10 @@ public function validate($type = null, $token = null) switch ($type) { case 'email': try { - $result = $this->getUsersTable()->validate($token, 'activateUser'); + $result = $this->getUsersTable()->getBehavior('Register')->validate($token, 'activateUser'); if ($result) { $event = $this->dispatchEvent( - Plugin::EVENT_AFTER_EMAIL_TOKEN_VALIDATION, + UsersPlugin::EVENT_AFTER_EMAIL_TOKEN_VALIDATION, ['user' => $result], ); $eventResult = $event->getResult(); @@ -60,7 +60,7 @@ public function validate($type = null, $token = null) } break; case 'password': - $result = $this->getUsersTable()->validate($token); + $result = $this->getUsersTable()->getBehavior('Register')->validate($token); if (!empty($result)) { $this->Flash->success(__d('cake_d_c/users', 'Reset password token was validated successfully')); $this->getRequest()->getSession()->write( @@ -79,7 +79,7 @@ public function validate($type = null, $token = null) } catch (UserNotFoundException $ex) { $this->Flash->error(__d('cake_d_c/users', 'Invalid token or user account already validated')); } catch (TokenExpiredException $ex) { - $event = $this->dispatchEvent(Plugin::EVENT_ON_EXPIRED_TOKEN, ['type' => $type]); + $event = $this->dispatchEvent(UsersPlugin::EVENT_ON_EXPIRED_TOKEN, ['type' => $type]); $eventResult = $event->getResult(); if (!empty($eventResult) && is_array($eventResult)) { return $this->redirect($eventResult); @@ -105,14 +105,14 @@ public function resendTokenValidation() $reference = $this->getRequest()->getData('reference'); try { if ( - $this->getUsersTable()->resetToken($reference, [ + $this->getUsersTable()->getBehavior('Password')->resetToken($reference, [ 'expiration' => Configure::read('Users.Token.expiration'), 'checkActive' => true, 'sendEmail' => true, 'type' => 'email', ]) ) { - $event = $this->dispatchEvent(Plugin::EVENT_AFTER_RESEND_TOKEN_VALIDATION); + $event = $this->dispatchEvent(UsersPlugin::EVENT_AFTER_RESEND_TOKEN_VALIDATION); $result = $event->getResult(); if (!empty($result) && is_array($result)) { return $this->redirect($result); diff --git a/src/Identifier/PasswordLockout/LockoutHandler.php b/src/Identifier/PasswordLockout/LockoutHandler.php index d63084cfd..275c97228 100644 --- a/src/Identifier/PasswordLockout/LockoutHandler.php +++ b/src/Identifier/PasswordLockout/LockoutHandler.php @@ -89,7 +89,7 @@ public function newFail(string|int $id): void $Table = $this->getTable(); $entity = $Table->newEntity([$this->getConfig('userForeignKeyField') => $id]); $Table->saveOrFail($entity); - $Table->deleteAll($Table->query()->newExpr()->lt('created', $timeWindow)); + $Table->deleteAll($Table->query()->expr()->lt('created', $timeWindow)); } /** @@ -137,7 +137,7 @@ protected function getAttemptsQuery(int|string $id, DateTime $timeWindow): Selec return $query ->where([ $this->getConfig('userForeignKeyField') => $id, - $query->newExpr()->gte('created', $timeWindow), + $query->expr()->gte('created', $timeWindow), ]) ->orderByDesc('created'); } diff --git a/src/Identifier/SocialIdentifier.php b/src/Identifier/SocialIdentifier.php index ab8028b63..7565d7666 100644 --- a/src/Identifier/SocialIdentifier.php +++ b/src/Identifier/SocialIdentifier.php @@ -17,10 +17,13 @@ use Cake\Core\Configure; use Cake\Event\EventDispatcherTrait; use Cake\ORM\Locator\LocatorAwareTrait; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; class SocialIdentifier extends AbstractIdentifier { + /** + * @use \Cake\Event\EventDispatcherTrait<\CakeDC\Users\Identifier\SocialIdentifier> + */ use EventDispatcherTrait; use LocatorAwareTrait; @@ -56,7 +59,7 @@ public function identify(array $credentials): \ArrayAccess|array|null } if ($user->get('social_accounts')) { - $this->dispatchEvent(Plugin::EVENT_AFTER_REGISTER, ['user' => $user]); + $this->dispatchEvent(UsersPlugin::EVENT_AFTER_REGISTER, ['user' => $user]); } return $this->findUser($user)->firstOrFail(); @@ -97,7 +100,10 @@ protected function createOrGetUser($data) 'token_expiration' => Configure::read('Users.Token.expiration'), ]; - return $this->getUsersTable()->socialLogin($data, $options); + /** @var \CakeDC\Users\Model\Behavior\SocialBehavior $socialBehavior */ + $socialBehavior = $this->getUsersTable()->getBehavior('Social'); + + return $socialBehavior->socialLogin($data, $options); } /** diff --git a/src/Model/Behavior/RegisterBehavior.php b/src/Model/Behavior/RegisterBehavior.php index 3e8b464cd..014eff9d7 100644 --- a/src/Model/Behavior/RegisterBehavior.php +++ b/src/Model/Behavior/RegisterBehavior.php @@ -113,7 +113,7 @@ public function validate($token, $callback = null) return $user; } - return $this->_table->{$callback}($user); + return $this->_table->getBehavior('Register')->{$callback}($user); } /** @@ -141,15 +141,14 @@ public function activateUser(EntityInterface $user) * @param \Cake\Event\Event $event event * @param \Cake\Validation\Validator $validator validator * @param string $name name - * @return \Cake\Validation\Validator */ - public function buildValidator(\Cake\Event\EventInterface $event, Validator $validator, $name) + public function buildValidator(\Cake\Event\EventInterface $event, Validator $validator, $name): void { if ($name === 'default') { - return $this->_emailValidator($validator, $this->validateEmail); + $validator = $this->_emailValidator($validator, $this->validateEmail); } - return $validator; + $event->setResult($validator); } /** diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index e6f953d5a..31ccf4407 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -21,8 +21,8 @@ use CakeDC\Users\Exception\AccountNotActiveException; use CakeDC\Users\Exception\MissingEmailException; use CakeDC\Users\Exception\UserNotActiveException; -use CakeDC\Users\Plugin; use CakeDC\Users\Traits\RandomStringTrait; +use CakeDC\Users\UsersPlugin; use DateTime; use InvalidArgumentException; @@ -31,6 +31,9 @@ */ class SocialBehavior extends BaseTokenBehavior { + /** + * @use \Cake\Event\EventDispatcherTrait<\CakeDC\Users\Model\Behavior\SocialBehavior> + */ use EventDispatcherTrait; use RandomStringTrait; @@ -99,7 +102,7 @@ public function socialLogin(array $data, array $options) $accountData = $this->extractAccountData($data); $this->_table->SocialAccounts->patchEntity($existingAccount, $accountData); $this->_table->SocialAccounts->save($existingAccount); - $event = $this->dispatchEvent(Plugin::EVENT_SOCIAL_LOGIN_EXISTING_ACCOUNT, [ + $event = $this->dispatchEvent(UsersPlugin::EVENT_SOCIAL_LOGIN_EXISTING_ACCOUNT, [ 'userEntity' => $user, 'data' => $data, ]); @@ -148,7 +151,7 @@ protected function _createSocialUser($data, $options = []) $user = $this->_populateUser($data, $existingUser, $useEmail, $validateEmail, $tokenExpiration); - $event = $this->dispatchEvent(Plugin::EVENT_BEFORE_SOCIAL_LOGIN_USER_CREATE, [ + $event = $this->dispatchEvent(UsersPlugin::EVENT_BEFORE_SOCIAL_LOGIN_USER_CREATE, [ 'userEntity' => $user, 'data' => $data, ]); diff --git a/src/Model/Entity/User.php b/src/Model/Entity/User.php index 99d4624f3..3dd58f941 100644 --- a/src/Model/Entity/User.php +++ b/src/Model/Entity/User.php @@ -36,7 +36,7 @@ * @property string $password * @property string $password_confirm * @property string $login_token - * @property \CakeDC\Users\Model\Entity\FrozenDate $login_token_date + * @property \Cake\I18n\DateTime $login_token_date * @property \Cake\I18n\DateTime $lockout_time */ class User extends Entity diff --git a/src/Model/Table/FailedPasswordAttemptsTable.php b/src/Model/Table/FailedPasswordAttemptsTable.php index 911247d77..d6339107f 100644 --- a/src/Model/Table/FailedPasswordAttemptsTable.php +++ b/src/Model/Table/FailedPasswordAttemptsTable.php @@ -25,6 +25,7 @@ * @method iterable<\CakeDC\Users\Model\Entity\FailedPasswordAttempt>|\Cake\Datasource\ResultSetInterface<\CakeDC\Users\Model\Entity\FailedPasswordAttempt>|false deleteMany(iterable $entities, array $options = []) * @method iterable<\CakeDC\Users\Model\Entity\FailedPasswordAttempt>|\Cake\Datasource\ResultSetInterface<\CakeDC\Users\Model\Entity\FailedPasswordAttempt> deleteManyOrFail(iterable $entities, array $options = []) * @mixin \Cake\ORM\Behavior\TimestampBehavior + * @extends \Cake\ORM\Table */ class FailedPasswordAttemptsTable extends Table { diff --git a/src/Model/Table/SocialAccountsTable.php b/src/Model/Table/SocialAccountsTable.php index 41ee74b88..0975372fe 100644 --- a/src/Model/Table/SocialAccountsTable.php +++ b/src/Model/Table/SocialAccountsTable.php @@ -22,6 +22,10 @@ * SocialAccounts Model * * @mixin \CakeDC\Users\Model\Behavior\SocialAccountBehavior + * @extends \Cake\ORM\Table */ class SocialAccountsTable extends Table { diff --git a/src/Model/Table/UsersTable.php b/src/Model/Table/UsersTable.php index bc346e92a..400886c88 100644 --- a/src/Model/Table/UsersTable.php +++ b/src/Model/Table/UsersTable.php @@ -37,6 +37,15 @@ * @mixin \CakeDC\Users\Model\Behavior\SocialBehavior * @mixin \CakeDC\Users\Model\Behavior\OneTimeLoginLinkBehavior * @property \CakeDC\Users\Model\Table\SocialAccountsTable $SocialAccounts + * @extends \Cake\ORM\Table */ class UsersTable extends Table { diff --git a/src/Traits/RandomStringTrait.php b/src/Traits/RandomStringTrait.php index 6caffad7b..8a8c687d4 100644 --- a/src/Traits/RandomStringTrait.php +++ b/src/Traits/RandomStringTrait.php @@ -18,7 +18,7 @@ trait RandomStringTrait /** * Generates random string * - * @param int $length String size. + * @param int|string $length String size. * @return string */ public function randomString($length = 10) diff --git a/src/Plugin.php b/src/UsersPlugin.php similarity index 98% rename from src/Plugin.php rename to src/UsersPlugin.php index 55da12afd..5c0f46b46 100644 --- a/src/Plugin.php +++ b/src/UsersPlugin.php @@ -21,7 +21,7 @@ use CakeDC\Users\Provider\AuthorizationServiceProvider; use CakeDC\Users\Provider\ServiceProviderLoaderTrait; -class Plugin extends BasePlugin +class UsersPlugin extends BasePlugin { use ServiceProviderLoaderTrait; diff --git a/tests/TestCase/Model/Behavior/RegisterBehaviorTest.php b/tests/TestCase/Model/Behavior/RegisterBehaviorTest.php index 8cc3b1b96..c29c08982 100644 --- a/tests/TestCase/Model/Behavior/RegisterBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/RegisterBehaviorTest.php @@ -95,7 +95,8 @@ public function testValidateRegisterNoValidateEmail() 'last_name' => 'user', 'tos' => 1, ]; - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 0]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 0]); $this->assertTrue($result->active); } @@ -107,7 +108,8 @@ public function testValidateRegisterNoValidateEmail() public function testValidateRegisterEmptyUser() { $user = []; - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); $this->assertFalse($result); } @@ -134,7 +136,8 @@ public function testValidateRegisterValidateEmailAndTos() 'last_name' => 'user', 'tos' => 1, ]; - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); $this->assertNotEmpty($result); $this->assertFalse($result->active); $this->assertNotEmpty($result->tos_date); @@ -208,7 +211,8 @@ public function testValidateRegisterTosRequired() 'first_name' => 'test', 'last_name' => 'user', ]; - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 1]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 1]); $this->assertFalse($result); } @@ -234,7 +238,8 @@ public function testValidateRegisterNoTosRequired() 'first_name' => 'test', 'last_name' => 'user', ]; - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 0]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 0]); $this->assertNotEmpty($result); } @@ -246,7 +251,7 @@ public function testValidateRegisterNoTosRequired() public function testActivateUser() { $user = $this->Table->find()->where(['id' => '00000000-0000-0000-0000-000000000001'])->first(); - $result = $this->Table->activateUser($user); + $result = $this->Table->getBehavior('Register')->activateUser($user); $this->assertTrue($result->active); } @@ -257,7 +262,8 @@ public function testActivateUser() */ public function testValidate() { - $result = $this->Table->validate('ae93ddbe32664ce7927cf0c5c5a5e59d', 'activateUser'); + $result = $this->Table->getBehavior('Register') + ->validate('ae93ddbe32664ce7927cf0c5c5a5e59d', 'activateUser'); $this->assertTrue($result->active); $this->assertEmpty($result->token_expires); } @@ -270,7 +276,7 @@ public function testValidate() public function testValidateUserWithExpiredToken() { $this->expectException(TokenExpiredException::class); - $this->Table->validate('token-5', 'activateUser'); + $this->Table->getBehavior('Register')->validate('token-5', 'activateUser'); } /** @@ -281,7 +287,7 @@ public function testValidateUserWithExpiredToken() public function testValidateNotExistingUser() { $this->expectException(UserNotFoundException::class); - $this->Table->validate('not-existing-token', 'activateUser'); + $this->Table->getBehavior('Register')->validate('not-existing-token', 'activateUser'); } /** @@ -319,10 +325,11 @@ public function testRegisterUsingDefaultRole() 'tos' => 1, ]; Configure::write('Users.Registration.defaultRole', false); - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, [ - 'token_expiration' => 3600, - 'validate_email' => 0, - ]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, [ + 'token_expiration' => 3600, + 'validate_email' => 0, + ]); $this->assertSame('user', $result['role']); } @@ -343,10 +350,11 @@ public function testRegisterUsingCustomRole() 'tos' => 1, ]; Configure::write('Users.Registration.defaultRole', 'emperor'); - $result = $this->Table->register($this->Table->newEmptyEntity(), $user, [ - 'token_expiration' => 3600, - 'validate_email' => 0, - ]); + $result = $this->Table->getBehavior('Register') + ->register($this->Table->newEmptyEntity(), $user, [ + 'token_expiration' => 3600, + 'validate_email' => 0, + ]); $this->assertSame('emperor', $result['role']); } } diff --git a/tests/TestCase/Model/Table/UsersTableTest.php b/tests/TestCase/Model/Table/UsersTableTest.php index 4d731996c..7005aa2f5 100644 --- a/tests/TestCase/Model/Table/UsersTableTest.php +++ b/tests/TestCase/Model/Table/UsersTableTest.php @@ -89,7 +89,8 @@ public function testValidateRegisterNoValidateEmail() 'last_name' => 'user', 'tos' => 1, ]; - $result = $this->Users->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 0]); + $result = $this->Users->getBehavior('Register') + ->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 0]); $this->assertTrue($result->active); } @@ -101,7 +102,8 @@ public function testValidateRegisterNoValidateEmail() public function testValidateRegisterEmptyUser() { $user = []; - $result = $this->Users->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); + $result = $this->Users->getBehavior('Register') + ->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); $this->assertFalse($result); } @@ -128,7 +130,8 @@ public function testValidateRegisterValidateEmail() 'last_name' => 'user', 'tos' => 1, ]; - $result = $this->Users->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); + $result = $this->Users->getBehavior('Register') + ->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]); $this->assertNotEmpty($result); $this->assertFalse($result->active); } @@ -147,7 +150,8 @@ public function testValidateRegisterTosRequired() 'last_name' => 'user', ]; $userEntity = $this->Users->newEmptyEntity(); - $this->Users->register($userEntity, $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 1]); + $this->Users->getBehavior('Register') + ->register($userEntity, $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 1]); $this->assertEquals(['tos' => ['_required' => 'This field is required']], $userEntity->getErrors()); } @@ -172,7 +176,8 @@ public function testValidateRegisterNoTosRequired() 'first_name' => 'test', 'last_name' => 'user', ]; - $result = $this->Users->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 0]); + $result = $this->Users->getBehavior('Register') + ->register($this->Users->newEmptyEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1, 'use_tos' => 0]); $this->assertNotEmpty($result); } @@ -184,7 +189,7 @@ public function testValidateRegisterNoTosRequired() public function testActivateUser() { $user = $this->Users->find()->where(['id' => '00000000-0000-0000-0000-000000000001'])->first(); - $result = $this->Users->activateUser($user); + $result = $this->Users->getBehavior('Register')->activateUser($user); $this->assertTrue($result->active); } @@ -210,7 +215,7 @@ public function testSocialLogin() 'validate_email' => 1, 'token_expiration' => 3600, ]; - $result = $this->Users->socialLogin($data, $options); + $result = $this->Users->getBehavior('Social')->socialLogin($data, $options); $this->assertEquals('user-2@test.com', $result->email); $this->assertTrue($result->active); } @@ -239,7 +244,7 @@ public function testSocialLoginInactiveAccount() 'validate_email' => 1, 'token_expiration' => 3600, ]; - $result = $this->Users->socialLogin($data, $options); + $result = $this->Users->getBehavior('Social')->socialLogin($data, $options); $this->assertEquals('user-2@test.com', $result->email); $this->assertFalse($result->active); } @@ -270,7 +275,7 @@ public function testSocialLoginCreateNewAccountWithNoCredentials() 'validate_email' => 1, 'token_expiration' => 3600, ]; - $result = $this->Users->socialLogin($data, $options); + $result = $this->Users->getBehavior('Social')->socialLogin($data, $options); $this->assertFalse($result); } @@ -312,7 +317,7 @@ public function testSocialLoginCreateNewAccount() 'validate_email' => 0, 'token_expiration' => 3600, ]; - $result = $this->Users->socialLogin($data, $options); + $result = $this->Users->getBehavior('Social')->socialLogin($data, $options); $this->assertNotEmpty($result); $this->assertEquals('no-existing-reference', $result->social_accounts[0]->reference); $this->assertEquals(1, count($result->social_accounts)); diff --git a/tests/TestCase/PluginTest.php b/tests/TestCase/PluginTest.php index 5961fb28d..9dd24178a 100644 --- a/tests/TestCase/PluginTest.php +++ b/tests/TestCase/PluginTest.php @@ -23,7 +23,7 @@ use CakeDC\Auth\Middleware\TwoFactorMiddleware; use CakeDC\Users\Middleware\SocialAuthMiddleware; use CakeDC\Users\Middleware\SocialEmailMiddleware; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; /** * PluginTest class @@ -46,7 +46,7 @@ public function testMiddleware() ]); Configure::write('Auth.Authorization.enable', true); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); @@ -82,7 +82,7 @@ public function testMiddlewareAuthorizationMiddlewareAndRbacMiddleware() \CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class, ]); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); @@ -116,7 +116,7 @@ public function testMiddlewareWithoutAuhorization() \CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class, ]); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); @@ -146,7 +146,7 @@ public function testMiddlewareNotSocial() \CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class, ]); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); @@ -174,7 +174,7 @@ public function testMiddlewareNotOneTimePasswordAuthenticator() Configure::write('TwoFactorProcessors', [ \CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class, ]); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); @@ -204,7 +204,7 @@ public function testMiddlewareNotGoogleAuthenticationAndNotSocial() Configure::write('TwoFactorProcessors', [ \CakeDC\Auth\Authentication\TwoFactorProcessor\OneTimePasswordProcessor::class, ]); - $plugin = new Plugin(); + $plugin = new UsersPlugin(); $middleware = new MiddlewareQueue(); diff --git a/tests/TestCase/View/Helper/UserHelperTest.php b/tests/TestCase/View/Helper/UserHelperTest.php index 580f5c04b..aa5118994 100644 --- a/tests/TestCase/View/Helper/UserHelperTest.php +++ b/tests/TestCase/View/Helper/UserHelperTest.php @@ -252,7 +252,10 @@ public function testAddReCaptcha() Configure::write('Users.reCaptcha.size', 'normal'); Configure::write('Users.reCaptcha.tabindex', '3'); $this->User->Form->create(); - $result = $this->User->addReCaptcha(); + $result = ''; + $this->deprecated(function () use (&$result) { + $result = $this->User->addReCaptcha(); + }); $this->assertEquals('
', $result); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1353ca63c..03bb7a425 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -97,7 +97,7 @@ ]); session_id('cli'); -Plugin::getCollection()->add(new \CakeDC\Users\Plugin([ +Plugin::getCollection()->add(new \CakeDC\Users\UsersPlugin([ 'path' => dirname(dirname(__FILE__)) . DS, 'routes' => true, ])); diff --git a/tests/test_app/TestApp/Application.php b/tests/test_app/TestApp/Application.php index b89fcea65..aaeedd26f 100644 --- a/tests/test_app/TestApp/Application.php +++ b/tests/test_app/TestApp/Application.php @@ -18,7 +18,7 @@ use Cake\Http\MiddlewareQueue; use Cake\Routing\Middleware\AssetMiddleware; use Cake\Routing\Middleware\RoutingMiddleware; -use CakeDC\Users\Plugin; +use CakeDC\Users\UsersPlugin; class Application extends BaseApplication { @@ -45,7 +45,7 @@ public function bootstrap(): void ], ]); } - $this->addPlugin(Plugin::class); + $this->addPlugin(UsersPlugin::class); } /**