From 7170a3f9ce9b56f60b237630933547fb9b283cee Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Thu, 13 Jul 2023 23:26:46 +0300 Subject: [PATCH] use url builder for login redirect --- src/Controller/Traits/UserValidationTrait.php | 5 ++-- .../Traits/UserValidationTraitTest.php | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Controller/Traits/UserValidationTrait.php b/src/Controller/Traits/UserValidationTrait.php index 16bcf90a0..5b03d516a 100644 --- a/src/Controller/Traits/UserValidationTrait.php +++ b/src/Controller/Traits/UserValidationTrait.php @@ -18,6 +18,7 @@ use CakeDC\Users\Exception\UserAlreadyActiveException; use CakeDC\Users\Exception\UserNotFoundException; use CakeDC\Users\Plugin; +use CakeDC\Users\Utility\UsersUrl; use Exception; /** @@ -81,7 +82,7 @@ public function validate($type = null, $token = null) $this->Flash->error(__d('cake_d_c/users', 'Token already expired')); } - return $this->redirect(['action' => 'login']); + return $this->redirect(UsersUrl::actionUrl('login')); } /** @@ -119,7 +120,7 @@ public function resendTokenValidation() $this->Flash->error(__d('cake_d_c/users', 'Token could not be reset')); } - return $this->redirect(['action' => 'login']); + return $this->redirect(UsersUrl::actionUrl('login')); } catch (UserNotFoundException $ex) { $this->Flash->error(__d('cake_d_c/users', 'User {0} was not found', $reference)); } catch (UserAlreadyActiveException $ex) { diff --git a/tests/TestCase/Controller/Traits/UserValidationTraitTest.php b/tests/TestCase/Controller/Traits/UserValidationTraitTest.php index 584d8596c..f6852f38e 100644 --- a/tests/TestCase/Controller/Traits/UserValidationTraitTest.php +++ b/tests/TestCase/Controller/Traits/UserValidationTraitTest.php @@ -50,7 +50,7 @@ public function testValidateHappyEmail() ->with('User account validated successfully'); $this->Trait->expects($this->once()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->validate('email', 'token-3'); $user = $this->table->findById($user->id)->first(); $this->assertTrue($user->active); @@ -89,7 +89,7 @@ public function testValidateUserNotFound() ->with('Invalid token or user account already validated'); $this->Trait->expects($this->once()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->validate('email', 'not-found'); } @@ -106,7 +106,7 @@ public function testValidateTokenExpired() ->with('Token already expired'); $this->Trait->expects($this->once()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->validate('email', '6614f65816754310a5f0553436dd89e9'); } @@ -143,7 +143,7 @@ public function testValidateInvalidOp() ->with('Invalid validation type'); $this->Trait->expects($this->once()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->validate('invalid-op', '6614f65816754310a5f0553436dd89e9'); } @@ -188,7 +188,7 @@ public function testResendTokenValidationHappy() ->with('Token has been reset successfully. Please check your email.'); $this->Trait->expects($this->once()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->resendTokenValidation(); } @@ -239,7 +239,7 @@ public function testResendTokenValidationAlreadyActive() ->with('User user-4 is already active'); $this->Trait->expects($this->never()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->resendTokenValidation(); } @@ -262,7 +262,22 @@ public function testResendTokenValidationNotFound() ->with('User not-found was not found'); $this->Trait->expects($this->never()) ->method('redirect') - ->with(['action' => 'login']); + ->with($this->loginUrl()); $this->Trait->resendTokenValidation(); } + + /** + * Login redirect url. + * + * @return array + */ + protected function loginUrl(): array + { + return [ + 'action' => 'login', + 'prefix' => false, + 'plugin' => 'CakeDC/Users', + 'controller' => 'Users', + ]; + } }