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    mod_choice
21
 * @copyright  2018 Jun Pataleta
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace mod_choice\privacy;
25
 
26
use core_privacy\local\metadata\collection;
27
use core_privacy\local\request\deletion_criteria;
28
use mod_choice\privacy\provider;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
/**
33
 * Privacy provider tests class.
34
 *
35
 * @package    mod_choice
36
 * @copyright  2018 Jun Pataleta
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
1441 ariadna 39
final class provider_test extends \core_privacy\tests\provider_testcase {
1 efrain 40
    /** @var stdClass The student object. */
41
    protected $student;
42
 
43
    /** @var stdClass The choice object. */
44
    protected $choice;
45
 
46
    /** @var stdClass The course object. */
47
    protected $course;
48
 
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function setUp(): void {
1441 ariadna 53
        parent::setUp();
1 efrain 54
        $this->resetAfterTest();
55
 
56
        global $DB;
57
        $generator = $this->getDataGenerator();
58
        $course = $generator->create_course();
59
        $options = ['fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'];
60
        $params = [
61
            'course' => $course->id,
62
            'option' => $options,
63
            'name' => 'First Choice Activity',
64
            'showpreview' => 0
65
        ];
66
 
67
        $plugingenerator = $generator->get_plugin_generator('mod_choice');
68
        // The choice activity the user will answer.
69
        $choice = $plugingenerator->create_instance($params);
70
        // Create another choice activity.
71
        $plugingenerator->create_instance($params);
72
        $cm = get_coursemodule_from_instance('choice', $choice->id);
73
 
74
        // Create a student which will make a choice.
75
        $student = $generator->create_user();
76
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
77
        $generator->enrol_user($student->id,  $course->id, $studentrole->id);
78
 
79
        $choicewithoptions = choice_get_choice($choice->id);
80
        $optionids = array_keys($choicewithoptions->option);
81
 
82
        choice_user_submit_response($optionids[2], $choice, $student->id, $course, $cm);
83
        $this->student = $student;
84
        $this->choice = $choice;
85
        $this->course = $course;
86
    }
87
 
88
    /**
89
     * Test for provider::get_metadata().
90
     */
11 efrain 91
    public function test_get_metadata(): void {
1 efrain 92
        $collection = new collection('mod_choice');
93
        $newcollection = provider::get_metadata($collection);
94
        $itemcollection = $newcollection->get_collection();
95
        $this->assertCount(1, $itemcollection);
96
 
97
        $table = reset($itemcollection);
98
        $this->assertEquals('choice_answers', $table->get_name());
99
 
100
        $privacyfields = $table->get_privacy_fields();
101
        $this->assertArrayHasKey('choiceid', $privacyfields);
102
        $this->assertArrayHasKey('optionid', $privacyfields);
103
        $this->assertArrayHasKey('userid', $privacyfields);
104
        $this->assertArrayHasKey('timemodified', $privacyfields);
105
 
106
        $this->assertEquals('privacy:metadata:choice_answers', $table->get_summary());
107
    }
108
 
109
    /**
110
     * Test for provider::get_contexts_for_userid().
111
     */
11 efrain 112
    public function test_get_contexts_for_userid(): void {
1 efrain 113
        $cm = get_coursemodule_from_instance('choice', $this->choice->id);
114
 
115
        $contextlist = provider::get_contexts_for_userid($this->student->id);
116
        $this->assertCount(1, $contextlist);
117
        $contextforuser = $contextlist->current();
118
        $cmcontext = \context_module::instance($cm->id);
119
        $this->assertEquals($cmcontext->id, $contextforuser->id);
120
    }
121
 
122
    /**
123
     * Test for provider::export_user_data().
124
     */
11 efrain 125
    public function test_export_for_context(): void {
1 efrain 126
        $cm = get_coursemodule_from_instance('choice', $this->choice->id);
127
        $cmcontext = \context_module::instance($cm->id);
128
 
129
        // Export all of the data for the context.
130
        $this->export_context_data_for_user($this->student->id, $cmcontext, 'mod_choice');
131
        $writer = \core_privacy\local\request\writer::with_context($cmcontext);
132
        $this->assertTrue($writer->has_any_data());
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
        $choice = $this->choice;
142
        $generator = $this->getDataGenerator();
143
        $cm = get_coursemodule_from_instance('choice', $this->choice->id);
144
 
145
        // Create another student who will answer the choice activity.
146
        $student = $generator->create_user();
147
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
148
        $generator->enrol_user($student->id, $this->course->id, $studentrole->id);
149
 
150
        $choicewithoptions = choice_get_choice($choice->id);
151
        $optionids = array_keys($choicewithoptions->option);
152
 
153
        choice_user_submit_response($optionids[1], $choice, $student->id, $this->course, $cm);
154
 
155
        // Before deletion, we should have 2 responses.
156
        $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
157
        $this->assertEquals(2, $count);
158
 
159
        // Delete data based on context.
160
        $cmcontext = \context_module::instance($cm->id);
161
        provider::delete_data_for_all_users_in_context($cmcontext);
162
 
163
        // After deletion, the choice answers for that choice activity should have been deleted.
164
        $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
165
        $this->assertEquals(0, $count);
166
    }
167
 
168
    /**
169
     * Test for provider::delete_data_for_user().
170
     */
11 efrain 171
    public function test_delete_data_for_user_(): void {
1 efrain 172
        global $DB;
173
 
174
        $choice = $this->choice;
175
        $generator = $this->getDataGenerator();
176
        $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
177
 
178
        // Create a second choice activity.
179
        $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
180
        $params = [
181
            'course' => $this->course->id,
182
            'option' => $options,
183
            'name' => 'Which do you think is the best island in the Philippines?',
184
            'showpreview' => 0
185
        ];
186
        $plugingenerator = $generator->get_plugin_generator('mod_choice');
187
        $choice2 = $plugingenerator->create_instance($params);
188
        $plugingenerator->create_instance($params);
189
        $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
190
 
191
        // Make a selection for the first student for the 2nd choice activity.
192
        $choicewithoptions = choice_get_choice($choice2->id);
193
        $optionids = array_keys($choicewithoptions->option);
194
        choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
195
 
196
        // Create another student who will answer the first choice activity.
197
        $otherstudent = $generator->create_user();
198
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
199
        $generator->enrol_user($otherstudent->id, $this->course->id, $studentrole->id);
200
 
201
        $choicewithoptions = choice_get_choice($choice->id);
202
        $optionids = array_keys($choicewithoptions->option);
203
 
204
        choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
205
 
206
        // Before deletion, we should have 2 responses.
207
        $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
208
        $this->assertEquals(2, $count);
209
 
210
        $context1 = \context_module::instance($cm1->id);
211
        $context2 = \context_module::instance($cm2->id);
212
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->student, 'choice',
213
            [\context_system::instance()->id, $context1->id, $context2->id]);
214
        provider::delete_data_for_user($contextlist);
215
 
216
        // After deletion, the choice answers for the first student should have been deleted.
217
        $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id, 'userid' => $this->student->id]);
218
        $this->assertEquals(0, $count);
219
 
220
        // Confirm that we only have one choice answer available.
221
        $choiceanswers = $DB->get_records('choice_answers');
222
        $this->assertCount(1, $choiceanswers);
223
        $lastresponse = reset($choiceanswers);
224
        // And that it's the other student's response.
225
        $this->assertEquals($otherstudent->id, $lastresponse->userid);
226
    }
227
 
228
    /**
229
     * Test for provider::get_users_in_context().
230
     */
11 efrain 231
    public function test_get_users_in_context(): void {
1 efrain 232
        $cm = get_coursemodule_from_instance('choice', $this->choice->id);
233
        $cmcontext = \context_module::instance($cm->id);
234
 
235
        $userlist = new \core_privacy\local\request\userlist($cmcontext, 'mod_choice');
236
        \mod_choice\privacy\provider::get_users_in_context($userlist);
237
 
238
        $this->assertEquals(
239
                [$this->student->id],
240
                $userlist->get_userids()
241
        );
242
    }
243
 
244
    /**
245
     * Test for provider::get_users_in_context() with invalid context type.
246
     */
11 efrain 247
    public function test_get_users_in_context_invalid_context_type(): void {
1 efrain 248
        $systemcontext = \context_system::instance();
249
 
250
        $userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_choice');
251
        \mod_choice\privacy\provider::get_users_in_context($userlist);
252
 
253
        $this->assertCount(0, $userlist->get_userids());
254
    }
255
 
256
    /**
257
     * Test for provider::delete_data_for_users().
258
     */
11 efrain 259
    public function test_delete_data_for_users(): void {
1 efrain 260
        global $DB;
261
 
262
        $choice = $this->choice;
263
        $generator = $this->getDataGenerator();
264
        $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
265
 
266
        // Create a second choice activity.
267
        $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
268
        $params = [
269
            'course' => $this->course->id,
270
            'option' => $options,
271
            'name' => 'Which do you think is the best island in the Philippines?',
272
            'showpreview' => 0
273
        ];
274
        $plugingenerator = $generator->get_plugin_generator('mod_choice');
275
        $choice2 = $plugingenerator->create_instance($params);
276
        $plugingenerator->create_instance($params);
277
        $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
278
 
279
        // Make a selection for the first student for the 2nd choice activity.
280
        $choicewithoptions = choice_get_choice($choice2->id);
281
        $optionids = array_keys($choicewithoptions->option);
282
        choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
283
 
284
        // Create 2 other students who will answer the first choice activity.
285
        $otherstudent = $generator->create_and_enrol($this->course, 'student');
286
        $anotherstudent = $generator->create_and_enrol($this->course, 'student');
287
 
288
        $choicewithoptions = choice_get_choice($choice->id);
289
        $optionids = array_keys($choicewithoptions->option);
290
 
291
        choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
292
        choice_user_submit_response($optionids[1], $choice, $anotherstudent->id, $this->course, $cm1);
293
 
294
        // Before deletion, we should have 3 responses in the first choice activity.
295
        $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
296
        $this->assertEquals(3, $count);
297
 
298
        $context1 = \context_module::instance($cm1->id);
299
        $approveduserlist = new \core_privacy\local\request\approved_userlist($context1, 'choice',
300
                [$this->student->id, $otherstudent->id]);
301
        provider::delete_data_for_users($approveduserlist);
302
 
303
        // After deletion, the choice answers of the 2 students provided above should have been deleted
304
        // from the first choice activity. So there should only remain 1 answer which is for $anotherstudent.
305
        $choiceanswers = $DB->get_records('choice_answers', ['choiceid' => $choice->id]);
306
        $this->assertCount(1, $choiceanswers);
307
        $lastresponse = reset($choiceanswers);
308
        $this->assertEquals($anotherstudent->id, $lastresponse->userid);
309
 
310
        // Confirm that the answer that was submitted in the other choice activity is intact.
311
        $choiceanswers = $DB->get_records_select('choice_answers', 'choiceid <> ?', [$choice->id]);
312
        $this->assertCount(1, $choiceanswers);
313
        $lastresponse = reset($choiceanswers);
314
        // And that it's for the choice2 activity.
315
        $this->assertEquals($choice2->id, $lastresponse->choiceid);
316
    }
317
}