|
| 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 qbank_questiongen\task; |
| 18 | + |
| 19 | +use qbank_questiongen\form\story_form; |
| 20 | +use stdClass; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit tests for the cleanup task. |
| 24 | + * |
| 25 | + * @package qbank_questiongen |
| 26 | + * @copyright 2025 ISB Bayern |
| 27 | + * @author Philipp Memmel |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + * @covers \local_ai_manager\local\observers |
| 30 | + */ |
| 31 | +final class cleanup_task_test extends \advanced_testcase { |
| 32 | + |
| 33 | + /** |
| 34 | + * Tests the functionality of the cleanup task. |
| 35 | + * |
| 36 | + * @covers \qbank_questiongen\task\cleanup_task::execute |
| 37 | + */ |
| 38 | + public function test_execute(): void { |
| 39 | + global $DB; |
| 40 | + $this->resetAfterTest(); |
| 41 | + |
| 42 | + $user = $this->getDataGenerator()->create_user(); |
| 43 | + |
| 44 | + set_config('cleanupdelay', DAYSECS * 11, 'qbank_questiongen'); |
| 45 | + // Set the clock to the past to simulate that a generate question task was created 10 days ago. |
| 46 | + $presenttime = time(); |
| 47 | + $clock = $this->mock_clock_with_frozen($presenttime - DAYSECS * 10); |
| 48 | + |
| 49 | + $record = new stdClass(); |
| 50 | + $record->category = 15; |
| 51 | + $record->userid = $user->id; |
| 52 | + $record->mode = story_form::QUESTIONGEN_MODE_TOPIC; |
| 53 | + $record->story = 'French revolution'; |
| 54 | + $record->numoftries = 3; |
| 55 | + $record->llmresponse = ''; |
| 56 | + $record->success = 0; |
| 57 | + $record->timecreated = $clock->time(); |
| 58 | + $record->timemodified = $clock->time(); |
| 59 | + $DB->insert_record('qbank_questiongen', $record); |
| 60 | + |
| 61 | + // Return to the present. |
| 62 | + $this->mock_clock_with_frozen($presenttime); |
| 63 | + |
| 64 | + // Let's run the task. Because the task is supposed to only delete entries with timemodified older than 11 days |
| 65 | + // and our entry is only 10 days old, it should not be deleted. |
| 66 | + ob_start(); |
| 67 | + $task = new cleanup_task(); |
| 68 | + $task->execute(); |
| 69 | + ob_end_clean(); |
| 70 | + |
| 71 | + $this->assertCount(1, $DB->get_records('qbank_questiongen')); |
| 72 | + |
| 73 | + // Two more days have passed, so now the DB entry should be 12 days old and should be deleted. |
| 74 | + $this->mock_clock_with_frozen($presenttime + DAYSECS * 2); |
| 75 | + ob_start(); |
| 76 | + $task = new cleanup_task(); |
| 77 | + $task->execute(); |
| 78 | + ob_end_clean(); |
| 79 | + $this->assertCount(0, $DB->get_records('qbank_questiongen')); |
| 80 | + } |
| 81 | +} |
0 commit comments