Skip to content

Commit

Permalink
Fix login tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Apr 27, 2022
1 parent c6e5b75 commit c4f01e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class User extends Entity
* when they are accessed.
*/
protected $casts = [
'username' => 'string',
'email' => 'string',
'active' => 'boolean',
'force_pass_reset' => 'boolean',
];
Expand Down Expand Up @@ -150,7 +152,7 @@ public function deactivate()
*/
public function isActivated(): bool
{
return isset($this->attributes['active']) && $this->attributes['active'] === true;
return $this->active;
}

/**
Expand Down
49 changes: 10 additions & 39 deletions tests/controllers/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function testAttemptLoginValidationErrors()
$this->asserttrue(isset($_SESSION['_ci_validation_errors']));
}

public function testAttemptLoginSuccess()
/**
* @dataProvider rememberMeProvider
*/
public function testAttemptLoginSuccess(bool $remembering)
{
// Create user
$user = [
Expand All @@ -81,7 +84,7 @@ public function testAttemptLoginSuccess()

// Just make sure since it's a default
$config = config('Auth');
$config->allowRemembering = false;
$config->allowRemembering = $remembering;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('login'))
Expand All @@ -91,46 +94,14 @@ public function testAttemptLoginSuccess()

$this->assertTrue($result->isRedirect());
$this->assertSame(lang('Auth.loginSuccess'), $_SESSION['message']);
$this->assertFalse($result->response()->hasCookie('remember'));
$this->assertSame($remembering, $result->response()->hasCookie('remember'));
}

public function testAttemptLoginSuccessWithRememberMe()
public function rememberMeProvider()
{
// Create user
$user = [
'username' => 'Joe Cool',
'email' => '[email protected]',
'password' => 'xaH96AhjglK',
'active' => 1,
return [
[true],
[false],
];
$this->createUser($user);

// Set form input
$data = [
'login' => $user['username'],
'password' => $user['password'],
'remember' => 'on',
];
$globals = [
'request' => $data,
'post' => $data,
];

$request = service('request', null, false);
$this->setPrivateProperty($request, 'globals', $globals);

// Just make sure since it's a default
$config = config('Auth');
$config->allowRemembering = true;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('login'))
->withRequest($request)
->controller(AuthController::class)
->execute('attemptLogin');

$this->assertTrue($result->isRedirect());
$this->assertSame(lang('Auth.loginSuccess'), $_SESSION['message']);
$this->assertTrue($result->response()->hasCookie('remember'));
}
}

0 comments on commit c4f01e5

Please sign in to comment.