Skip to content

Commit

Permalink
use url builder for login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
skie committed Jul 13, 2023
1 parent 5c8c5a0 commit 7170a3f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
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',
];
}
}

0 comments on commit 7170a3f

Please sign in to comment.