Commit | Line | Data |
---|---|---|
9f1ab2db JL |
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 | * Workshop module external functions tests | |
19 | * | |
20 | * @package mod_workshop | |
21 | * @category external | |
22 | * @copyright 2017 Juan Leyva <juan@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @since Moodle 3.4 | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | ||
31 | require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | |
32 | require_once($CFG->dirroot . '/mod/workshop/lib.php'); | |
33 | ||
34 | use mod_workshop\external\workshop_summary_exporter; | |
35 | ||
36 | /** | |
37 | * Workshop module external functions tests | |
38 | * | |
39 | * @package mod_workshop | |
40 | * @category external | |
41 | * @copyright 2017 Juan Leyva <juan@moodle.com> | |
42 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
43 | * @since Moodle 3.4 | |
44 | */ | |
45 | class mod_workshop_external_testcase extends externallib_advanced_testcase { | |
46 | ||
47 | /** @var stdClass course object */ | |
48 | private $course; | |
49 | /** @var stdClass workshop object */ | |
50 | private $workshop; | |
51 | /** @var stdClass context object */ | |
52 | private $context; | |
53 | /** @var stdClass cm object */ | |
54 | private $cm; | |
55 | /** @var stdClass student object */ | |
56 | private $student; | |
57 | /** @var stdClass teacher object */ | |
58 | private $teacher; | |
59 | /** @var stdClass student role object */ | |
60 | private $studentrole; | |
61 | /** @var stdClass teacher role object */ | |
62 | private $teacherrole; | |
63 | ||
64 | /** | |
65 | * Set up for every test | |
66 | */ | |
67 | public function setUp() { | |
68 | global $DB; | |
69 | $this->resetAfterTest(); | |
70 | $this->setAdminUser(); | |
71 | ||
72 | // Setup test data. | |
73 | $this->course = $this->getDataGenerator()->create_course(); | |
74 | $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course->id)); | |
75 | $this->context = context_module::instance($this->workshop->cmid); | |
76 | $this->cm = get_coursemodule_from_instance('workshop', $this->workshop->id); | |
77 | ||
78 | // Create users. | |
79 | $this->student = self::getDataGenerator()->create_user(); | |
80 | $this->teacher = self::getDataGenerator()->create_user(); | |
81 | ||
82 | // Users enrolments. | |
83 | $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); | |
84 | $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); | |
85 | $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); | |
86 | $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Test test_mod_workshop_get_workshops_by_courses | |
91 | */ | |
92 | public function test_mod_workshop_get_workshops_by_courses() { | |
93 | global $DB; | |
94 | ||
95 | // Create additional course. | |
96 | $course2 = self::getDataGenerator()->create_course(); | |
97 | ||
98 | // Second workshop. | |
99 | $record = new stdClass(); | |
100 | $record->course = $course2->id; | |
101 | $workshop2 = self::getDataGenerator()->create_module('workshop', $record); | |
102 | ||
103 | // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. | |
104 | $enrol = enrol_get_plugin('manual'); | |
105 | $enrolinstances = enrol_get_instances($course2->id, true); | |
106 | foreach ($enrolinstances as $courseenrolinstance) { | |
107 | if ($courseenrolinstance->enrol == "manual") { | |
108 | $instance2 = $courseenrolinstance; | |
109 | break; | |
110 | } | |
111 | } | |
112 | $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id); | |
113 | ||
114 | self::setUser($this->student); | |
115 | ||
116 | $returndescription = mod_workshop_external::get_workshops_by_courses_returns(); | |
117 | ||
118 | // Create what we expect to be returned when querying the two courses. | |
119 | $properties = workshop_summary_exporter::read_properties_definition(); | |
120 | $expectedfields = array_keys($properties); | |
121 | ||
122 | // Add expected coursemodule and data. | |
123 | $workshop1 = $this->workshop; | |
124 | $workshop1->coursemodule = $workshop1->cmid; | |
125 | $workshop1->introformat = 1; | |
126 | $workshop1->introfiles = []; | |
127 | $workshop1->instructauthorsfiles = []; | |
128 | $workshop1->instructauthorsformat = 1; | |
129 | $workshop1->instructreviewersfiles = []; | |
130 | $workshop1->instructreviewersformat = 1; | |
131 | $workshop1->conclusionfiles = []; | |
132 | $workshop1->conclusionformat = 1; | |
133 | ||
134 | $workshop2->coursemodule = $workshop2->cmid; | |
135 | $workshop2->introformat = 1; | |
136 | $workshop2->introfiles = []; | |
137 | $workshop2->instructauthorsfiles = []; | |
138 | $workshop2->instructauthorsformat = 1; | |
139 | $workshop2->instructreviewersfiles = []; | |
140 | $workshop2->instructreviewersformat = 1; | |
141 | $workshop2->conclusionfiles = []; | |
142 | $workshop2->conclusionformat = 1; | |
143 | ||
144 | foreach ($expectedfields as $field) { | |
145 | if (!empty($properties[$field]) && $properties[$field]['type'] == PARAM_BOOL) { | |
146 | $workshop1->{$field} = (bool) $workshop1->{$field}; | |
147 | $workshop2->{$field} = (bool) $workshop2->{$field}; | |
148 | } | |
149 | $expected1[$field] = $workshop1->{$field}; | |
150 | $expected2[$field] = $workshop2->{$field}; | |
151 | } | |
152 | ||
153 | $expectedworkshops = array($expected2, $expected1); | |
154 | ||
155 | // Call the external function passing course ids. | |
156 | $result = mod_workshop_external::get_workshops_by_courses(array($course2->id, $this->course->id)); | |
157 | $result = external_api::clean_returnvalue($returndescription, $result); | |
158 | ||
159 | $this->assertEquals($expectedworkshops, $result['workshops']); | |
160 | $this->assertCount(0, $result['warnings']); | |
161 | ||
162 | // Call the external function without passing course id. | |
163 | $result = mod_workshop_external::get_workshops_by_courses(); | |
164 | $result = external_api::clean_returnvalue($returndescription, $result); | |
165 | $this->assertEquals($expectedworkshops, $result['workshops']); | |
166 | $this->assertCount(0, $result['warnings']); | |
167 | ||
168 | // Unenrol user from second course and alter expected workshops. | |
169 | $enrol->unenrol_user($instance2, $this->student->id); | |
170 | array_shift($expectedworkshops); | |
171 | ||
172 | // Call the external function without passing course id. | |
173 | $result = mod_workshop_external::get_workshops_by_courses(); | |
174 | $result = external_api::clean_returnvalue($returndescription, $result); | |
175 | $this->assertEquals($expectedworkshops, $result['workshops']); | |
176 | ||
177 | // Call for the second course we unenrolled the user from, expected warning. | |
178 | $result = mod_workshop_external::get_workshops_by_courses(array($course2->id)); | |
179 | $this->assertCount(1, $result['warnings']); | |
180 | $this->assertEquals('1', $result['warnings'][0]['warningcode']); | |
181 | $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); | |
182 | } | |
183 | } |