Merge branch 'wip-MDL-61937-master' of git://github.com/marinaglancy/moodle
[moodle.git] / competency / tests / privacy_test.php
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/>.
17 /**
18  * Data provider tests.
19  *
20  * @package    core_competency
21  * @category   test
22  * @copyright  2018 Frédéric Massart
23  * @author     Frédéric Massart <fred@branchup.tech>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
28 global $CFG, $DB;
30 use core_privacy\tests\provider_testcase;
31 use core_privacy\local\request\contextlist;
32 use core_privacy\local\request\approved_contextlist;
33 use core_privacy\local\request\transform;
34 use core_privacy\local\request\writer;
35 use core_competency\api;
36 use core_competency\privacy\provider;
38 /**
39  * Data provider testcase class.
40  *
41  * @package    core_competency
42  * @category   test
43  * @copyright  2018 Frédéric Massart
44  * @author     Frédéric Massart <fred@branchup.tech>
45  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46  */
47 class core_competency_privacy_testcase extends provider_testcase {
49     public function setUp() {
50         global $PAGE;
51         $this->resetAfterTest();
53         // We need this or exporters (core_competency\external\exporter) do not receive the right renderer.
54         $PAGE->get_renderer('core');
55     }
57     public function test_get_contexts_for_userid_with_usermodified_for_framework() {
58         $dg = $this->getDataGenerator();
59         $ccg = $dg->get_plugin_generator('core_competency');
61         $cat1 = $dg->create_category();
62         $cat2 = $dg->create_category();
63         $u1 = $dg->create_user();
64         $u2 = $dg->create_user();
65         $u3 = $dg->create_user();
66         $u4 = $dg->create_user();
68         $sysctx = context_system::instance();
69         $cat1ctx = context_coursecat::instance($cat1->id);
70         $cat2ctx = context_coursecat::instance($cat2->id);
72         // Test recovery through framework context.
73         $this->setUser($u1);
74         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
75         $f1 = $ccg->create_framework();
76         $contextlist = provider::get_contexts_for_userid($u1->id);
77         $this->assert_contextlist($contextlist, [$sysctx]);
78         $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
79         $contextlist = provider::get_contexts_for_userid($u1->id);
80         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
82         // Test recovery of category context alone.
83         $this->setUser($u2);
84         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
85         $ccg->create_framework(['contextid' => $cat2ctx->id]);
86         $contextlist = provider::get_contexts_for_userid($u2->id);
87         $this->assert_contextlist($contextlist, [$cat2ctx]);
89         // Test recovery through competency.
90         $this->setUser($u3);
91         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
92         $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
93         $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
94         $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
95         $contextlist = provider::get_contexts_for_userid($u3->id);
96         $this->assert_contextlist($contextlist, [$sysctx]);
97         $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
98         $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
99         $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
100         $contextlist = provider::get_contexts_for_userid($u3->id);
101         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
103         // Test recovery through related competency.
104         $this->setUser($u4);
105         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
106         $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]);
107         $contextlist = provider::get_contexts_for_userid($u4->id);
108         $this->assert_contextlist($contextlist, [$sysctx]);
109         $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]);
110         $contextlist = provider::get_contexts_for_userid($u4->id);
111         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
112     }
114     public function test_get_contexts_for_userid_with_usermodified_for_template() {
115         $dg = $this->getDataGenerator();
116         $ccg = $dg->get_plugin_generator('core_competency');
118         $cat1 = $dg->create_category();
119         $cat2 = $dg->create_category();
120         $u1 = $dg->create_user();
121         $u2 = $dg->create_user();
122         $u3 = $dg->create_user();
123         $u4 = $dg->create_user();
124         $cohort = $dg->create_cohort();
126         $sysctx = context_system::instance();
127         $cat1ctx = context_coursecat::instance($cat1->id);
128         $cat2ctx = context_coursecat::instance($cat2->id);
130         $f1 = $ccg->create_framework();
131         $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
132         $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
133         $cs = [];
135         foreach ([$f1, $f2, $f3] as $f) {
136             $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
137         }
139         // Test recovery through template context.
140         $this->setUser($u1);
141         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
142         $t1 = $ccg->create_template();
143         $contextlist = provider::get_contexts_for_userid($u1->id);
144         $this->assert_contextlist($contextlist, [$sysctx]);
145         $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
146         $contextlist = provider::get_contexts_for_userid($u1->id);
147         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
149         // Test recovery of category context alone.
150         $this->setUser($u2);
151         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
152         $ccg->create_template(['contextid' => $cat2ctx->id]);
153         $contextlist = provider::get_contexts_for_userid($u2->id);
154         $this->assert_contextlist($contextlist, [$cat2ctx]);
156         // Test recovery through template competency.
157         $this->setUser($u3);
158         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
159         $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]);
160         $contextlist = provider::get_contexts_for_userid($u3->id);
161         $this->assert_contextlist($contextlist, [$sysctx]);
162         $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]);
163         $contextlist = provider::get_contexts_for_userid($u3->id);
164         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
166         // Test recovery through template cohort.
167         $this->setUser($u4);
168         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
169         $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]);
170         $contextlist = provider::get_contexts_for_userid($u4->id);
171         $this->assert_contextlist($contextlist, [$sysctx]);
172         $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]);
173         $contextlist = provider::get_contexts_for_userid($u4->id);
174         $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
175     }
177     public function test_get_contexts_for_userid_with_usermodified_for_course() {
178         $dg = $this->getDataGenerator();
179         $ccg = $dg->get_plugin_generator('core_competency');
180         $c1 = $dg->create_course();
181         $c2 = $dg->create_course();
182         $u0 = $dg->create_user();
183         $u1 = $dg->create_user();
184         $u2 = $dg->create_user();
185         $u3 = $dg->create_user();
186         $u4 = $dg->create_user();
187         $c1ctx = context_course::instance($c1->id);
188         $c2ctx = context_course::instance($c2->id);
190         $f = $ccg->create_framework();
191         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
192         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
194         $this->setUser($u1);
195         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
196         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
197         $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]);
198         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
199         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
201         $this->setUser($u2);
202         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
203         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
204         $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]);
205         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
206         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c2ctx]);
207         $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]);
208         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
209         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
211         $this->setUser($u3);
212         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
213         $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
214         $ccs->create();
215         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
216         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
217         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
219         $this->setUser($u4);
220         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
221         $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]);
222         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
223         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
224         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
225         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$c2ctx]);
226     }
228     public function test_get_contexts_for_userid_with_usermodified_for_module() {
229         $dg = $this->getDataGenerator();
230         $ccg = $dg->get_plugin_generator('core_competency');
231         $c1 = $dg->create_course();
232         $m1 = $dg->create_module('choice', ['course' => $c1]);
233         $m2 = $dg->create_module('choice', ['course' => $c1]);
234         $u1 = $dg->create_user();
235         $u2 = $dg->create_user();
236         $m1ctx = context_module::instance($m1->cmid);
237         $m2ctx = context_module::instance($m2->cmid);
239         $f = $ccg->create_framework();
240         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
241         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
243         $this->setUser($u1);
244         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
245         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
246         $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]);
247         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
248         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
250         $this->setUser($u2);
251         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
252         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
253         $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]);
254         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
255         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m2ctx]);
256         $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]);
257         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
258         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m1ctx, $m2ctx]);
259     }
261     public function test_get_contexts_for_userid_with_usermodified_for_plan() {
262         $dg = $this->getDataGenerator();
263         $ccg = $dg->get_plugin_generator('core_competency');
264         $u0 = $dg->create_user();
265         $u1 = $dg->create_user();
266         $u2 = $dg->create_user();
267         $u3 = $dg->create_user();
268         $u0ctx = context_user::instance($u0->id);
270         $f = $ccg->create_framework();
271         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
272         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
274         $this->setUser($u1);
275         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
276         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
277         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
278         $plan = $ccg->create_plan(['userid' => $u0->id]);
279         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
280         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
281         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
283         $this->setUser($u2);
284         $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]);
285         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
286         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
287         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
289         $this->setUser($u3);
290         $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'),
291             'userid' => $u0->id]);
292         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
293         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
294         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
295     }
297     public function test_get_contexts_for_userid_with_usermodified_for_competency_data() {
298         $dg = $this->getDataGenerator();
299         $ccg = $dg->get_plugin_generator('core_competency');
300         $u0 = $dg->create_user();
301         $u1 = $dg->create_user();
302         $u2 = $dg->create_user();
303         $u3 = $dg->create_user();
304         $u4 = $dg->create_user();
305         $u5 = $dg->create_user();
306         $u6 = $dg->create_user();
307         $u7 = $dg->create_user();
308         $u8 = $dg->create_user();
309         $u0ctx = context_user::instance($u0->id);
311         $f = $ccg->create_framework();
312         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
313         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
315         $this->setUser($u1);
316         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
317         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
318         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
319         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
320         $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
321         $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), []);
322         $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'),
323             'reviewerid' => $u6->id]);
324         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
325         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
326         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
327         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
328         $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
329         $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
331         $this->setUser($u2);
332         $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]);
333         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
334         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
335         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
336         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
337         $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
338         $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
340         $this->setUser($u3);
341         $ccg->create_user_evidence(['userid' => $u0->id]);
342         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
343         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
344         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
345         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
346         $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
347         $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
349         $this->setUser($u4);
350         $ccg->create_user_evidence(['userid' => $u0->id]);
351         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
352         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
353         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
354         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u0ctx]);
355         $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
356         $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
358         // Comment on competency.
359         $this->allow_anyone_to_comment_anywhere();
360         $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), []);
361         $this->setUser($u7);
362         $comments = $uc->get_comment_object();
363         $comments->add('Hello there!');
364         $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), [$u0ctx]);
366         // Comment on plan.
367         $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), []);
368         $this->setUser($u8);
369         $plan = $ccg->create_plan(['userid' => $u0->id]);
370         $comments = $plan->get_comment_object();
371         $comments->add('Hi, planet!');
372         $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), [$u0ctx]);
373     }
375     public function test_get_contexts_for_userid_with_actual_data_and_actual_data_is_goooood() {
376         $dg = $this->getDataGenerator();
377         $ccg = $dg->get_plugin_generator('core_competency');
378         $c1 = $dg->create_course();
379         $u1 = $dg->create_user();
380         $u2 = $dg->create_user();
381         $u3 = $dg->create_user();
382         $u4 = $dg->create_user();
384         $c1ctx = context_course::instance($c1->id);
385         $u1ctx = context_user::instance($u1->id);
386         $u2ctx = context_user::instance($u2->id);
387         $u3ctx = context_user::instance($u3->id);
388         $u4ctx = context_user::instance($u4->id);
390         $f = $ccg->create_framework();
391         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
392         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
394         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
395         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
396         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
397         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
399         $ccg->create_plan(['userid' => $u1->id]);
400         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
401         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
402         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
403         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
405         $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
406         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
407         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
408         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
409         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
411         $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
412         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
413         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
414         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
415         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
417         $ccg->create_user_evidence(['userid' => $u4->id]);
418         $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
419         $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
420         $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
421         $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u4ctx]);
422     }
424     public function test_delete_data_for_user() {
425         $dg = $this->getDataGenerator();
426         $ccg = $dg->get_plugin_generator('core_competency');
428         $c1 = $dg->create_course();
429         $c2 = $dg->create_course();
430         $u1 = $dg->create_user();
431         $u2 = $dg->create_user();
433         $c1ctx = context_course::instance($c1->id);
434         $u1ctx = context_user::instance($u1->id);
435         $u2ctx = context_user::instance($u2->id);
437         $f = $ccg->create_framework();
438         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
439         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
441         $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
442         $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
443         $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
444         $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
445             'competencyid' => $comp1->get('id')]);
446         $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
447             'competencyid' => $comp2->get('id')]);
448         $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
449             'competencyid' => $comp1->get('id')]);
451         $p1a = $ccg->create_plan(['userid' => $u1->id]);
452         $p1b = $ccg->create_plan(['userid' => $u1->id]);
453         $p2 = $ccg->create_plan(['userid' => $u2->id]);
454         $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
455         $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
456         $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
457         $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
458             'competencyid' => $comp1->get('id')]);
459         $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
460             'competencyid' => $comp2->get('id')]);
461         $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
462             'competencyid' => $comp1->get('id')]);
464         $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
465         $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
466         $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
467         $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
468         $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
469         $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
471         $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
472             'competencyid' => $comp1->get('id')]);
473         $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
474             'competencyid' => $comp1->get('id')]);
475         $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
476             'competencyid' => $comp1->get('id')]);
478         // User 1 comments on both plans.
479         $this->allow_anyone_to_comment_anywhere();
480         $this->setUser($u1);
481         $p1a->get_comment_object()->add('Hi...');
482         $p1a->get_comment_object()->add('mister');
483         $p2->get_comment_object()->add('Ahoy!');
485         // User 2 comments on both competencies.
486         $this->setUser($u2);
487         $uc1a->get_comment_object()->add('Hi, too!');
488         $uc1a->get_comment_object()->add('How are you?');
489         $uc2->get_comment_object()->add('Ahoy, too!');
491         $p1acommentobj = $p1a->get_comment_object();
492         $p2commentobj = $p2->get_comment_object();
493         $uc1acommentobj = $uc1a->get_comment_object();
494         $uc2commentobj = $uc2->get_comment_object();
496         $this->setAdminUser();
497         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
498         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
499         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
500         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
501         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
502         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
503         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
504         $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
505         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
506         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
507         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
508         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
509         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
510         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
511         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
512         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
513         $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
514         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
515         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
516         $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
517         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
518         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
519         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
520         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
521         $this->assert_has_comments($p1acommentobj);
522         $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id));
523         $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
524         $this->assert_has_comments($p2commentobj);
525         $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
526         $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
527         $this->assert_has_comments($uc1acommentobj);
528         $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
529         $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id));
530         $this->assert_has_comments($uc2commentobj);
531         $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
532         $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
534         // Deleting user context only.
535         $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id]);
536         provider::delete_data_for_user($appctx);
538         $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
539         $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
540         $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
541         $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
542         $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
543         $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
544         $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
545         $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
546         $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
547         $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
548         $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
549         $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
550         $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
551         $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
553         $this->assert_has_no_comments($p1acommentobj);
554         $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id));
555         $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
556         $this->assert_has_no_comments($uc1acommentobj);
557         $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
558         $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id));
560         // This should not have been affected.
561         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
562         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
564         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
565         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
566         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
567         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
568         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
569         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
570         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
571         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
572         $this->assert_has_comments($p2commentobj);
573         $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
574         $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
575         $this->assert_has_comments($uc2commentobj);
576         $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
577         $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
579         // Deleting course context as well.
580         $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $c1ctx->id]);
581         provider::delete_data_for_user($appctx);
583         $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
585         // The rest belongs to another course, or the other user.
586         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
587         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
588         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
589         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
590         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
591         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
592         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
593         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
594         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
595     }
597     public function test_delete_data_for_user_with_other_user_context() {
598         $dg = $this->getDataGenerator();
599         $ccg = $dg->get_plugin_generator('core_competency');
601         $u1 = $dg->create_user();
602         $u2 = $dg->create_user();
604         $u1ctx = context_user::instance($u1->id);
605         $u2ctx = context_user::instance($u2->id);
607         $f = $ccg->create_framework();
608         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
610         // Create a bunch of data for user 1.
611         $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
612         $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
613             'competencyid' => $comp1->get('id')]);
614         $p1a = $ccg->create_plan(['userid' => $u1->id]);
615         $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
616         $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
617             'competencyid' => $comp1->get('id')]);
618         $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
619         $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
621         $p2a = $ccg->create_plan(['userid' => $u2->id]);
623         // User 2 comments.
624         $this->allow_anyone_to_comment_anywhere();
625         $this->setUser($u2);
626         $p1a->get_comment_object()->add('Hi...');
627         $p2a->get_comment_object()->add('Hi, hi!');
628         $uc1a->get_comment_object()->add('Hi, too!');
630         // Confirm state.
631         $this->setAdminUser();
632         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
633         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
634         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
635         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
636         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
637         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
638         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
639         $this->assert_has_comments($p1a->get_comment_object());
640         $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
641         $this->assert_has_comments($p2a->get_comment_object());
642         $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
643         $this->assert_has_comments($uc1a->get_comment_object());
644         $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
646         $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
648         // Delete for user 2, but we pass u1 context.
649         provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id]));
651         // Nothing should have happened.
652         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
653         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
654         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
655         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
656         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
657         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
658         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
659         $this->assert_has_comments($p1a->get_comment_object());
660         $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
661         $this->assert_has_comments($p2a->get_comment_object());
662         $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
663         $this->assert_has_comments($uc1a->get_comment_object());
664         $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
666         $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
668         // Delete for user 2, but we pass u1 and u2 context.
669         $p2acommentobj = $p2a->get_comment_object();
670         provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
672         // The plan got deleted.
673         $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id')));
674         $this->assert_has_no_comments($p2acommentobj);
676         // Nothing should have happened for u1.
677         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
678         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
679         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
680         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
681         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
682         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
683         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
684         $this->assert_has_comments($p1a->get_comment_object());
685         $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
686         $this->assert_has_comments($uc1a->get_comment_object());
687         $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
688     }
690     public function test_delete_data_for_all_users_in_context() {
691         $dg = $this->getDataGenerator();
692         $ccg = $dg->get_plugin_generator('core_competency');
694         $c1 = $dg->create_course();
695         $c2 = $dg->create_course();
696         $u1 = $dg->create_user();
697         $u2 = $dg->create_user();
699         $c1ctx = context_course::instance($c1->id);
700         $u1ctx = context_user::instance($u1->id);
701         $u2ctx = context_user::instance($u2->id);
703         $f = $ccg->create_framework();
704         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
705         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
707         $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
708         $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
709         $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
710         $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
711             'competencyid' => $comp1->get('id')]);
712         $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
713             'competencyid' => $comp2->get('id')]);
714         $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
715             'competencyid' => $comp1->get('id')]);
717         $p1a = $ccg->create_plan(['userid' => $u1->id]);
718         $p1b = $ccg->create_plan(['userid' => $u1->id]);
719         $p2 = $ccg->create_plan(['userid' => $u2->id]);
720         $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
721         $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
722         $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
723         $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
724             'competencyid' => $comp1->get('id')]);
725         $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
726             'competencyid' => $comp2->get('id')]);
727         $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
728             'competencyid' => $comp1->get('id')]);
730         $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
731         $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
732         $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
733         $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
734         $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
735         $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
737         $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
738             'competencyid' => $comp1->get('id')]);
739         $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
740             'competencyid' => $comp1->get('id')]);
741         $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
742             'competencyid' => $comp1->get('id')]);
744         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
745         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
746         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
747         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
748         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
749         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
750         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
751         $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
752         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
753         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
754         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
755         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
756         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
757         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
758         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
759         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
760         $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
761         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
762         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
763         $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
764         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
765         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
766         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
767         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
769         // Deleting the course 1 context.
770         provider::delete_data_for_all_users_in_context($c1ctx);
771         $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
772         $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
774         // Not affected.
775         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
776         $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
777         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
778         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
779         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
780         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
781         $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
782         $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
783         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
784         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
785         $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
786         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
787         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
788         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
789         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
790         $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
791         $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
792         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
793         $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
794         $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
795         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
796         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
798         // Deleting the user 1 context.
799         provider::delete_data_for_all_users_in_context($u1ctx);
800         $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
801         $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
802         $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
803         $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
804         $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
805         $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
806         $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
807         $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
808         $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
809         $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
810         $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
811         $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
812         $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
813         $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
815         // Not affected.
816         $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
817         $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
818         $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
819         $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
820         $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
821         $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
822         $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
823         $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
824     }
826     public function test_export_data_for_user_in_module_context_where_usermodified_matches() {
827         $dg = $this->getDataGenerator();
828         $ccg = $dg->get_plugin_generator('core_competency');
830         $c1 = $dg->create_course();
831         $u1 = $dg->create_user();
832         $u2 = $dg->create_user();
833         $m1 = $dg->create_module('page', ['course' => $c1]);
834         $m2 = $dg->create_module('page', ['course' => $c1]);
836         $m1ctx = context_module::instance($m1->cmid);
837         $m2ctx = context_module::instance($m2->cmid);
839         $f = $ccg->create_framework();
840         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
841         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
842         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
843         $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m1->cmid]);
845         $this->setUser($u1);
846         $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $m1->cmid]);
847         $ccg->create_course_module_competency(['competencyid' => $comp2->get('id'), 'cmid' => $m2->cmid]);
849         $this->setUser($u2);
850         $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m2->cmid]);
852         // Export.
853         $this->setAdminUser();
854         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id]));
856         // Check exported context 1.
857         $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
858         $this->assertCount(1, $data->associations);
859         $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
861         // Check exported context 2.
862         $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
863         $this->assertEmpty($data);
865         // Export both contexts.
866         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id, $m2ctx->id]));
868         // Check exported context 1.
869         $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
870         $this->assertCount(1, $data->associations);
871         $this->assertEquals($comp1->get('shortname'), $data->associations[0]['name']);
872         $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
874         // Check exported context 2.
875         $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
876         $this->assertCount(1, $data->associations);
877         $this->assertEquals($comp2->get('shortname'), $data->associations[0]['name']);
878         $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
879     }
881     public function test_export_data_for_user_in_course_context_where_usermodified_matches() {
882         $dg = $this->getDataGenerator();
883         $ccg = $dg->get_plugin_generator('core_competency');
885         $c1 = $dg->create_course();
886         $c2 = $dg->create_course();
887         $u0 = $dg->create_user();
888         $u1 = $dg->create_user();
889         $u2 = $dg->create_user();
890         $u3 = $dg->create_user();
891         $u4 = $dg->create_user();
893         $c1ctx = context_course::instance($c1->id);
894         $c2ctx = context_course::instance($c2->id);
896         $f = $ccg->create_framework();
897         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
898         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
899         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
900         $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
901         $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id]);
902         $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
904         $this->setUser($u1);
905         $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
906         $ccg->create_course_competency(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id]);
907         $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
908         $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
909         $ccg->create_user_competency_course(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
910         $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
911         $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
912         $ccs->create();
914         $this->setUser($u2);
915         $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id]);
916         $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
917         $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c2->id]);
918         $ccs->create();
920         // Export.
921         $this->setAdminUser();
922         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
924         // Check exported context 1.
925         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
926         $this->assertCount(2, $data->competencies);
927         $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
928         $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
929         $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
930         $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
931         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
932         $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
933         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
934         $this->assertCount(2, $data->ratings);
935         $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
936         $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
938         // Check exported context 2.
939         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
940         $this->assertEmpty($data);
941         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
942         $this->assertEmpty($data);
943         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
944         $this->assertEmpty($data);
946         // Export both contexts.
947         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id, $c2ctx->id]));
949         // Check exported context 1.
950         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
951         $this->assertCount(2, $data->competencies);
952         $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
953         $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
954         $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
955         $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
956         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
957         $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
958         $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
959         $this->assertCount(2, $data->ratings);
960         $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
961         $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
963         // Check exported context 2.
964         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
965         $this->assertCount(1, $data->competencies);
966         $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
967         $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
968         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
969         $this->assertEmpty($data);
970         $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
971         $this->assertCount(1, $data->ratings);
972         $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
973     }
975     public function test_export_data_for_user_in_course_context_with_real_data() {
976         $dg = $this->getDataGenerator();
977         $ccg = $dg->get_plugin_generator('core_competency');
979         $c1 = $dg->create_course();
980         $c2 = $dg->create_course();
981         $u1 = $dg->create_user();
982         $u2 = $dg->create_user();
984         $c1ctx = context_course::instance($c1->id);
985         $c2ctx = context_course::instance($c2->id);
987         $f = $ccg->create_framework();
988         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
989         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
990         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
992         $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
993             'userid' => $u1->id, 'grade' => 1, 'proficiency' => true]);
994         $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
995             'userid' => $u1->id, 'grade' => 2, 'proficiency' => false]);
996         $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
997             'userid' => $u1->id, 'grade' => 3, 'proficiency' => false]);
998         $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id,
999             'userid' => $u1->id]);
1001         $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u2->id]);
1002         $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u2->id]);
1004         // Export user 1, in course 1.
1005         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
1007         // Check course 1.
1008         $data = writer::with_context($c1ctx)->get_data([get_string('competencies', 'core_competency')]);
1009         $this->assertCount(2, $data->ratings);
1010         $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1011         $this->assertEquals('A', $data->ratings[0]['rating']['rating']);
1012         $this->assertEquals(transform::yesno(true), $data->ratings[0]['rating']['proficient']);
1013         $this->assertEquals($comp2->get('shortname'), $data->ratings[1]['name']);
1014         $this->assertEquals('B', $data->ratings[1]['rating']['rating']);
1015         $this->assertEquals(transform::yesno(false), $data->ratings[1]['rating']['proficient']);
1017         // Check course 2.
1018         $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1019         $this->assertEmpty($data);
1021         // Export user 1, in course 2.
1022         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c2ctx->id]));
1023         $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1024         $this->assertCount(2, $data->ratings);
1025         $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
1026         $this->assertEquals('C', $data->ratings[0]['rating']['rating']);
1027         $this->assertEquals(transform::yesno(false), $data->ratings[0]['rating']['proficient']);
1028         $this->assertEquals($comp3->get('shortname'), $data->ratings[1]['name']);
1029         $this->assertEquals('-', $data->ratings[1]['rating']['rating']);
1030         $this->assertEquals('-', $data->ratings[1]['rating']['proficient']);
1031     }
1033     public function test_export_data_for_user_in_system_and_category_contexts() {
1034         $dg = $this->getDataGenerator();
1035         $ccg = $dg->get_plugin_generator('core_competency');
1037         $c1 = $dg->create_cohort();
1038         $c2 = $dg->create_cohort();
1039         $cat1 = $dg->create_category();
1040         $cat2 = $dg->create_category();
1042         $cat1ctx = context_coursecat::instance($cat1->id);
1043         $cat2ctx = context_coursecat::instance($cat2->id);
1044         $sysctx = context_system::instance();
1046         $u1 = $dg->create_user();
1047         $u2 = $dg->create_user();
1048         $u3 = $dg->create_user();
1049         $u4 = $dg->create_user();
1050         $u2 = $dg->create_user();
1052         $this->setUser($u1);
1053         $f1 = $ccg->create_framework();
1054         $f1bis = $ccg->create_framework();
1055         $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
1056         $c2a = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
1057         $c2b = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
1059         $t1 = $ccg->create_template();
1060         $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
1061         $tc2a = $ccg->create_template_competency(['templateid' => $t2->get('id'), 'competencyid' => $c2a->get('id')]);
1062         $tch2 = $ccg->create_template_cohort(['templateid' => $t2->get('id'), 'cohortid' => $c1->id]);
1064         $this->setUser($u2);
1065         $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
1066         $c1a = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
1067         $c1b = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
1068         $c3a = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1069         $c3b = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1070         $c3c = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1071         $c3d = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1072         $rc1 = $ccg->create_related_competency(['competencyid' => $c2a->get('id'), 'relatedcompetencyid' => $c2b->get('id')]);
1074         $t3 = $ccg->create_template(['contextid' => $cat2ctx->id]);
1075         $tch1 = $ccg->create_template_cohort(['templateid' => $t1->get('id'), 'cohortid' => $c2->id]);
1076         $tc1a = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c1a->get('id')]);
1077         $tc1b = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c2a->get('id')]);
1078         $tc3a = $ccg->create_template_competency(['templateid' => $t3->get('id'), 'competencyid' => $c3a->get('id')]);
1080         $this->setUser($u1);
1081         $rc2 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3b->get('id')]);
1082         $rc3 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3c->get('id')]);
1084         $this->setAdminUser();
1085         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id]));
1087         // Check frameworks for u1 in system.
1088         $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1089         $this->assertCount(2, $data->frameworks);
1090         $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']);
1091         $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']);
1092         $this->assertEquals($f1bis->get('shortname'), $data->frameworks[1]['name']);
1093         $this->assertEquals(transform::yesno(true), $data->frameworks[1]['created_or_modified_by_you']);
1094         $this->assertEmpty($data->frameworks[0]['competencies']);
1095         $this->assertEmpty($data->frameworks[1]['competencies']);
1097         // Check templates for u1 in system.
1098         $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1099         $this->assertCount(1, $data->templates);
1100         $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']);
1101         $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1102         $this->assertEmpty($data->templates[0]['competencies']);
1103         $this->assertEmpty($data->templates[0]['cohorts']);
1105         // Check frameworks for u1 in cat1.
1106         $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1107         $this->assertCount(1, $data->frameworks);
1108         $this->assertEquals($f2->get('shortname'), $data->frameworks[0]['name']);
1109         $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']);
1110         $this->assertCount(2, $data->frameworks[0]['competencies']);
1111         $this->assertEquals($c2a->get('shortname'), $data->frameworks[0]['competencies'][0]['name']);
1112         $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][0]['created_or_modified_by_you']);
1113         $this->assertEquals($c2b->get('shortname'), $data->frameworks[0]['competencies'][1]['name']);
1114         $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][1]['created_or_modified_by_you']);
1116         // Check templates for u1 in cat1.
1117         $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1118         $this->assertCount(1, $data->templates);
1119         $this->assertEquals($t2->get('shortname'), $data->templates[0]['name']);
1120         $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1121         $this->assertCount(1, $data->templates[0]['competencies']);
1122         $this->assertEquals($c2a->get('shortname'), $data->templates[0]['competencies'][0]['name']);
1123         $this->assertEquals(transform::yesno(true), $data->templates[0]['competencies'][0]['created_or_modified_by_you']);
1124         $this->assertCount(1, $data->templates[0]['cohorts']);
1125         $this->assertEquals($c1->name, $data->templates[0]['cohorts'][0]['name']);
1126         $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']);
1128         // Check frameworks for u1 in cat2.
1129         $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1130         $this->assertCount(1, $data->frameworks);
1131         $this->assertEquals($f3->get('shortname'), $data->frameworks[0]['name']);
1132         $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1133         $this->assertCount(3, $data->frameworks[0]['competencies']);
1134         $competency = $data->frameworks[0]['competencies'][0];
1135         $this->assertEquals($c3a->get('shortname'), $competency['name']);
1136         $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']);
1137         $this->assertCount(2, $competency['related_competencies']);
1138         $this->assertEquals($c3b->get('shortname'), $competency['related_competencies'][0]['name']);
1139         $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']);
1140         $this->assertEquals($c3c->get('shortname'), $competency['related_competencies'][1]['name']);
1141         $this->assertEquals(transform::yesno(true), $competency['related_competencies'][1]['created_or_modified_by_you']);
1142         $competency = $data->frameworks[0]['competencies'][1];
1143         $this->assertEquals($c3b->get('shortname'), $competency['name']);
1144         $this->assertCount(1, $competency['related_competencies']);
1145         $competency = $data->frameworks[0]['competencies'][2];
1146         $this->assertEquals($c3c->get('shortname'), $competency['name']);
1147         $this->assertCount(1, $competency['related_competencies']);
1149         // Check templates for u1 in cat2.
1150         $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1151         $this->assertEmpty($data->templates);
1153         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id]));
1155         // Check frameworks for u2 in system.
1156         $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1157         $this->assertCount(1, $data->frameworks);
1158         $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']);
1159         $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1160         $this->assertCount(2, $data->frameworks[0]['competencies']);
1161         $competency = $data->frameworks[0]['competencies'][0];
1162         $this->assertEquals($c1a->get('shortname'), $competency['name']);
1163         $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1164         $competency = $data->frameworks[0]['competencies'][1];
1165         $this->assertEquals($c1b->get('shortname'), $competency['name']);
1166         $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1168         // Check templates for u2 in system.
1169         $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1170         $this->assertCount(1, $data->templates);
1171         $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']);
1172         $this->assertEquals(transform::yesno(false), $data->templates[0]['created_or_modified_by_you']);
1173         $this->assertCount(2, $data->templates[0]['competencies']);
1174         $competency = $data->templates[0]['competencies'][0];
1175         $this->assertEquals($c1a->get('shortname'), $competency['name']);
1176         $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1177         $competency = $data->templates[0]['competencies'][1];
1178         $this->assertEquals($c2a->get('shortname'), $competency['name']);
1179         $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1180         $this->assertCount(1, $data->templates[0]['cohorts']);
1181         $this->assertEquals($c2->name, $data->templates[0]['cohorts'][0]['name']);
1182         $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']);
1184         // Check frameworks for u2 in cat1.
1185         $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1186         $this->assertCount(1, $data->frameworks);
1187         $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1188         $this->assertCount(2, $data->frameworks[0]['competencies']);
1189         $competency = $data->frameworks[0]['competencies'][0];
1190         $this->assertEquals($c2a->get('shortname'), $competency['name']);
1191         $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']);
1192         $this->assertCount(1, $competency['related_competencies']);
1193         $this->assertEquals($c2b->get('shortname'), $competency['related_competencies'][0]['name']);
1194         $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']);
1196         // Check templates for u2 in system.
1197         $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1198         $this->assertCount(1, $data->templates);
1199         $this->assertEquals($t3->get('shortname'), $data->templates[0]['name']);
1200         $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1201         $this->assertCount(1, $data->templates[0]['competencies']);
1202         $competency = $data->templates[0]['competencies'][0];
1203         $this->assertEquals($c3a->get('shortname'), $competency['name']);
1204         $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1205     }
1207     public function test_export_data_for_user_with_related_learning_plans() {
1208         global $DB;
1210         $path = [
1211             get_string('competencies', 'core_competency'),
1212             get_string('privacy:path:relatedtome', 'core_competency'),
1213             get_string('privacy:path:plans', 'core_competency'),
1214         ];
1215         $yes = transform::yesno(true);
1216         $no = transform::yesno(false);
1218         $dg = $this->getDataGenerator();
1219         $ccg = $dg->get_plugin_generator('core_competency');
1221         $u0 = $dg->create_user();
1222         $u1 = $dg->create_user();
1223         $u2 = $dg->create_user();
1224         $u3 = $dg->create_user();
1225         $u4 = $dg->create_user();
1226         $u5 = $dg->create_user();
1227         $u6 = $dg->create_user();
1228         $u7 = $dg->create_user();
1229         $u8 = $dg->create_user();
1231         $dg->role_assign($DB->get_field('role', 'id', ['archetype' => 'manager'], IGNORE_MULTIPLE), $u6->id);
1232         $u0ctx = context_user::instance($u0->id);
1234         $f = $ccg->create_framework();
1235         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1236         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1237         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1238         $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1240         $t = $ccg->create_template();
1241         $tc1 = $ccg->create_template_competency(['competencyid' => $comp1->get('id'), 'templateid' => $t->get('id')]);
1242         $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]);
1243         $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]);
1244         $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]);
1246         $this->setUser($u1);
1247         $p1 = $ccg->create_plan(['templateid' => $t->get('id'), 'userid' => $u0->id]);
1249         $this->setUser($u2);
1250         $p2 = $ccg->create_plan(['userid' => $u0->id, 'reviewerid' => $u7->id]);
1252         $this->setUser($u3);
1253         $p1c1 = $ccg->create_plan_competency(['planid' => $p1->get('id'), 'competencyid' => $comp1->get('id')]);
1254         $p2c2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp2->get('id')]);
1255         $p2c3 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp3->get('id')]);
1257         $this->setUser($u4);
1258         $uc1 = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u0->id, 'grade' => 1,
1259             'proficiency' => true]);
1260         $uc2 = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u0->id, 'grade' => 2,
1261             'proficiency' => false]);
1262         $uc3 = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u0->id]);
1263         $uc4 = $ccg->create_user_competency(['competencyid' => $comp4->get('id'), 'userid' => $u0->id, 'reviewerid' => $u5->id]);
1265         $this->setUser($u5);
1266         $p3 = $ccg->create_plan(['userid' => $u0->id]);
1267         $p3c1 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp1->get('id')]);
1268         $p3c3 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp3->get('id')]);
1270         // Add comments on plan.
1271         $this->allow_anyone_to_comment_anywhere();
1272         $this->setUser($u0);
1273         $p1->get_comment_object()->add('Hello.');
1274         $this->setUser($u8);
1275         $p1->get_comment_object()->add('Hi.');
1277         // Export data for user 1.
1278         writer::reset();
1279         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
1280         $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1281         $data = writer::with_context($u0ctx)->get_data($planpath);
1282         $this->assertEquals($p1->get('name'), $data->name);
1283         $this->assertEquals($yes, $data->created_or_modified_by_you);
1285         // Export data for user 2.
1286         writer::reset();
1287         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
1288         $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1289         $data = writer::with_context($u0ctx)->get_data($planpath);
1290         $this->assertEquals($p2->get('name'), $data->name);
1291         $this->assertEquals($yes, $data->created_or_modified_by_you);
1293         // Export data for user 3.
1294         writer::reset();
1295         provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
1296         $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1297         $data = writer::with_context($u0ctx)->get_data($planpath);
1298         $this->assertEquals($p1->get('name'), $data->name);
1299         $this->assertEquals($no, $data->created_or_modified_by_you);
1300         $this->assertCount(1, $data->competencies);
1301         $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1302         $this->assertEquals($yes, $data->competencies[0]['created_or_modified_by_you']);
1304         $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1305         $data = writer::with_context($u0ctx)->get_data($planpath);
1306         $this->assertEquals($p2->get('name'), $data->name);
1307         $this->assertEquals($no, $data->created_or_modified_by_you);
1308         $competencies = $data->competencies;
1309         $this->assertCount(2, $competencies);
1310         $this->assertEquals($comp2->get('shortname'), $competencies[0]['name']);
1311         $this->assertEquals($yes, $competencies[0]['created_or_modified_by_you']);
1312         $this->assertEquals($comp3->get('shortname'), $competencies[1]['name']);
1313         $this->assertEquals($yes, $competencies[1]['created_or_modified_by_you']);
1315         // Export data for user 4.
1316         writer::reset();
1317         provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id]));
1318         foreach ([$p1, $p2, $p3] as $plan) {
1319             $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1320             $data = writer::with_context($u0ctx)->get_data($planpath);
1321             $this->assertEmpty($data);
1322         }
1324         // Export data for user 5.
1325         writer::reset();
1326         provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id]));
1327         $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]);
1328         $data = writer::with_context($u0ctx)->get_data($planpath);
1329         $this->assertEquals($p3->get('name'), $data->name);
1330         $this->assertEquals($yes, $data->created_or_modified_by_you);
1331         $this->assertCount(2, $data->competencies);
1332         $competency = $data->competencies[0];
1333         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1334         $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1335         $competency = $data->competencies[1];
1336         $this->assertEquals($comp3->get('shortname'), $competency['name']);
1337         $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1339         // Do some stuff.
1340         $this->setUser($u6);
1341         api::complete_plan($p3);
1343         // Export data for user 6.
1344         writer::reset();
1345         provider::export_user_data(new approved_contextlist($u6, 'core_competency', [$u0ctx->id]));
1346         $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]);
1347         $data = writer::with_context($u0ctx)->get_data($planpath);
1348         $this->assertEquals($p3->get('name'), $data->name);
1349         $this->assertEquals($yes, $data->created_or_modified_by_you);
1350         $this->assertCount(2, $data->competencies);
1351         $competency = $data->competencies[0];
1352         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1353         $this->assertArrayNotHasKey('created_or_modified_by_you', $competency);
1354         $this->assertEquals('A', $competency['rating']['rating']);
1355         $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1356         $competency = $data->competencies[1];
1357         $this->assertEquals($comp3->get('shortname'), $competency['name']);
1358         $this->assertArrayNotHasKey('created_or_modified_by_you', $competency);
1359         $this->assertEquals('-', $competency['rating']['rating']);
1360         $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1362         // Export data for user 7.
1363         writer::reset();
1364         provider::export_user_data(new approved_contextlist($u7, 'core_competency', [$u0ctx->id]));
1365         $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1366         $data = writer::with_context($u0ctx)->get_data($planpath);
1367         $this->assertEquals($p2->get('name'), $data->name);
1368         $this->assertEquals($no, $data->created_or_modified_by_you);
1369         $this->assertEquals($yes, $data->reviewer_is_you);
1371         // Export data for user 8.
1372         writer::reset();
1373         $this->setUser($u8);
1374         provider::export_user_data(new approved_contextlist($u8, 'core_competency', [$u0ctx->id]));
1375         $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1376         $data = writer::with_context($u0ctx)->get_data($planpath);
1377         $this->assertEquals($p1->get('name'), $data->name);
1378         $this->assertEquals($no, $data->created_or_modified_by_you);
1379         $this->assertEquals($no, $data->reviewer_is_you);
1380         $commentspath = array_merge($planpath,  [get_string('commentsubcontext', 'core_comment')]);
1381         $data = writer::with_context($u0ctx)->get_data($commentspath);
1382         $this->assert_exported_comments(['Hi.'], $data->comments);
1383     }
1385     public function test_export_data_for_user_with_related_competencies() {
1386         $path = [
1387             get_string('competencies', 'core_competency'),
1388             get_string('privacy:path:relatedtome', 'core_competency'),
1389             get_string('competencies', 'core_competency'),
1390         ];
1391         $yes = transform::yesno(true);
1392         $no = transform::yesno(false);
1393         $makecomppath = function($comp) use ($path) {
1394             return array_merge($path, ["{$comp->get('shortname')} ({$comp->get('id')})"]);
1395         };
1397         $dg = $this->getDataGenerator();
1398         $ccg = $dg->get_plugin_generator('core_competency');
1400         $u0 = $dg->create_user();
1401         $u1 = $dg->create_user();
1402         $u2 = $dg->create_user();
1403         $u3 = $dg->create_user();
1404         $u4 = $dg->create_user();
1405         $u5 = $dg->create_user();
1407         $u0ctx = context_user::instance($u0->id);
1409         $f = $ccg->create_framework();
1410         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1411         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1412         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1413         $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1415         $this->setUser($u1);
1416         api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
1417             'privacy:metadata:competency_evidence', 'core_competency');
1418         api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
1419             'privacy:metadata:competency_evidence', 'core_competency');
1420         api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
1421             'privacy:metadata:competency_evidence', 'core_competency');
1423         $this->setUser($u2);
1424         api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_COMPLETE,
1425             'privacy:metadata:competency_evidence', 'core_competency', null, false, null, null, $u3->id);
1427         $this->setUser($u3);
1428         api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_OVERRIDE,
1429             'privacy:metadata:competency_evidence', 'core_competency', null, false, null, 1, $u4->id, 'Ze note');
1431         $this->setUser($u4);
1432         $uc3 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp3->get('id')]);
1433         $uc4 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp4->get('id'), 'reviewerid' => $u2->id]);
1435         $this->allow_anyone_to_comment_anywhere();
1436         $this->setUser($u0);
1437         $uc3->get_comment_object()->add('...');
1438         $this->setUser($u5);
1439         $uc3->get_comment_object()->add('Hello!');
1440         $uc3->get_comment_object()->add('It\'s me...');
1442         // Export data for user 1.
1443         writer::reset();
1444         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
1445         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
1446         $competency = (array) $data;
1447         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1448         $evidence = $competency['evidence'];
1449         $this->assertCount(2, $evidence);
1450         $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']);
1451         $this->assertEquals('-', $evidence[0]['actionuserid']);
1452         $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
1453         $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
1454         $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[1]['action']);
1455         $this->assertEquals('-', $evidence[1]['actionuserid']);
1456         $this->assertEquals($no, $evidence[1]['acting_user_is_you']);
1457         $this->assertEquals($yes, $evidence[1]['created_or_modified_by_you']);
1458         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
1459         $competency = (array) $data;
1460         $this->assertEquals($comp2->get('shortname'), $competency['name']);
1461         $evidence = $competency['evidence'];
1462         $this->assertCount(1, $evidence);
1463         $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']);
1464         $this->assertEquals('-', $evidence[0]['actionuserid']);
1465         $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
1466         $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
1468         // Export data for user 2.
1469         writer::reset();
1470         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
1471         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
1472         $competency = (array) $data;
1473         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1474         $evidence = $competency['evidence'];
1475         $this->assertCount(1, $evidence);
1476         $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $evidence[0]['action']);
1477         $this->assertEquals($u3->id, $evidence[0]['actionuserid']);
1478         $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
1479         $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
1480         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4));
1481         $competency = (array) $data;
1482         $this->assertEquals($comp4->get('shortname'), $competency['name']);
1483         $this->assertCount(0, $competency['evidence']);
1484         $this->assertEquals($yes, $competency['rating']['reviewer_is_you']);
1485         $this->assertEquals($no, $competency['rating']['created_or_modified_by_you']);
1487         // Export data for user 3.
1488         writer::reset();
1489         provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
1490         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
1491         $competency = (array) $data;
1492         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1493         $evidence = $competency['evidence'];
1494         $this->assertCount(1, $evidence);
1495         $this->assertEquals($u3->id, $evidence[0]['actionuserid']);
1496         $this->assertEquals($yes, $evidence[0]['acting_user_is_you']);
1497         $this->assertEquals($no, $evidence[0]['created_or_modified_by_you']);
1498         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
1499         $competency = (array) $data;
1500         $this->assertEquals($comp2->get('shortname'), $competency['name']);
1501         $evidence = $competency['evidence'];
1502         $this->assertCount(1, $evidence);
1503         $this->assertEquals(get_string('privacy:evidence:action:override', 'core_competency'), $evidence[0]['action']);
1504         $this->assertEquals($u4->id, $evidence[0]['actionuserid']);
1505         $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
1506         $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
1508         // Export data for user 4.
1509         writer::reset();
1510         provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id]));
1511         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
1512         $competency = (array) $data;
1513         $this->assertEquals($comp2->get('shortname'), $competency['name']);
1514         $this->assertNull($competency['rating']);
1515         $this->assertCount(1, $competency['evidence']);
1516         $evidence = $competency['evidence'][0];
1517         $this->assertEquals($u4->id, $evidence['actionuserid']);
1518         $this->assertEquals($yes, $evidence['acting_user_is_you']);
1519         $this->assertEquals($no, $evidence['created_or_modified_by_you']);
1520         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3));
1521         $competency = (array) $data;
1522         $this->assertEquals($comp3->get('shortname'), $competency['name']);
1523         $this->assertEquals($no, $competency['rating']['reviewer_is_you']);
1524         $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1525         $this->assertEmpty($competency['evidence']);
1526         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4));
1527         $competency = (array) $data;
1528         $this->assertEquals($comp4->get('shortname'), $competency['name']);
1529         $this->assertEquals($no, $competency['rating']['reviewer_is_you']);
1530         $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1531         $this->assertEmpty($competency['evidence']);
1533         // Export data for user 5.
1534         $this->setUser($u5);
1535         writer::reset();
1536         provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id]));
1537         $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3));
1538         $competency = (array) $data;
1539         $this->assertEquals($comp3->get('shortname'), $competency['name']);
1540         $data = writer::with_context($u0ctx)->get_data(array_merge($makecomppath($comp3),
1541             [get_string('commentsubcontext', 'core_comment')]));
1542         $this->assert_exported_comments(['Hello!', 'It\'s me...'], $data->comments);
1543     }
1545     public function test_export_data_for_user_with_related_user_evidence() {
1546         $path = [
1547             get_string('competencies', 'core_competency'),
1548             get_string('privacy:path:relatedtome', 'core_competency'),
1549             get_string('privacy:path:userevidence', 'core_competency')
1550         ];
1551         $yes = transform::yesno(true);
1552         $no = transform::yesno(false);
1554         $dg = $this->getDataGenerator();
1555         $ccg = $dg->get_plugin_generator('core_competency');
1557         $u0 = $dg->create_user();
1558         $u0b = $dg->create_user();
1559         $u1 = $dg->create_user();
1560         $u2 = $dg->create_user();
1561         $u3 = $dg->create_user();
1562         $u4 = $dg->create_user();
1564         $u0ctx = context_user::instance($u0->id);
1566         $f = $ccg->create_framework();
1567         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1568         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1569         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1571         $this->setUser($u0);
1572         $ue0 = $ccg->create_user_evidence(['userid' => $u0->id]);
1574         $this->setUser($u1);
1575         $ue1 = $ccg->create_user_evidence(['userid' => $u0->id]);
1576         $ue1b = $ccg->create_user_evidence(['userid' => $u0b->id]);
1578         $this->setUser($u2);
1579         $ccg->create_user_evidence_competency(['userevidenceid' => $ue1->get('id'), 'competencyid' => $comp1->get('id')]);
1580         $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp1->get('id')]);
1581         $ue2 = $ccg->create_user_evidence(['userid' => $u0->id]);
1582         $ue2b = $ccg->create_user_evidence(['userid' => $u0b->id]);
1584         $this->setUser($u3);
1585         $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp2->get('id')]);
1586         $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp3->get('id')]);
1588         // Export for user 1.
1589         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
1590         $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]);
1591         $data = writer::with_context($u0ctx)->get_data($uepath);
1592         $this->assertEquals($ue1->get('name'), $data->name);
1593         $this->assertEquals($yes, $data->created_or_modified_by_you);
1594         $this->assertEmpty($data->competencies);
1596         // Export for user 2.
1597         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
1598         $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]);
1599         $data = writer::with_context($u0ctx)->get_data($uepath);
1600         $this->assertEquals($ue1->get('name'), $data->name);
1601         $this->assertEquals($no, $data->created_or_modified_by_you);
1602         $this->assertCount(1, $data->competencies);
1603         $competency = $data->competencies[0];
1604         $this->assertEquals($comp1->get('shortname'), $competency['name']);
1605         $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1607         $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]);
1608         $data = writer::with_context($u0ctx)->get_data($uepath);
1609         $this->assertEquals($ue2->get('name'), $data->name);
1610         $this->assertEquals($yes, $data->created_or_modified_by_you);
1611         $this->assertEmpty($data->competencies);
1613         // Export for user 3.
1614         provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
1615         $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]);
1616         $evidence = writer::with_context($u0ctx)->get_data($uepath);
1617         $this->assertEquals($ue2->get('name'), $evidence->name);
1618         $this->assertEquals($no, $evidence->created_or_modified_by_you);
1619         $this->assertCount(2, $evidence->competencies);
1620         $competency = $evidence->competencies[0];
1621         $this->assertEquals($comp2->get('shortname'), $competency['name']);
1622         $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1623         $competency = $evidence->competencies[1];
1624         $this->assertEquals($comp3->get('shortname'), $competency['name']);
1625         $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1626     }
1628     public function test_export_data_for_user_about_their_learning_plans() {
1629         $this->setAdminUser();
1630         $dg = $this->getDataGenerator();
1631         $ccg = $dg->get_plugin_generator('core_competency');
1632         $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:plans', 'core_competency')];
1633         $yes = transform::yesno(true);
1634         $no = transform::yesno(false);
1636         $u1 = $dg->create_user();
1637         $u2 = $dg->create_user();
1638         $u3 = $dg->create_user();
1639         $u1ctx = context_user::instance($u1->id);
1640         $u2ctx = context_user::instance($u2->id);
1642         $f = $ccg->create_framework();
1643         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1644         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1645         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1646         $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1648         $t = $ccg->create_template();
1649         $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]);
1650         $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]);
1651         $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]);
1653         $p1a = $ccg->create_plan(['userid' => $u1->id, 'templateid' => $t->get('id'),
1654             'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW]);
1655         $p1b = $ccg->create_plan(['userid' => $u1->id]);
1656         $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp1->get('id')]);
1657         $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
1658         $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp4->get('id')]);
1659         $p1c = $ccg->create_plan(['userid' => $u1->id]);
1660         $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp1->get('id')]);
1661         $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp3->get('id')]);
1662         $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp4->get('id')]);
1663         $p1d = $ccg->create_plan(['userid' => $u1->id]);
1665         $p2a = $ccg->create_plan(['userid' => $u2->id]);
1666         $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp1->get('id')]);
1667         $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp2->get('id')]);
1669         $uc1a = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u1->id,
1670             'grade' => 2, 'proficiency' => false]);
1671         $uc1b = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u1->id,
1672             'grade' => 3, 'proficiency' => false]);
1673         $uc1c = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u1->id]);
1675         // Add comments on plan.
1676         $this->allow_anyone_to_comment_anywhere();
1677         $this->setUser($u1);
1678         $p1a->get_comment_object()->add('Hello.');
1679         $p1a->get_comment_object()->add('It\'s me.');
1680         $this->setUser($u3);
1681         $p1a->get_comment_object()->add('After all these years...');
1683         // Complete the plan to create archiving, and modify the user competency again.
1684         api::complete_plan($p1c);
1685         $uc1a->set('grade', 1);
1686         $uc1a->set('proficiency', true);
1687         $uc1a->update();
1689         // Export user data in both contexts.
1690         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1692         // This plan is based off a template.
1693         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
1694         $this->assertNotEmpty($data);
1695         $this->assertEquals($p1a->get('name'), $data->name);
1696         $this->assertEquals($p1a->get_statusname(), $data->status);
1697         $this->assertCount(3, $data->competencies);
1698         $comp = $data->competencies[0];
1699         $this->assertEquals($comp2->get('shortname'), $comp['name']);
1700         $this->assertEquals('C', $comp['rating']['rating']);
1701         $comp = $data->competencies[1];
1702         $this->assertEquals($comp3->get('shortname'), $comp['name']);
1703         $this->assertEquals('-', $comp['rating']['rating']);
1704         $comp = $data->competencies[2];
1705         $this->assertEquals($comp4->get('shortname'), $comp['name']);
1706         $this->assertNull($comp['rating']['rating']);
1707         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})",
1708             get_string('commentsubcontext', 'core_comment')]));
1709         $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments);
1711         // This plan is manually created.
1712         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
1713         $this->assertNotEmpty($data);
1714         $this->assertEquals($p1b->get('name'), $data->name);
1715         $this->assertCount(3, $data->competencies);
1716         $comp = $data->competencies[0];
1717         $this->assertEquals($comp1->get('shortname'), $comp['name']);
1718         $this->assertEquals('A', $comp['rating']['rating']);
1719         $comp = $data->competencies[1];
1720         $this->assertEquals($comp2->get('shortname'), $comp['name']);
1721         $this->assertEquals('C', $comp['rating']['rating']);
1722         $comp = $data->competencies[2];
1723         $this->assertEquals($comp4->get('shortname'), $comp['name']);
1724         $this->assertNull($comp['rating']['rating']);
1726         // This plan is complete.
1727         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
1728         $this->assertNotEmpty($data);
1729         $this->assertEquals($p1c->get('name'), $data->name);
1730         $this->assertCount(3, $data->competencies);
1731         $comp = $data->competencies[0];
1732         $this->assertEquals($comp1->get('shortname'), $comp['name']);
1733         $this->assertEquals('B', $comp['rating']['rating']);
1734         $comp = $data->competencies[1];
1735         $this->assertEquals($comp3->get('shortname'), $comp['name']);
1736         $this->assertEquals('-', $comp['rating']['rating']);
1737         $comp = $data->competencies[2];
1738         $this->assertEquals($comp4->get('shortname'), $comp['name']);
1739         $this->assertEquals('-', $comp['rating']['rating']);
1741         // This plan is empty.
1742         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1d->get('name')} ({$p1d->get('id')})"]));
1743         $this->assertNotEmpty($data);
1744         $this->assertEquals($p1d->get('name'), $data->name);
1745         $this->assertEquals($p1d->get_statusname(), $data->status);
1746         $this->assertEmpty($data->competencies);
1748         // Confirm that we do not get export what we shouldn't.
1749         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
1750         $this->assertEmpty($data);
1751         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
1752         $this->assertEmpty($data);
1753         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
1754         $this->assertEmpty($data);
1755         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
1756         $this->assertEmpty($data);
1757         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
1758         $this->assertEmpty($data);
1760         // Export for user 2.
1761         writer::reset();
1762         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1764         // Validate the basic plan.
1765         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
1766         $this->assertNotEmpty($data);
1767         $this->assertEquals($p2a->get('name'), $data->name);
1768         $this->assertCount(2, $data->competencies);
1769         $comp = $data->competencies[0];
1770         $this->assertEquals($comp1->get('shortname'), $comp['name']);
1771         $this->assertNull($comp['rating']);
1772         $comp = $data->competencies[1];
1773         $this->assertEquals($comp2->get('shortname'), $comp['name']);
1774         $this->assertNull($comp['rating']);
1776         // Confirm that we do not get export what we shouldn't.
1777         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
1778         $this->assertEmpty($data);
1779         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
1780         $this->assertEmpty($data);
1781         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
1782         $this->assertEmpty($data);
1783         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
1784         $this->assertEmpty($data);
1785         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
1786         $this->assertEmpty($data);
1787         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
1788         $this->assertEmpty($data);
1789     }
1791     public function test_export_data_for_user_about_their_competencies() {
1792         $dg = $this->getDataGenerator();
1793         $ccg = $dg->get_plugin_generator('core_competency');
1794         $path = [get_string('competencies', 'core_competency'), get_string('competencies', 'core_competency')];
1795         $no = transform::yesno(false);
1797         $u1 = $dg->create_user();
1798         $u2 = $dg->create_user();
1799         $u3 = $dg->create_user();
1800         $u1ctx = context_user::instance($u1->id);
1801         $u2ctx = context_user::instance($u2->id);
1803         $f = $ccg->create_framework();
1804         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1805         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1806         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1808         $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1809         $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id'),
1810             'grade' => 2, 'proficiency' => false]);
1811         $uc1c = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp3->get('id')]);
1812         $e1a1 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'),
1813             'action' => \core_competency\evidence::ACTION_COMPLETE, 'grade' => 1]);
1814         $e1a2 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'), 'note' => 'Not too bad']);
1815         $e1b1 = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id'), 'url' => 'https://example.com']);
1817         $uc2a = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
1818         $uc2b = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
1819         $e2a1 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'A']);
1820         $e2a2 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'B']);
1821         $e2a3 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'C']);
1823         // Add comments on competency.
1824         $this->allow_anyone_to_comment_anywhere();
1825         $this->setUser($u1);
1826         $uc1a->get_comment_object()->add('Hello.');
1827         $uc1a->get_comment_object()->add('It\'s me.');
1828         $this->setUser($u3);
1829         $uc1a->get_comment_object()->add('After all these years...');
1831         // Export for user 1 in both contexts.
1832         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1833         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
1834         $this->assertNotEmpty($data);
1835         $this->assertEquals($comp1->get('shortname'), $data->name);
1836         $this->assertEquals('-', $data->rating['rating']);
1837         $this->assertCount(2, $data->evidence);
1838         $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $data->evidence[1]['action']);
1839         $this->assertEquals('Not too bad', $data->evidence[0]['note']);
1840         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})",
1841             get_string('commentsubcontext', 'core_comment')]));
1842         $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments);
1844         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
1845         $this->assertNotEmpty($data);
1846         $this->assertEquals($comp2->get('shortname'), $data->name);
1847         $this->assertEquals('B', $data->rating['rating']);
1848         $this->assertEquals($no, $data->rating['proficient']);
1849         $this->assertCount(1, $data->evidence);
1850         $this->assertEquals('https://example.com', $data->evidence[0]['url']);
1852         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
1853         $this->assertNotEmpty($data);
1854         $this->assertEquals($comp3->get('shortname'), $data->name);
1855         $this->assertEquals('-', $data->rating['rating']);
1856         $this->assertEquals('-', $data->rating['proficient']);
1857         $this->assertEmpty($data->evidence);
1859         // We don't know anything about user 2.
1860         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
1861         $this->assertEmpty($data);
1862         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
1863         $this->assertEmpty($data);
1864         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
1865         $this->assertEmpty($data);
1867         // Export for user 2 in both contexts.
1868         writer::reset();
1869         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1870         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
1871         $this->assertNotEmpty($data);
1872         $this->assertEquals($comp1->get('shortname'), $data->name);
1873         $this->assertEquals('-', $data->rating['rating']);
1874         $this->assertCount(0, $data->evidence);
1876         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
1877         $this->assertNotEmpty($data);
1878         $this->assertEquals($comp2->get('shortname'), $data->name);
1879         $this->assertEquals('-', $data->rating['rating']);
1880         $this->assertCount(3, $data->evidence);
1881         $this->assertEquals('C', $data->evidence[0]['note']);
1882         $this->assertEquals('B', $data->evidence[1]['note']);
1883         $this->assertEquals('A', $data->evidence[2]['note']);
1885         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
1886         $this->assertEmpty($data);
1888         // We don't know anything about user 1.
1889         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
1890         $this->assertEmpty($data);
1891         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
1892         $this->assertEmpty($data);
1893         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
1894         $this->assertEmpty($data);
1895     }
1897     public function test_export_data_for_user_about_their_user_evidence() {
1898         $dg = $this->getDataGenerator();
1899         $ccg = $dg->get_plugin_generator('core_competency');
1900         $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:userevidence', 'core_competency')];
1902         $u1 = $dg->create_user();
1903         $u2 = $dg->create_user();
1904         $u3 = $dg->create_user();
1906         $u1ctx = context_user::instance($u1->id);
1907         $u2ctx = context_user::instance($u2->id);
1908         $u3ctx = context_user::instance($u3->id);
1910         $f = $ccg->create_framework();
1911         $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1912         $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1913         $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1915         $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1916         $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
1917         $ue2a = $ccg->create_user_evidence(['userid' => $u2->id]);
1918         $ue3a = $ccg->create_user_evidence(['userid' => $u3->id]);
1919         $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp1->get('id')]);
1920         $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp2->get('id')]);
1921         $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp2->get('id')]);
1922         $ccg->create_user_evidence_competency(['userevidenceid' => $ue2a->get('id'), 'competencyid' => $comp2->get('id')]);
1924         // Export for user 1 in two contexts to make sure.
1925         provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1926         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
1927         $this->assertNotEmpty($data);
1928         $this->assertEquals($ue1a->get('name'), $data->name);
1929         $this->assertCount(2, $data->competencies);
1930         $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1931         $this->assertEquals($comp2->get('shortname'), $data->competencies[1]['name']);
1933         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
1934         $this->assertNotEmpty($data);
1935         $this->assertEquals($ue1b->get('name'), $data->name);
1936         $this->assertCount(1, $data->competencies);
1937         $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
1939         // We should not have access to other's info.
1940         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
1941         $this->assertEmpty($data);
1942         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
1943         $this->assertEmpty($data);
1945         // Export for user 2 in two contexts to make sure.
1946         writer::reset();
1947         provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u2ctx->id, $u1ctx->id]));
1948         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
1949         $this->assertNotEmpty($data);
1950         $this->assertEquals($ue2a->get('name'), $data->name);
1951         $this->assertCount(1, $data->competencies);
1952         $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
1954         // We should not have access to other's info.
1955         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
1956         $this->assertEmpty($data);
1957         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
1958         $this->assertEmpty($data);
1959         $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
1960         $this->assertEmpty($data);
1961         $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
1962         $this->assertEmpty($data);
1964         // Export for user 3.
1965         writer::reset();
1966         provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u3ctx->id]));
1967         $data = writer::with_context($u3ctx)->get_data(array_merge($path, ["{$ue3a->get('name')} ({$ue3a->get('id')})"]));
1968         $this->assertNotEmpty($data);
1969         $this->assertEquals($ue3a->get('name'), $data->name);
1970         $this->assertCount(0, $data->competencies);
1971     }
1973     /**
1974      * Helps testing comments on plans.
1975      *
1976      * @return void
1977      */
1978     protected function allow_anyone_to_comment_anywhere() {
1979         global $DB;
1980         $roleid = $DB->get_field('role', 'id', ['archetype' => 'user'], MUST_EXIST);
1981         assign_capability('moodle/competency:plancomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
1982         assign_capability('moodle/competency:planmanage', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
1983         assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
1984         assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
1985         assign_capability('moodle/competency:usercompetencyview', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
1986         accesslib_clear_all_caches_for_unit_testing();
1987     }
1989     /**
1990      * Assert the content of a contextlist.
1991      *
1992      * @param contextlist $contextlist The list.
1993      * @param array $expectedcontextsorids The expected content.
1994      * @return void
1995      */
1996     protected function assert_contextlist(contextlist $contextlist, $expectedcontextsorids) {
1997         $contextids = array_unique($contextlist->get_contextids());
1998         $expectedids = array_unique(array_map(function($item) {
1999             return $item instanceof context ? $item->id : $id;
2000         }, $expectedcontextsorids));
2001         $this->assert_array_match($contextids, $expectedids);
2002     }
2004     /**
2005      * Assert that array match.
2006      *
2007      * @param array $array1 The first one.
2008      * @param array $array2 The second one.
2009      * @return void
2010      */
2011     protected function assert_array_match($array1, $array2) {
2012         $array1 = (array) (object) $array1;
2013         $array2 = (array) (object) $array2;
2014         sort($array1);
2015         sort($array2);
2016         $this->assertEquals($array1, $array2);
2017     }
2019     /**
2020      * Assert the content of exported comments.
2021      *
2022      * @param array $expected The content of the comments.
2023      * @param array $comments The exported comments.
2024      * @return void
2025      */
2026     protected function assert_exported_comments($expected, $comments) {
2027         $this->assertCount(count($expected), $comments);
2028         $contents = array_map(function($comment) {
2029             return strip_tags($comment->content);
2030         }, $comments);
2031         $this->assert_array_match($expected, $contents);
2032     }
2034     /**
2035      * Assert that a comment object has comments.
2036      *
2037      * @param \comment $comment The comment object.
2038      * @return void
2039      */
2040     protected function assert_has_comments(\comment $comment) {
2041         global $DB;
2042         $this->assertTrue($DB->record_exists('comments', [
2043             'contextid' => $comment->get_context()->id,
2044             'component' => $comment->get_component(),
2045             'commentarea' => $comment->get_commentarea(),
2046             'itemid' => $comment->get_itemid()
2047         ]));
2048     }
2050     /**
2051      * Assert that a comment object does not have any comments.
2052      *
2053      * @param \comment $comment The comment object.
2054      * @return void
2055      */
2056     protected function assert_has_no_comments(\comment $comment) {
2057         global $DB;
2058         $this->assertFalse($DB->record_exists('comments', [
2059             'contextid' => $comment->get_context()->id,
2060             'component' => $comment->get_component(),
2061             'commentarea' => $comment->get_commentarea(),
2062             'itemid' => $comment->get_itemid()
2063         ]));
2064     }
2066     /**
2067      * Get the count of comments.
2068      *
2069      * @param \comment $comment The comment object.
2070      * @param int $userid The user ID.
2071      * @return int
2072      */
2073     protected function get_comments_count(\comment $comment, $userid = null) {
2074         global $DB;
2075         $params = [
2076             'contextid' => $comment->get_context()->id,
2077             'component' => $comment->get_component(),
2078             'commentarea' => $comment->get_commentarea(),
2079             'itemid' => $comment->get_itemid(),
2080         ];
2081         if ($userid) {
2082             $params['userid'] = $userid;
2083         }
2084         return $DB->count_records('comments', $params);
2085     }