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/>.
18 * Lesson module external functions tests
22 * @copyright 2017 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/mod/lesson/locallib.php');
35 * Lesson module external functions tests
39 * @copyright 2017 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class mod_lesson_external_testcase extends externallib_advanced_testcase {
46 * Set up for every test
48 public function setUp() {
50 $this->resetAfterTest();
51 $this->setAdminUser();
54 $this->course = $this->getDataGenerator()->create_course();
55 $this->lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $this->course->id));
56 $this->context = context_module::instance($this->lesson->cmid);
57 $this->cm = get_coursemodule_from_instance('lesson', $this->lesson->id);
60 $this->student = self::getDataGenerator()->create_user();
61 $this->teacher = self::getDataGenerator()->create_user();
64 $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
65 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
66 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
67 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
72 * Test test_mod_lesson_get_lessons_by_courses
74 public function test_mod_lesson_get_lessons_by_courses() {
77 // Create additional course.
78 $course2 = self::getDataGenerator()->create_course();
81 $record = new stdClass();
82 $record->course = $course2->id;
83 $lesson2 = self::getDataGenerator()->create_module('lesson', $record);
85 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
86 $enrol = enrol_get_plugin('manual');
87 $enrolinstances = enrol_get_instances($course2->id, true);
88 foreach ($enrolinstances as $courseenrolinstance) {
89 if ($courseenrolinstance->enrol == "manual") {
90 $instance2 = $courseenrolinstance;
94 $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
96 self::setUser($this->student);
98 $returndescription = mod_lesson_external::get_lessons_by_courses_returns();
100 // Create what we expect to be returned when querying the two courses.
101 // First for the student user.
102 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'practice',
103 'modattempts', 'usepassword', 'grade', 'custom', 'ongoing', 'usemaxgrade',
104 'maxanswers', 'maxattempts', 'review', 'nextpagedefault', 'feedback', 'minquestions',
105 'maxpages', 'timelimit', 'retake', 'mediafile', 'mediafiles', 'mediaheight', 'mediawidth',
106 'mediaclose', 'slideshow', 'width', 'height', 'bgcolor', 'displayleft', 'displayleftif',
109 // Add expected coursemodule and data.
110 $lesson1 = $this->lesson;
111 $lesson1->coursemodule = $lesson1->cmid;
112 $lesson1->introformat = 1;
113 $lesson1->section = 0;
114 $lesson1->visible = true;
115 $lesson1->groupmode = 0;
116 $lesson1->groupingid = 0;
117 $lesson1->introfiles = [];
118 $lesson1->mediafiles = [];
120 $lesson2->coursemodule = $lesson2->cmid;
121 $lesson2->introformat = 1;
122 $lesson2->section = 0;
123 $lesson2->visible = true;
124 $lesson2->groupmode = 0;
125 $lesson2->groupingid = 0;
126 $lesson2->introfiles = [];
127 $lesson2->mediafiles = [];
129 foreach ($expectedfields as $field) {
130 $expected1[$field] = $lesson1->{$field};
131 $expected2[$field] = $lesson2->{$field};
134 $expectedlessons = array($expected2, $expected1);
136 // Call the external function passing course ids.
137 $result = mod_lesson_external::get_lessons_by_courses(array($course2->id, $this->course->id));
138 $result = external_api::clean_returnvalue($returndescription, $result);
140 $this->assertEquals($expectedlessons, $result['lessons']);
141 $this->assertCount(0, $result['warnings']);
143 // Call the external function without passing course id.
144 $result = mod_lesson_external::get_lessons_by_courses();
145 $result = external_api::clean_returnvalue($returndescription, $result);
146 $this->assertEquals($expectedlessons, $result['lessons']);
147 $this->assertCount(0, $result['warnings']);
149 // Unenrol user from second course and alter expected lessons.
150 $enrol->unenrol_user($instance2, $this->student->id);
151 array_shift($expectedlessons);
153 // Call the external function without passing course id.
154 $result = mod_lesson_external::get_lessons_by_courses();
155 $result = external_api::clean_returnvalue($returndescription, $result);
156 $this->assertEquals($expectedlessons, $result['lessons']);
158 // Call for the second course we unenrolled the user from, expected warning.
159 $result = mod_lesson_external::get_lessons_by_courses(array($course2->id));
160 $this->assertCount(1, $result['warnings']);
161 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
162 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
164 // Now, try as a teacher for getting all the additional fields.
165 self::setUser($this->teacher);
167 $additionalfields = array('password', 'dependency', 'conditions', 'activitylink', 'available', 'deadline',
168 'timemodified', 'completionendreached', 'completiontimespent');
170 foreach ($additionalfields as $field) {
171 $expectedlessons[0][$field] = $lesson1->{$field};
174 $result = mod_lesson_external::get_lessons_by_courses();
175 $result = external_api::clean_returnvalue($returndescription, $result);
176 $this->assertEquals($expectedlessons, $result['lessons']);
178 // Admin also should get all the information.
179 self::setAdminUser();
181 $result = mod_lesson_external::get_lessons_by_courses(array($this->course->id));
182 $result = external_api::clean_returnvalue($returndescription, $result);
183 $this->assertEquals($expectedlessons, $result['lessons']);
185 // Now, add a restriction.
186 $this->setUser($this->student);
187 $DB->set_field('lesson', 'usepassword', 1, array('id' => $lesson1->id));
188 $DB->set_field('lesson', 'password', 'abc', array('id' => $lesson1->id));
190 $lessons = mod_lesson_external::get_lessons_by_courses(array($this->course->id));
191 $lessons = external_api::clean_returnvalue(mod_lesson_external::get_lessons_by_courses_returns(), $lessons);
192 $this->assertFalse(isset($lessons['lessons'][0]['intro']));