Skip to content

Commit 58c9290

Browse files
authored
Merge pull request #261 from kenjis/refactor-use-Time
refactor: use Time::now()
2 parents 0dd0562 + d6b7340 commit 58c9290

5 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/Authentication/Authenticators/Session.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,9 @@ protected function rememberUser(User $user): void
836836

837837
private function calcExpires(): string
838838
{
839-
return date(
840-
'Y-m-d H:i:s',
841-
time() + setting('Auth.sessionConfig')['rememberLength']
842-
);
839+
$timestamp = Time::now()->getTimestamp() + setting('Auth.sessionConfig')['rememberLength'];
840+
841+
return Time::createFromTimestamp($timestamp)->format('Y-m-d H:i:s');
843842
}
844843

845844
private function setRememberMeCookie(string $rawToken): void

src/Models/LoginModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function recordLoginAttempt(
6161
'id_type' => $idType,
6262
'identifier' => $identifier,
6363
'user_id' => $userId,
64-
'date' => date('Y-m-d H:i:s'),
64+
'date' => Time::now()->format('Y-m-d H:i:s'),
6565
'success' => (int) $success,
6666
]);
6767

src/Models/UserIdentityModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeIgniter\Shield\Models;
44

5+
use CodeIgniter\I18n\Time;
56
use CodeIgniter\Model;
67
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
78
use CodeIgniter\Shield\Authentication\Authenticators\Session;
@@ -247,7 +248,7 @@ public function getIdentitiesByTypes(User $user, array $types): array
247248
*/
248249
public function touchIdentity(UserIdentity $identity): void
249250
{
250-
$identity->last_used_at = date('Y-m-d H:i:s');
251+
$identity->last_used_at = Time::now()->format('Y-m-d H:i:s');
251252

252253
$return = $this->save($identity);
253254

tests/Authorization/AuthorizableTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,6 @@ public function testCreatedAtIfDefaultLocaleSetFaWithAddGroup(): void
316316
]);
317317

318318
Locale::setDefault($currentLocale);
319+
Time::setTestNow();
319320
}
320321
}

tests/Controllers/LoginTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function testLoginBadEmail(): void
6464

6565
public function testLoginActionEmailSuccess(): void
6666
{
67+
Time::setTestNow('March 10, 2017', 'America/Chicago');
68+
6769
$this->user->createEmailIdentity([
6870
'email' => 'foo@example.com',
6971
'password' => 'secret123',
@@ -87,13 +89,15 @@ public function testLoginActionEmailSuccess(): void
8789
]);
8890
// Last Used date should have been set
8991
$identity = $this->user->getEmailIdentity();
90-
$this->assertCloseEnough($identity->last_used_at->getTimestamp(), Time::now()->getTimestamp());
92+
$this->assertSame(Time::now()->getTimestamp(), $identity->last_used_at->getTimestamp());
9193

9294
// Session should have `logged_in` value with user's id
9395
$this->assertSame($this->user->id, session('user')['id']);
96+
97+
Time::setTestNow();
9498
}
9599

96-
public function testAfterLoggedInNotDesplayLoginPage(): void
100+
public function testAfterLoggedInNotDisplayLoginPage(): void
97101
{
98102
$this->user->createEmailIdentity([
99103
'email' => 'foo@example.com',
@@ -111,6 +115,8 @@ public function testAfterLoggedInNotDesplayLoginPage(): void
111115

112116
public function testLoginActionUsernameSuccess(): void
113117
{
118+
Time::setTestNow('March 10, 2017', 'America/Chicago');
119+
114120
// Change the validation rules
115121
$config = new class () extends Validation {
116122
public $login = [
@@ -143,10 +149,12 @@ public function testLoginActionUsernameSuccess(): void
143149
]);
144150
// Last Used date should have been set
145151
$identity = $this->user->getEmailIdentity();
146-
$this->assertCloseEnough($identity->last_used_at->getTimestamp(), Time::now()->getTimestamp());
152+
$this->assertSame(Time::now()->getTimestamp(), $identity->last_used_at->getTimestamp());
147153

148154
// Session should have `logged_in` value with user's id
149155
$this->assertSame($this->user->id, session('user')['id']);
156+
157+
Time::setTestNow();
150158
}
151159

152160
public function testLogoutAction(): void

0 commit comments

Comments
 (0)