Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
 * Privacy provider tests.
19
 *
20
 * @package    enrol_lti
21
 * @copyright  2018 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace enrol_lti\privacy;
25
 
26
use enrol_lti\privacy\provider;
27
use stdClass;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
/**
32
 * Privacy provider tests class.
33
 *
34
 * @package    enrol_lti
35
 * @copyright  2018 Mark Nelson <markn@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1441 ariadna 38
final class provider_test extends \core_privacy\tests\provider_testcase {
1 efrain 39
 
40
    /**
41
     * @var stdClass The user
42
     */
43
    protected $user = null;
44
 
45
    /**
46
     * @var stdClass Another user
47
     */
48
    protected $anotheruser = null;
49
 
50
    /**
51
     * @var stdClass The course
52
     */
53
    protected $course = null;
54
 
55
    /**
56
     * @var stdClass The activity
57
     */
58
    protected $activity = null;
59
 
60
    /**
61
     * Basic setup for these tests.
62
     */
63
    public function setUp(): void {
1441 ariadna 64
        parent::setUp();
1 efrain 65
        $this->resetAfterTest();
66
 
67
        $this->course = $this->getDataGenerator()->create_course();
68
        $this->user = $this->getDataGenerator()->create_user();
69
        $this->activity = $this->getDataGenerator()->create_module('forum', ['course' => $this->course->id]);
70
 
71
        // Get the course and activity contexts.
72
        $coursecontext = \context_course::instance($this->course->id);
73
        $cmcontext = \context_module::instance($this->activity->cmid);
74
 
75
        // Create LTI tools in different contexts.
76
        $this->create_lti_users($coursecontext, $this->user->id);
77
        $this->create_lti_users($coursecontext, $this->user->id);
78
        $this->create_lti_users($cmcontext, $this->user->id);
79
 
80
        // Create another LTI user.
81
        $this->anotheruser = $this->getDataGenerator()->create_user();
82
        $this->create_lti_users($coursecontext, $this->anotheruser->id);
83
    }
84
 
85
    /**
86
     * Test getting the context for the user ID related to this plugin.
87
     */
11 efrain 88
    public function test_get_contexts_for_userid(): void {
1 efrain 89
        $contextlist = provider::get_contexts_for_userid($this->user->id);
90
 
91
        $this->assertCount(2, $contextlist);
92
 
93
        $coursectx = \context_course::instance($this->course->id);
94
        $activityctx = \context_module::instance($this->activity->cmid);
95
        $expectedids = [$coursectx->id, $activityctx->id];
96
 
97
        $actualids = $contextlist->get_contextids();
98
        $this->assertEqualsCanonicalizing($expectedids, $actualids);
99
    }
100
 
101
    /**
102
     * Test for provider::export_user_data().
103
     */
11 efrain 104
    public function test_export_for_context(): void {
1 efrain 105
        $coursecontext = \context_course::instance($this->course->id);
106
        $cmcontext = \context_module::instance($this->activity->cmid);
107
 
108
        // Export all of the data for the course context.
109
        $this->export_context_data_for_user($this->user->id, $coursecontext, 'enrol_lti');
110
        $writer = \core_privacy\local\request\writer::with_context($coursecontext);
111
        $this->assertTrue($writer->has_any_data());
112
 
113
        $data = (array) $writer->get_data(['enrol_lti_users']);
114
        $this->assertCount(2, $data);
115
        foreach ($data as $ltiuser) {
116
            $this->assertArrayHasKey('lastgrade', $ltiuser);
117
            $this->assertArrayHasKey('timecreated', $ltiuser);
118
            $this->assertArrayHasKey('timemodified', $ltiuser);
119
        }
120
 
121
        // Export all of the data for the activity context.
122
        $this->export_context_data_for_user($this->user->id, $cmcontext, 'enrol_lti');
123
        $writer = \core_privacy\local\request\writer::with_context($cmcontext);
124
        $this->assertTrue($writer->has_any_data());
125
 
126
        $data = (array) $writer->get_data(['enrol_lti_users']);
127
        $this->assertCount(1, $data);
128
        foreach ($data as $ltiuser) {
129
            $this->assertArrayHasKey('lastgrade', $ltiuser);
130
            $this->assertArrayHasKey('timecreated', $ltiuser);
131
            $this->assertArrayHasKey('timemodified', $ltiuser);
132
        }
133
    }
134
 
135
    /**
136
     * Test for provider::delete_data_for_all_users_in_context().
137
     */
11 efrain 138
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 139
        global $DB;
140
 
141
        $count = $DB->count_records('enrol_lti_users');
142
        $this->assertEquals(4, $count);
143
 
144
        // Delete data based on context.
145
        $coursecontext = \context_course::instance($this->course->id);
146
        provider::delete_data_for_all_users_in_context($coursecontext);
147
 
148
        $ltiusers = $DB->get_records('enrol_lti_users');
149
        $this->assertCount(1, $ltiusers);
150
 
151
        $ltiuser = reset($ltiusers);
152
        $this->assertEquals($ltiuser->userid, $this->user->id);
153
    }
154
 
155
    /**
156
     * Test for provider::delete_data_for_user().
157
     */
11 efrain 158
    public function test_delete_data_for_user(): void {
1 efrain 159
        global $DB;
160
 
161
        $cmcontext = \context_module::instance($this->activity->cmid);
162
        $coursecontext = \context_course::instance($this->course->id);
163
 
164
        $count = $DB->count_records('enrol_lti_users');
165
        $this->assertEquals(4, $count);
166
 
167
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->user, 'enrol_lti',
168
            [\context_system::instance()->id, $coursecontext->id, $cmcontext->id]);
169
        provider::delete_data_for_user($contextlist);
170
 
171
        $ltiusers = $DB->get_records('enrol_lti_users');
172
        $this->assertCount(1, $ltiusers);
173
 
174
        $ltiuser = reset($ltiusers);
175
        $this->assertNotEquals($ltiuser->userid, $this->user->id);
176
    }
177
 
178
    /**
179
     * Creates a LTI user given the provided context
180
     *
181
     * @param \context $context
182
     * @param int $userid
183
     */
184
    private function create_lti_users(\context $context, int $userid) {
185
        global $DB;
186
 
187
        // Create a tool.
188
        $ltitool = (object) [
189
            'enrolid' => 5,
190
            'contextid' => $context->id,
191
            'roleinstructor' => 5,
192
            'rolelearner' => 5,
193
            'timecreated' => time(),
194
            'timemodified' => time() + DAYSECS
195
        ];
196
        $toolid = $DB->insert_record('enrol_lti_tools', $ltitool);
197
 
198
        // Create a user.
199
        $ltiuser = (object) [
200
            'userid' => $userid,
201
            'toolid' => $toolid,
202
            'lastgrade' => 50,
203
            'lastaccess' => time() + DAYSECS,
204
            'timecreated' => time()
205
        ];
206
        $DB->insert_record('enrol_lti_users', $ltiuser);
207
    }
208
 
209
    /**
210
     * Test for provider::get_users_in_context() when the context is a course.
211
     */
11 efrain 212
    public function test_get_users_in_context_course(): void {
1 efrain 213
        $coursecontext = \context_course::instance($this->course->id);
214
        $userlist = new \core_privacy\local\request\userlist($coursecontext, 'enrol_paypal');
215
        provider::get_users_in_context($userlist);
216
 
217
        $this->assertEqualsCanonicalizing(
218
                [$this->user->id, $this->anotheruser->id],
219
                $userlist->get_userids());
220
    }
221
 
222
    /**
223
     * Test for provider::get_users_in_context() when the context is an activity.
224
     */
11 efrain 225
    public function test_get_users_in_context_activity(): void {
1 efrain 226
        $activityctx = \context_module::instance($this->activity->cmid);
227
        $userlist = new \core_privacy\local\request\userlist($activityctx, 'enrol_paypal');
228
        provider::get_users_in_context($userlist);
229
 
230
        $this->assertEquals(
231
                [$this->user->id],
232
                $userlist->get_userids());
233
    }
234
 
235
    /**
236
     * Test for provider::delete_data_for_users() when the context is a course.
237
     */
11 efrain 238
    public function test_delete_data_for_users_course(): void {
1 efrain 239
        global $DB;
240
 
241
        $coursecontext = \context_course::instance($this->course->id);
242
 
243
        $count = $DB->count_records('enrol_lti_users');
244
        $this->assertEquals(4, $count);
245
 
246
        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext, 'enrol_paypal',
247
                [$this->user->id]);
248
        provider::delete_data_for_users($approveduserlist);
249
 
250
        $ltiusers = $DB->get_records('enrol_lti_users');
251
        $this->assertCount(2, $ltiusers);
252
 
253
        foreach ($ltiusers as $ltiuser) {
254
            $leftover = false;
255
            if ($ltiuser->userid == $this->user->id) {
256
                $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
257
                if ($contextid == $coursecontext->id) {
258
                    $leftover = true;
259
                }
260
            }
261
        }
262
        $this->assertFalse($leftover);
263
    }
264
 
265
    /**
266
     * Test for provider::delete_data_for_users() when the context is an activity.
267
     */
11 efrain 268
    public function test_delete_data_for_users_activity(): void {
1 efrain 269
        global $DB;
270
 
271
        $cmcontext = \context_module::instance($this->activity->cmid);
272
 
273
        $count = $DB->count_records('enrol_lti_users');
274
        $this->assertEquals(4, $count);
275
 
276
        $approveduserlist = new \core_privacy\local\request\approved_userlist($cmcontext, 'enrol_paypal',
277
                [$this->user->id]);
278
        provider::delete_data_for_users($approveduserlist);
279
 
280
        $ltiusers = $DB->get_records('enrol_lti_users');
281
        $this->assertCount(3, $ltiusers);
282
 
283
        foreach ($ltiusers as $ltiuser) {
284
            $leftover = false;
285
            if ($ltiuser->userid == $this->user->id) {
286
                $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
287
                if ($contextid == $cmcontext->id) {
288
                    $leftover = true;
289
                }
290
            }
291
        }
292
        $this->assertFalse($leftover);
293
    }
294
}