Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
/**
18
 * Tests for the enrol_lti_plugin class.
19
 *
20
 * @package enrol_lti
21
 * @copyright 2016 Jun Pataleta <jun@moodle.com>
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace enrol_lti;
25
 
26
use course_enrolment_manager;
27
use enrol_lti_plugin;
28
use IMSGlobal\LTI\ToolProvider\ResourceLink;
29
use IMSGlobal\LTI\ToolProvider\ToolConsumer;
30
use IMSGlobal\LTI\ToolProvider\ToolProvider;
31
use IMSGlobal\LTI\ToolProvider\User;
32
 
33
defined('MOODLE_INTERNAL') || die();
34
 
35
require_once(__DIR__ . '/local/ltiadvantage/lti_advantage_testcase.php');
36
 
37
/**
38
 * Tests for the enrol_lti_plugin class.
39
 *
40
 * @package enrol_lti
41
 * @copyright 2016 Jun Pataleta <jun@moodle.com>
42
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class lib_test extends \lti_advantage_testcase {
45
 
46
    /**
47
     * Test set up.
48
     *
49
     * This is executed before running any tests in this file.
50
     */
51
    public function setUp(): void {
52
        $this->resetAfterTest();
53
        $this->setAdminUser();
54
    }
55
 
56
    /**
57
     * Test for enrol_lti_plugin::delete_instance().
58
     */
59
    public function test_delete_instance() {
60
        global $DB;
61
 
62
        // Create tool enrolment instance.
63
        $data = new \stdClass();
64
        $data->enrolstartdate = time();
65
        $data->secret = 'secret';
66
        $tool = $this->getDataGenerator()->create_lti_tool($data);
67
 
68
        // Create consumer and related data.
69
        $dataconnector = new data_connector();
70
        $consumer = new ToolConsumer('testkey', $dataconnector);
71
        $consumer->secret = $tool->secret;
72
        $consumer->ltiVersion = ToolProvider::LTI_VERSION1;
73
        $consumer->name = 'TEST CONSUMER NAME';
74
        $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
75
        $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
76
        $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
77
        $consumer->enabled = true;
78
        $consumer->protected = true;
79
        $consumer->save();
80
 
81
        $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
82
        $resourcelink->save();
83
 
84
        $ltiuser = User::fromResourceLink($resourcelink, '');
85
        $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId';
86
        $ltiuser->ltiUserId = 'testuserid';
87
        $ltiuser->email = 'user1@example.com';
88
        $ltiuser->save();
89
 
90
        $tp = new tool_provider($tool->id);
91
        $tp->user = $ltiuser;
92
        $tp->resourceLink = $resourcelink;
93
        $tp->consumer = $consumer;
94
        $tp->map_tool_to_consumer();
95
 
96
        $mappingparams = [
97
            'toolid' => $tool->id,
98
            'consumerid' => $tp->consumer->getRecordId()
99
        ];
100
 
101
        // Check first that the related records exist.
102
        $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
103
        $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ]));
104
        $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ]));
105
        $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ]));
106
 
107
        // Perform deletion.
108
        $enrollti = new enrol_lti_plugin();
109
        $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]);
110
        $enrollti->delete_instance($instance);
111
 
112
        // Check that the related records have been deleted.
113
        $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
114
        $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ]));
115
        $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ]));
116
        $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ]));
117
 
118
        // Check that the enrolled users and the tool instance has been deleted.
119
        $this->assertFalse($DB->record_exists('enrol_lti_users', [ 'toolid' => $tool->id ]));
120
        $this->assertFalse($DB->record_exists('enrol_lti_tools', [ 'id' => $tool->id ]));
121
        $this->assertFalse($DB->record_exists('enrol', [ 'id' => $instance->id ]));
122
    }
123
 
124
    /**
125
     * Test confirming that relevant data is removed after enrol instance removal.
126
     *
127
     * @covers \enrol_lti_plugin::delete_instance
128
     */
129
    public function test_delete_instance_lti_advantage() {
130
        global $DB;
131
        // Setup.
132
        [
133
            $course,
134
            $modresource,
135
            $modresource2,
136
            $courseresource,
137
            $registration,
138
            $deployment
139
        ] = $this->create_test_environment();
140
 
141
        // Launch the tool.
142
        $mockuser = $this->get_mock_launch_users_with_ids(['1p3_1'])[0];
143
        $mocklaunch = $this->get_mock_launch($modresource, $mockuser);
144
        $instructoruser = $this->getDataGenerator()->create_user();
145
        $launchservice = $this->get_tool_launch_service();
146
        $launchservice->user_launches_tool($instructoruser, $mocklaunch);
147
 
148
        // Verify data exists.
149
        $this->assertEquals(1, $DB->count_records('enrol_lti_user_resource_link'));
150
        $this->assertEquals(1, $DB->count_records('enrol_lti_resource_link'));
151
        $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration'));
152
        $this->assertEquals(1, $DB->count_records('enrol_lti_deployment'));
153
        $this->assertEquals(1, $DB->count_records('enrol_lti_context'));
154
        $this->assertEquals(1, $DB->count_records('enrol_lti_users'));
155
 
156
        // Now delete the enrol instance.
157
        $enrollti = new enrol_lti_plugin();
158
        $instance = $DB->get_record('enrol', ['id' => $modresource->enrolid]);
159
        $enrollti->delete_instance($instance);
160
 
161
        $this->assertEquals(0, $DB->count_records('enrol_lti_user_resource_link'));
162
        $this->assertEquals(0, $DB->count_records('enrol_lti_resource_link'));
163
        $this->assertEquals(0, $DB->count_records('enrol_lti_users'));
164
 
165
        // App registration, Deployment and Context tables are not affected by instance removal.
166
        $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration'));
167
        $this->assertEquals(1, $DB->count_records('enrol_lti_deployment'));
168
        $this->assertEquals(1, $DB->count_records('enrol_lti_context'));
169
    }
170
 
171
    /**
172
     * Test for getting user enrolment actions.
173
     */
174
    public function test_get_user_enrolment_actions() {
175
        global $CFG, $DB, $PAGE;
176
        $this->resetAfterTest();
177
 
178
        // Set page URL to prevent debugging messages.
179
        $PAGE->set_url('/enrol/editinstance.php');
180
 
181
        $pluginname = 'lti';
182
 
183
        // Only enable the lti enrol plugin.
184
        $CFG->enrol_plugins_enabled = $pluginname;
185
 
186
        $generator = $this->getDataGenerator();
187
 
188
        // Get the enrol plugin.
189
        $plugin = enrol_get_plugin($pluginname);
190
 
191
        // Create a course.
192
        $course = $generator->create_course();
193
        $context = \context_course::instance($course->id);
194
        $teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST);
195
        $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
196
 
197
        // Enable this enrol plugin for the course.
198
        $fields = ['contextid' => $context->id, 'roleinstructor' => $teacherroleid, 'rolelearner' => $studentroleid];
199
        $plugin->add_instance($course, $fields);
200
 
201
        // Create a student.
202
        $student = $generator->create_user();
203
        // Enrol the student to the course.
204
        $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
205
 
206
        // Teachers don't have enrol/lti:unenrol capability by default. Login as admin for simplicity.
207
        $this->setAdminUser();
208
 
209
        require_once($CFG->dirroot . '/enrol/locallib.php');
210
        $manager = new course_enrolment_manager($PAGE, $course);
211
        $userenrolments = $manager->get_user_enrolments($student->id);
212
        $this->assertCount(1, $userenrolments);
213
 
214
        $ue = reset($userenrolments);
215
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
216
        // LTI enrolment has 1 enrol actions for active users -- unenrol.
217
        $this->assertCount(1, $actions);
218
    }
219
 
220
    /**
221
     * Test the behaviour of an enrolment method when the activity to which it provides access is deleted.
222
     *
223
     * @covers \enrol_lti_pre_course_module_delete
224
     */
225
    public function test_course_module_deletion() {
226
        // Create two modules and publish them.
227
        $course = $this->getDataGenerator()->create_course();
228
        $mod = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
229
        $mod2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
230
        $tooldata = [
231
            'cmid' => $mod->cmid,
232
            'courseid' => $course->id,
233
        ];
234
        $tool = $this->getDataGenerator()->create_lti_tool((object)$tooldata);
235
        $tooldata['cmid'] = $mod2->cmid;
236
        $tool2 = $this->getDataGenerator()->create_lti_tool((object)$tooldata);
237
 
238
        // Verify the instances are both enabled.
239
        $modinstance = helper::get_lti_tool($tool->id);
240
        $mod2instance = helper::get_lti_tool($tool2->id);
241
        $this->assertEquals(ENROL_INSTANCE_ENABLED, $modinstance->status);
242
        $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status);
243
 
244
        // Delete a module and verify the associated instance is disabled.
245
        course_delete_module($mod->cmid);
246
        $modinstance = helper::get_lti_tool($tool->id);
247
        $mod2instance = helper::get_lti_tool($tool2->id);
248
        $this->assertEquals(ENROL_INSTANCE_DISABLED, $modinstance->status);
249
        $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status);
250
    }
251
}