Differences Between: [Versions 39 and 310]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Data provider tests. 19 * 20 * @package 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 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 global $CFG, $DB; 29 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\approved_userlist; 34 use core_privacy\local\request\transform; 35 use core_privacy\local\request\userlist; 36 use core_privacy\local\request\writer; 37 use core_competency\api; 38 use core_competency\privacy\provider; 39 40 /** 41 * Data provider testcase class. 42 * 43 * @package core_competency 44 * @category test 45 * @copyright 2018 Frédéric Massart 46 * @author Frédéric Massart <fred@branchup.tech> 47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 48 */ 49 class core_competency_privacy_testcase extends provider_testcase { 50 51 public function setUp(): void { 52 global $PAGE; 53 $this->resetAfterTest(); 54 55 // We need this or exporters (core\external\exporter) do not receive the right renderer. 56 $PAGE->get_renderer('core'); 57 } 58 59 public function test_get_contexts_for_userid_with_usermodified_for_framework() { 60 $dg = $this->getDataGenerator(); 61 $ccg = $dg->get_plugin_generator('core_competency'); 62 63 $cat1 = $dg->create_category(); 64 $cat2 = $dg->create_category(); 65 $u1 = $dg->create_user(); 66 $u2 = $dg->create_user(); 67 $u3 = $dg->create_user(); 68 $u4 = $dg->create_user(); 69 70 $sysctx = context_system::instance(); 71 $cat1ctx = context_coursecat::instance($cat1->id); 72 $cat2ctx = context_coursecat::instance($cat2->id); 73 74 // Test recovery through framework context. 75 $this->setUser($u1); 76 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 77 $f1 = $ccg->create_framework(); 78 $contextlist = provider::get_contexts_for_userid($u1->id); 79 $this->assert_contextlist($contextlist, [$sysctx]); 80 $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]); 81 $contextlist = provider::get_contexts_for_userid($u1->id); 82 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 83 84 // Test recovery of category context alone. 85 $this->setUser($u2); 86 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 87 $ccg->create_framework(['contextid' => $cat2ctx->id]); 88 $contextlist = provider::get_contexts_for_userid($u2->id); 89 $this->assert_contextlist($contextlist, [$cat2ctx]); 90 91 // Test recovery through competency. 92 $this->setUser($u3); 93 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 94 $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 95 $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 96 $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 97 $contextlist = provider::get_contexts_for_userid($u3->id); 98 $this->assert_contextlist($contextlist, [$sysctx]); 99 $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 100 $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 101 $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 102 $contextlist = provider::get_contexts_for_userid($u3->id); 103 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 104 105 // Test recovery through related competency. 106 $this->setUser($u4); 107 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 108 $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]); 109 $contextlist = provider::get_contexts_for_userid($u4->id); 110 $this->assert_contextlist($contextlist, [$sysctx]); 111 $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]); 112 $contextlist = provider::get_contexts_for_userid($u4->id); 113 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 114 } 115 116 public function test_get_users_in_context_with_usermodified_for_framework() { 117 $dg = $this->getDataGenerator(); 118 $ccg = $dg->get_plugin_generator('core_competency'); 119 120 $cat1 = $dg->create_category(); 121 $cat2 = $dg->create_category(); 122 $u1 = $dg->create_user(); 123 $u2 = $dg->create_user(); 124 $u3 = $dg->create_user(); 125 $u4 = $dg->create_user(); 126 127 $sysctx = context_system::instance(); 128 $cat1ctx = context_coursecat::instance($cat1->id); 129 $cat2ctx = context_coursecat::instance($cat2->id); 130 131 // Add frameworks. 132 $this->setUser($u1); 133 $f1 = $ccg->create_framework(); 134 $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]); 135 136 $this->setUser($u2); 137 $ccg->create_framework(['contextid' => $cat2ctx->id]); 138 139 // Add competencies. 140 $this->setUser($u3); 141 $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 142 $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 143 $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 144 $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 145 $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 146 $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 147 148 // Add related competencies. 149 $this->setUser($u4); 150 $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]); 151 $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]); 152 153 // Test correct users appear in each context. 154 $component = 'core_competency'; 155 156 $userlist = new userlist($sysctx, $component); 157 provider::get_users_in_context($userlist); 158 $expected = [$u1->id, $u3->id, $u4->id]; 159 $this->assert_array_match($expected, $userlist->get_userids()); 160 161 $userlist = new userlist($cat1ctx, $component); 162 provider::get_users_in_context($userlist); 163 $expected = [$u1->id, $u3->id, $u4->id]; 164 $this->assert_array_match($expected, $userlist->get_userids()); 165 166 $userlist = new userlist($cat2ctx, $component); 167 provider::get_users_in_context($userlist); 168 $expected = [$u2->id]; 169 $this->assert_array_match($expected, $userlist->get_userids()); 170 } 171 172 public function test_get_contexts_for_userid_with_usermodified_for_template() { 173 $dg = $this->getDataGenerator(); 174 $ccg = $dg->get_plugin_generator('core_competency'); 175 176 $cat1 = $dg->create_category(); 177 $cat2 = $dg->create_category(); 178 $u1 = $dg->create_user(); 179 $u2 = $dg->create_user(); 180 $u3 = $dg->create_user(); 181 $u4 = $dg->create_user(); 182 $cohort = $dg->create_cohort(); 183 184 $sysctx = context_system::instance(); 185 $cat1ctx = context_coursecat::instance($cat1->id); 186 $cat2ctx = context_coursecat::instance($cat2->id); 187 188 $f1 = $ccg->create_framework(); 189 $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]); 190 $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]); 191 $cs = []; 192 193 foreach ([$f1, $f2, $f3] as $f) { 194 $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 195 } 196 197 // Test recovery through template context. 198 $this->setUser($u1); 199 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 200 $t1 = $ccg->create_template(); 201 $contextlist = provider::get_contexts_for_userid($u1->id); 202 $this->assert_contextlist($contextlist, [$sysctx]); 203 $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]); 204 $contextlist = provider::get_contexts_for_userid($u1->id); 205 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 206 207 // Test recovery of category context alone. 208 $this->setUser($u2); 209 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 210 $ccg->create_template(['contextid' => $cat2ctx->id]); 211 $contextlist = provider::get_contexts_for_userid($u2->id); 212 $this->assert_contextlist($contextlist, [$cat2ctx]); 213 214 // Test recovery through template competency. 215 $this->setUser($u3); 216 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 217 $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]); 218 $contextlist = provider::get_contexts_for_userid($u3->id); 219 $this->assert_contextlist($contextlist, [$sysctx]); 220 $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]); 221 $contextlist = provider::get_contexts_for_userid($u3->id); 222 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 223 224 // Test recovery through template cohort. 225 $this->setUser($u4); 226 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 227 $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]); 228 $contextlist = provider::get_contexts_for_userid($u4->id); 229 $this->assert_contextlist($contextlist, [$sysctx]); 230 $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]); 231 $contextlist = provider::get_contexts_for_userid($u4->id); 232 $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]); 233 } 234 235 public function test_get_users_in_context_with_usermodified_for_template() { 236 $dg = $this->getDataGenerator(); 237 $ccg = $dg->get_plugin_generator('core_competency'); 238 239 $cat1 = $dg->create_category(); 240 $cat2 = $dg->create_category(); 241 $u1 = $dg->create_user(); 242 $u2 = $dg->create_user(); 243 $u3 = $dg->create_user(); 244 $u4 = $dg->create_user(); 245 $cohort = $dg->create_cohort(); 246 247 $sysctx = context_system::instance(); 248 $cat1ctx = context_coursecat::instance($cat1->id); 249 $cat2ctx = context_coursecat::instance($cat2->id); 250 251 $f1 = $ccg->create_framework(); 252 $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]); 253 $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]); 254 $cs = []; 255 256 foreach ([$f1, $f2, $f3] as $f) { 257 $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 258 } 259 260 // Create template context. 261 $this->setUser($u1); 262 $t1 = $ccg->create_template(); 263 $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]); 264 265 // Add to category context. 266 $this->setUser($u2); 267 $ccg->create_template(['contextid' => $cat2ctx->id]); 268 269 // Create template competencies. 270 $this->setUser($u3); 271 $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]); 272 $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]); 273 274 // Create template cohorts. 275 $this->setUser($u4); 276 $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]); 277 $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]); 278 279 // Test correct users appear in each context. 280 $component = 'core_competency'; 281 282 $userlist = new userlist($sysctx, $component); 283 provider::get_users_in_context($userlist); 284 $expected = [$u1->id, $u3->id, $u4->id]; 285 $this->assert_array_match($expected, $userlist->get_userids()); 286 287 $userlist = new userlist($cat1ctx, $component); 288 provider::get_users_in_context($userlist); 289 $expected = [$u1->id, $u3->id, $u4->id]; 290 $this->assert_array_match($expected, $userlist->get_userids()); 291 292 $userlist = new userlist($cat2ctx, $component); 293 provider::get_users_in_context($userlist); 294 $expected = [$u2->id]; 295 $this->assert_array_match($expected, $userlist->get_userids()); 296 } 297 298 public function test_get_contexts_for_userid_with_usermodified_for_course() { 299 $dg = $this->getDataGenerator(); 300 $ccg = $dg->get_plugin_generator('core_competency'); 301 $c1 = $dg->create_course(); 302 $c2 = $dg->create_course(); 303 $u0 = $dg->create_user(); 304 $u1 = $dg->create_user(); 305 $u2 = $dg->create_user(); 306 $u3 = $dg->create_user(); 307 $u4 = $dg->create_user(); 308 $c1ctx = context_course::instance($c1->id); 309 $c2ctx = context_course::instance($c2->id); 310 311 $f = $ccg->create_framework(); 312 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 313 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 314 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 $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]); 319 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 320 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 321 322 $this->setUser($u2); 323 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 324 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 325 $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]); 326 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 327 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c2ctx]); 328 $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]); 329 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 330 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]); 331 332 $this->setUser($u3); 333 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 334 $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]); 335 $ccs->create(); 336 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 337 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]); 338 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]); 339 340 $this->setUser($u4); 341 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 342 $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]); 343 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]); 344 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]); 345 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]); 346 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$c2ctx]); 347 } 348 349 public function test_get_users_in_context_with_usermodified_for_course() { 350 $dg = $this->getDataGenerator(); 351 $ccg = $dg->get_plugin_generator('core_competency'); 352 $c1 = $dg->create_course(); 353 $c2 = $dg->create_course(); 354 $u0 = $dg->create_user(); 355 $u1 = $dg->create_user(); 356 $u2 = $dg->create_user(); 357 $u3 = $dg->create_user(); 358 $u4 = $dg->create_user(); 359 $c1ctx = context_course::instance($c1->id); 360 $c2ctx = context_course::instance($c2->id); 361 362 $f = $ccg->create_framework(); 363 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 364 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 365 366 $this->setUser($u1); 367 $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]); 368 369 $this->setUser($u2); 370 $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]); 371 $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]); 372 373 $this->setUser($u3); 374 $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]); 375 $ccs->create(); 376 377 $this->setUser($u4); 378 $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]); 379 380 // Test correct users appear in each context. 381 $component = 'core_competency'; 382 383 $userlist = new userlist($c1ctx, $component); 384 provider::get_users_in_context($userlist); 385 $expected = [$u1->id, $u2->id, $u3->id]; 386 $this->assert_array_match($expected, $userlist->get_userids()); 387 388 $userlist = new userlist($c2ctx, $component); 389 provider::get_users_in_context($userlist); 390 $expected = [$u0->id, $u2->id, $u4->id]; 391 $this->assert_array_match($expected, $userlist->get_userids()); 392 } 393 394 public function test_get_contexts_for_userid_with_usermodified_for_module() { 395 $dg = $this->getDataGenerator(); 396 $ccg = $dg->get_plugin_generator('core_competency'); 397 $c1 = $dg->create_course(); 398 $m1 = $dg->create_module('choice', ['course' => $c1]); 399 $m2 = $dg->create_module('choice', ['course' => $c1]); 400 $u1 = $dg->create_user(); 401 $u2 = $dg->create_user(); 402 $m1ctx = context_module::instance($m1->cmid); 403 $m2ctx = context_module::instance($m2->cmid); 404 405 $f = $ccg->create_framework(); 406 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 407 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 408 409 $this->setUser($u1); 410 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 411 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 412 $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]); 413 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]); 414 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 415 416 $this->setUser($u2); 417 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]); 418 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 419 $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]); 420 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]); 421 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m2ctx]); 422 $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]); 423 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]); 424 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m1ctx, $m2ctx]); 425 } 426 427 public function test_get_users_in_context_with_usermodified_for_module() { 428 $dg = $this->getDataGenerator(); 429 $ccg = $dg->get_plugin_generator('core_competency'); 430 $c1 = $dg->create_course(); 431 $m1 = $dg->create_module('choice', ['course' => $c1]); 432 $m2 = $dg->create_module('choice', ['course' => $c1]); 433 $u1 = $dg->create_user(); 434 $u2 = $dg->create_user(); 435 $m1ctx = context_module::instance($m1->cmid); 436 $m2ctx = context_module::instance($m2->cmid); 437 438 $f = $ccg->create_framework(); 439 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 440 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 441 442 $this->setUser($u1); 443 $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]); 444 445 $this->setUser($u2); 446 $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]); 447 $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]); 448 449 // Test correct users appear in each context. 450 $component = 'core_competency'; 451 452 $userlist = new userlist($m1ctx, $component); 453 provider::get_users_in_context($userlist); 454 $expected = [$u1->id, $u2->id]; 455 $this->assert_array_match($expected, $userlist->get_userids()); 456 457 $userlist = new userlist($m2ctx, $component); 458 provider::get_users_in_context($userlist); 459 $expected = [$u2->id]; 460 $this->assert_array_match($expected, $userlist->get_userids()); 461 } 462 463 public function test_get_contexts_for_userid_with_usermodified_for_plan() { 464 $dg = $this->getDataGenerator(); 465 $ccg = $dg->get_plugin_generator('core_competency'); 466 $u0 = $dg->create_user(); 467 $u1 = $dg->create_user(); 468 $u2 = $dg->create_user(); 469 $u3 = $dg->create_user(); 470 $u0ctx = context_user::instance($u0->id); 471 472 $f = $ccg->create_framework(); 473 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 474 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 475 476 $this->setUser($u1); 477 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 478 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 479 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 480 $plan = $ccg->create_plan(['userid' => $u0->id]); 481 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 482 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 483 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 484 485 $this->setUser($u2); 486 $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]); 487 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 488 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]); 489 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 490 491 $this->setUser($u3); 492 $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'), 493 'userid' => $u0->id]); 494 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 495 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]); 496 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]); 497 } 498 499 public function test_get_users_in_context_with_usermodified_for_plan() { 500 $dg = $this->getDataGenerator(); 501 $ccg = $dg->get_plugin_generator('core_competency'); 502 $u0 = $dg->create_user(); 503 $u1 = $dg->create_user(); 504 $u2 = $dg->create_user(); 505 $u3 = $dg->create_user(); 506 $u0ctx = context_user::instance($u0->id); 507 508 $f = $ccg->create_framework(); 509 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 510 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 511 512 $this->setUser($u1); 513 $plan = $ccg->create_plan(['userid' => $u0->id]); 514 515 $this->setUser($u2); 516 $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]); 517 518 $this->setUser($u3); 519 $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'), 520 'userid' => $u0->id]); 521 522 // Test correct users appear in the context. 523 $component = 'core_competency'; 524 525 $userlist = new userlist($u0ctx, $component); 526 provider::get_users_in_context($userlist); 527 $expected = [$u0->id, $u1->id, $u2->id, $u3->id]; 528 $this->assert_array_match($expected, $userlist->get_userids()); 529 } 530 531 public function test_get_contexts_for_userid_with_usermodified_for_competency_data() { 532 $dg = $this->getDataGenerator(); 533 $ccg = $dg->get_plugin_generator('core_competency'); 534 $u0 = $dg->create_user(); 535 $u1 = $dg->create_user(); 536 $u2 = $dg->create_user(); 537 $u3 = $dg->create_user(); 538 $u4 = $dg->create_user(); 539 $u5 = $dg->create_user(); 540 $u6 = $dg->create_user(); 541 $u7 = $dg->create_user(); 542 $u8 = $dg->create_user(); 543 $u0ctx = context_user::instance($u0->id); 544 545 $f = $ccg->create_framework(); 546 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 547 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 548 549 $this->setUser($u1); 550 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 551 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 552 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 553 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 554 $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []); 555 $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), []); 556 $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'), 557 'reviewerid' => $u6->id]); 558 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 559 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 560 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 561 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 562 $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []); 563 $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]); 564 565 $this->setUser($u2); 566 $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]); 567 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 568 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]); 569 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 570 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 571 $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]); 572 $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]); 573 574 $this->setUser($u3); 575 $ccg->create_user_evidence(['userid' => $u0->id]); 576 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 577 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]); 578 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]); 579 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 580 $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]); 581 $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]); 582 583 $this->setUser($u4); 584 $ccg->create_user_evidence(['userid' => $u0->id]); 585 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]); 586 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]); 587 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]); 588 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u0ctx]); 589 $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]); 590 $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]); 591 592 // Comment on competency. 593 $this->allow_anyone_to_comment_anywhere(); 594 $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), []); 595 $this->setUser($u7); 596 $comments = $uc->get_comment_object(); 597 $comments->add('Hello there!'); 598 $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), [$u0ctx]); 599 600 // Comment on plan. 601 $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), []); 602 $this->setUser($u8); 603 $plan = $ccg->create_plan(['userid' => $u0->id]); 604 $comments = $plan->get_comment_object(); 605 $comments->add('Hi, planet!'); 606 $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), [$u0ctx]); 607 } 608 609 public function test_get_users_in_context_with_usermodified_for_competency_data() { 610 $dg = $this->getDataGenerator(); 611 $ccg = $dg->get_plugin_generator('core_competency'); 612 $u0 = $dg->create_user(); 613 $u1 = $dg->create_user(); 614 $u2 = $dg->create_user(); 615 $u3 = $dg->create_user(); 616 $u4 = $dg->create_user(); 617 $u5 = $dg->create_user(); 618 $u6 = $dg->create_user(); 619 $u7 = $dg->create_user(); 620 $u8 = $dg->create_user(); 621 $u0ctx = context_user::instance($u0->id); 622 623 $f = $ccg->create_framework(); 624 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 625 626 $this->setUser($u1); 627 $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'), 628 'reviewerid' => $u6->id]); 629 630 $this->setUser($u2); 631 $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]); 632 633 $this->setUser($u3); 634 $ccg->create_user_evidence(['userid' => $u0->id]); 635 636 $this->setUser($u4); 637 $ccg->create_user_evidence(['userid' => $u0->id]); 638 639 // Comment on competency. 640 $this->allow_anyone_to_comment_anywhere(); 641 $this->setUser($u7); 642 $comments = $uc->get_comment_object(); 643 $comments->add('Hello there!'); 644 645 // Comment on plan. 646 $this->setUser($u8); 647 $plan = $ccg->create_plan(['userid' => $u0->id]); 648 $comments = $plan->get_comment_object(); 649 $comments->add('Hi, planet!'); 650 651 // Test correct users appear in the context. 652 $component = 'core_competency'; 653 654 $userlist = new userlist($u0ctx, $component); 655 provider::get_users_in_context($userlist); 656 $expected = [$u0->id, $u1->id, $u2->id, $u3->id, $u4->id, $u5->id, $u6->id, $u7->id, $u8->id]; 657 $this->assert_array_match($expected, $userlist->get_userids()); 658 } 659 660 public function test_get_contexts_for_userid_with_actual_data_and_actual_data_is_goooood() { 661 $dg = $this->getDataGenerator(); 662 $ccg = $dg->get_plugin_generator('core_competency'); 663 $c1 = $dg->create_course(); 664 $u1 = $dg->create_user(); 665 $u2 = $dg->create_user(); 666 $u3 = $dg->create_user(); 667 $u4 = $dg->create_user(); 668 669 $c1ctx = context_course::instance($c1->id); 670 $u1ctx = context_user::instance($u1->id); 671 $u2ctx = context_user::instance($u2->id); 672 $u3ctx = context_user::instance($u3->id); 673 $u4ctx = context_user::instance($u4->id); 674 675 $f = $ccg->create_framework(); 676 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 677 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 678 679 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []); 680 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 681 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 682 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 683 684 $ccg->create_plan(['userid' => $u1->id]); 685 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]); 686 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []); 687 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 688 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 689 690 $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]); 691 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]); 692 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]); 693 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []); 694 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 695 696 $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]); 697 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]); 698 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]); 699 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]); 700 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []); 701 702 $ccg->create_user_evidence(['userid' => $u4->id]); 703 $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]); 704 $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]); 705 $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]); 706 $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u4ctx]); 707 } 708 709 public function test_get_users_in_context_with_actual_data_and_actual_data_is_goooood() { 710 $dg = $this->getDataGenerator(); 711 $ccg = $dg->get_plugin_generator('core_competency'); 712 $c1 = $dg->create_course(); 713 $u1 = $dg->create_user(); 714 $u2 = $dg->create_user(); 715 $u3 = $dg->create_user(); 716 $u4 = $dg->create_user(); 717 718 $c1ctx = context_course::instance($c1->id); 719 $u1ctx = context_user::instance($u1->id); 720 $u2ctx = context_user::instance($u2->id); 721 $u3ctx = context_user::instance($u3->id); 722 $u4ctx = context_user::instance($u4->id); 723 724 $f = $ccg->create_framework(); 725 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 726 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 727 728 $ccg->create_plan(['userid' => $u1->id]); 729 730 $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]); 731 732 $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]); 733 734 $ccg->create_user_evidence(['userid' => $u4->id]); 735 736 // Test correct users appear in each context. 737 $component = 'core_competency'; 738 739 $userlist = new userlist($u1ctx, $component); 740 provider::get_users_in_context($userlist); 741 $this->assert_array_match([$u1->id], $userlist->get_userids()); 742 743 $userlist = new userlist($u2ctx, $component); 744 provider::get_users_in_context($userlist); 745 $this->assert_array_match([$u2->id], $userlist->get_userids()); 746 747 $userlist = new userlist($c1ctx, $component); 748 provider::get_users_in_context($userlist); 749 $this->assert_array_match([$u3->id], $userlist->get_userids()); 750 751 $userlist = new userlist($u4ctx, $component); 752 provider::get_users_in_context($userlist); 753 $this->assert_array_match([$u4->id], $userlist->get_userids()); 754 } 755 756 public function test_delete_data_for_user() { 757 $dg = $this->getDataGenerator(); 758 $ccg = $dg->get_plugin_generator('core_competency'); 759 760 $c1 = $dg->create_course(); 761 $c2 = $dg->create_course(); 762 $u1 = $dg->create_user(); 763 $u2 = $dg->create_user(); 764 765 $c1ctx = context_course::instance($c1->id); 766 $u1ctx = context_user::instance($u1->id); 767 $u2ctx = context_user::instance($u2->id); 768 769 $f = $ccg->create_framework(); 770 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 771 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 772 773 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 774 $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]); 775 $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]); 776 $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 777 'competencyid' => $comp1->get('id')]); 778 $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 779 'competencyid' => $comp2->get('id')]); 780 $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 781 'competencyid' => $comp1->get('id')]); 782 783 $p1a = $ccg->create_plan(['userid' => $u1->id]); 784 $p1b = $ccg->create_plan(['userid' => $u1->id]); 785 $p2 = $ccg->create_plan(['userid' => $u2->id]); 786 $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]); 787 $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]); 788 $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]); 789 $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'), 790 'competencyid' => $comp1->get('id')]); 791 $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'), 792 'competencyid' => $comp2->get('id')]); 793 $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'), 794 'competencyid' => $comp1->get('id')]); 795 796 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 797 $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]); 798 $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]); 799 $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]); 800 $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]); 801 $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]); 802 803 $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id, 804 'competencyid' => $comp1->get('id')]); 805 $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id, 806 'competencyid' => $comp1->get('id')]); 807 $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id, 808 'competencyid' => $comp1->get('id')]); 809 810 // User 1 comments on both plans. 811 $this->allow_anyone_to_comment_anywhere(); 812 $this->setUser($u1); 813 $p1a->get_comment_object()->add('Hi...'); 814 $p1a->get_comment_object()->add('mister'); 815 $p2->get_comment_object()->add('Ahoy!'); 816 817 // User 2 comments on both competencies. 818 $this->setUser($u2); 819 $uc1a->get_comment_object()->add('Hi, too!'); 820 $uc1a->get_comment_object()->add('How are you?'); 821 $uc2->get_comment_object()->add('Ahoy, too!'); 822 823 $p1acommentobj = $p1a->get_comment_object(); 824 $p2commentobj = $p2->get_comment_object(); 825 $uc1acommentobj = $uc1a->get_comment_object(); 826 $uc2commentobj = $uc2->get_comment_object(); 827 828 $this->setAdminUser(); 829 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 830 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 831 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 832 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 833 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 834 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 835 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 836 $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id'))); 837 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 838 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 839 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 840 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 841 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 842 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 843 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 844 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 845 $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id'))); 846 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 847 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 848 $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id'))); 849 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 850 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 851 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 852 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 853 $this->assert_has_comments($p1acommentobj); 854 $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id)); 855 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id)); 856 $this->assert_has_comments($p2commentobj); 857 $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id)); 858 $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id)); 859 $this->assert_has_comments($uc1acommentobj); 860 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id)); 861 $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id)); 862 $this->assert_has_comments($uc2commentobj); 863 $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id)); 864 $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id)); 865 866 // Deleting user context only. 867 $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id]); 868 provider::delete_data_for_user($appctx); 869 870 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 871 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 872 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 873 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 874 $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id'))); 875 $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id'))); 876 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 877 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 878 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 879 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 880 $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id'))); 881 $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id'))); 882 $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id'))); 883 $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id'))); 884 885 $this->assert_has_no_comments($p1acommentobj); 886 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id)); 887 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id)); 888 $this->assert_has_no_comments($uc1acommentobj); 889 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id)); 890 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id)); 891 892 // This should not have been affected. 893 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 894 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 895 896 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 897 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 898 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 899 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 900 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 901 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 902 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 903 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 904 $this->assert_has_comments($p2commentobj); 905 $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id)); 906 $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id)); 907 $this->assert_has_comments($uc2commentobj); 908 $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id)); 909 $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id)); 910 911 // Deleting course context as well. 912 $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $c1ctx->id]); 913 provider::delete_data_for_user($appctx); 914 915 $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 916 917 // The rest belongs to another course, or the other user. 918 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 919 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 920 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 921 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 922 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 923 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 924 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 925 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 926 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 927 } 928 929 public function test_delete_data_for_user_with_other_user_context() { 930 $dg = $this->getDataGenerator(); 931 $ccg = $dg->get_plugin_generator('core_competency'); 932 933 $u1 = $dg->create_user(); 934 $u2 = $dg->create_user(); 935 936 $u1ctx = context_user::instance($u1->id); 937 $u2ctx = context_user::instance($u2->id); 938 939 $f = $ccg->create_framework(); 940 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 941 942 // Create a bunch of data for user 1. 943 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 944 $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 945 'competencyid' => $comp1->get('id')]); 946 $p1a = $ccg->create_plan(['userid' => $u1->id]); 947 $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]); 948 $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'), 949 'competencyid' => $comp1->get('id')]); 950 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 951 $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]); 952 953 $p2a = $ccg->create_plan(['userid' => $u2->id]); 954 955 // User 2 comments. 956 $this->allow_anyone_to_comment_anywhere(); 957 $this->setUser($u2); 958 $p1a->get_comment_object()->add('Hi...'); 959 $p2a->get_comment_object()->add('Hi, hi!'); 960 $uc1a->get_comment_object()->add('Hi, too!'); 961 962 // Confirm state. 963 $this->setAdminUser(); 964 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 965 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 966 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 967 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 968 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 969 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 970 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 971 $this->assert_has_comments($p1a->get_comment_object()); 972 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 973 $this->assert_has_comments($p2a->get_comment_object()); 974 $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id)); 975 $this->assert_has_comments($uc1a->get_comment_object()); 976 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 977 978 $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id'))); 979 980 // Delete for user 2, but we pass u1 context. 981 provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id])); 982 983 // Nothing should have happened. 984 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 985 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 986 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 987 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 988 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 989 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 990 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 991 $this->assert_has_comments($p1a->get_comment_object()); 992 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 993 $this->assert_has_comments($p2a->get_comment_object()); 994 $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id)); 995 $this->assert_has_comments($uc1a->get_comment_object()); 996 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 997 998 $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id'))); 999 1000 // Delete for user 2, but we pass u1 and u2 context. 1001 $p2acommentobj = $p2a->get_comment_object(); 1002 provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id])); 1003 1004 // The plan got deleted. 1005 $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id'))); 1006 $this->assert_has_no_comments($p2acommentobj); 1007 1008 // Nothing should have happened for u1. 1009 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1010 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1011 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1012 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1013 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1014 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1015 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1016 $this->assert_has_comments($p1a->get_comment_object()); 1017 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 1018 $this->assert_has_comments($uc1a->get_comment_object()); 1019 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 1020 } 1021 1022 public function test_delete_data_for_users() { 1023 $dg = $this->getDataGenerator(); 1024 $ccg = $dg->get_plugin_generator('core_competency'); 1025 1026 $c1 = $dg->create_course(); 1027 $c2 = $dg->create_course(); 1028 $u1 = $dg->create_user(); 1029 $u2 = $dg->create_user(); 1030 1031 $c1ctx = context_course::instance($c1->id); 1032 $u1ctx = context_user::instance($u1->id); 1033 1034 $f = $ccg->create_framework(); 1035 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1036 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1037 1038 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 1039 $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]); 1040 $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]); 1041 $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 1042 'competencyid' => $comp1->get('id')]); 1043 $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 1044 'competencyid' => $comp2->get('id')]); 1045 $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 1046 'competencyid' => $comp1->get('id')]); 1047 1048 $p1a = $ccg->create_plan(['userid' => $u1->id]); 1049 $p1b = $ccg->create_plan(['userid' => $u1->id]); 1050 $p2 = $ccg->create_plan(['userid' => $u2->id]); 1051 $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]); 1052 $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]); 1053 $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]); 1054 $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'), 1055 'competencyid' => $comp1->get('id')]); 1056 $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'), 1057 'competencyid' => $comp2->get('id')]); 1058 $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'), 1059 'competencyid' => $comp1->get('id')]); 1060 1061 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 1062 $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]); 1063 $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]); 1064 $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]); 1065 $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]); 1066 $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]); 1067 1068 $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id, 1069 'competencyid' => $comp1->get('id')]); 1070 $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id, 1071 'competencyid' => $comp1->get('id')]); 1072 $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id, 1073 'competencyid' => $comp1->get('id')]); 1074 1075 // User 1 comments on both plans. 1076 $this->allow_anyone_to_comment_anywhere(); 1077 $this->setUser($u1); 1078 $p1a->get_comment_object()->add('Hi...'); 1079 $p1a->get_comment_object()->add('mister'); 1080 $p2->get_comment_object()->add('Ahoy!'); 1081 1082 // User 2 comments on both competencies. 1083 $this->setUser($u2); 1084 $uc1a->get_comment_object()->add('Hi, too!'); 1085 $uc1a->get_comment_object()->add('How are you?'); 1086 $uc2->get_comment_object()->add('Ahoy, too!'); 1087 1088 $p1acommentobj = $p1a->get_comment_object(); 1089 $p2commentobj = $p2->get_comment_object(); 1090 $uc1acommentobj = $uc1a->get_comment_object(); 1091 $uc2commentobj = $uc2->get_comment_object(); 1092 1093 $this->setAdminUser(); 1094 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1095 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 1096 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1097 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1098 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 1099 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1100 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1101 $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id'))); 1102 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1103 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1104 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 1105 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1106 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1107 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 1108 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1109 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1110 $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id'))); 1111 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1112 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1113 $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id'))); 1114 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1115 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 1116 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1117 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 1118 $this->assert_has_comments($p1acommentobj); 1119 $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id)); 1120 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id)); 1121 $this->assert_has_comments($p2commentobj); 1122 $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id)); 1123 $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id)); 1124 $this->assert_has_comments($uc1acommentobj); 1125 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id)); 1126 $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id)); 1127 $this->assert_has_comments($uc2commentobj); 1128 $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id)); 1129 $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id)); 1130 1131 // Deleting user context. 1132 $userlist = new approved_userlist($u1ctx, 'core_competency', [$u1->id, $u2->id]); 1133 provider::delete_data_for_users($userlist); 1134 1135 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1136 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 1137 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1138 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 1139 $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id'))); 1140 $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id'))); 1141 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1142 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 1143 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1144 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 1145 $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1146 $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id'))); 1147 $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id'))); 1148 $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id'))); 1149 1150 $this->assert_has_no_comments($p1acommentobj); 1151 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id)); 1152 $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id)); 1153 $this->assert_has_no_comments($uc1acommentobj); 1154 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id)); 1155 $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id)); 1156 1157 // This should not have been affected. 1158 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 1159 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1160 1161 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1162 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1163 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1164 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1165 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1166 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1167 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1168 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 1169 $this->assert_has_comments($p2commentobj); 1170 $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id)); 1171 $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id)); 1172 $this->assert_has_comments($uc2commentobj); 1173 $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id)); 1174 $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id)); 1175 1176 // Deleting course context as well. 1177 $userlist = new approved_userlist($c1ctx, 'core_competency', [$u1->id]); 1178 provider::delete_data_for_users($userlist); 1179 1180 $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 1181 1182 // The rest belongs to another course, or the other user. 1183 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1184 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1185 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1186 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1187 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1188 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1189 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1190 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1191 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 1192 } 1193 1194 public function test_delete_data_for_users_with_other_user_context() { 1195 $dg = $this->getDataGenerator(); 1196 $ccg = $dg->get_plugin_generator('core_competency'); 1197 1198 $u1 = $dg->create_user(); 1199 $u2 = $dg->create_user(); 1200 1201 $u1ctx = context_user::instance($u1->id); 1202 $u2ctx = context_user::instance($u2->id); 1203 1204 $f = $ccg->create_framework(); 1205 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1206 1207 // Create a bunch of data for user 1. 1208 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 1209 $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 1210 'competencyid' => $comp1->get('id')]); 1211 $p1a = $ccg->create_plan(['userid' => $u1->id]); 1212 $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]); 1213 $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'), 1214 'competencyid' => $comp1->get('id')]); 1215 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 1216 $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]); 1217 1218 $p2a = $ccg->create_plan(['userid' => $u2->id]); 1219 1220 // User 2 comments. 1221 $this->allow_anyone_to_comment_anywhere(); 1222 $this->setUser($u2); 1223 $p1a->get_comment_object()->add('Hi...'); 1224 $p2a->get_comment_object()->add('Hi, hi!'); 1225 $uc1a->get_comment_object()->add('Hi, too!'); 1226 1227 // Confirm state. 1228 $this->setAdminUser(); 1229 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1230 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1231 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1232 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1233 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1234 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1235 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1236 $this->assert_has_comments($p1a->get_comment_object()); 1237 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 1238 $this->assert_has_comments($p2a->get_comment_object()); 1239 $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id)); 1240 $this->assert_has_comments($uc1a->get_comment_object()); 1241 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 1242 1243 $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id'))); 1244 1245 // Delete for user 2, but we pass u1 context. 1246 $userlist = new approved_userlist($u1ctx, 'core_competency', [$u2->id]); 1247 provider::delete_data_for_users($userlist); 1248 1249 // Nothing should have happened. 1250 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1251 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1252 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1253 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1254 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1255 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1256 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1257 $this->assert_has_comments($p1a->get_comment_object()); 1258 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 1259 $this->assert_has_comments($p2a->get_comment_object()); 1260 $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id)); 1261 $this->assert_has_comments($uc1a->get_comment_object()); 1262 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 1263 1264 $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id'))); 1265 1266 // Delete for user 2, in user 2 context. 1267 $p2acommentobj = $p2a->get_comment_object(); 1268 $userlist = new approved_userlist($u2ctx, 'core_competency', [$u2->id]); 1269 provider::delete_data_for_users($userlist); 1270 1271 // The plan got deleted. 1272 $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id'))); 1273 $this->assert_has_no_comments($p2acommentobj); 1274 1275 // Nothing should have happened for u1. 1276 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1277 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1278 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1279 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1280 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1281 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1282 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1283 $this->assert_has_comments($p1a->get_comment_object()); 1284 $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id)); 1285 $this->assert_has_comments($uc1a->get_comment_object()); 1286 $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id)); 1287 } 1288 1289 public function test_delete_data_for_all_users_in_context() { 1290 $dg = $this->getDataGenerator(); 1291 $ccg = $dg->get_plugin_generator('core_competency'); 1292 1293 $c1 = $dg->create_course(); 1294 $c2 = $dg->create_course(); 1295 $u1 = $dg->create_user(); 1296 $u2 = $dg->create_user(); 1297 1298 $c1ctx = context_course::instance($c1->id); 1299 $u1ctx = context_user::instance($u1->id); 1300 $u2ctx = context_user::instance($u2->id); 1301 1302 $f = $ccg->create_framework(); 1303 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1304 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1305 1306 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 1307 $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]); 1308 $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]); 1309 $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 1310 'competencyid' => $comp1->get('id')]); 1311 $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 1312 'competencyid' => $comp2->get('id')]); 1313 $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 1314 'competencyid' => $comp1->get('id')]); 1315 1316 $p1a = $ccg->create_plan(['userid' => $u1->id]); 1317 $p1b = $ccg->create_plan(['userid' => $u1->id]); 1318 $p2 = $ccg->create_plan(['userid' => $u2->id]); 1319 $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]); 1320 $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]); 1321 $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]); 1322 $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'), 1323 'competencyid' => $comp1->get('id')]); 1324 $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'), 1325 'competencyid' => $comp2->get('id')]); 1326 $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'), 1327 'competencyid' => $comp1->get('id')]); 1328 1329 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 1330 $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]); 1331 $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]); 1332 $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]); 1333 $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]); 1334 $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]); 1335 1336 $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id, 1337 'competencyid' => $comp1->get('id')]); 1338 $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id, 1339 'competencyid' => $comp1->get('id')]); 1340 $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id, 1341 'competencyid' => $comp1->get('id')]); 1342 1343 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1344 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 1345 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1346 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1347 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 1348 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1349 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1350 $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id'))); 1351 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1352 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1353 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 1354 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1355 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1356 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 1357 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1358 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1359 $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id'))); 1360 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1361 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1362 $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id'))); 1363 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1364 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 1365 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1366 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 1367 1368 // Deleting the course 1 context. 1369 provider::delete_data_for_all_users_in_context($c1ctx); 1370 $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id'))); 1371 $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc2->get('id'))); 1372 1373 // Not affected. 1374 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1375 $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 1376 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1377 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1378 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 1379 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1380 $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id'))); 1381 $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id'))); 1382 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1383 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1384 $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 1385 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1386 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1387 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 1388 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1389 $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1390 $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id'))); 1391 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1392 $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id'))); 1393 $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id'))); 1394 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1395 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1396 1397 // Deleting the user 1 context. 1398 provider::delete_data_for_all_users_in_context($u1ctx); 1399 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id'))); 1400 $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id'))); 1401 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id'))); 1402 $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id'))); 1403 $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id'))); 1404 $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id'))); 1405 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id'))); 1406 $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id'))); 1407 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id'))); 1408 $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id'))); 1409 $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id'))); 1410 $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id'))); 1411 $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id'))); 1412 $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id'))); 1413 1414 // Not affected. 1415 $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id'))); 1416 $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id'))); 1417 $this->assertTrue(\core_competency\plan::record_exists($p2->get('id'))); 1418 $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id'))); 1419 $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id'))); 1420 $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id'))); 1421 $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id'))); 1422 $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id'))); 1423 } 1424 1425 public function test_export_data_for_user_in_module_context_where_usermodified_matches() { 1426 $dg = $this->getDataGenerator(); 1427 $ccg = $dg->get_plugin_generator('core_competency'); 1428 1429 $c1 = $dg->create_course(); 1430 $u1 = $dg->create_user(); 1431 $u2 = $dg->create_user(); 1432 $m1 = $dg->create_module('page', ['course' => $c1]); 1433 $m2 = $dg->create_module('page', ['course' => $c1]); 1434 1435 $m1ctx = context_module::instance($m1->cmid); 1436 $m2ctx = context_module::instance($m2->cmid); 1437 1438 $f = $ccg->create_framework(); 1439 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1440 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1441 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1442 $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m1->cmid]); 1443 1444 $this->setUser($u1); 1445 $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $m1->cmid]); 1446 $ccg->create_course_module_competency(['competencyid' => $comp2->get('id'), 'cmid' => $m2->cmid]); 1447 1448 $this->setUser($u2); 1449 $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m2->cmid]); 1450 1451 // Export. 1452 $this->setAdminUser(); 1453 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id])); 1454 1455 // Check exported context 1. 1456 $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]); 1457 $this->assertCount(1, $data->associations); 1458 $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']); 1459 1460 // Check exported context 2. 1461 $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]); 1462 $this->assertEmpty($data); 1463 1464 // Export both contexts. 1465 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id, $m2ctx->id])); 1466 1467 // Check exported context 1. 1468 $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]); 1469 $this->assertCount(1, $data->associations); 1470 $this->assertEquals($comp1->get('shortname'), $data->associations[0]['name']); 1471 $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']); 1472 1473 // Check exported context 2. 1474 $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]); 1475 $this->assertCount(1, $data->associations); 1476 $this->assertEquals($comp2->get('shortname'), $data->associations[0]['name']); 1477 $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']); 1478 } 1479 1480 public function test_export_data_for_user_in_course_context_where_usermodified_matches() { 1481 $dg = $this->getDataGenerator(); 1482 $ccg = $dg->get_plugin_generator('core_competency'); 1483 1484 $c1 = $dg->create_course(); 1485 $c2 = $dg->create_course(); 1486 $u0 = $dg->create_user(); 1487 $u1 = $dg->create_user(); 1488 $u2 = $dg->create_user(); 1489 $u3 = $dg->create_user(); 1490 $u4 = $dg->create_user(); 1491 1492 $c1ctx = context_course::instance($c1->id); 1493 $c2ctx = context_course::instance($c2->id); 1494 1495 $f = $ccg->create_framework(); 1496 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1497 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1498 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1499 $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1500 $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id]); 1501 $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]); 1502 1503 $this->setUser($u1); 1504 $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]); 1505 $ccg->create_course_competency(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id]); 1506 $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]); 1507 $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]); 1508 $ccg->create_user_competency_course(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]); 1509 $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]); 1510 $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]); 1511 $ccs->create(); 1512 1513 $this->setUser($u2); 1514 $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id]); 1515 $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]); 1516 $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c2->id]); 1517 $ccs->create(); 1518 1519 // Export. 1520 $this->setAdminUser(); 1521 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id])); 1522 1523 // Check exported context 1. 1524 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations'); 1525 $this->assertCount(2, $data->competencies); 1526 $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']); 1527 $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']); 1528 $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']); 1529 $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']); 1530 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings'); 1531 $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you); 1532 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me'); 1533 $this->assertCount(2, $data->ratings); 1534 $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']); 1535 $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']); 1536 1537 // Check exported context 2. 1538 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations'); 1539 $this->assertEmpty($data); 1540 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings'); 1541 $this->assertEmpty($data); 1542 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me'); 1543 $this->assertEmpty($data); 1544 1545 // Export both contexts. 1546 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id, $c2ctx->id])); 1547 1548 // Check exported context 1. 1549 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations'); 1550 $this->assertCount(2, $data->competencies); 1551 $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']); 1552 $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']); 1553 $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']); 1554 $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']); 1555 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings'); 1556 $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you); 1557 $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me'); 1558 $this->assertCount(2, $data->ratings); 1559 $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']); 1560 $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']); 1561 1562 // Check exported context 2. 1563 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations'); 1564 $this->assertCount(1, $data->competencies); 1565 $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']); 1566 $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']); 1567 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings'); 1568 $this->assertEmpty($data); 1569 $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me'); 1570 $this->assertCount(1, $data->ratings); 1571 $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']); 1572 } 1573 1574 public function test_export_data_for_user_in_course_context_with_real_data() { 1575 $dg = $this->getDataGenerator(); 1576 $ccg = $dg->get_plugin_generator('core_competency'); 1577 1578 $c1 = $dg->create_course(); 1579 $c2 = $dg->create_course(); 1580 $u1 = $dg->create_user(); 1581 $u2 = $dg->create_user(); 1582 1583 $c1ctx = context_course::instance($c1->id); 1584 $c2ctx = context_course::instance($c2->id); 1585 1586 $f = $ccg->create_framework(); 1587 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1588 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1589 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1590 1591 $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id, 1592 'userid' => $u1->id, 'grade' => 1, 'proficiency' => true]); 1593 $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id, 1594 'userid' => $u1->id, 'grade' => 2, 'proficiency' => false]); 1595 $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id, 1596 'userid' => $u1->id, 'grade' => 3, 'proficiency' => false]); 1597 $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 1598 'userid' => $u1->id]); 1599 1600 $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u2->id]); 1601 $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u2->id]); 1602 1603 // Export user 1, in course 1. 1604 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id])); 1605 1606 // Check course 1. 1607 $data = writer::with_context($c1ctx)->get_data([get_string('competencies', 'core_competency')]); 1608 $this->assertCount(2, $data->ratings); 1609 $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']); 1610 $this->assertEquals('A', $data->ratings[0]['rating']['rating']); 1611 $this->assertEquals(transform::yesno(true), $data->ratings[0]['rating']['proficient']); 1612 $this->assertEquals($comp2->get('shortname'), $data->ratings[1]['name']); 1613 $this->assertEquals('B', $data->ratings[1]['rating']['rating']); 1614 $this->assertEquals(transform::yesno(false), $data->ratings[1]['rating']['proficient']); 1615 1616 // Check course 2. 1617 $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]); 1618 $this->assertEmpty($data); 1619 1620 // Export user 1, in course 2. 1621 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c2ctx->id])); 1622 $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]); 1623 $this->assertCount(2, $data->ratings); 1624 $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']); 1625 $this->assertEquals('C', $data->ratings[0]['rating']['rating']); 1626 $this->assertEquals(transform::yesno(false), $data->ratings[0]['rating']['proficient']); 1627 $this->assertEquals($comp3->get('shortname'), $data->ratings[1]['name']); 1628 $this->assertEquals('-', $data->ratings[1]['rating']['rating']); 1629 $this->assertEquals('-', $data->ratings[1]['rating']['proficient']); 1630 } 1631 1632 public function test_export_data_for_user_in_system_and_category_contexts() { 1633 $dg = $this->getDataGenerator(); 1634 $ccg = $dg->get_plugin_generator('core_competency'); 1635 1636 $c1 = $dg->create_cohort(); 1637 $c2 = $dg->create_cohort(); 1638 $cat1 = $dg->create_category(); 1639 $cat2 = $dg->create_category(); 1640 1641 $cat1ctx = context_coursecat::instance($cat1->id); 1642 $cat2ctx = context_coursecat::instance($cat2->id); 1643 $sysctx = context_system::instance(); 1644 1645 $u1 = $dg->create_user(); 1646 $u2 = $dg->create_user(); 1647 $u3 = $dg->create_user(); 1648 $u4 = $dg->create_user(); 1649 $u2 = $dg->create_user(); 1650 1651 $this->setUser($u1); 1652 $f1 = $ccg->create_framework(); 1653 $f1bis = $ccg->create_framework(); 1654 $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]); 1655 $c2a = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 1656 $c2b = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]); 1657 1658 $t1 = $ccg->create_template(); 1659 $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]); 1660 $tc2a = $ccg->create_template_competency(['templateid' => $t2->get('id'), 'competencyid' => $c2a->get('id')]); 1661 $tch2 = $ccg->create_template_cohort(['templateid' => $t2->get('id'), 'cohortid' => $c1->id]); 1662 1663 $this->setUser($u2); 1664 $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]); 1665 $c1a = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 1666 $c1b = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]); 1667 $c3a = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]); 1668 $c3b = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]); 1669 $c3c = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]); 1670 $c3d = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]); 1671 $rc1 = $ccg->create_related_competency(['competencyid' => $c2a->get('id'), 'relatedcompetencyid' => $c2b->get('id')]); 1672 1673 $t3 = $ccg->create_template(['contextid' => $cat2ctx->id]); 1674 $tch1 = $ccg->create_template_cohort(['templateid' => $t1->get('id'), 'cohortid' => $c2->id]); 1675 $tc1a = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c1a->get('id')]); 1676 $tc1b = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c2a->get('id')]); 1677 $tc3a = $ccg->create_template_competency(['templateid' => $t3->get('id'), 'competencyid' => $c3a->get('id')]); 1678 1679 $this->setUser($u1); 1680 $rc2 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3b->get('id')]); 1681 $rc3 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3c->get('id')]); 1682 1683 $this->setAdminUser(); 1684 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id])); 1685 1686 // Check frameworks for u1 in system. 1687 $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks'); 1688 $this->assertCount(2, $data->frameworks); 1689 $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']); 1690 $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']); 1691 $this->assertEquals($f1bis->get('shortname'), $data->frameworks[1]['name']); 1692 $this->assertEquals(transform::yesno(true), $data->frameworks[1]['created_or_modified_by_you']); 1693 $this->assertEmpty($data->frameworks[0]['competencies']); 1694 $this->assertEmpty($data->frameworks[1]['competencies']); 1695 1696 // Check templates for u1 in system. 1697 $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates'); 1698 $this->assertCount(1, $data->templates); 1699 $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']); 1700 $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']); 1701 $this->assertEmpty($data->templates[0]['competencies']); 1702 $this->assertEmpty($data->templates[0]['cohorts']); 1703 1704 // Check frameworks for u1 in cat1. 1705 $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks'); 1706 $this->assertCount(1, $data->frameworks); 1707 $this->assertEquals($f2->get('shortname'), $data->frameworks[0]['name']); 1708 $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']); 1709 $this->assertCount(2, $data->frameworks[0]['competencies']); 1710 $this->assertEquals($c2a->get('shortname'), $data->frameworks[0]['competencies'][0]['name']); 1711 $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][0]['created_or_modified_by_you']); 1712 $this->assertEquals($c2b->get('shortname'), $data->frameworks[0]['competencies'][1]['name']); 1713 $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][1]['created_or_modified_by_you']); 1714 1715 // Check templates for u1 in cat1. 1716 $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates'); 1717 $this->assertCount(1, $data->templates); 1718 $this->assertEquals($t2->get('shortname'), $data->templates[0]['name']); 1719 $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']); 1720 $this->assertCount(1, $data->templates[0]['competencies']); 1721 $this->assertEquals($c2a->get('shortname'), $data->templates[0]['competencies'][0]['name']); 1722 $this->assertEquals(transform::yesno(true), $data->templates[0]['competencies'][0]['created_or_modified_by_you']); 1723 $this->assertCount(1, $data->templates[0]['cohorts']); 1724 $this->assertEquals($c1->name, $data->templates[0]['cohorts'][0]['name']); 1725 $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']); 1726 1727 // Check frameworks for u1 in cat2. 1728 $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks'); 1729 $this->assertCount(1, $data->frameworks); 1730 $this->assertEquals($f3->get('shortname'), $data->frameworks[0]['name']); 1731 $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']); 1732 $this->assertCount(3, $data->frameworks[0]['competencies']); 1733 $competency = $data->frameworks[0]['competencies'][0]; 1734 $this->assertEquals($c3a->get('shortname'), $competency['name']); 1735 $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']); 1736 $this->assertCount(2, $competency['related_competencies']); 1737 $this->assertEquals($c3b->get('shortname'), $competency['related_competencies'][0]['name']); 1738 $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']); 1739 $this->assertEquals($c3c->get('shortname'), $competency['related_competencies'][1]['name']); 1740 $this->assertEquals(transform::yesno(true), $competency['related_competencies'][1]['created_or_modified_by_you']); 1741 $competency = $data->frameworks[0]['competencies'][1]; 1742 $this->assertEquals($c3b->get('shortname'), $competency['name']); 1743 $this->assertCount(1, $competency['related_competencies']); 1744 $competency = $data->frameworks[0]['competencies'][2]; 1745 $this->assertEquals($c3c->get('shortname'), $competency['name']); 1746 $this->assertCount(1, $competency['related_competencies']); 1747 1748 // Check templates for u1 in cat2. 1749 $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates'); 1750 $this->assertEmpty($data->templates); 1751 1752 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id])); 1753 1754 // Check frameworks for u2 in system. 1755 $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks'); 1756 $this->assertCount(1, $data->frameworks); 1757 $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']); 1758 $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']); 1759 $this->assertCount(2, $data->frameworks[0]['competencies']); 1760 $competency = $data->frameworks[0]['competencies'][0]; 1761 $this->assertEquals($c1a->get('shortname'), $competency['name']); 1762 $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']); 1763 $competency = $data->frameworks[0]['competencies'][1]; 1764 $this->assertEquals($c1b->get('shortname'), $competency['name']); 1765 $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']); 1766 1767 // Check templates for u2 in system. 1768 $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates'); 1769 $this->assertCount(1, $data->templates); 1770 $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']); 1771 $this->assertEquals(transform::yesno(false), $data->templates[0]['created_or_modified_by_you']); 1772 $this->assertCount(2, $data->templates[0]['competencies']); 1773 $competency = $data->templates[0]['competencies'][0]; 1774 $this->assertEquals($c1a->get('shortname'), $competency['name']); 1775 $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']); 1776 $competency = $data->templates[0]['competencies'][1]; 1777 $this->assertEquals($c2a->get('shortname'), $competency['name']); 1778 $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']); 1779 $this->assertCount(1, $data->templates[0]['cohorts']); 1780 $this->assertEquals($c2->name, $data->templates[0]['cohorts'][0]['name']); 1781 $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']); 1782 1783 // Check frameworks for u2 in cat1. 1784 $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks'); 1785 $this->assertCount(1, $data->frameworks); 1786 $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']); 1787 $this->assertCount(2, $data->frameworks[0]['competencies']); 1788 $competency = $data->frameworks[0]['competencies'][0]; 1789 $this->assertEquals($c2a->get('shortname'), $competency['name']); 1790 $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']); 1791 $this->assertCount(1, $competency['related_competencies']); 1792 $this->assertEquals($c2b->get('shortname'), $competency['related_competencies'][0]['name']); 1793 $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']); 1794 1795 // Check templates for u2 in system. 1796 $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates'); 1797 $this->assertCount(1, $data->templates); 1798 $this->assertEquals($t3->get('shortname'), $data->templates[0]['name']); 1799 $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']); 1800 $this->assertCount(1, $data->templates[0]['competencies']); 1801 $competency = $data->templates[0]['competencies'][0]; 1802 $this->assertEquals($c3a->get('shortname'), $competency['name']); 1803 $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']); 1804 } 1805 1806 public function test_export_data_for_user_with_related_learning_plans() { 1807 global $DB; 1808 1809 $path = [ 1810 get_string('competencies', 'core_competency'), 1811 get_string('privacy:path:relatedtome', 'core_competency'), 1812 get_string('privacy:path:plans', 'core_competency'), 1813 ]; 1814 $yes = transform::yesno(true); 1815 $no = transform::yesno(false); 1816 1817 $dg = $this->getDataGenerator(); 1818 $ccg = $dg->get_plugin_generator('core_competency'); 1819 1820 $u0 = $dg->create_user(); 1821 $u1 = $dg->create_user(); 1822 $u2 = $dg->create_user(); 1823 $u3 = $dg->create_user(); 1824 $u4 = $dg->create_user(); 1825 $u5 = $dg->create_user(); 1826 $u6 = $dg->create_user(); 1827 $u7 = $dg->create_user(); 1828 $u8 = $dg->create_user(); 1829 1830 $dg->role_assign($DB->get_field('role', 'id', ['archetype' => 'manager'], IGNORE_MULTIPLE), $u6->id); 1831 $u0ctx = context_user::instance($u0->id); 1832 1833 $f = $ccg->create_framework(); 1834 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1835 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1836 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1837 $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 1838 1839 $t = $ccg->create_template(); 1840 $tc1 = $ccg->create_template_competency(['competencyid' => $comp1->get('id'), 'templateid' => $t->get('id')]); 1841 $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]); 1842 $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]); 1843 $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]); 1844 1845 $this->setUser($u1); 1846 $p1 = $ccg->create_plan(['templateid' => $t->get('id'), 'userid' => $u0->id]); 1847 1848 $this->setUser($u2); 1849 $p2 = $ccg->create_plan(['userid' => $u0->id, 'reviewerid' => $u7->id]); 1850 1851 $this->setUser($u3); 1852 $p1c1 = $ccg->create_plan_competency(['planid' => $p1->get('id'), 'competencyid' => $comp1->get('id')]); 1853 $p2c2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp2->get('id')]); 1854 $p2c3 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp3->get('id')]); 1855 1856 $this->setUser($u4); 1857 $uc1 = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u0->id, 'grade' => 1, 1858 'proficiency' => true]); 1859 $uc2 = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u0->id, 'grade' => 2, 1860 'proficiency' => false]); 1861 $uc3 = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u0->id]); 1862 $uc4 = $ccg->create_user_competency(['competencyid' => $comp4->get('id'), 'userid' => $u0->id, 'reviewerid' => $u5->id]); 1863 1864 $this->setUser($u5); 1865 $p3 = $ccg->create_plan(['userid' => $u0->id]); 1866 $p3c1 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp1->get('id')]); 1867 $p3c3 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp3->get('id')]); 1868 1869 // Add comments on plan. 1870 $this->allow_anyone_to_comment_anywhere(); 1871 $this->setUser($u0); 1872 $p1->get_comment_object()->add('Hello.'); 1873 $this->setUser($u8); 1874 $p1->get_comment_object()->add('Hi.'); 1875 1876 // Export data for user 1. 1877 writer::reset(); 1878 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id])); 1879 $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]); 1880 $data = writer::with_context($u0ctx)->get_data($planpath); 1881 $this->assertEquals($p1->get('name'), $data->name); 1882 $this->assertEquals($yes, $data->created_or_modified_by_you); 1883 1884 // Export data for user 2. 1885 writer::reset(); 1886 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id])); 1887 $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]); 1888 $data = writer::with_context($u0ctx)->get_data($planpath); 1889 $this->assertEquals($p2->get('name'), $data->name); 1890 $this->assertEquals($yes, $data->created_or_modified_by_you); 1891 1892 // Export data for user 3. 1893 writer::reset(); 1894 provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id])); 1895 $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]); 1896 $data = writer::with_context($u0ctx)->get_data($planpath); 1897 $this->assertEquals($p1->get('name'), $data->name); 1898 $this->assertEquals($no, $data->created_or_modified_by_you); 1899 $this->assertCount(1, $data->competencies); 1900 $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']); 1901 $this->assertEquals($yes, $data->competencies[0]['created_or_modified_by_you']); 1902 1903 $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]); 1904 $data = writer::with_context($u0ctx)->get_data($planpath); 1905 $this->assertEquals($p2->get('name'), $data->name); 1906 $this->assertEquals($no, $data->created_or_modified_by_you); 1907 $competencies = $data->competencies; 1908 $this->assertCount(2, $competencies); 1909 $this->assertEquals($comp2->get('shortname'), $competencies[0]['name']); 1910 $this->assertEquals($yes, $competencies[0]['created_or_modified_by_you']); 1911 $this->assertEquals($comp3->get('shortname'), $competencies[1]['name']); 1912 $this->assertEquals($yes, $competencies[1]['created_or_modified_by_you']); 1913 1914 // Export data for user 4. 1915 writer::reset(); 1916 provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id])); 1917 foreach ([$p1, $p2, $p3] as $plan) { 1918 $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]); 1919 $data = writer::with_context($u0ctx)->get_data($planpath); 1920 $this->assertEmpty($data); 1921 } 1922 1923 // Export data for user 5. 1924 writer::reset(); 1925 provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id])); 1926 $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]); 1927 $data = writer::with_context($u0ctx)->get_data($planpath); 1928 $this->assertEquals($p3->get('name'), $data->name); 1929 $this->assertEquals($yes, $data->created_or_modified_by_you); 1930 $this->assertCount(2, $data->competencies); 1931 $competency = $data->competencies[0]; 1932 $this->assertEquals($comp1->get('shortname'), $competency['name']); 1933 $this->assertEquals($yes, $competency['created_or_modified_by_you']); 1934 $competency = $data->competencies[1]; 1935 $this->assertEquals($comp3->get('shortname'), $competency['name']); 1936 $this->assertEquals($yes, $competency['created_or_modified_by_you']); 1937 1938 // Do some stuff. 1939 $this->setUser($u6); 1940 api::complete_plan($p3); 1941 1942 // Export data for user 6. 1943 writer::reset(); 1944 provider::export_user_data(new approved_contextlist($u6, 'core_competency', [$u0ctx->id])); 1945 $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]); 1946 $data = writer::with_context($u0ctx)->get_data($planpath); 1947 $this->assertEquals($p3->get('name'), $data->name); 1948 $this->assertEquals($yes, $data->created_or_modified_by_you); 1949 $this->assertCount(2, $data->competencies); 1950 $competency = $data->competencies[0]; 1951 $this->assertEquals($comp1->get('shortname'), $competency['name']); 1952 $this->assertArrayNotHasKey('created_or_modified_by_you', $competency); 1953 $this->assertEquals('A', $competency['rating']['rating']); 1954 $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']); 1955 $competency = $data->competencies[1]; 1956 $this->assertEquals($comp3->get('shortname'), $competency['name']); 1957 $this->assertArrayNotHasKey('created_or_modified_by_you', $competency); 1958 $this->assertEquals('-', $competency['rating']['rating']); 1959 $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']); 1960 1961 // Export data for user 7. 1962 writer::reset(); 1963 provider::export_user_data(new approved_contextlist($u7, 'core_competency', [$u0ctx->id])); 1964 $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]); 1965 $data = writer::with_context($u0ctx)->get_data($planpath); 1966 $this->assertEquals($p2->get('name'), $data->name); 1967 $this->assertEquals($no, $data->created_or_modified_by_you); 1968 $this->assertEquals($yes, $data->reviewer_is_you); 1969 1970 // Export data for user 8. 1971 writer::reset(); 1972 $this->setUser($u8); 1973 provider::export_user_data(new approved_contextlist($u8, 'core_competency', [$u0ctx->id])); 1974 $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]); 1975 $data = writer::with_context($u0ctx)->get_data($planpath); 1976 $this->assertEquals($p1->get('name'), $data->name); 1977 $this->assertEquals($no, $data->created_or_modified_by_you); 1978 $this->assertEquals($no, $data->reviewer_is_you); 1979 $commentspath = array_merge($planpath, [get_string('commentsubcontext', 'core_comment')]); 1980 $data = writer::with_context($u0ctx)->get_data($commentspath); 1981 $this->assert_exported_comments(['Hi.'], $data->comments); 1982 } 1983 1984 public function test_export_data_for_user_with_related_competencies() { 1985 $path = [ 1986 get_string('competencies', 'core_competency'), 1987 get_string('privacy:path:relatedtome', 'core_competency'), 1988 get_string('competencies', 'core_competency'), 1989 ]; 1990 $yes = transform::yesno(true); 1991 $no = transform::yesno(false); 1992 $makecomppath = function($comp) use ($path) { 1993 return array_merge($path, ["{$comp->get('shortname')} ({$comp->get('id')})"]); 1994 }; 1995 1996 $dg = $this->getDataGenerator(); 1997 $ccg = $dg->get_plugin_generator('core_competency'); 1998 1999 $u0 = $dg->create_user(); 2000 $u1 = $dg->create_user(); 2001 $u2 = $dg->create_user(); 2002 $u3 = $dg->create_user(); 2003 $u4 = $dg->create_user(); 2004 $u5 = $dg->create_user(); 2005 2006 $u0ctx = context_user::instance($u0->id); 2007 2008 $f = $ccg->create_framework(); 2009 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2010 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2011 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2012 $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2013 2014 $this->setUser($u1); 2015 api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG, 2016 'privacy:metadata:competency_evidence', 'core_competency'); 2017 api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG, 2018 'privacy:metadata:competency_evidence', 'core_competency'); 2019 api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG, 2020 'privacy:metadata:competency_evidence', 'core_competency'); 2021 2022 $this->setUser($u2); 2023 api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_COMPLETE, 2024 'privacy:metadata:competency_evidence', 'core_competency', null, false, null, null, $u3->id); 2025 2026 $this->setUser($u3); 2027 api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_OVERRIDE, 2028 'privacy:metadata:competency_evidence', 'core_competency', null, false, null, 1, $u4->id, 'Ze note'); 2029 2030 $this->setUser($u4); 2031 $uc3 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp3->get('id')]); 2032 $uc4 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp4->get('id'), 'reviewerid' => $u2->id]); 2033 2034 $this->allow_anyone_to_comment_anywhere(); 2035 $this->setUser($u0); 2036 $uc3->get_comment_object()->add('...'); 2037 $this->setUser($u5); 2038 $uc3->get_comment_object()->add('Hello!'); 2039 $uc3->get_comment_object()->add('It\'s me...'); 2040 2041 // Export data for user 1. 2042 writer::reset(); 2043 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id])); 2044 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1)); 2045 $competency = (array) $data; 2046 $this->assertEquals($comp1->get('shortname'), $competency['name']); 2047 $evidence = $competency['evidence']; 2048 $this->assertCount(2, $evidence); 2049 $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']); 2050 $this->assertEquals('-', $evidence[0]['actionuserid']); 2051 $this->assertEquals($no, $evidence[0]['acting_user_is_you']); 2052 $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']); 2053 $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[1]['action']); 2054 $this->assertEquals('-', $evidence[1]['actionuserid']); 2055 $this->assertEquals($no, $evidence[1]['acting_user_is_you']); 2056 $this->assertEquals($yes, $evidence[1]['created_or_modified_by_you']); 2057 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2)); 2058 $competency = (array) $data; 2059 $this->assertEquals($comp2->get('shortname'), $competency['name']); 2060 $evidence = $competency['evidence']; 2061 $this->assertCount(1, $evidence); 2062 $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']); 2063 $this->assertEquals('-', $evidence[0]['actionuserid']); 2064 $this->assertEquals($no, $evidence[0]['acting_user_is_you']); 2065 $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']); 2066 2067 // Export data for user 2. 2068 writer::reset(); 2069 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id])); 2070 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1)); 2071 $competency = (array) $data; 2072 $this->assertEquals($comp1->get('shortname'), $competency['name']); 2073 $evidence = $competency['evidence']; 2074 $this->assertCount(1, $evidence); 2075 $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $evidence[0]['action']); 2076 $this->assertEquals($u3->id, $evidence[0]['actionuserid']); 2077 $this->assertEquals($no, $evidence[0]['acting_user_is_you']); 2078 $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']); 2079 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4)); 2080 $competency = (array) $data; 2081 $this->assertEquals($comp4->get('shortname'), $competency['name']); 2082 $this->assertCount(0, $competency['evidence']); 2083 $this->assertEquals($yes, $competency['rating']['reviewer_is_you']); 2084 $this->assertEquals($no, $competency['rating']['created_or_modified_by_you']); 2085 2086 // Export data for user 3. 2087 writer::reset(); 2088 provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id])); 2089 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1)); 2090 $competency = (array) $data; 2091 $this->assertEquals($comp1->get('shortname'), $competency['name']); 2092 $evidence = $competency['evidence']; 2093 $this->assertCount(1, $evidence); 2094 $this->assertEquals($u3->id, $evidence[0]['actionuserid']); 2095 $this->assertEquals($yes, $evidence[0]['acting_user_is_you']); 2096 $this->assertEquals($no, $evidence[0]['created_or_modified_by_you']); 2097 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2)); 2098 $competency = (array) $data; 2099 $this->assertEquals($comp2->get('shortname'), $competency['name']); 2100 $evidence = $competency['evidence']; 2101 $this->assertCount(1, $evidence); 2102 $this->assertEquals(get_string('privacy:evidence:action:override', 'core_competency'), $evidence[0]['action']); 2103 $this->assertEquals($u4->id, $evidence[0]['actionuserid']); 2104 $this->assertEquals($no, $evidence[0]['acting_user_is_you']); 2105 $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']); 2106 2107 // Export data for user 4. 2108 writer::reset(); 2109 provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id])); 2110 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2)); 2111 $competency = (array) $data; 2112 $this->assertEquals($comp2->get('shortname'), $competency['name']); 2113 $this->assertNull($competency['rating']); 2114 $this->assertCount(1, $competency['evidence']); 2115 $evidence = $competency['evidence'][0]; 2116 $this->assertEquals($u4->id, $evidence['actionuserid']); 2117 $this->assertEquals($yes, $evidence['acting_user_is_you']); 2118 $this->assertEquals($no, $evidence['created_or_modified_by_you']); 2119 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3)); 2120 $competency = (array) $data; 2121 $this->assertEquals($comp3->get('shortname'), $competency['name']); 2122 $this->assertEquals($no, $competency['rating']['reviewer_is_you']); 2123 $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']); 2124 $this->assertEmpty($competency['evidence']); 2125 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4)); 2126 $competency = (array) $data; 2127 $this->assertEquals($comp4->get('shortname'), $competency['name']); 2128 $this->assertEquals($no, $competency['rating']['reviewer_is_you']); 2129 $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']); 2130 $this->assertEmpty($competency['evidence']); 2131 2132 // Export data for user 5. 2133 $this->setUser($u5); 2134 writer::reset(); 2135 provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id])); 2136 $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3)); 2137 $competency = (array) $data; 2138 $this->assertEquals($comp3->get('shortname'), $competency['name']); 2139 $data = writer::with_context($u0ctx)->get_data(array_merge($makecomppath($comp3), 2140 [get_string('commentsubcontext', 'core_comment')])); 2141 $this->assert_exported_comments(['Hello!', 'It\'s me...'], $data->comments); 2142 } 2143 2144 public function test_export_data_for_user_with_related_user_evidence() { 2145 $path = [ 2146 get_string('competencies', 'core_competency'), 2147 get_string('privacy:path:relatedtome', 'core_competency'), 2148 get_string('privacy:path:userevidence', 'core_competency') 2149 ]; 2150 $yes = transform::yesno(true); 2151 $no = transform::yesno(false); 2152 2153 $dg = $this->getDataGenerator(); 2154 $ccg = $dg->get_plugin_generator('core_competency'); 2155 2156 $u0 = $dg->create_user(); 2157 $u0b = $dg->create_user(); 2158 $u1 = $dg->create_user(); 2159 $u2 = $dg->create_user(); 2160 $u3 = $dg->create_user(); 2161 $u4 = $dg->create_user(); 2162 2163 $u0ctx = context_user::instance($u0->id); 2164 2165 $f = $ccg->create_framework(); 2166 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2167 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2168 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2169 2170 $this->setUser($u0); 2171 $ue0 = $ccg->create_user_evidence(['userid' => $u0->id]); 2172 2173 $this->setUser($u1); 2174 $ue1 = $ccg->create_user_evidence(['userid' => $u0->id]); 2175 $ue1b = $ccg->create_user_evidence(['userid' => $u0b->id]); 2176 2177 $this->setUser($u2); 2178 $ccg->create_user_evidence_competency(['userevidenceid' => $ue1->get('id'), 'competencyid' => $comp1->get('id')]); 2179 $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp1->get('id')]); 2180 $ue2 = $ccg->create_user_evidence(['userid' => $u0->id]); 2181 $ue2b = $ccg->create_user_evidence(['userid' => $u0b->id]); 2182 2183 $this->setUser($u3); 2184 $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp2->get('id')]); 2185 $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp3->get('id')]); 2186 2187 // Export for user 1. 2188 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id])); 2189 $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]); 2190 $data = writer::with_context($u0ctx)->get_data($uepath); 2191 $this->assertEquals($ue1->get('name'), $data->name); 2192 $this->assertEquals($yes, $data->created_or_modified_by_you); 2193 $this->assertEmpty($data->competencies); 2194 2195 // Export for user 2. 2196 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id])); 2197 $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]); 2198 $data = writer::with_context($u0ctx)->get_data($uepath); 2199 $this->assertEquals($ue1->get('name'), $data->name); 2200 $this->assertEquals($no, $data->created_or_modified_by_you); 2201 $this->assertCount(1, $data->competencies); 2202 $competency = $data->competencies[0]; 2203 $this->assertEquals($comp1->get('shortname'), $competency['name']); 2204 $this->assertEquals($yes, $competency['created_or_modified_by_you']); 2205 2206 $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]); 2207 $data = writer::with_context($u0ctx)->get_data($uepath); 2208 $this->assertEquals($ue2->get('name'), $data->name); 2209 $this->assertEquals($yes, $data->created_or_modified_by_you); 2210 $this->assertEmpty($data->competencies); 2211 2212 // Export for user 3. 2213 provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id])); 2214 $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]); 2215 $evidence = writer::with_context($u0ctx)->get_data($uepath); 2216 $this->assertEquals($ue2->get('name'), $evidence->name); 2217 $this->assertEquals($no, $evidence->created_or_modified_by_you); 2218 $this->assertCount(2, $evidence->competencies); 2219 $competency = $evidence->competencies[0]; 2220 $this->assertEquals($comp2->get('shortname'), $competency['name']); 2221 $this->assertEquals($yes, $competency['created_or_modified_by_you']); 2222 $competency = $evidence->competencies[1]; 2223 $this->assertEquals($comp3->get('shortname'), $competency['name']); 2224 $this->assertEquals($yes, $competency['created_or_modified_by_you']); 2225 } 2226 2227 public function test_export_data_for_user_about_their_learning_plans() { 2228 $this->setAdminUser(); 2229 $dg = $this->getDataGenerator(); 2230 $ccg = $dg->get_plugin_generator('core_competency'); 2231 $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:plans', 'core_competency')]; 2232 $yes = transform::yesno(true); 2233 $no = transform::yesno(false); 2234 2235 $u1 = $dg->create_user(); 2236 $u2 = $dg->create_user(); 2237 $u3 = $dg->create_user(); 2238 $u1ctx = context_user::instance($u1->id); 2239 $u2ctx = context_user::instance($u2->id); 2240 2241 $f = $ccg->create_framework(); 2242 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2243 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2244 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2245 $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2246 2247 $t = $ccg->create_template(); 2248 $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]); 2249 $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]); 2250 $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]); 2251 2252 $p1a = $ccg->create_plan(['userid' => $u1->id, 'templateid' => $t->get('id'), 2253 'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW]); 2254 $p1b = $ccg->create_plan(['userid' => $u1->id]); 2255 $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp1->get('id')]); 2256 $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]); 2257 $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp4->get('id')]); 2258 $p1c = $ccg->create_plan(['userid' => $u1->id]); 2259 $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp1->get('id')]); 2260 $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp3->get('id')]); 2261 $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp4->get('id')]); 2262 $p1d = $ccg->create_plan(['userid' => $u1->id]); 2263 2264 $p2a = $ccg->create_plan(['userid' => $u2->id]); 2265 $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp1->get('id')]); 2266 $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp2->get('id')]); 2267 2268 $uc1a = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u1->id, 2269 'grade' => 2, 'proficiency' => false]); 2270 $uc1b = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u1->id, 2271 'grade' => 3, 'proficiency' => false]); 2272 $uc1c = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u1->id]); 2273 2274 // Add comments on plan. 2275 $this->allow_anyone_to_comment_anywhere(); 2276 $this->setUser($u1); 2277 $p1a->get_comment_object()->add('Hello.'); 2278 $p1a->get_comment_object()->add('It\'s me.'); 2279 $this->setUser($u3); 2280 $p1a->get_comment_object()->add('After all these years...'); 2281 2282 // Complete the plan to create archiving, and modify the user competency again. 2283 api::complete_plan($p1c); 2284 $uc1a->set('grade', 1); 2285 $uc1a->set('proficiency', true); 2286 $uc1a->update(); 2287 2288 // Export user data in both contexts. 2289 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id])); 2290 2291 // This plan is based off a template. 2292 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"])); 2293 $this->assertNotEmpty($data); 2294 $this->assertEquals($p1a->get('name'), $data->name); 2295 $this->assertEquals($p1a->get_statusname(), $data->status); 2296 $this->assertCount(3, $data->competencies); 2297 $comp = $data->competencies[0]; 2298 $this->assertEquals($comp2->get('shortname'), $comp['name']); 2299 $this->assertEquals('C', $comp['rating']['rating']); 2300 $comp = $data->competencies[1]; 2301 $this->assertEquals($comp3->get('shortname'), $comp['name']); 2302 $this->assertEquals('-', $comp['rating']['rating']); 2303 $comp = $data->competencies[2]; 2304 $this->assertEquals($comp4->get('shortname'), $comp['name']); 2305 $this->assertNull($comp['rating']); 2306 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})", 2307 get_string('commentsubcontext', 'core_comment')])); 2308 $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments); 2309 2310 // This plan is manually created. 2311 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"])); 2312 $this->assertNotEmpty($data); 2313 $this->assertEquals($p1b->get('name'), $data->name); 2314 $this->assertCount(3, $data->competencies); 2315 $comp = $data->competencies[0]; 2316 $this->assertEquals($comp1->get('shortname'), $comp['name']); 2317 $this->assertEquals('A', $comp['rating']['rating']); 2318 $comp = $data->competencies[1]; 2319 $this->assertEquals($comp2->get('shortname'), $comp['name']); 2320 $this->assertEquals('C', $comp['rating']['rating']); 2321 $comp = $data->competencies[2]; 2322 $this->assertEquals($comp4->get('shortname'), $comp['name']); 2323 $this->assertNull($comp['rating']); 2324 2325 // This plan is complete. 2326 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"])); 2327 $this->assertNotEmpty($data); 2328 $this->assertEquals($p1c->get('name'), $data->name); 2329 $this->assertCount(3, $data->competencies); 2330 $comp = $data->competencies[0]; 2331 $this->assertEquals($comp1->get('shortname'), $comp['name']); 2332 $this->assertEquals('B', $comp['rating']['rating']); 2333 $comp = $data->competencies[1]; 2334 $this->assertEquals($comp3->get('shortname'), $comp['name']); 2335 $this->assertEquals('-', $comp['rating']['rating']); 2336 $comp = $data->competencies[2]; 2337 $this->assertEquals($comp4->get('shortname'), $comp['name']); 2338 $this->assertEquals('-', $comp['rating']['rating']); 2339 2340 // This plan is empty. 2341 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1d->get('name')} ({$p1d->get('id')})"])); 2342 $this->assertNotEmpty($data); 2343 $this->assertEquals($p1d->get('name'), $data->name); 2344 $this->assertEquals($p1d->get_statusname(), $data->status); 2345 $this->assertEmpty($data->competencies); 2346 2347 // Confirm that we do not get export what we shouldn't. 2348 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"])); 2349 $this->assertEmpty($data); 2350 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"])); 2351 $this->assertEmpty($data); 2352 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"])); 2353 $this->assertEmpty($data); 2354 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"])); 2355 $this->assertEmpty($data); 2356 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"])); 2357 $this->assertEmpty($data); 2358 2359 // Export for user 2. 2360 writer::reset(); 2361 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id])); 2362 2363 // Validate the basic plan. 2364 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"])); 2365 $this->assertNotEmpty($data); 2366 $this->assertEquals($p2a->get('name'), $data->name); 2367 $this->assertCount(2, $data->competencies); 2368 $comp = $data->competencies[0]; 2369 $this->assertEquals($comp1->get('shortname'), $comp['name']); 2370 $this->assertNull($comp['rating']); 2371 $comp = $data->competencies[1]; 2372 $this->assertEquals($comp2->get('shortname'), $comp['name']); 2373 $this->assertNull($comp['rating']); 2374 2375 // Confirm that we do not get export what we shouldn't. 2376 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"])); 2377 $this->assertEmpty($data); 2378 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"])); 2379 $this->assertEmpty($data); 2380 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"])); 2381 $this->assertEmpty($data); 2382 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"])); 2383 $this->assertEmpty($data); 2384 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"])); 2385 $this->assertEmpty($data); 2386 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"])); 2387 $this->assertEmpty($data); 2388 } 2389 2390 public function test_export_data_for_user_about_their_competencies() { 2391 $dg = $this->getDataGenerator(); 2392 $ccg = $dg->get_plugin_generator('core_competency'); 2393 $path = [get_string('competencies', 'core_competency'), get_string('competencies', 'core_competency')]; 2394 $no = transform::yesno(false); 2395 2396 $u1 = $dg->create_user(); 2397 $u2 = $dg->create_user(); 2398 $u3 = $dg->create_user(); 2399 $u1ctx = context_user::instance($u1->id); 2400 $u2ctx = context_user::instance($u2->id); 2401 2402 $f = $ccg->create_framework(); 2403 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2404 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2405 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2406 2407 $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]); 2408 $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id'), 2409 'grade' => 2, 'proficiency' => false]); 2410 $uc1c = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp3->get('id')]); 2411 $e1a1 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'), 2412 'action' => \core_competency\evidence::ACTION_COMPLETE, 'grade' => 1]); 2413 $e1a2 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'), 'note' => 'Not too bad']); 2414 $e1b1 = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id'), 'url' => 'https://example.com']); 2415 2416 $uc2a = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]); 2417 $uc2b = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]); 2418 $e2a1 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'A']); 2419 $e2a2 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'B']); 2420 $e2a3 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'C']); 2421 2422 // Add comments on competency. 2423 $this->allow_anyone_to_comment_anywhere(); 2424 $this->setUser($u1); 2425 $uc1a->get_comment_object()->add('Hello.'); 2426 $uc1a->get_comment_object()->add('It\'s me.'); 2427 $this->setUser($u3); 2428 $uc1a->get_comment_object()->add('After all these years...'); 2429 2430 // Export for user 1 in both contexts. 2431 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id])); 2432 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"])); 2433 $this->assertNotEmpty($data); 2434 $this->assertEquals($comp1->get('shortname'), $data->name); 2435 $this->assertEquals('-', $data->rating['rating']); 2436 $this->assertCount(2, $data->evidence); 2437 $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $data->evidence[1]['action']); 2438 $this->assertEquals('Not too bad', $data->evidence[0]['note']); 2439 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})", 2440 get_string('commentsubcontext', 'core_comment')])); 2441 $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments); 2442 2443 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"])); 2444 $this->assertNotEmpty($data); 2445 $this->assertEquals($comp2->get('shortname'), $data->name); 2446 $this->assertEquals('B', $data->rating['rating']); 2447 $this->assertEquals($no, $data->rating['proficient']); 2448 $this->assertCount(1, $data->evidence); 2449 $this->assertEquals('https://example.com', $data->evidence[0]['url']); 2450 2451 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"])); 2452 $this->assertNotEmpty($data); 2453 $this->assertEquals($comp3->get('shortname'), $data->name); 2454 $this->assertEquals('-', $data->rating['rating']); 2455 $this->assertEquals('-', $data->rating['proficient']); 2456 $this->assertEmpty($data->evidence); 2457 2458 // We don't know anything about user 2. 2459 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"])); 2460 $this->assertEmpty($data); 2461 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"])); 2462 $this->assertEmpty($data); 2463 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"])); 2464 $this->assertEmpty($data); 2465 2466 // Export for user 2 in both contexts. 2467 writer::reset(); 2468 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id])); 2469 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"])); 2470 $this->assertNotEmpty($data); 2471 $this->assertEquals($comp1->get('shortname'), $data->name); 2472 $this->assertEquals('-', $data->rating['rating']); 2473 $this->assertCount(0, $data->evidence); 2474 2475 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"])); 2476 $this->assertNotEmpty($data); 2477 $this->assertEquals($comp2->get('shortname'), $data->name); 2478 $this->assertEquals('-', $data->rating['rating']); 2479 $this->assertCount(3, $data->evidence); 2480 $this->assertEquals('C', $data->evidence[0]['note']); 2481 $this->assertEquals('B', $data->evidence[1]['note']); 2482 $this->assertEquals('A', $data->evidence[2]['note']); 2483 2484 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"])); 2485 $this->assertEmpty($data); 2486 2487 // We don't know anything about user 1. 2488 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"])); 2489 $this->assertEmpty($data); 2490 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"])); 2491 $this->assertEmpty($data); 2492 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"])); 2493 $this->assertEmpty($data); 2494 } 2495 2496 public function test_export_data_for_user_about_their_user_evidence() { 2497 $dg = $this->getDataGenerator(); 2498 $ccg = $dg->get_plugin_generator('core_competency'); 2499 $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:userevidence', 'core_competency')]; 2500 2501 $u1 = $dg->create_user(); 2502 $u2 = $dg->create_user(); 2503 $u3 = $dg->create_user(); 2504 2505 $u1ctx = context_user::instance($u1->id); 2506 $u2ctx = context_user::instance($u2->id); 2507 $u3ctx = context_user::instance($u3->id); 2508 2509 $f = $ccg->create_framework(); 2510 $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2511 $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2512 $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]); 2513 2514 $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]); 2515 $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]); 2516 $ue2a = $ccg->create_user_evidence(['userid' => $u2->id]); 2517 $ue3a = $ccg->create_user_evidence(['userid' => $u3->id]); 2518 $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp1->get('id')]); 2519 $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp2->get('id')]); 2520 $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp2->get('id')]); 2521 $ccg->create_user_evidence_competency(['userevidenceid' => $ue2a->get('id'), 'competencyid' => $comp2->get('id')]); 2522 2523 // Export for user 1 in two contexts to make sure. 2524 provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id])); 2525 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"])); 2526 $this->assertNotEmpty($data); 2527 $this->assertEquals($ue1a->get('name'), $data->name); 2528 $this->assertCount(2, $data->competencies); 2529 $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']); 2530 $this->assertEquals($comp2->get('shortname'), $data->competencies[1]['name']); 2531 2532 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"])); 2533 $this->assertNotEmpty($data); 2534 $this->assertEquals($ue1b->get('name'), $data->name); 2535 $this->assertCount(1, $data->competencies); 2536 $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']); 2537 2538 // We should not have access to other's info. 2539 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"])); 2540 $this->assertEmpty($data); 2541 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"])); 2542 $this->assertEmpty($data); 2543 2544 // Export for user 2 in two contexts to make sure. 2545 writer::reset(); 2546 provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u2ctx->id, $u1ctx->id])); 2547 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"])); 2548 $this->assertNotEmpty($data); 2549 $this->assertEquals($ue2a->get('name'), $data->name); 2550 $this->assertCount(1, $data->competencies); 2551 $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']); 2552 2553 // We should not have access to other's info. 2554 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"])); 2555 $this->assertEmpty($data); 2556 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"])); 2557 $this->assertEmpty($data); 2558 $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"])); 2559 $this->assertEmpty($data); 2560 $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"])); 2561 $this->assertEmpty($data); 2562 2563 // Export for user 3. 2564 writer::reset(); 2565 provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u3ctx->id])); 2566 $data = writer::with_context($u3ctx)->get_data(array_merge($path, ["{$ue3a->get('name')} ({$ue3a->get('id')})"])); 2567 $this->assertNotEmpty($data); 2568 $this->assertEquals($ue3a->get('name'), $data->name); 2569 $this->assertCount(0, $data->competencies); 2570 } 2571 2572 /** 2573 * Helps testing comments on plans. 2574 * 2575 * @return void 2576 */ 2577 protected function allow_anyone_to_comment_anywhere() { 2578 global $DB; 2579 $roleid = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST); 2580 assign_capability('moodle/competency:plancomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true); 2581 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $roleid, SYSCONTEXTID, true); 2582 assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $roleid, SYSCONTEXTID, true); 2583 assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true); 2584 assign_capability('moodle/competency:usercompetencyview', CAP_ALLOW, $roleid, SYSCONTEXTID, true); 2585 accesslib_clear_all_caches_for_unit_testing(); 2586 } 2587 2588 /** 2589 * Assert the content of a contextlist. 2590 * 2591 * @param contextlist $contextlist The list. 2592 * @param array $expectedcontextsorids The expected content. 2593 * @return void 2594 */ 2595 protected function assert_contextlist(contextlist $contextlist, $expectedcontextsorids) { 2596 $contextids = array_unique($contextlist->get_contextids()); 2597 $expectedids = array_unique(array_map(function($item) { 2598 return $item instanceof context ? $item->id : $id; 2599 }, $expectedcontextsorids)); 2600 $this->assert_array_match($contextids, $expectedids); 2601 } 2602 2603 /** 2604 * Assert that array match. 2605 * 2606 * @param array $array1 The first one. 2607 * @param array $array2 The second one. 2608 * @return void 2609 */ 2610 protected function assert_array_match($array1, $array2) { 2611 $array1 = (array) (object) $array1; 2612 $array2 = (array) (object) $array2; 2613 sort($array1); 2614 sort($array2); 2615 $this->assertEquals($array1, $array2); 2616 } 2617 2618 /** 2619 * Assert the content of exported comments. 2620 * 2621 * @param array $expected The content of the comments. 2622 * @param array $comments The exported comments. 2623 * @return void 2624 */ 2625 protected function assert_exported_comments($expected, $comments) { 2626 $this->assertCount(count($expected), $comments); 2627 $contents = array_map(function($comment) { 2628 return strip_tags($comment->content); 2629 }, $comments); 2630 $this->assert_array_match($expected, $contents); 2631 } 2632 2633 /** 2634 * Assert that a comment object has comments. 2635 * 2636 * @param \comment $comment The comment object. 2637 * @return void 2638 */ 2639 protected function assert_has_comments(\comment $comment) { 2640 global $DB; 2641 $this->assertTrue($DB->record_exists('comments', [ 2642 'contextid' => $comment->get_context()->id, 2643 'component' => $comment->get_component(), 2644 'commentarea' => $comment->get_commentarea(), 2645 'itemid' => $comment->get_itemid() 2646 ])); 2647 } 2648 2649 /** 2650 * Assert that a comment object does not have any comments. 2651 * 2652 * @param \comment $comment The comment object. 2653 * @return void 2654 */ 2655 protected function assert_has_no_comments(\comment $comment) { 2656 global $DB; 2657 $this->assertFalse($DB->record_exists('comments', [ 2658 'contextid' => $comment->get_context()->id, 2659 'component' => $comment->get_component(), 2660 'commentarea' => $comment->get_commentarea(), 2661 'itemid' => $comment->get_itemid() 2662 ])); 2663 } 2664 2665 /** 2666 * Get the count of comments. 2667 * 2668 * @param \comment $comment The comment object. 2669 * @param int $userid The user ID. 2670 * @return int 2671 */ 2672 protected function get_comments_count(\comment $comment, $userid = null) { 2673 global $DB; 2674 $params = [ 2675 'contextid' => $comment->get_context()->id, 2676 'component' => $comment->get_component(), 2677 'commentarea' => $comment->get_commentarea(), 2678 'itemid' => $comment->get_itemid(), 2679 ]; 2680 if ($userid) { 2681 $params['userid'] = $userid; 2682 } 2683 return $DB->count_records('comments', $params); 2684 } 2685 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body