1441 |
ariadna |
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 core_courseformat\local\overview;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tests for course
|
|
|
21 |
*
|
|
|
22 |
* @package core_courseformat
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2025 Ferran Recio <ferran@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @covers \core_courseformat\local\overview\overviewfactory
|
|
|
27 |
*/
|
|
|
28 |
final class overviewfactory_test extends \advanced_testcase {
|
|
|
29 |
#[\Override()]
|
|
|
30 |
public static function setUpBeforeClass(): void {
|
|
|
31 |
global $CFG;
|
|
|
32 |
require_once($CFG->dirroot . '/course/format/tests/fixtures/wrongcm_activityoverview.php');
|
|
|
33 |
parent::setUpBeforeClass();
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Test create method on resource activities.
|
|
|
38 |
*
|
|
|
39 |
* @covers ::create
|
|
|
40 |
* @dataProvider create_resource_provider
|
|
|
41 |
* @param string $resourcetype
|
|
|
42 |
*/
|
|
|
43 |
public function test_create_resource(
|
|
|
44 |
string $resourcetype,
|
|
|
45 |
?string $expected,
|
|
|
46 |
): void {
|
|
|
47 |
$this->resetAfterTest();
|
|
|
48 |
$this->setAdminUser();
|
|
|
49 |
|
|
|
50 |
$generator = $this->getDataGenerator();
|
|
|
51 |
$course = $generator->create_course();
|
|
|
52 |
$activity = $this->getDataGenerator()->create_module($resourcetype, ['course' => $course->id]);
|
|
|
53 |
$modinfo = get_fast_modinfo($course);
|
|
|
54 |
$cm = $modinfo->get_cm($activity->cmid);
|
|
|
55 |
|
|
|
56 |
$overview = overviewfactory::create($cm);
|
|
|
57 |
|
|
|
58 |
$this->assertInstanceOf($expected, $overview);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Data provider for test_create_resource.
|
|
|
63 |
*
|
|
|
64 |
* @return array
|
|
|
65 |
*/
|
|
|
66 |
public static function create_resource_provider(): array {
|
|
|
67 |
return [
|
|
|
68 |
// Resource activities.
|
|
|
69 |
'book' => [
|
|
|
70 |
'resourcetype' => 'book',
|
|
|
71 |
'expected' => resourceoverview::class,
|
|
|
72 |
],
|
|
|
73 |
'folder' => [
|
|
|
74 |
'resourcetype' => 'folder',
|
|
|
75 |
'expected' => resourceoverview::class,
|
|
|
76 |
],
|
|
|
77 |
'page' => [
|
|
|
78 |
'resourcetype' => 'page',
|
|
|
79 |
'expected' => resourceoverview::class,
|
|
|
80 |
],
|
|
|
81 |
'resource' => [
|
|
|
82 |
'resourcetype' => 'resource',
|
|
|
83 |
'expected' => resourceoverview::class,
|
|
|
84 |
],
|
|
|
85 |
'url' => [
|
|
|
86 |
'resourcetype' => 'url',
|
|
|
87 |
'expected' => resourceoverview::class,
|
|
|
88 |
],
|
|
|
89 |
// Fallbacks and integrations.
|
|
|
90 |
'assign' => [
|
|
|
91 |
'resourcetype' => 'assign',
|
|
|
92 |
'expected' => \mod_assign\courseformat\overview::class,
|
|
|
93 |
],
|
|
|
94 |
'bigbluebuttonbn' => [
|
|
|
95 |
'resourcetype' => 'bigbluebuttonbn',
|
|
|
96 |
'expected' => resourceoverview::class,
|
|
|
97 |
],
|
|
|
98 |
'choice' => [
|
|
|
99 |
'resourcetype' => 'choice',
|
|
|
100 |
'expected' => resourceoverview::class,
|
|
|
101 |
],
|
|
|
102 |
'data' => [
|
|
|
103 |
'resourcetype' => 'data',
|
|
|
104 |
'expected' => resourceoverview::class,
|
|
|
105 |
],
|
|
|
106 |
'feedback' => [
|
|
|
107 |
'resourcetype' => 'feedback',
|
|
|
108 |
'expected' => \mod_feedback\courseformat\overview::class,
|
|
|
109 |
],
|
|
|
110 |
'forum' => [
|
|
|
111 |
'resourcetype' => 'forum',
|
|
|
112 |
'expected' => resourceoverview::class,
|
|
|
113 |
],
|
|
|
114 |
'glossary' => [
|
|
|
115 |
'resourcetype' => 'glossary',
|
|
|
116 |
'expected' => resourceoverview::class,
|
|
|
117 |
],
|
|
|
118 |
'h5pactivity' => [
|
|
|
119 |
'resourcetype' => 'h5pactivity',
|
|
|
120 |
'expected' => resourceoverview::class,
|
|
|
121 |
],
|
|
|
122 |
'lesson' => [
|
|
|
123 |
'resourcetype' => 'lesson',
|
|
|
124 |
'expected' => resourceoverview::class,
|
|
|
125 |
],
|
|
|
126 |
'lti' => [
|
|
|
127 |
'resourcetype' => 'lti',
|
|
|
128 |
'expected' => resourceoverview::class,
|
|
|
129 |
],
|
|
|
130 |
'qbank' => [
|
|
|
131 |
'resourcetype' => 'qbank',
|
|
|
132 |
'expected' => resourceoverview::class,
|
|
|
133 |
],
|
|
|
134 |
'quiz' => [
|
|
|
135 |
'resourcetype' => 'quiz',
|
|
|
136 |
'expected' => resourceoverview::class,
|
|
|
137 |
],
|
|
|
138 |
'scorm' => [
|
|
|
139 |
'resourcetype' => 'scorm',
|
|
|
140 |
'expected' => resourceoverview::class,
|
|
|
141 |
],
|
|
|
142 |
'wiki' => [
|
|
|
143 |
'resourcetype' => 'wiki',
|
|
|
144 |
'expected' => resourceoverview::class,
|
|
|
145 |
],
|
|
|
146 |
'workshop' => [
|
|
|
147 |
'resourcetype' => 'workshop',
|
|
|
148 |
'expected' => \mod_workshop\courseformat\overview::class,
|
|
|
149 |
],
|
|
|
150 |
];
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Test create method on a fake activity with a wrong class.
|
|
|
155 |
*
|
|
|
156 |
* @covers ::create
|
|
|
157 |
*/
|
|
|
158 |
public function test_create_exception(
|
|
|
159 |
): void {
|
|
|
160 |
$this->resetAfterTest();
|
|
|
161 |
$this->setAdminUser();
|
|
|
162 |
|
|
|
163 |
$generator = $this->getDataGenerator();
|
|
|
164 |
|
|
|
165 |
$course = $generator->create_course();
|
|
|
166 |
$activity = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
|
|
|
167 |
$modinfo = get_fast_modinfo($course);
|
|
|
168 |
$cm = $modinfo->get_cm($activity->cmid);
|
|
|
169 |
|
|
|
170 |
// We know the factory will only use the modname to create the overview,
|
|
|
171 |
// this is a small trick to make the factory to use a wrong class and
|
|
|
172 |
// won't happen in a real code. However, this is the easiest way to test
|
|
|
173 |
// the exception.
|
|
|
174 |
$reflection = new \ReflectionClass($cm);
|
|
|
175 |
$property = $reflection->getProperty('modname');
|
|
|
176 |
$property->setAccessible(true);
|
|
|
177 |
$property->setValue($cm, 'wrongcm');
|
|
|
178 |
|
|
|
179 |
$this->expectException(\coding_exception::class);
|
|
|
180 |
$this->expectExceptionMessageMatches("/.* must extend core_courseformat\\\\activityoverviewbase.*/");
|
|
|
181 |
overviewfactory::create($cm);
|
|
|
182 |
}
|
|
|
183 |
}
|