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_data
21
 * @copyright  2018 Marina Glancy
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace mod_data\privacy;
25
 
26
use core_privacy\local\metadata\collection;
27
use core_privacy\local\request\approved_userlist;
28
use mod_data\privacy\provider;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
/**
33
 * Privacy provider tests class.
34
 *
35
 * @package    mod_data
36
 * @copyright  2018 Marina Glancy
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
    /** @var stdClass The student object. */
43
    protected $student2;
44
    /** @var stdClass The student object. */
45
    protected $student3;
46
 
47
    /** @var stdClass The data object. */
48
    protected $datamodule;
49
 
50
    /** @var stdClass The course object. */
51
    protected $course;
52
 
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function setUp(): void {
1441 ariadna 57
        parent::setUp();
1 efrain 58
        $this->resetAfterTest();
59
 
60
        global $DB;
61
        $generator = $this->getDataGenerator();
62
        $course = $generator->create_course();
63
        $params = [
64
            'course' => $course->id,
65
            'name' => 'Database module',
66
            'comments' => 1,
67
            'assessed' => 1,
68
        ];
69
 
70
        // The database activity.
71
        $datamodule = $this->get_generator()->create_instance($params);
72
 
73
        $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
74
            'latlong', 'file', 'picture');
75
        // Creating test Fields with default parameter values.
76
        foreach ($fieldtypes as $count => $fieldtype) {
77
            // Creating variables dynamically.
78
            $fieldname = 'field' . $count;
79
            $record = new \stdClass();
80
            $record->name = $fieldname;
81
            $record->description = $fieldname . ' descr';
82
            $record->type = $fieldtype;
83
 
84
            ${$fieldname} = $this->get_generator()->create_field($record, $datamodule);
85
        }
86
 
87
        $cm = get_coursemodule_from_instance('data', $datamodule->id);
88
 
89
        // Create a student.
90
        $student1 = $generator->create_user();
91
        $student2 = $generator->create_user();
92
        $student3 = $generator->create_user();
93
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
94
        $generator->enrol_user($student1->id,  $course->id, $studentrole->id);
95
        $generator->enrol_user($student2->id,  $course->id, $studentrole->id);
96
        $generator->enrol_user($student3->id,  $course->id, $studentrole->id);
97
 
98
        // Add records.
99
        $this->setUser($student1);
100
        $record1id = $this->generate_data_record($datamodule);
101
        $this->generate_data_record($datamodule);
102
 
103
        $this->setUser($student2);
104
        $this->generate_data_record($datamodule);
105
        $this->generate_data_record($datamodule);
106
        $this->generate_data_record($datamodule);
107
 
108
        $this->setUser($student3);
109
        $this->generate_data_record($datamodule);
110
 
111
        $this->student = $student1;
112
        $this->student2 = $student2;
113
        $this->student3 = $student3;
114
        $this->datamodule = $datamodule;
115
        $this->course = $course;
116
    }
117
 
118
    /**
119
     * Get mod_data generator
120
     *
121
     * @return mod_data_generator
122
     */
123
    protected function get_generator() {
124
        return $this->getDataGenerator()->get_plugin_generator('mod_data');
125
    }
126
 
127
    /**
128
     * Generates one record in the database module as the current student
129
     *
130
     * @param stdClass $datamodule
131
     * @return mixed
132
     */
133
    protected function generate_data_record($datamodule) {
134
        global $DB;
135
 
136
        static $counter = 0;
137
        $counter++;
138
 
139
        $contents = array();
140
        $contents[] = array('opt1', 'opt2', 'opt3', 'opt4');
141
        $contents[] = sprintf("%02f", $counter) . '-01-2000';
142
        $contents[] = 'menu1';
143
        $contents[] = array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4');
144
        $contents[] = 5 * $counter;
145
        $contents[] = 'radioopt1';
146
        $contents[] = 'text for testing' . $counter;
147
        $contents[] = "<p>text area testing $counter<br /></p>";
148
        $contents[] = array('example.url', 'sampleurl' . $counter);
149
        $contents[] = [-31.9489873, 115.8382036]; // Latlong.
150
        $contents[] = "Filename{$counter}.pdf"; // File - filename.
151
        $contents[] = array("Cat{$counter}.jpg", 'Cat' . $counter); // Picture - filename with alt text.
152
        $count = 0;
153
        $fieldcontents = array();
154
        $fields = $DB->get_records('data_fields', array('dataid' => $datamodule->id), 'id');
155
        foreach ($fields as $fieldrecord) {
156
            $fieldcontents[$fieldrecord->id] = $contents[$count++];
157
        }
158
        $tags = ['Cats', 'mice' . $counter];
159
        return $this->get_generator()->create_entry($datamodule, $fieldcontents, 0, $tags);
160
    }
161
 
162
    /**
163
     * Test for provider::get_metadata().
164
     */
11 efrain 165
    public function test_get_metadata(): void {
1 efrain 166
        $collection = new collection('mod_data');
167
        $newcollection = provider::get_metadata($collection);
168
        $itemcollection = $newcollection->get_collection();
169
        $this->assertCount(7, $itemcollection);
170
 
171
        $table = reset($itemcollection);
172
        $this->assertEquals('data_records', $table->get_name());
173
 
174
        $table = next($itemcollection);
175
        $this->assertEquals('data_content', $table->get_name());
176
    }
177
 
178
    /**
179
     * Test for provider::get_contexts_for_userid().
180
     */
11 efrain 181
    public function test_get_contexts_for_userid(): void {
1 efrain 182
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
183
 
184
        $contextlist = provider::get_contexts_for_userid($this->student->id);
185
        $this->assertCount(1, $contextlist);
186
        $contextforuser = $contextlist->current();
187
        $cmcontext = \context_module::instance($cm->id);
188
        $this->assertEquals($cmcontext->id, $contextforuser->id);
189
    }
190
 
191
    /**
192
     * Test for provider::get_users_in_context().
193
     */
11 efrain 194
    public function test_get_users_in_context(): void {
1 efrain 195
        $component = 'mod_data';
196
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
197
        $cmcontext = \context_module::instance($cm->id);
198
 
199
        $userlist = new \core_privacy\local\request\userlist($cmcontext, $component);
200
        provider::get_users_in_context($userlist);
201
 
202
        $this->assertCount(3, $userlist);
203
 
204
        $expected = [$this->student->id, $this->student2->id, $this->student3->id];
205
        $actual = $userlist->get_userids();
206
        sort($expected);
207
        sort($actual);
208
 
209
        $this->assertEquals($expected, $actual);
210
    }
211
 
212
    /**
213
     * Get test privacy writer
214
     *
215
     * @param context $context
216
     * @return \core_privacy\tests\request\content_writer
217
     */
218
    protected function get_writer($context) {
219
        return \core_privacy\local\request\writer::with_context($context);
220
    }
221
 
222
    /**
223
     * Test for provider::export_user_data().
224
     */
11 efrain 225
    public function test_export_for_context(): void {
1 efrain 226
        global $DB;
227
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
228
        $cmcontext = \context_module::instance($cm->id);
229
        $records = $DB->get_records_select('data_records', 'userid = :userid ORDER BY id', ['userid' => $this->student->id]);
230
        $record = reset($records);
231
        $contents = $DB->get_records('data_content', ['recordid' => $record->id]);
232
 
233
        // Export all of the data for the context.
234
        $this->export_context_data_for_user($this->student->id, $cmcontext, 'mod_data');
235
        $writer = $this->get_writer($cmcontext);
236
        $data = $writer->get_data([$record->id]);
237
        $this->assertNotEmpty($data);
238
        foreach ($contents as $content) {
239
            $data = $writer->get_data([$record->id, $content->id]);
240
            $this->assertNotEmpty($data);
241
            $hasfile = in_array($data->field['type'], ['file', 'picture']);
242
            $this->assertEquals($hasfile, !empty($writer->get_files([$record->id, $content->id])));
243
        }
244
        $tags = $writer->get_related_data([$record->id], 'tags');
245
        $this->assertNotEmpty($tags);
246
    }
247
 
248
    /**
249
     * Test for provider::delete_data_for_all_users_in_context().
250
     */
11 efrain 251
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 252
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
253
        $cmcontext = \context_module::instance($cm->id);
254
 
255
        provider::delete_data_for_all_users_in_context($cmcontext);
256
 
257
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
258
        provider::export_user_data($appctxt);
259
        $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
260
    }
261
 
262
    /**
263
     * Test for provider::delete_data_for_user().
264
     */
11 efrain 265
    public function test_delete_data_for_user(): void {
1 efrain 266
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
267
        $cmcontext = \context_module::instance($cm->id);
268
 
269
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
270
        provider::delete_data_for_user($appctxt);
271
 
272
        provider::export_user_data($appctxt);
273
        $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
274
    }
275
 
276
    /**
277
     * Test for provider::delete_data_for_users().
278
     */
11 efrain 279
    public function test_delete_data_for_users(): void {
1 efrain 280
        $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
281
        $cmcontext = \context_module::instance($cm->id);
282
        $userstodelete = [$this->student->id, $this->student2->id];
283
 
284
        // Ensure student, student 2 and student 3 have data before being deleted.
285
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
286
        provider::export_user_data($appctxt);
287
        $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
288
 
289
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student2, 'mod_data', [$cmcontext->id]);
290
        provider::export_user_data($appctxt);
291
        $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
292
 
293
        // Delete data for student 1 and 2.
294
        $approvedlist = new approved_userlist($cmcontext, 'mod_data', $userstodelete);
295
        provider::delete_data_for_users($approvedlist);
296
 
297
        // Reset the writer so it doesn't contain the data from before deletion.
298
        \core_privacy\local\request\writer::reset();
299
 
300
        // Ensure data is now deleted for student and student 2.
301
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
302
        provider::export_user_data($appctxt);
303
 
304
        $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
305
 
306
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student2, 'mod_data', [$cmcontext->id]);
307
        provider::export_user_data($appctxt);
308
 
309
        $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
310
 
311
        // Ensure data still intact for student 3.
312
        $appctxt = new \core_privacy\local\request\approved_contextlist($this->student3, 'mod_data', [$cmcontext->id]);
313
        provider::export_user_data($appctxt);
314
 
315
        $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
316
    }
317
}