Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * Contains the test class for the content_item_readonly_repository class.
19
 *
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
24
 */
25
namespace core_course;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core_course\local\entity\content_item;
30
use core_course\local\repository\content_item_readonly_repository;
31
 
32
/**
33
 * The test class for the content_item_readonly_repository class.
34
 *
35
 * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1441 ariadna 38
final class content_item_readonly_repository_test extends \advanced_testcase {
1 efrain 39
    /**
40
     * Test the repository method, find_all_for_course().
41
     */
11 efrain 42
    public function test_find_all_for_course(): void {
1 efrain 43
        $this->resetAfterTest();
44
 
45
        $course = $this->getDataGenerator()->create_course();
46
        $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
47
        $cir = new content_item_readonly_repository();
48
 
49
        $items = $cir->find_all_for_course($course, $user);
1441 ariadna 50
        $modqbankfound = false;
1 efrain 51
        foreach ($items as $key => $item) {
52
            $this->assertInstanceOf(content_item::class, $item);
53
            $this->assertEquals($course->id, $item->get_link()->param('id'));
54
            $this->assertNotNull($item->get_link()->param('add'));
1441 ariadna 55
 
56
            // We should never get an instance of mod_qbank, as they have FEATURE_CAN_DISPLAY set to false.
57
            if ($item->get_component_name() === 'mod_qbank') {
58
                $modqbankfound = true;
59
            }
1 efrain 60
        }
1441 ariadna 61
        $this->assertFalse($modqbankfound);
1 efrain 62
    }
63
 
64
    /**
65
     * Test verifying that content items for hidden modules are not returned.
66
     */
11 efrain 67
    public function test_find_all_for_course_hidden_module(): void {
1 efrain 68
        $this->resetAfterTest();
69
        global $DB;
70
 
71
        $course = $this->getDataGenerator()->create_course();
72
        $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
73
        $cir = new content_item_readonly_repository();
74
 
75
        // Hide a module.
76
        $module = $DB->get_record('modules', ['id' => 1]);
77
        $DB->set_field("modules", "visible", "0", ["id" => $module->id]);
78
 
79
        $items = $cir->find_all_for_course($course, $user);
80
        $this->assertArrayNotHasKey($module->name, $items);
81
    }
82
 
83
    /**
84
     * Test confirming that all content items can be fetched, even those which require certain caps when in a course.
85
     */
11 efrain 86
    public function test_find_all(): void {
1 efrain 87
        $this->resetAfterTest();
88
 
89
        global $DB, $CFG;
90
        require_once($CFG->dirroot . '/mod/lti/tests/generator/lib.php');
91
        require_once($CFG->dirroot . '/mod/lti/locallib.php');
92
 
93
        // We'll compare our results to those which are course-specific, using mod_lti as an example.
94
        $course = $this->getDataGenerator()->create_course();
95
        $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
96
        /** @var \mod_lti_generator $ltigenerator */
97
        $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
98
        $ltigenerator->create_tool_types([
99
            'name' => 'site tool',
100
            'baseurl' => 'http://example.com',
101
            'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
102
            'state' => LTI_TOOL_STATE_CONFIGURED
103
        ]);
104
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
105
        assign_capability('mod/lti:addpreconfiguredinstance', CAP_PROHIBIT, $teacherrole->id,
106
            \core\context\course::instance($course->id));
107
        $cir = new content_item_readonly_repository();
108
        $this->setUser($user); // This is needed since the underlying lti code needs the global user despite the api accepting user.
109
 
110
        // Course specific - the tool won't be returned as the user doesn't have the capability required to use preconfigured tools.
111
        $forcourse = $cir->find_all_for_course($course, $user);
112
        $forcourse = array_filter($forcourse, function($contentitem) {
113
            return str_contains($contentitem->get_name(), 'lti_type');
114
        });
115
        $this->assertEmpty($forcourse);
116
 
117
        // All - all items are returned, including the lti site tool.
118
        $all = $cir->find_all();
119
        $all = array_filter($all, function($contentitem) {
120
            return str_contains($contentitem->get_name(), 'lti_type');
121
        });
122
        $this->assertCount(1, $all);
1441 ariadna 123
 
124
        // We should never get an instance of mod_qbank, as they have FEATURE_CAN_DISPLAY set to false.
125
        $all = $cir->find_all();
126
        $all = array_filter($all, static fn($contentitem) => str_contains($contentitem->get_component_name(), 'mod_qbank'));
127
 
128
        $this->assertCount(0, $all);
1 efrain 129
    }
130
}