|
20 | 20 | use dml_exception; |
21 | 21 |
|
22 | 22 | /** |
23 | | - * Unit tests for the class cleanup_oidc_sid_test |
| 23 | + * Unit tests for the class cleanup_oidc_sid |
24 | 24 | * |
25 | 25 | * @package auth_oidc |
26 | 26 | * @copyright 2025 eDaktik GmbH {@link https://www.edaktik.at/} |
27 | 27 | * @author Christian Abila <christian.abila@edaktik.at> |
28 | 28 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + * @group auth_oidc |
| 30 | + * @group office365 |
29 | 31 | * @coversDefaultClass \auth_oidc\task\cleanup_oidc_sid |
30 | 32 | */ |
31 | 33 | final class cleanup_oidc_sid_test extends advanced_testcase { |
32 | 34 | /** |
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. |
34 | 39 | * |
35 | 40 | * @return void |
36 | 41 | * @throws dml_exception |
37 | 42 | * @covers ::execute |
38 | 43 | */ |
39 | 44 | public function test_sids_older_than_yesterday_are_deleted(): void { |
40 | | - global $DB, $USER; |
| 45 | + global $DB; |
41 | 46 | $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. |
46 | 61 | $entry1id = $DB->insert_record( |
47 | 62 | 'auth_oidc_sid', |
48 | | - ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $twodaysago], |
| 63 | + ['userid' => $user->id, 'sid' => 'sid_old_1', 'timecreated' => $twodaysago], |
49 | 64 | ); |
50 | 65 | $entry2id = $DB->insert_record( |
51 | 66 | 'auth_oidc_sid', |
52 | | - ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => ($twodaysago - 1000)], |
| 67 | + ['userid' => $user->id, 'sid' => 'sid_old_2', 'timecreated' => ($twodaysago - 1000)], |
53 | 68 | ); |
54 | 69 | $entry3id = $DB->insert_record( |
55 | 70 | 'auth_oidc_sid', |
56 | | - ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $today], |
| 71 | + ['userid' => $user->id, 'sid' => 'sid_new_1', 'timecreated' => $today], |
57 | 72 | ); |
58 | 73 | $entry4id = $DB->insert_record( |
59 | 74 | 'auth_oidc_sid', |
60 | | - ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $yesterday], |
| 75 | + ['userid' => $user->id, 'sid' => 'sid_boundary', 'timecreated' => $yesterday], |
61 | 76 | ); |
62 | 77 |
|
63 | 78 | $cleanup = new cleanup_oidc_sid(); |
|
0 commit comments