Skip to content

Commit 7ebd865

Browse files
committed
Fix time inconsistency, avoid using $USER in unit test
1 parent a897536 commit 7ebd865

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

auth/oidc/tests/task/cleanup_oidc_sid_test.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,59 @@
2020
use dml_exception;
2121

2222
/**
23-
* Unit tests for the class cleanup_oidc_sid_test
23+
* Unit tests for the class cleanup_oidc_sid
2424
*
2525
* @package auth_oidc
2626
* @copyright 2025 eDaktik GmbH {@link https://www.edaktik.at/}
2727
* @author Christian Abila <christian.abila@edaktik.at>
2828
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
* @group auth_oidc
30+
* @group office365
2931
* @coversDefaultClass \auth_oidc\task\cleanup_oidc_sid
3032
*/
3133
final class cleanup_oidc_sid_test extends advanced_testcase {
3234
/**
33-
* SIDs created before yesterday are deleted.
35+
* SIDs older than 1 day are deleted.
36+
*
37+
* The cleanup task deletes records where timecreated < strtotime('-1 day').
38+
* Records created exactly 1 day ago or more recently are kept.
3439
*
3540
* @return void
3641
* @throws dml_exception
3742
* @covers ::execute
3843
*/
3944
public function test_sids_older_than_yesterday_are_deleted(): void {
40-
global $DB, $USER;
45+
global $DB;
4146
$this->resetAfterTest();
42-
$twodaysago = strtotime('-2 day');
43-
$yesterday = strtotime('-1 day');
44-
$today = time();
45-
// Create entries in auth_oidc_sid.
47+
48+
// Create a test user to own the SID records.
49+
$user = $this->getDataGenerator()->create_user();
50+
51+
// Calculate cutoff time once to avoid timing issues if test runs slowly.
52+
// This matches what the cleanup task will calculate during execute().
53+
$cutofftime = strtotime('-1 day');
54+
55+
// Create timestamps relative to the cutoff time.
56+
$twodaysago = $cutofftime - DAYSECS; // Older than cutoff, should be deleted.
57+
$yesterday = $cutofftime; // Exactly at cutoff, should be kept (< operator).
58+
$today = time(); // Newer than cutoff, should be kept.
59+
60+
// Create entries in auth_oidc_sid with unique SIDs.
4661
$entry1id = $DB->insert_record(
4762
'auth_oidc_sid',
48-
['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $twodaysago],
63+
['userid' => $user->id, 'sid' => 'sid_old_1', 'timecreated' => $twodaysago],
4964
);
5065
$entry2id = $DB->insert_record(
5166
'auth_oidc_sid',
52-
['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => ($twodaysago - 1000)],
67+
['userid' => $user->id, 'sid' => 'sid_old_2', 'timecreated' => ($twodaysago - 1000)],
5368
);
5469
$entry3id = $DB->insert_record(
5570
'auth_oidc_sid',
56-
['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $today],
71+
['userid' => $user->id, 'sid' => 'sid_new_1', 'timecreated' => $today],
5772
);
5873
$entry4id = $DB->insert_record(
5974
'auth_oidc_sid',
60-
['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $yesterday],
75+
['userid' => $user->id, 'sid' => 'sid_boundary', 'timecreated' => $yesterday],
6176
);
6277

6378
$cleanup = new cleanup_oidc_sid();

0 commit comments

Comments
 (0)