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 |
namespace mod_folder;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use externallib_advanced_testcase;
|
|
|
21 |
use mod_folder_external;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* External mod_folder functions unit tests
|
|
|
31 |
*
|
|
|
32 |
* @package mod_folder
|
|
|
33 |
* @category external
|
|
|
34 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @since Moodle 3.0
|
|
|
37 |
*/
|
|
|
38 |
class externallib_test extends externallib_advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Test view_folder
|
|
|
42 |
*/
|
11 |
efrain |
43 |
public function test_view_folder(): void {
|
1 |
efrain |
44 |
global $DB;
|
|
|
45 |
|
|
|
46 |
$this->resetAfterTest(true);
|
|
|
47 |
|
|
|
48 |
$this->setAdminUser();
|
|
|
49 |
// Setup test data.
|
|
|
50 |
$course = $this->getDataGenerator()->create_course();
|
|
|
51 |
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
|
|
|
52 |
$context = \context_module::instance($folder->cmid);
|
|
|
53 |
$cm = get_coursemodule_from_instance('folder', $folder->id);
|
|
|
54 |
|
|
|
55 |
// Test invalid instance id.
|
|
|
56 |
try {
|
|
|
57 |
mod_folder_external::view_folder(0);
|
|
|
58 |
$this->fail('Exception expected due to invalid mod_folder instance id.');
|
|
|
59 |
} catch (\moodle_exception $e) {
|
|
|
60 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Test not-enrolled user.
|
|
|
64 |
$user = self::getDataGenerator()->create_user();
|
|
|
65 |
$this->setUser($user);
|
|
|
66 |
try {
|
|
|
67 |
mod_folder_external::view_folder($folder->id);
|
|
|
68 |
$this->fail('Exception expected due to not enrolled user.');
|
|
|
69 |
} catch (\moodle_exception $e) {
|
|
|
70 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Test user with full capabilities.
|
|
|
74 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
75 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
76 |
|
|
|
77 |
// Trigger and capture the event.
|
|
|
78 |
$sink = $this->redirectEvents();
|
|
|
79 |
|
|
|
80 |
$result = mod_folder_external::view_folder($folder->id);
|
|
|
81 |
$result = external_api::clean_returnvalue(mod_folder_external::view_folder_returns(), $result);
|
|
|
82 |
|
|
|
83 |
$events = $sink->get_events();
|
|
|
84 |
$this->assertCount(1, $events);
|
|
|
85 |
$event = array_shift($events);
|
|
|
86 |
|
|
|
87 |
// Checking that the event contains the expected values.
|
|
|
88 |
$this->assertInstanceOf('\mod_folder\event\course_module_viewed', $event);
|
|
|
89 |
$this->assertEquals($context, $event->get_context());
|
|
|
90 |
$moodlefolder = new \moodle_url('/mod/folder/view.php', array('id' => $cm->id));
|
|
|
91 |
$this->assertEquals($moodlefolder, $event->get_url());
|
|
|
92 |
$this->assertEventContextNotUsed($event);
|
|
|
93 |
$this->assertNotEmpty($event->get_name());
|
|
|
94 |
|
|
|
95 |
// Test user with no capabilities.
|
|
|
96 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
97 |
assign_capability('mod/folder:view', CAP_PROHIBIT, $studentrole->id, $context->id);
|
|
|
98 |
// Empty all the caches that may be affected by this change.
|
|
|
99 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
100 |
\course_modinfo::clear_instance_cache();
|
|
|
101 |
|
|
|
102 |
try {
|
|
|
103 |
mod_folder_external::view_folder($folder->id);
|
|
|
104 |
$this->fail('Exception expected due to missing capability.');
|
|
|
105 |
} catch (\moodle_exception $e) {
|
|
|
106 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Test test_mod_folder_get_folders_by_courses
|
|
|
112 |
*/
|
11 |
efrain |
113 |
public function test_mod_folder_get_folders_by_courses(): void {
|
1 |
efrain |
114 |
global $DB;
|
|
|
115 |
|
|
|
116 |
$this->resetAfterTest(true);
|
|
|
117 |
|
|
|
118 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
119 |
$course2 = self::getDataGenerator()->create_course();
|
|
|
120 |
|
|
|
121 |
$student = self::getDataGenerator()->create_user();
|
|
|
122 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
123 |
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
|
|
|
124 |
|
|
|
125 |
self::setUser($student);
|
|
|
126 |
|
|
|
127 |
// First folder.
|
|
|
128 |
$record = new \stdClass();
|
|
|
129 |
$record->course = $course1->id;
|
|
|
130 |
$record->forcedownload = 1;
|
|
|
131 |
$folder1 = self::getDataGenerator()->create_module('folder', $record);
|
|
|
132 |
|
|
|
133 |
// Second folder.
|
|
|
134 |
$record = new \stdClass();
|
|
|
135 |
$record->course = $course2->id;
|
|
|
136 |
$record->forcedownload = 0;
|
|
|
137 |
$folder2 = self::getDataGenerator()->create_module('folder', $record);
|
|
|
138 |
|
|
|
139 |
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
|
|
|
140 |
$enrol = enrol_get_plugin('manual');
|
|
|
141 |
$enrolinstances = enrol_get_instances($course2->id, true);
|
|
|
142 |
foreach ($enrolinstances as $courseenrolinstance) {
|
|
|
143 |
if ($courseenrolinstance->enrol == "manual") {
|
|
|
144 |
$instance2 = $courseenrolinstance;
|
|
|
145 |
break;
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
|
|
|
149 |
|
|
|
150 |
$returndescription = mod_folder_external::get_folders_by_courses_returns();
|
|
|
151 |
|
|
|
152 |
// Create what we expect to be returned when querying the two courses.
|
|
|
153 |
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'lang', 'revision',
|
|
|
154 |
'timemodified', 'display', 'showexpanded', 'showdownloadfolder', 'section', 'visible',
|
|
|
155 |
'forcedownload', 'groupmode', 'groupingid');
|
|
|
156 |
|
|
|
157 |
// Add expected coursemodule and data.
|
|
|
158 |
$folder1->coursemodule = $folder1->cmid;
|
|
|
159 |
$folder1->introformat = 1;
|
|
|
160 |
$folder1->section = 0;
|
|
|
161 |
$folder1->visible = true;
|
|
|
162 |
$folder1->groupmode = 0;
|
|
|
163 |
$folder1->groupingid = 0;
|
|
|
164 |
$folder1->introfiles = [];
|
|
|
165 |
$folder1->lang = '';
|
|
|
166 |
|
|
|
167 |
$folder2->coursemodule = $folder2->cmid;
|
|
|
168 |
$folder2->introformat = 1;
|
|
|
169 |
$folder2->section = 0;
|
|
|
170 |
$folder2->visible = true;
|
|
|
171 |
$folder2->groupmode = 0;
|
|
|
172 |
$folder2->groupingid = 0;
|
|
|
173 |
$folder2->introfiles = [];
|
|
|
174 |
$folder2->lang = '';
|
|
|
175 |
|
|
|
176 |
foreach ($expectedfields as $field) {
|
|
|
177 |
$expected1[$field] = $folder1->{$field};
|
|
|
178 |
$expected2[$field] = $folder2->{$field};
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$expectedfolders = array($expected2, $expected1);
|
|
|
182 |
|
|
|
183 |
// Call the external function passing course ids.
|
|
|
184 |
$result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id));
|
|
|
185 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
186 |
|
|
|
187 |
$this->assertEquals($expectedfolders, $result['folders']);
|
|
|
188 |
$this->assertCount(0, $result['warnings']);
|
|
|
189 |
|
|
|
190 |
// Call the external function without passing course id.
|
|
|
191 |
$result = mod_folder_external::get_folders_by_courses();
|
|
|
192 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
193 |
|
|
|
194 |
$this->assertEquals($expectedfolders, $result['folders']);
|
|
|
195 |
$this->assertCount(0, $result['warnings']);
|
|
|
196 |
|
|
|
197 |
// Add a file to the intro.
|
|
|
198 |
$fileintroname = "fileintro.txt";
|
|
|
199 |
$filerecordinline = array(
|
|
|
200 |
'contextid' => \context_module::instance($folder2->cmid)->id,
|
|
|
201 |
'component' => 'mod_folder',
|
|
|
202 |
'filearea' => 'intro',
|
|
|
203 |
'itemid' => 0,
|
|
|
204 |
'filepath' => '/',
|
|
|
205 |
'filename' => $fileintroname,
|
|
|
206 |
);
|
|
|
207 |
$fs = get_file_storage();
|
|
|
208 |
$timepost = time();
|
|
|
209 |
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
210 |
|
|
|
211 |
$result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id));
|
|
|
212 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
213 |
|
|
|
214 |
$this->assertCount(1, $result['folders'][0]['introfiles']);
|
|
|
215 |
$this->assertEquals($fileintroname, $result['folders'][0]['introfiles'][0]['filename']);
|
|
|
216 |
|
|
|
217 |
// Unenrol user from second course.
|
|
|
218 |
$enrol->unenrol_user($instance2, $student->id);
|
|
|
219 |
array_shift($expectedfolders);
|
|
|
220 |
|
|
|
221 |
// Call the external function without passing course id.
|
|
|
222 |
$result = mod_folder_external::get_folders_by_courses();
|
|
|
223 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
224 |
|
|
|
225 |
$this->assertEquals($expectedfolders, $result['folders']);
|
|
|
226 |
|
|
|
227 |
// Call for the second course we unenrolled the user from, expected warning.
|
|
|
228 |
$result = mod_folder_external::get_folders_by_courses(array($course2->id));
|
|
|
229 |
$this->assertCount(1, $result['warnings']);
|
|
|
230 |
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
|
|
|
231 |
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
|
|
|
232 |
}
|
|
|
233 |
}
|