|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace auth_oidc\task; |
| 18 | + |
| 19 | +use advanced_testcase; |
| 20 | +use dml_exception; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit tests for the class cleanup_oidc_sid_test |
| 24 | + * |
| 25 | + * @package auth_oidc |
| 26 | + * @copyright 2025 eDaktik GmbH {@link https://www.edaktik.at/} |
| 27 | + * @author Christian Abila <christian.abila@edaktik.at> |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + * @coversDefaultClass \auth_oidc\task\cleanup_oidc_sid |
| 30 | + */ |
| 31 | +final class cleanup_oidc_sid_test extends advanced_testcase { |
| 32 | + /** |
| 33 | + * SIDs created before yesterday are deleted. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + * @throws dml_exception |
| 37 | + * @covers ::execute |
| 38 | + */ |
| 39 | + public function test_sids_older_than_yesterday_are_deleted(): void { |
| 40 | + global $DB, $USER; |
| 41 | + $this->resetAfterTest(); |
| 42 | + $twodaysago = strtotime('-2 day'); |
| 43 | + $yesterday = strtotime('-1 day'); |
| 44 | + $today = time(); |
| 45 | + // Create entries in auth_oidc_sid. |
| 46 | + $entry1id = $DB->insert_record( |
| 47 | + 'auth_oidc_sid', |
| 48 | + ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $twodaysago], |
| 49 | + ); |
| 50 | + $entry2id = $DB->insert_record( |
| 51 | + 'auth_oidc_sid', |
| 52 | + ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => ($twodaysago - 1000)], |
| 53 | + ); |
| 54 | + $entry3id = $DB->insert_record( |
| 55 | + 'auth_oidc_sid', |
| 56 | + ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $today], |
| 57 | + ); |
| 58 | + $entry4id = $DB->insert_record( |
| 59 | + 'auth_oidc_sid', |
| 60 | + ['userid' => $USER->id, 'sid' => 'sid', 'timecreated' => $yesterday], |
| 61 | + ); |
| 62 | + |
| 63 | + $cleanup = new cleanup_oidc_sid(); |
| 64 | + |
| 65 | + $cleanup->execute(); |
| 66 | + |
| 67 | + $records = $DB->get_records('auth_oidc_sid'); |
| 68 | + |
| 69 | + $this->assertCount(2, $records); |
| 70 | + |
| 71 | + $this->assertTrue($DB->record_exists('auth_oidc_sid', ['id' => $entry3id])); |
| 72 | + $this->assertFalse($DB->record_exists('auth_oidc_sid', ['id' => $entry1id])); |
| 73 | + $this->assertFalse($DB->record_exists('auth_oidc_sid', ['id' => $entry2id])); |
| 74 | + $this->assertTrue($DB->record_exists('auth_oidc_sid', ['id' => $entry4id])); |
| 75 | + } |
| 76 | +} |
0 commit comments