|
| 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 mod_learningmap; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for Learning map helper class |
| 21 | + * |
| 22 | + * @package mod_learningmap |
| 23 | + * @category test |
| 24 | + * @copyright 2025 ISB Bayern |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + */ |
| 27 | +#[\PHPUnit\Framework\Attributes\CoversClass(helper::class)] |
| 28 | +#[\PHPUnit\Framework\Attributes\CoversMethod(helper::class, 'repair_learningmap_record')] |
| 29 | +final class mod_learningmap_helper_test extends \advanced_testcase { |
| 30 | + /** |
| 31 | + * Tests the repair_learningmap_record method. |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function test_repair_learningmap_record(): void { |
| 36 | + global $DB; |
| 37 | + $this->resetAfterTest(); |
| 38 | + $this->setAdminUser(); |
| 39 | + $course = $this->getDataGenerator()->create_course(); |
| 40 | + $learningmapid = $this->getDataGenerator()->create_module('learningmap', ['course' => $course->id])->id; |
| 41 | + |
| 42 | + $recordbefore = $DB->get_record('learningmap', ['id' => $learningmapid], '*', MUST_EXIST); |
| 43 | + helper::repair_learningmap_record($learningmapid); |
| 44 | + $recordafter = $DB->get_record('learningmap', ['id' => $learningmapid], '*', MUST_EXIST); |
| 45 | + $this->assertEquals($recordbefore->course, $recordafter->course, 'The learning map record should not have changed.'); |
| 46 | + |
| 47 | + $DB->update_record('learningmap', ['id' => $learningmapid, 'course' => -1]); |
| 48 | + helper::repair_learningmap_record($learningmapid); |
| 49 | + $recordafter = $DB->get_record('learningmap', ['id' => $learningmapid], '*', MUST_EXIST); |
| 50 | + $this->assertEquals( |
| 51 | + $course->id, |
| 52 | + $recordafter->course, |
| 53 | + 'The course id should have been updated in the learning map record.' |
| 54 | + ); |
| 55 | + |
| 56 | + $DB->delete_records('course', ['id' => $course->id]); |
| 57 | + helper::repair_learningmap_record($learningmapid); |
| 58 | + $recordafter = $DB->get_record('learningmap', ['id' => $learningmapid], '*', MUST_EXIST); |
| 59 | + $this->assertEquals( |
| 60 | + $recordbefore->course, |
| 61 | + $recordafter->course, |
| 62 | + 'The learning map record should not have changed after trying to repair it with a non-existing course.' |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
0 commit comments