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
 * Data provider tests.
19
 *
20
 * @package    mod_wiki
21
 * @category   test
22
 * @copyright  2018 Marina Glancy
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace mod_wiki\privacy;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
global $CFG;
29
 
30
use core_privacy\tests\provider_testcase;
31
use mod_wiki\privacy\provider;
32
use core_privacy\local\request\approved_contextlist;
33
use core_privacy\local\request\approved_userlist;
34
use core_privacy\local\request\writer;
35
 
36
require_once($CFG->dirroot.'/mod/wiki/locallib.php');
37
 
38
/**
39
 * Data provider testcase class.
40
 *
41
 * @package    mod_wiki
42
 * @category   test
43
 * @copyright  2018 Marina Glancy
44
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
1441 ariadna 46
final class provider_test extends provider_testcase {
1 efrain 47
 
48
    /** @var array */
49
    protected $users = [];
50
    /** @var array */
51
    protected $pages = [];
52
    /** @var array */
53
    protected $contexts = [];
54
    /** @var array */
55
    protected $subwikis = [];
56
    /** @var array */
57
    protected $pagepaths = [];
58
 
59
    /**
60
     * Set up for each test.
61
     *
62
     * There are three users and four wikis.
63
     * 1 : collaborative wiki, has context $this->contexts[1] and has a single subwiki $this->subwikis[1]
64
     * 2 : individual wiki, has context $this->contexts[2] and three subwikis (one for each user):
65
     *        $this->subwikis[21], $this->subwikis[22], $this->subwikis[23],
66
     *        the subwiki for the third user is empty
67
     * 3 : collaborative wiki, has context $this->contexts[3] and has a single subwiki $this->subwikis[3]
68
     * 4 : collaborative wiki, has context $this->contexts[4], this wiki is empty
69
     *
70
     * Each subwiki (except for "23") has pages, for example, in $this->subwiki[1] there are pages
71
     *   $this->pages[1][1], $this->pages[1][2] and $this->pages[1][3]
72
     *   In the export data they have paths:
73
     *   $this->pagepaths[1][1], $this->pagepaths[1][2], $this->pagepaths[1][3]
74
     */
75
    public function setUp(): void {
76
        global $DB;
1441 ariadna 77
        parent::setUp();
1 efrain 78
        $this->resetAfterTest();
79
 
80
        $dg = $this->getDataGenerator();
81
        $course = $dg->create_course();
82
 
83
        $this->users[1] = $dg->create_user();
84
        $this->users[2] = $dg->create_user();
85
        $this->users[3] = $dg->create_user();
86
        $this->users[4] = $dg->create_user();
87
 
88
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
89
        $this->getDataGenerator()->enrol_user($this->users[1]->id, $course->id, $studentrole->id, 'manual');
90
        $this->getDataGenerator()->enrol_user($this->users[2]->id, $course->id, $studentrole->id, 'manual');
91
        $this->getDataGenerator()->enrol_user($this->users[3]->id, $course->id, $studentrole->id, 'manual');
92
        $this->getDataGenerator()->enrol_user($this->users[4]->id, $course->id, $studentrole->id, 'manual');
93
 
94
        $cm1 = $this->getDataGenerator()->create_module('wiki', ['course' => $course->id]);
95
        $cm2 = $this->getDataGenerator()->create_module('wiki', ['course' => $course->id, 'wikimode' => 'individual']);
96
        $cm3 = $this->getDataGenerator()->create_module('wiki', ['course' => $course->id]);
97
        $cm4 = $this->getDataGenerator()->create_module('wiki', ['course' => $course->id]); // Empty.
98
 
99
        // User1.
100
        $this->setUser($this->users[1]);
101
 
102
        // Create and modify pages in collaborative wiki.
103
        $this->pages[1][1] = $this->create_first_page($cm1);
104
        $this->pages[1][2] = $this->create_page($cm1, ['content' => 'initial content']);
105
        $this->update_page($cm1, $this->pages[1][2], ['content' => 'update1 <img src="@@PLUGINFILE@@/Dog%20jump.jpg">']);
106
        $this->attach_file($cm1, "Dog jump.jpg", 'jpg:Doggy');
107
        $this->update_page($cm1, $this->pages[1][2], ['content' => 'update2']);
108
 
109
        // Create pages in individual wiki, add files that are not used in text.
110
        $this->pages[21][1] = $this->create_first_page($cm2);
111
        $this->pages[21][2] = $this->create_page($cm2);
112
        $this->attach_file($cm2, "mycat.jpg", 'jpg:Cat');
113
 
114
        // User2.
115
        $this->setUser($this->users[2]);
116
 
117
        // Modify existing pages in the first collaborative wiki.
118
        $this->update_page($cm1, $this->pages[1][2], ['content' => 'update3 <img src="@@PLUGINFILE@@/Hamster.jpg">']);
119
        $this->attach_file($cm1, "Hamster.jpg", 'jpg:Hamster');
120
 
121
        // Create pages in individual wiki.
122
        $this->pages[22][1] = $this->create_first_page($cm2);
123
        $this->pages[22][2] = $this->create_page($cm2);
124
 
125
        // Create pages in the third wiki.
126
        $this->pages[3][1] = $this->create_first_page($cm3);
127
 
128
        // User3 (testing locks and empty subwiki).
129
        $this->setUser($this->users[3]);
130
 
131
        // Create a subwiki in the individual wiki without any pages.
132
        $subwiki23 = $dg->get_plugin_generator('mod_wiki')->get_subwiki($cm2);
133
 
134
        // Create a page in the first wiki and then lock it.
135
        $this->pages[1][3] = $this->create_page($cm1);
136
        wiki_set_lock($this->pages[1][3]->id, $this->users[3]->id, null, true);
137
 
138
        // Lock a page in the third wiki without having any revisions on it.
139
        wiki_set_lock($this->pages[3][1]->id, $this->users[3]->id, null, true);
140
 
141
        // User 4 - added to the first wiki, so all users are not part of all edited contexts.
142
        $this->setUser($this->users[4]);
143
        $this->pages[1][4] = $this->create_page($cm1);
144
 
145
        $this->subwikis = [
146
            1 => $this->pages[1][1]->subwikiid,
147
            21 => $this->pages[21][1]->subwikiid,
148
            22 => $this->pages[22][1]->subwikiid,
149
            23 => $subwiki23,
150
            3 => $this->pages[3][1]->subwikiid,
151
        ];
152
 
153
        $this->contexts = [
154
            1 => \context_module::instance($cm1->cmid),
155
            2 => \context_module::instance($cm2->cmid),
156
            3 => \context_module::instance($cm3->cmid),
157
            4 => \context_module::instance($cm4->cmid),
158
        ];
159
 
160
        $this->pagepaths = [
161
            1 => [
162
                1 => $this->pages[1][1]->id . ' ' . $this->pages[1][1]->title,
163
                2 => $this->pages[1][2]->id . ' ' . $this->pages[1][2]->title,
164
                3 => $this->pages[1][3]->id . ' ' . $this->pages[1][3]->title,
165
                4 => $this->pages[1][4]->id . ' ' . $this->pages[1][4]->title,
166
            ],
167
            21 => [
168
                1 => $this->pages[21][1]->id . ' ' . $this->pages[21][1]->title,
169
                2 => $this->pages[21][2]->id . ' ' . $this->pages[21][2]->title,
170
            ],
171
            22 => [
172
                1 => $this->pages[22][1]->id . ' ' . $this->pages[22][1]->title,
173
                2 => $this->pages[22][2]->id . ' ' . $this->pages[22][2]->title,
174
            ],
175
            3 => [
176
                1 => $this->pages[3][1]->id . ' ' . $this->pages[3][1]->title,
177
            ]
178
        ];
179
    }
180
 
181
    /**
182
     * Generate first page in wiki as current user
183
     *
184
     * @param stdClass $wiki
185
     * @param array $record
186
     * @return mixed
187
     */
188
    protected function create_first_page($wiki, $record = []) {
189
        $dg = $this->getDataGenerator();
190
        $wg = $dg->get_plugin_generator('mod_wiki');
191
        return $wg->create_first_page($wiki, $record);
192
    }
193
 
194
    /**
195
     * Generate a page in wiki as current user
196
     *
197
     * @param stdClass $wiki
198
     * @param array $record
199
     * @return mixed
200
     */
201
    protected function create_page($wiki, $record = []) {
202
        $dg = $this->getDataGenerator();
203
        $wg = $dg->get_plugin_generator('mod_wiki');
204
        return $wg->create_page($wiki, $record);
205
    }
206
 
207
    /**
208
     * Update an existing page in wiki as current user
209
     *
210
     * @param stdClass $wiki
211
     * @param stdClass $page
212
     * @param array $record
213
     * @return mixed
214
     */
215
    protected function update_page($wiki, $page, $record = []) {
216
        $dg = $this->getDataGenerator();
217
        $wg = $dg->get_plugin_generator('mod_wiki');
218
        return $wg->create_page($wiki, ['title' => $page->title] + $record);
219
    }
220
 
221
    /**
222
     * Attach file to a wiki as a current user
223
     *
224
     * @param stdClass $wiki
225
     * @param string $filename
226
     * @param string $filecontent
227
     * @return stored_file
228
     */
229
    protected function attach_file($wiki, $filename, $filecontent) {
230
        $dg = $this->getDataGenerator();
231
        $wg = $dg->get_plugin_generator('mod_wiki');
232
        $subwikiid = $wg->get_subwiki($wiki);
233
 
234
        $fs = get_file_storage();
235
        return $fs->create_file_from_string([
236
            'contextid' => \context_module::instance($wiki->cmid)->id,
237
            'component' => 'mod_wiki',
238
            'filearea' => 'attachments',
239
            'itemid' => $subwikiid,
240
            'filepath' => '/',
241
            'filename' => $filename,
242
        ], $filecontent);
243
    }
244
 
245
    /**
246
     * Test getting the contexts for a user.
247
     */
11 efrain 248
    public function test_get_contexts_for_userid(): void {
1 efrain 249
 
250
        // Get contexts for the first user.
251
        $contextids = provider::get_contexts_for_userid($this->users[1]->id)->get_contextids();
252
        $this->assertEqualsCanonicalizing([
253
            $this->contexts[1]->id,
254
            $this->contexts[2]->id,
1441 ariadna 255
        ], array_values($contextids));
1 efrain 256
 
257
        // Get contexts for the second user.
258
        $contextids = provider::get_contexts_for_userid($this->users[2]->id)->get_contextids();
259
        $this->assertEqualsCanonicalizing([
260
            $this->contexts[1]->id,
261
            $this->contexts[2]->id,
262
            $this->contexts[3]->id,
1441 ariadna 263
        ], array_values($contextids));
1 efrain 264
 
265
        // Get contexts for the third user.
266
        $contextids = provider::get_contexts_for_userid($this->users[3]->id)->get_contextids();
267
        $this->assertEqualsCanonicalizing([
268
            $this->contexts[1]->id,
269
            $this->contexts[2]->id,
270
            $this->contexts[3]->id,
1441 ariadna 271
        ], array_values($contextids));
1 efrain 272
    }
273
 
274
    /**
275
     * Test getting the users within a context.
276
     */
11 efrain 277
    public function test_get_users_in_context(): void {
1 efrain 278
        global $DB;
279
        $component = 'mod_wiki';
280
 
281
        // Add a comment from user 4 in context 3.
282
        $this->setUser($this->users[4]);
283
        $this->add_comment($this->pages[3][1], 'Look at me, getting involved!');
284
 
285
        // Ensure userlist for context 1 contains all users.
286
        $userlist = new \core_privacy\local\request\userlist($this->contexts[1], $component);
287
        provider::get_users_in_context($userlist);
288
 
289
        $this->assertCount(4, $userlist);
290
 
291
        $expected = [$this->users[1]->id, $this->users[2]->id, $this->users[3]->id, $this->users[4]->id];
292
        $actual = $userlist->get_userids();
293
        sort($expected);
294
        sort($actual);
295
        $this->assertEquals($expected, $actual);
296
 
297
        // Ensure userlist for context 2 contains users 1-3 only.
298
        $userlist = new \core_privacy\local\request\userlist($this->contexts[2], $component);
299
        provider::get_users_in_context($userlist);
300
 
301
        $this->assertCount(3, $userlist);
302
 
303
        $expected = [$this->users[1]->id, $this->users[2]->id, $this->users[3]->id];
304
        $actual = $userlist->get_userids();
305
        sort($expected);
306
        sort($actual);
307
        $this->assertEquals($expected, $actual);
308
 
309
        // Ensure userlist for context 3 contains users 2, 3 and 4 only.
310
        $userlist = new \core_privacy\local\request\userlist($this->contexts[3], $component);
311
        provider::get_users_in_context($userlist);
312
 
313
        $this->assertCount(3, $userlist);
314
 
315
        $expected = [$this->users[2]->id, $this->users[3]->id, $this->users[4]->id];
316
        $actual = $userlist->get_userids();
317
        sort($expected);
318
        sort($actual);
319
        $this->assertEquals($expected, $actual);
320
 
321
        // Ensure userlist for context 4 is empty.
322
        $userlist = new \core_privacy\local\request\userlist($this->contexts[4], $component);
323
        provider::get_users_in_context($userlist);
324
 
325
        $this->assertEmpty($userlist);
326
    }
327
 
328
    /**
329
     * Export data for user 1
330
     */
11 efrain 331
    public function test_export_user_data1(): void {
1 efrain 332
 
333
        // Export all contexts for the first user.
334
        $contextids = array_values(array_map(function($c) {
335
            return $c->id;
336
        }, $this->contexts));
337
        $appctx = new approved_contextlist($this->users[1], 'mod_wiki', $contextids);
338
        provider::export_user_data($appctx);
339
 
340
        // First wiki has two pages ever touched by this user.
341
        $data = writer::with_context($this->contexts[1])->get_related_data([$this->subwikis[1]]);
342
        $this->assertEquals([
343
                $this->pagepaths[1][1],
344
                $this->pagepaths[1][2]
345
            ], array_keys($data));
346
        // First page was initially created by this user and all its information is returned to this user.
347
        $data11 = $data[$this->pagepaths[1][1]];
348
        $this->assertEquals($this->pages[1][1]->cachedcontent, $data11['page']['cachedcontent']);
349
        $this->assertNotEmpty($data11['page']['timecreated']);
350
        // Wiki creates two revisions when page is created, first one with empty content.
351
        $this->assertEquals(2, count($data11['revisions']));
352
        $this->assertFalse(array_key_exists('locks', $data11));
353
        // Only one file is returned that was in the revision made by this user.
354
 
355
        // The second page was last modified by a different user, so userid in the wiki_pages table is different,
356
        // additional page information is not exported.
357
        $data12 = $data[$this->pagepaths[1][2]];
358
        $this->assertFalse(isset($data12['page']['timecreated']));
359
        // There are two revisions for creating the page and two additional revisions made by this user.
360
        $this->assertEquals(4, count($data12['revisions']));
361
        $lastrevision = array_pop($data12['revisions']);
362
        $this->assertEquals('update2', $lastrevision['content']);
363
 
364
        // There is one file that was used in this user's contents - "Dog face.jpg" and one file in page cachedcontents.
365
        $files = writer::with_context($this->contexts[1])->get_files([$this->subwikis[1]]);
366
        $this->assertEqualsCanonicalizing(['Dog jump.jpg', 'Hamster.jpg'], array_keys($files));
367
 
368
        // Second (individual) wiki for the first user, two pages are returned for this user's subwiki.
369
        $data = writer::with_context($this->contexts[2])->get_related_data([$this->subwikis[21]]);
370
        $this->assertEquals([
371
            $this->pagepaths[21][1],
372
            $this->pagepaths[21][2]
373
        ], array_keys($data));
374
        $files = writer::with_context($this->contexts[2])->get_files([$this->subwikis[21]]);
375
        $this->assertEquals(['mycat.jpg'], array_keys($files));
376
 
377
        // Second (individual) wiki for the first user, nothing is returned for the second user's subwiki.
378
        $this->assertFalse(writer::with_context($this->contexts[2])->has_any_data([$this->subwikis[22]]));
379
 
380
        // Third wiki for the first user, there were no contributions by the first user.
381
        $this->assertFalse(writer::with_context($this->contexts[3])->has_any_data([$this->subwikis[3]]));
382
    }
383
 
384
    /**
385
     * Test export data for user 2
386
     */
11 efrain 387
    public function test_export_user_data2(): void {
1 efrain 388
 
389
        // Export all contexts for the second user.
390
        $contextids = array_values(array_map(function($c) {
391
            return $c->id;
392
        }, $this->contexts));
393
        $appctx = new approved_contextlist($this->users[2], 'mod_wiki', $contextids);
394
        provider::export_user_data($appctx);
395
 
396
        // First wiki - this user only modified the second page.
397
        $data = writer::with_context($this->contexts[1])->get_related_data([$this->subwikis[1]]);
398
        $this->assertEquals([
399
            $this->pagepaths[1][2]
400
        ], array_keys($data));
401
 
402
        // This user was the last one to modify this page, so the page info is returned.
403
        $data12 = $data[$this->pagepaths[1][2]];
404
        $this->assertEquals('update3 <img src="files/Hamster.jpg" alt="Hamster.jpg" />', trim($data12['page']['cachedcontent']));
405
        // He made one revision.
406
        $this->assertEquals(1, count($data12['revisions']));
407
        $lastrevision = reset($data12['revisions']);
408
        $this->assertEquals('update3 <img src="files/Hamster.jpg">', trim($lastrevision['content']));
409
 
410
        // Only one file was used in the first wiki by this user - Hamster.jpg.
411
        $files = writer::with_context($this->contexts[1])->get_files([$this->subwikis[1]]);
412
        $this->assertEquals(['Hamster.jpg'], array_keys($files));
413
 
414
        // Export second (individual) wiki, nothing is returned for the other user's subwiki.
415
        $this->assertFalse(writer::with_context($this->contexts[2])->has_any_data([$this->subwikis[21]]));
416
 
417
        // Export second (individual) wiki, two pages are returned for this user's subwiki.
418
        $data = writer::with_context($this->contexts[2])->get_related_data([$this->subwikis[22]]);
419
        $this->assertEquals([
420
            $this->pagepaths[22][1],
421
            $this->pagepaths[22][2]
422
        ], array_keys($data));
423
        $files = writer::with_context($this->contexts[2])->get_files([$this->subwikis[22]]);
424
        $this->assertEmpty($files);
425
 
426
        // Second user made contributions to the third wiki.
427
        $data = writer::with_context($this->contexts[3])->get_related_data([$this->subwikis[3]]);
428
        $this->assertEquals([
429
            $this->pagepaths[3][1]
430
        ], array_keys($data));
431
        $files = writer::with_context($this->contexts[3])->get_files([$this->subwikis[3]]);
432
        $this->assertEmpty($files);
433
    }
434
 
435
    /**
436
     * Test export data for user 3 (locks, empty individual wiki)
437
     */
11 efrain 438
    public function test_export_user_data3(): void {
1 efrain 439
 
440
        // Export all contexts for the third user.
441
        $contextids = array_values(array_map(function($c) {
442
            return $c->id;
443
        }, $this->contexts));
444
        $appctx = new approved_contextlist($this->users[3], 'mod_wiki', $contextids);
445
        provider::export_user_data($appctx);
446
 
447
        // For the third page of the first wiki there are 2 revisions and 1 lock.
448
        $data = writer::with_context($this->contexts[1])->get_related_data([$this->subwikis[1]]);
449
        $this->assertEquals([
450
            $this->pagepaths[1][3]
451
        ], array_keys($data));
452
 
453
        $data13 = $data[$this->pagepaths[1][3]];
454
        $this->assertNotEmpty($data13['page']['timecreated']);
455
        $this->assertEquals(2, count($data13['revisions']));
456
        $this->assertEquals(1, count($data13['locks']));
457
        $files = writer::with_context($this->contexts[1])->get_files([$this->subwikis[1]]);
458
        $this->assertEmpty($files);
459
 
460
        // Empty individual wiki.
461
        $this->assertTrue(writer::with_context($this->contexts[2])->has_any_data());
462
        $data = writer::with_context($this->contexts[2])->get_data([$this->subwikis[23]]);
463
        $this->assertEquals((object)[
464
            'groupid' => 0,
465
            'userid' => $this->users[3]->id
466
        ], $data);
467
        $files = writer::with_context($this->contexts[2])->get_files([$this->subwikis[23]]);
468
        $this->assertEmpty($files);
469
 
470
        // For the third wiki there is no page information, no revisions and one lock.
471
        $data = writer::with_context($this->contexts[3])->get_related_data([$this->subwikis[3]]);
472
        $this->assertEquals([
473
            $this->pagepaths[3][1]
474
        ], array_keys($data));
475
 
476
        $data31 = $data[$this->pagepaths[3][1]];
477
        $this->assertTrue(empty($data31['page']['timecreated']));
478
        $this->assertTrue(empty($data31['revisions']));
479
        $this->assertEquals(1, count($data31['locks']));
480
 
481
        $files = writer::with_context($this->contexts[3])->get_files([$this->subwikis[3]]);
482
        $this->assertEmpty($files);
483
 
484
        // No data for the forth wiki.
485
        $this->assertFalse(writer::with_context($this->contexts[4])->has_any_data());
486
    }
487
 
488
    /**
489
     * Creates a comment object
490
     *
491
     * @param  stdClass $page
492
     * @param  string   $text
493
     * @return comment The comment object.
494
     */
495
    protected function add_comment($page, $text) {
496
        global $DB, $CFG, $USER;
497
        require_once($CFG->dirroot . '/comment/lib.php');
498
        $record = $DB->get_record_sql('SELECT cm.id, cm.course FROM {course_modules} cm
499
            JOIN {modules} m ON m.name = ? AND m.id = cm.module
500
            JOIN {wiki} w ON cm.instance = w.id
501
            JOIN {wiki_subwikis} s ON s.wikiid = w.id
502
            WHERE s.id=?', ['wiki', $page->subwikiid]);
503
        $context = \context_module::instance($record->id);
504
        $args = new \stdClass;
505
        $args->context = $context;
506
        $args->courseid = $record->course;
507
        $args->area = 'wiki_page';
508
        $args->itemid = $page->id;
509
        $args->component = 'mod_wiki';
510
        $comment = new \comment($args);
511
        $comment->set_post_permission(true);
512
        $comment->add($text);
513
        return $comment;
514
    }
515
 
516
    /**
517
     * Test export data when there are comments.
518
     */
11 efrain 519
    public function test_export_user_data_with_comments(): void {
1 efrain 520
        global $DB;
521
        // Comment on each page in the first wiki as the first user.
522
        $this->setUser($this->users[1]);
523
        $this->add_comment($this->pages[1][1], 'Hello111');
524
        $this->add_comment($this->pages[1][2], 'Hello112');
525
        $this->add_comment($this->pages[1][3], 'Hello113');
526
 
527
        // Comment on second and third page as the third user.
528
        $this->setUser($this->users[3]);
529
        $this->add_comment($this->pages[1][2], 'Hello312');
530
        $this->add_comment($this->pages[1][3], 'Hello313');
531
 
532
        // Export all contexts for the third user.
533
        $contextids = array_values(array_map(function($c) {
534
            return $c->id;
535
        }, $this->contexts));
536
        $appctx = new approved_contextlist($this->users[3], 'mod_wiki', $contextids);
537
        provider::export_user_data($appctx);
538
 
539
        $data = writer::with_context($this->contexts[1])->get_related_data([$this->subwikis[1]]);
540
        // Now user has two pages (comparing to previous test where he had one).
541
        $this->assertEquals([
542
            $this->pagepaths[1][2],
543
            $this->pagepaths[1][3]
544
        ], array_keys($data));
545
 
546
        // Page 1-2 was exported and it has one comment that this user made (comment from another user was not exported).
547
        $data12 = $data[$this->pagepaths[1][2]];
548
        $this->assertTrue(empty($data12['page']['timecreated']));
549
        $this->assertTrue(empty($data12['revisions']));
550
        $this->assertTrue(empty($data12['locks']));
551
        $this->assertEquals(1, count($data12['page']['comments']));
552
 
553
        // Page 1-3 was exported same way as in the previous test and it has two comments.
554
        $data13 = $data[$this->pagepaths[1][3]];
555
        $this->assertNotEmpty($data13['page']['timecreated']);
556
        $this->assertEquals(2, count($data13['revisions']));
557
        $this->assertEquals(1, count($data13['locks']));
558
        $this->assertEquals(2, count($data13['page']['comments']));
559
    }
560
 
561
    /**
562
     * Test for delete_data_for_all_users_in_context().
563
     */
11 efrain 564
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 565
        provider::delete_data_for_all_users_in_context($this->contexts[1]);
566
 
567
        $appctx = new approved_contextlist($this->users[1], 'mod_wiki',
568
            [$this->contexts[1]->id, $this->contexts[2]->id]);
569
        provider::export_user_data($appctx);
570
        $this->assertFalse(writer::with_context($this->contexts[1])->has_any_data());
571
        $this->assertTrue(writer::with_context($this->contexts[2])->has_any_data());
572
 
573
        writer::reset();
574
        $appctx = new approved_contextlist($this->users[2], 'mod_wiki', [$this->contexts[1]->id]);
575
        provider::export_user_data($appctx);
576
        $this->assertFalse(writer::with_context($this->contexts[1])->has_any_data());
577
 
578
        writer::reset();
579
        $appctx = new approved_contextlist($this->users[3], 'mod_wiki', [$this->contexts[1]->id]);
580
        provider::export_user_data($appctx);
581
        $this->assertFalse(writer::with_context($this->contexts[1])->has_any_data());
582
    }
583
 
584
    /**
585
     * Test for delete_data_for_user().
586
     */
11 efrain 587
    public function test_delete_data_for_user(): void {
1 efrain 588
        $appctx = new approved_contextlist($this->users[1], 'mod_wiki',
589
            [$this->contexts[1]->id, $this->contexts[1]->id]);
590
        provider::delete_data_for_user($appctx);
591
 
592
        provider::export_user_data($appctx);
593
        $this->assertTrue(writer::with_context($this->contexts[1])->has_any_data());
594
        $this->assertFalse(writer::with_context($this->contexts[2])->has_any_data());
595
    }
596
 
597
    /**
598
     * Test for delete_data_for_users().
599
     */
11 efrain 600
    public function test_delete_data_for_users(): void {
1 efrain 601
        $component = 'mod_wiki';
602
 
603
        // Ensure data exists within context 2 - individual wikis.
604
        // Since each user owns their own subwiki in this context, they can be deleted.
605
        $u1ctx2 = new approved_contextlist($this->users[1], 'mod_wiki', [$this->contexts[2]->id]);
606
        provider::export_user_data($u1ctx2);
607
        $u2ctx2 = new approved_contextlist($this->users[2], 'mod_wiki', [$this->contexts[2]->id]);
608
        provider::export_user_data($u2ctx2);
609
        $u3ctx2 = new approved_contextlist($this->users[3], 'mod_wiki', [$this->contexts[2]->id]);
610
        provider::export_user_data($u3ctx2);
611
 
612
        $this->assertTrue(writer::with_context($this->contexts[2])->has_any_data());
613
        writer::reset();
614
 
615
        // Delete user 1 and 2 data, user 3's wiki still remains.
616
        $approveduserids = [$this->users[1]->id, $this->users[2]->id];
617
        $approvedlist = new approved_userlist($this->contexts[2], $component, $approveduserids);
618
        provider::delete_data_for_users($approvedlist);
619
 
620
        $u1ctx2 = new approved_contextlist($this->users[1], 'mod_wiki', [$this->contexts[2]->id]);
621
        provider::export_user_data($u1ctx2);
622
        $u2ctx2 = new approved_contextlist($this->users[2], 'mod_wiki', [$this->contexts[2]->id]);
623
        provider::export_user_data($u2ctx2);
624
        $u3ctx2 = new approved_contextlist($this->users[3], 'mod_wiki', [$this->contexts[2]->id]);
625
        provider::export_user_data($u3ctx2);
626
 
627
        $this->assertTrue(writer::with_context($this->contexts[2])->has_any_data());
628
        writer::reset();
629
 
630
        // Delete user 3's wiki. All 3 subwikis now deleted, so ensure no data is found in this context.
631
        $approveduserids = [$this->users[3]->id];
632
        $approvedlist = new approved_userlist($this->contexts[2], $component, $approveduserids);
633
        provider::delete_data_for_users($approvedlist);
634
 
635
        $u1ctx2 = new approved_contextlist($this->users[1], 'mod_wiki', [$this->contexts[2]->id]);
636
        provider::export_user_data($u1ctx2);
637
        $u2ctx2 = new approved_contextlist($this->users[2], 'mod_wiki', [$this->contexts[2]->id]);
638
        provider::export_user_data($u2ctx2);
639
        $u3ctx2 = new approved_contextlist($this->users[3], 'mod_wiki', [$this->contexts[2]->id]);
640
        provider::export_user_data($u3ctx2);
641
 
642
        $this->assertFalse(writer::with_context($this->contexts[2])->has_any_data());
643
        writer::reset();
644
 
645
        // Ensure Context 1 still contains data.
646
        $u1ctx1 = new approved_contextlist($this->users[1], 'mod_wiki', [$this->contexts[1]->id]);
647
        provider::export_user_data($u1ctx1);
648
        $u2ctx1 = new approved_contextlist($this->users[2], 'mod_wiki', [$this->contexts[1]->id]);
649
        provider::export_user_data($u2ctx1);
650
        $u3ctx1 = new approved_contextlist($this->users[3], 'mod_wiki', [$this->contexts[1]->id]);
651
        provider::export_user_data($u3ctx1);
652
 
653
        $this->assertTrue(writer::with_context($this->contexts[1])->has_any_data());
654
    }
655
}