|
| 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 unilabeltype_grid; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for the grid content type. |
| 21 | + * |
| 22 | + * @package unilabeltype_grid |
| 23 | + * @category test |
| 24 | + * @copyright 2026 ISB Bayern |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + */ |
| 27 | +final class content_type_test extends \advanced_testcase { |
| 28 | + /** |
| 29 | + * Create a grid activity with one tile. |
| 30 | + * |
| 31 | + * @param \stdClass $course |
| 32 | + * @param string $content |
| 33 | + * @return array [course module, tile id] |
| 34 | + */ |
| 35 | + private function create_grid_with_tile(\stdClass $course, string $content): array { |
| 36 | + global $DB; |
| 37 | + $unilabel = $this->getDataGenerator()->create_module('unilabel', ['course' => $course->id, 'unilabeltype' => 'grid']); |
| 38 | + $gridid = $DB->insert_record('unilabeltype_grid', (object) ['unilabelid' => $unilabel->id, 'columns' => 4]); |
| 39 | + $tileid = $DB->insert_record('unilabeltype_grid_tile', (object) [ |
| 40 | + 'gridid' => $gridid, |
| 41 | + 'title' => 'Tile', |
| 42 | + 'content' => $content, |
| 43 | + 'visible' => 1, |
| 44 | + ]); |
| 45 | + return [get_coursemodule_from_instance('unilabel', $unilabel->id), $tileid]; |
| 46 | + } |
| 47 | + |
| 48 | + #[\PHPUnit\Framework\Attributes\Group('baseline')] |
| 49 | + /** |
| 50 | + * A tile of another grid is not returned for the own course module. |
| 51 | + * |
| 52 | + * @covers \unilabeltype_grid\content_type::get_fragment |
| 53 | + */ |
| 54 | + public function test_get_fragment_rejects_foreign_tile(): void { |
| 55 | + $this->resetAfterTest(); |
| 56 | + $own = $this->getDataGenerator()->create_course(); |
| 57 | + $foreign = $this->getDataGenerator()->create_course(); |
| 58 | + [$cm] = $this->create_grid_with_tile($own, 'Own tile'); |
| 59 | + [, $foreigntileid] = $this->create_grid_with_tile($foreign, 'Foreign secret'); |
| 60 | + $student = $this->getDataGenerator()->create_and_enrol($own, 'student'); |
| 61 | + $this->setUser($student); |
| 62 | + |
| 63 | + $this->expectException(\dml_missing_record_exception::class); |
| 64 | + (new content_type())->get_fragment([ |
| 65 | + 'contextid' => \context_module::instance($cm->id)->id, |
| 66 | + 'cmid' => $cm->id, |
| 67 | + 'id' => $foreigntileid, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + #[\PHPUnit\Framework\Attributes\Group('baseline')] |
| 72 | + /** |
| 73 | + * A tile of the own grid is returned. |
| 74 | + * |
| 75 | + * @covers \unilabeltype_grid\content_type::get_fragment |
| 76 | + */ |
| 77 | + public function test_get_fragment_returns_own_tile(): void { |
| 78 | + $this->resetAfterTest(); |
| 79 | + $own = $this->getDataGenerator()->create_course(); |
| 80 | + [$cm, $tileid] = $this->create_grid_with_tile($own, 'Own tile'); |
| 81 | + $student = $this->getDataGenerator()->create_and_enrol($own, 'student'); |
| 82 | + $this->setUser($student); |
| 83 | + |
| 84 | + $content = (new content_type())->get_fragment([ |
| 85 | + 'contextid' => \context_module::instance($cm->id)->id, |
| 86 | + 'cmid' => $cm->id, |
| 87 | + 'id' => $tileid, |
| 88 | + ]); |
| 89 | + $this->assertStringContainsString('Own tile', $content); |
| 90 | + } |
| 91 | +} |
0 commit comments