|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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_cms; |
| 18 | + |
| 19 | +use core_course\local\entity\content_item; |
| 20 | +use mod_cms\local\lib; |
| 21 | +use mod_cms\local\model\cms_types; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Unit tests for mod_cms |
| 26 | + * |
| 27 | + * @package mod_cms |
| 28 | + * @author Jason den Dulk <[email protected]> |
| 29 | + * @copyright 2023, Catalyst IT |
| 30 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 31 | + */ |
| 32 | +class get_course_content_items_test extends \advanced_testcase { |
| 33 | + /** |
| 34 | + * Set up before each test |
| 35 | + */ |
| 36 | + protected function setUp(): void { |
| 37 | + parent::setUp(); |
| 38 | + $this->resetAfterTest(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a default content item based on the logic in content_item_readonly_repository::find_all_for_course() |
| 43 | + * |
| 44 | + * @return content_item |
| 45 | + */ |
| 46 | + public function create_default_item(): content_item { |
| 47 | + global $OUTPUT, $DB; |
| 48 | + |
| 49 | + $mod = $DB->get_record('modules', ['name' => 'cms']); |
| 50 | + |
| 51 | + $archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER); |
| 52 | + $purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER); |
| 53 | + |
| 54 | + $icon = 'monologo'; |
| 55 | + |
| 56 | + return new content_item( |
| 57 | + $mod->id, |
| 58 | + $mod->name, |
| 59 | + new \core_course\local\entity\lang_string_title("modulename", $mod->name), |
| 60 | + new \moodle_url('/course/mod.php', ['id' => 0, 'add' => $mod->name]), |
| 61 | + $OUTPUT->pix_icon($icon, '', $mod->name, ['class' => "activityicon"]), |
| 62 | + 'help', |
| 63 | + $archetype, |
| 64 | + 'mod_' . $mod->name, |
| 65 | + $purpose, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Tests the lib::get_course_content_items function |
| 71 | + * @covers \mod_cms\local\lib::get_course_content_items |
| 72 | + */ |
| 73 | + public function test_get_course_content_items() { |
| 74 | + $types = [ |
| 75 | + [ 'name' => 'CMS1', 'description' => 'help1'], |
| 76 | + [ 'name' => 'CMS2', 'description' => 'help2'], |
| 77 | + [ 'name' => 'betamax', 'description' => 'some description'], |
| 78 | + ]; |
| 79 | + |
| 80 | + foreach ($types as $type) { |
| 81 | + $ct = new cms_types(0, (object) $type); |
| 82 | + $ct->save(); |
| 83 | + } |
| 84 | + |
| 85 | + $user = (object) []; |
| 86 | + $course = (object) []; |
| 87 | + |
| 88 | + $items = lib::get_course_content_items($this->create_default_item(), $user, $course); |
| 89 | + |
| 90 | + // Make sure the two arrays have the same ordering so they can be compared by index. |
| 91 | + usort( |
| 92 | + $types, |
| 93 | + function($a, $b) { |
| 94 | + return strcmp($a['name'], $b['name']); |
| 95 | + } |
| 96 | + ); |
| 97 | + usort( |
| 98 | + $items, |
| 99 | + function($a, $b) { |
| 100 | + return strcmp($a->get_title()->get_value(), $b->get_title()->get_value()); |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + $this->assertEquals(count($types), count($items)); |
| 105 | + foreach ($types as $idx => $type) { |
| 106 | + $this->assertEquals($type['name'], $items[$idx]->get_title()->get_value()); |
| 107 | + $this->assertEquals($type['description'], $items[$idx]->get_help()); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments