Skip to content

Commit 84fcbb8

Browse files
authored
Merge pull request #116 from MagnusSamuelsson/fix/test-time-issue
Fix/test time issue
2 parents 4f9cbec + 61ffb01 commit 84fcbb8

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

classes/core_userkey_manager.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
namespace auth_userkey;
1818

19+
use core\di;
20+
use core\clock;
21+
1922
/**
2023
* Key manager class.
2124
*
@@ -62,9 +65,9 @@ public function create_key($userid, $allowedips = null) {
6265
$this->delete_keys($userid);
6366

6467
if (isset($this->config->keylifetime) && (int)$this->config->keylifetime > 0) {
65-
$validuntil = time() + $this->config->keylifetime;
68+
$validuntil = di::get(clock::class)->time() + $this->config->keylifetime;
6669
} else {
67-
$validuntil = time() + self::DEFAULT_KEY_LIFE_TIME_IN_SECONDS;
70+
$validuntil = di::get(clock::class)->time() + self::DEFAULT_KEY_LIFE_TIME_IN_SECONDS;
6871
}
6972

7073
$iprestriction = null;
@@ -116,7 +119,7 @@ public function validate_key($keyvalue) {
116119
throw new \moodle_exception('invalidkey');
117120
}
118121

119-
if (!empty($key->validuntil) && $key->validuntil < time()) {
122+
if (!empty($key->validuntil) && $key->validuntil < di::get(clock::class)->time()) {
120123
throw new \moodle_exception('expiredkey');
121124
}
122125

tests/core_userkey_manager_test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private function validate_iprestriction(
7171
int $keylifetime = 60
7272
): void {
7373
global $DB;
74+
$clock = $this->mock_clock_with_frozen();
7475
$manager = new core_userkey_manager($this->config);
7576
if ($allowips) {
7677
$value = $manager->create_key($this->user->id, $allowips);
@@ -83,7 +84,7 @@ private function validate_iprestriction(
8384
$this->assertEquals($script, $actualkey->script);
8485
$this->assertEquals($this->user->id, $actualkey->instance);
8586
$this->assertEquals($iprestriction, $actualkey->iprestriction);
86-
$this->assertEquals(time() + $keylifetime, $actualkey->validuntil);
87+
$this->assertEquals($clock->time() + $keylifetime, $actualkey->validuntil);
8788
}
8889
/**
8990
* Test that core_userkey_manager implements userkey_manager_interface interface.

0 commit comments

Comments
 (0)