MDL-80949 mod_data: Remove unused Allow autolink param for text field
[moodle.git] / course / tests / content_item_readonly_repository_test.php
blob681851bd5ce24cd8c1c199851e8dbf84a1bf7f0b
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.
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/>.
17 /**
18 * Contains the test class for the content_item_readonly_repository class.
20 * @package core
21 * @subpackage course
22 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_course;
27 defined('MOODLE_INTERNAL') || die();
29 use core_course\local\entity\content_item;
30 use core_course\local\repository\content_item_readonly_repository;
32 /**
33 * The test class for the content_item_readonly_repository class.
35 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class content_item_readonly_repository_test extends \advanced_testcase {
39 /**
40 * Test the repository method, find_all_for_course().
42 public function test_find_all_for_course() {
43 $this->resetAfterTest();
45 $course = $this->getDataGenerator()->create_course();
46 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
47 $cir = new content_item_readonly_repository();
49 $items = $cir->find_all_for_course($course, $user);
50 foreach ($items as $key => $item) {
51 $this->assertInstanceOf(content_item::class, $item);
52 $this->assertEquals($course->id, $item->get_link()->param('id'));
53 $this->assertNotNull($item->get_link()->param('add'));
57 /**
58 * Test verifying that content items for hidden modules are not returned.
60 public function test_find_all_for_course_hidden_module() {
61 $this->resetAfterTest();
62 global $DB;
64 $course = $this->getDataGenerator()->create_course();
65 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
66 $cir = new content_item_readonly_repository();
68 // Hide a module.
69 $module = $DB->get_record('modules', ['id' => 1]);
70 $DB->set_field("modules", "visible", "0", ["id" => $module->id]);
72 $items = $cir->find_all_for_course($course, $user);
73 $this->assertArrayNotHasKey($module->name, $items);
76 /**
77 * Test confirming that all content items can be fetched, even those which require certain caps when in a course.
79 public function test_find_all() {
80 $this->resetAfterTest();
82 global $DB, $CFG;
83 require_once($CFG->dirroot . '/mod/lti/tests/generator/lib.php');
84 require_once($CFG->dirroot . '/mod/lti/locallib.php');
86 // We'll compare our results to those which are course-specific, using mod_lti as an example.
87 $course = $this->getDataGenerator()->create_course();
88 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
89 /** @var \mod_lti_generator $ltigenerator */
90 $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
91 $ltigenerator->create_tool_types([
92 'name' => 'site tool',
93 'baseurl' => 'http://example.com',
94 'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
95 'state' => LTI_TOOL_STATE_CONFIGURED
96 ]);
97 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
98 assign_capability('mod/lti:addpreconfiguredinstance', CAP_PROHIBIT, $teacherrole->id,
99 \core\context\course::instance($course->id));
100 $cir = new content_item_readonly_repository();
101 $this->setUser($user); // This is needed since the underlying lti code needs the global user despite the api accepting user.
103 // Course specific - the tool won't be returned as the user doesn't have the capability required to use preconfigured tools.
104 $forcourse = $cir->find_all_for_course($course, $user);
105 $forcourse = array_filter($forcourse, function($contentitem) {
106 return str_contains($contentitem->get_name(), 'lti_type');
108 $this->assertEmpty($forcourse);
110 // All - all items are returned, including the lti site tool.
111 $all = $cir->find_all();
112 $all = array_filter($all, function($contentitem) {
113 return str_contains($contentitem->get_name(), 'lti_type');
115 $this->assertCount(1, $all);