2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 use core_external\external_api
;
20 use externallib_advanced_testcase
;
21 use mod_label_external
;
23 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
30 * External mod_label functions unit tests
34 * @copyright 2017 Juan Leyva <juan@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class externallib_test
extends externallib_advanced_testcase
{
41 * Test test_mod_label_get_labels_by_courses
43 public function test_mod_label_get_labels_by_courses(): void
{
46 $this->resetAfterTest(true);
48 $course1 = self
::getDataGenerator()->create_course();
49 $course2 = self
::getDataGenerator()->create_course();
51 $student = self
::getDataGenerator()->create_user();
52 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
53 $this->getDataGenerator()->enrol_user($student->id
, $course1->id
, $studentrole->id
);
56 $record = new \
stdClass();
57 $record->course
= $course1->id
;
58 $label1 = self
::getDataGenerator()->create_module('label', $record);
61 $record = new \
stdClass();
62 $record->course
= $course2->id
;
63 $label2 = self
::getDataGenerator()->create_module('label', $record);
65 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
66 $enrol = enrol_get_plugin('manual');
67 $enrolinstances = enrol_get_instances($course2->id
, true);
68 foreach ($enrolinstances as $courseenrolinstance) {
69 if ($courseenrolinstance->enrol
== "manual") {
70 $instance2 = $courseenrolinstance;
74 $enrol->enrol_user($instance2, $student->id
, $studentrole->id
);
76 self
::setUser($student);
78 $returndescription = mod_label_external
::get_labels_by_courses_returns();
80 // Create what we expect to be returned when querying the two courses.
81 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'timemodified',
82 'section', 'visible', 'groupmode', 'groupingid', 'lang');
84 // Add expected coursemodule and data.
85 $label1->coursemodule
= $label1->cmid
;
86 $label1->introformat
= 1;
88 $label1->visible
= true;
89 $label1->groupmode
= 0;
90 $label1->groupingid
= 0;
91 $label1->introfiles
= [];
94 $label2->coursemodule
= $label2->cmid
;
95 $label2->introformat
= 1;
97 $label2->visible
= true;
98 $label2->groupmode
= 0;
99 $label2->groupingid
= 0;
100 $label2->introfiles
= [];
103 foreach ($expectedfields as $field) {
104 $expected1[$field] = $label1->{$field};
105 $expected2[$field] = $label2->{$field};
108 $expectedlabels = array($expected2, $expected1);
110 // Call the external function passing course ids.
111 $result = mod_label_external
::get_labels_by_courses(array($course2->id
, $course1->id
));
112 $result = external_api
::clean_returnvalue($returndescription, $result);
114 $this->assertEquals($expectedlabels, $result['labels']);
115 $this->assertCount(0, $result['warnings']);
117 // Call the external function without passing course id.
118 $result = mod_label_external
::get_labels_by_courses();
119 $result = external_api
::clean_returnvalue($returndescription, $result);
120 $this->assertEquals($expectedlabels, $result['labels']);
121 $this->assertCount(0, $result['warnings']);
123 // Add a file to the intro.
124 $filename = "file.txt";
125 $filerecordinline = array(
126 'contextid' => \context_module
::instance($label2->cmid
)->id
,
127 'component' => 'mod_label',
128 'filearea' => 'intro',
131 'filename' => $filename,
133 $fs = get_file_storage();
135 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
137 $result = mod_label_external
::get_labels_by_courses(array($course2->id
, $course1->id
));
138 $result = external_api
::clean_returnvalue($returndescription, $result);
140 $this->assertCount(1, $result['labels'][0]['introfiles']);
141 $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']);
143 // Unenrol user from second course.
144 $enrol->unenrol_user($instance2, $student->id
);
145 array_shift($expectedlabels);
147 // Call the external function without passing course id.
148 $result = mod_label_external
::get_labels_by_courses();
149 $result = external_api
::clean_returnvalue($returndescription, $result);
150 $this->assertEquals($expectedlabels, $result['labels']);
152 // Call for the second course we unenrolled the user from, expected warning.
153 $result = mod_label_external
::get_labels_by_courses(array($course2->id
));
154 $this->assertCount(1, $result['warnings']);
155 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
156 $this->assertEquals($course2->id
, $result['warnings'][0]['itemid']);