MDL-52035 enrol_lti: added fix for oracle
[moodle.git] / enrol / lti / tests / helper_test.php
blob971fa505bb58b639b84ac00f30e93c0db7346172
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.
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/>.
17 /**
18 * Test the helper functionality.
20 * @package enrol_lti
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Test the helper functionality.
30 * @package enrol_lti
31 * @copyright 2016 Mark Nelson <markn@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class enrol_lti_helper_testcase extends advanced_testcase {
36 /**
37 * @var stdClass $user1 A user.
39 public $user1;
41 /**
42 * @var stdClass $user2 A user.
44 public $user2;
46 /**
47 * Test set up.
49 * This is executed before running any test in this file.
51 public function setUp() {
52 $this->resetAfterTest();
54 // Set this user as the admin.
55 $this->setAdminUser();
57 // Get some of the information we need.
58 $this->user1 = self::getDataGenerator()->create_user();
59 $this->user2 = self::getDataGenerator()->create_user();
62 /**
63 * Test the update user profile image function.
65 public function test_update_user_profile_image() {
66 global $DB, $CFG;
68 // Set the profile image.
69 \enrol_lti\helper::update_user_profile_image($this->user1->id, $this->getExternalTestFileUrl('/test.jpg'));
71 // Get the new user record.
72 $this->user1 = $DB->get_record('user', array('id' => $this->user1->id));
74 // Set the page details.
75 $page = new moodle_page();
76 $page->set_url('/user/profile.php');
77 $page->set_context(context_system::instance());
78 $renderer = $page->get_renderer('core');
79 $usercontext = context_user::instance($this->user1->id);
81 // Get the user's profile picture and make sure it is correct.
82 $userpicture = new user_picture($this->user1);
83 $this->assertSame($CFG->wwwroot . '/pluginfile.php/' . $usercontext->id . '/user/icon/clean/f2?rev=' .$this->user1->picture,
84 $userpicture->get_url($page, $renderer)->out(false));
87 /**
88 * Test that we can not enrol past the maximum number of users allowed.
90 public function test_enrol_user_max_enrolled() {
91 global $DB;
93 // Set up the LTI enrolment tool.
94 $data = new stdClass();
95 $data->maxenrolled = 1;
96 $tool = $this->create_tool($data);
98 // Now get all the information we need.
99 $tool = \enrol_lti\helper::get_lti_tool($tool->id);
101 // Enrol a user.
102 $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
104 // Check that the user was enrolled.
105 $this->assertEquals(true, $result);
106 $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
108 // Try and enrol another user - should not happen.
109 $result = \enrol_lti\helper::enrol_user($tool, $this->user2->id);
111 // Check that this user was not enrolled and we are told why.
112 $this->assertEquals(\enrol_lti\helper::ENROLMENT_MAX_ENROLLED, $result);
113 $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
117 * Test that we can not enrol when the enrolment has not started.
119 public function test_enrol_user_enrolment_not_started() {
120 global $DB;
122 // Set up the LTI enrolment tool.
123 $data = new stdClass();
124 $data->enrolstartdate = time() + DAYSECS; // Make sure it is in the future.
125 $tool = $this->create_tool($data);
127 // Now get all the information we need.
128 $tool = \enrol_lti\helper::get_lti_tool($tool->id);
130 // Try and enrol a user - should not happen.
131 $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
133 // Check that this user was not enrolled and we are told why.
134 $this->assertEquals(\enrol_lti\helper::ENROLMENT_NOT_STARTED, $result);
135 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
139 * Test that we can not enrol when the enrolment has finished.
141 public function test_enrol_user_enrolment_finished() {
142 global $DB;
144 // Set up the LTI enrolment tool.
145 $data = new stdClass();
146 $data->enrolenddate = time() - DAYSECS; // Make sure it is in the past.
147 $tool = $this->create_tool($data);
149 // Now get all the information we need.
150 $tool = \enrol_lti\helper::get_lti_tool($tool->id);
152 // Try and enrol a user - should not happen.
153 $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
155 // Check that this user was not enrolled and we are told why.
156 $this->assertEquals(\enrol_lti\helper::ENROLMENT_FINISHED, $result);
157 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
161 * Test returning the number of available tools.
163 public function test_count_lti_tools() {
164 // Create two tools belonging to the same course.
165 $course1 = $this->getDataGenerator()->create_course();
166 $data = new stdClass();
167 $data->courseid = $course1->id;
168 $this->create_tool($data);
169 $this->create_tool($data);
171 // Create two more tools in a separate course.
172 $course2 = $this->getDataGenerator()->create_course();
173 $data = new stdClass();
174 $data->courseid = $course2->id;
175 $this->create_tool($data);
177 // Set the next tool to disabled.
178 $data->status = ENROL_INSTANCE_DISABLED;
179 $this->create_tool($data);
181 // Count all the tools.
182 $count = \enrol_lti\helper::count_lti_tools();
183 $this->assertEquals(4, $count);
185 // Count all the tools in course 1.
186 $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course1->id));
187 $this->assertEquals(2, $count);
189 // Count all the tools in course 2 that are disabled.
190 $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
191 $this->assertEquals(1, $count);
193 // Count all the tools that are enabled.
194 $count = \enrol_lti\helper::count_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
195 $this->assertEquals(3, $count);
199 * Test returning the list of available tools.
201 public function test_get_lti_tools() {
202 // Create two tools belonging to the same course.
203 $course1 = $this->getDataGenerator()->create_course();
204 $data = new stdClass();
205 $data->courseid = $course1->id;
206 $tool1 = $this->create_tool($data);
207 $tool2 = $this->create_tool($data);
209 // Create two more tools in a separate course.
210 $course2 = $this->getDataGenerator()->create_course();
211 $data = new stdClass();
212 $data->courseid = $course2->id;
213 $tool3 = $this->create_tool($data);
215 // Set the next tool to disabled.
216 $data->status = ENROL_INSTANCE_DISABLED;
217 $tool4 = $this->create_tool($data);
219 // Get all the tools.
220 $tools = \enrol_lti\helper::get_lti_tools();
222 // Check that we got all the tools.
223 $this->assertEquals(4, count($tools));
225 // Get all the tools in course 1.
226 $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course1->id));
228 // Check that we got all the tools in course 1.
229 $this->assertEquals(2, count($tools));
230 $this->assertTrue(isset($tools[$tool1->id]));
231 $this->assertTrue(isset($tools[$tool2->id]));
233 // Get all the tools in course 2 that are disabled.
234 $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
236 // Check that we got all the tools in course 2 that are disabled.
237 $this->assertEquals(1, count($tools));
238 $this->assertTrue(isset($tools[$tool4->id]));
240 // Get all the tools that are enabled.
241 $tools = \enrol_lti\helper::get_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
243 // Check that we got all the tools that are enabled.
244 $this->assertEquals(3, count($tools));
245 $this->assertTrue(isset($tools[$tool1->id]));
246 $this->assertTrue(isset($tools[$tool2->id]));
247 $this->assertTrue(isset($tools[$tool3->id]));
251 * Helper function used to create a tool.
253 * @param array $data
254 * @return stdClass the tool
256 protected function create_tool($data = array()) {
257 global $DB;
259 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
260 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
262 // Create a course if no course id was specified.
263 if (empty($data->courseid)) {
264 $course = $this->getDataGenerator()->create_course();
265 $data->courseid = $course->id;
266 } else {
267 $course = get_course($data->courseid);
270 // Set it to enabled if no status was specified.
271 if (!isset($data->status)) {
272 $data->status = ENROL_INSTANCE_ENABLED;
275 // Add some extra necessary fields to the data.
276 $data->name = 'Test LTI';
277 $data->contextid = context_course::instance($data->courseid)->id;
278 $data->roleinstructor = $studentrole->id;
279 $data->rolelearner = $teacherrole->id;
281 // Get the enrol LTI plugin.
282 $enrolplugin = enrol_get_plugin('lti');
283 $instanceid = $enrolplugin->add_instance($course, (array) $data);
285 // Get the tool associated with this instance.
286 return $DB->get_record('enrol_lti_tools', array('enrolid' => $instanceid));