Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use url builder for login redirect #1041

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Controller/Traits/UserValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 22 additions & 7 deletions tests/TestCase/Controller/Traits/UserValidationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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',
];
}
}