Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  namespace core_competency\privacy;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  global $CFG, $DB;
  30  
  31  use core_privacy\tests\provider_testcase;
  32  use core_privacy\local\request\contextlist;
  33  use core_privacy\local\request\approved_contextlist;
  34  use core_privacy\local\request\approved_userlist;
  35  use core_privacy\local\request\transform;
  36  use core_privacy\local\request\userlist;
  37  use core_privacy\local\request\writer;
  38  use core_competency\api;
  39  use core_competency\privacy\provider;
  40  
  41  /**
  42   * Data provider testcase class.
  43   *
  44   * @package    core_competency
  45   * @category   test
  46   * @copyright  2018 Frédéric Massart
  47   * @author     Frédéric Massart <fred@branchup.tech>
  48   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   */
  50  class provider_test extends provider_testcase {
  51  
  52      public function setUp(): void {
  53          global $PAGE;
  54          $this->resetAfterTest();
  55  
  56          // We need this or exporters (core\external\exporter) do not receive the right renderer.
  57          $PAGE->get_renderer('core');
  58      }
  59  
  60      public function test_get_contexts_for_userid_with_usermodified_for_framework() {
  61          $dg = $this->getDataGenerator();
  62          $ccg = $dg->get_plugin_generator('core_competency');
  63  
  64          $cat1 = $dg->create_category();
  65          $cat2 = $dg->create_category();
  66          $u1 = $dg->create_user();
  67          $u2 = $dg->create_user();
  68          $u3 = $dg->create_user();
  69          $u4 = $dg->create_user();
  70  
  71          $sysctx = \context_system::instance();
  72          $cat1ctx = \context_coursecat::instance($cat1->id);
  73          $cat2ctx = \context_coursecat::instance($cat2->id);
  74  
  75          // Test recovery through framework context.
  76          $this->setUser($u1);
  77          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
  78          $f1 = $ccg->create_framework();
  79          $contextlist = provider::get_contexts_for_userid($u1->id);
  80          $this->assert_contextlist($contextlist, [$sysctx]);
  81          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
  82          $contextlist = provider::get_contexts_for_userid($u1->id);
  83          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
  84  
  85          // Test recovery of category context alone.
  86          $this->setUser($u2);
  87          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
  88          $ccg->create_framework(['contextid' => $cat2ctx->id]);
  89          $contextlist = provider::get_contexts_for_userid($u2->id);
  90          $this->assert_contextlist($contextlist, [$cat2ctx]);
  91  
  92          // Test recovery through competency.
  93          $this->setUser($u3);
  94          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
  95          $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  96          $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  97          $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  98          $contextlist = provider::get_contexts_for_userid($u3->id);
  99          $this->assert_contextlist($contextlist, [$sysctx]);
 100          $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 101          $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 102          $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 103          $contextlist = provider::get_contexts_for_userid($u3->id);
 104          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 105  
 106          // Test recovery through related competency.
 107          $this->setUser($u4);
 108          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 109          $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]);
 110          $contextlist = provider::get_contexts_for_userid($u4->id);
 111          $this->assert_contextlist($contextlist, [$sysctx]);
 112          $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]);
 113          $contextlist = provider::get_contexts_for_userid($u4->id);
 114          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 115      }
 116  
 117      public function test_get_users_in_context_with_usermodified_for_framework() {
 118          $dg = $this->getDataGenerator();
 119          $ccg = $dg->get_plugin_generator('core_competency');
 120  
 121          $cat1 = $dg->create_category();
 122          $cat2 = $dg->create_category();
 123          $u1 = $dg->create_user();
 124          $u2 = $dg->create_user();
 125          $u3 = $dg->create_user();
 126          $u4 = $dg->create_user();
 127  
 128          $sysctx = \context_system::instance();
 129          $cat1ctx = \context_coursecat::instance($cat1->id);
 130          $cat2ctx = \context_coursecat::instance($cat2->id);
 131  
 132          // Add frameworks.
 133          $this->setUser($u1);
 134          $f1 = $ccg->create_framework();
 135          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 136  
 137          $this->setUser($u2);
 138          $ccg->create_framework(['contextid' => $cat2ctx->id]);
 139  
 140          // Add competencies.
 141          $this->setUser($u3);
 142          $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 143          $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 144          $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 145          $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 146          $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 147          $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 148  
 149          // Add related competencies.
 150          $this->setUser($u4);
 151          $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]);
 152          $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]);
 153  
 154          // Test correct users appear in each context.
 155          $component = 'core_competency';
 156  
 157          $userlist = new userlist($sysctx, $component);
 158          provider::get_users_in_context($userlist);
 159          $expected = [$u1->id, $u3->id, $u4->id];
 160          $this->assert_array_match($expected, $userlist->get_userids());
 161  
 162          $userlist = new userlist($cat1ctx, $component);
 163          provider::get_users_in_context($userlist);
 164          $expected = [$u1->id, $u3->id, $u4->id];
 165          $this->assert_array_match($expected, $userlist->get_userids());
 166  
 167          $userlist = new userlist($cat2ctx, $component);
 168          provider::get_users_in_context($userlist);
 169          $expected = [$u2->id];
 170          $this->assert_array_match($expected, $userlist->get_userids());
 171      }
 172  
 173      public function test_get_contexts_for_userid_with_usermodified_for_template() {
 174          $dg = $this->getDataGenerator();
 175          $ccg = $dg->get_plugin_generator('core_competency');
 176  
 177          $cat1 = $dg->create_category();
 178          $cat2 = $dg->create_category();
 179          $u1 = $dg->create_user();
 180          $u2 = $dg->create_user();
 181          $u3 = $dg->create_user();
 182          $u4 = $dg->create_user();
 183          $cohort = $dg->create_cohort();
 184  
 185          $sysctx = \context_system::instance();
 186          $cat1ctx = \context_coursecat::instance($cat1->id);
 187          $cat2ctx = \context_coursecat::instance($cat2->id);
 188  
 189          $f1 = $ccg->create_framework();
 190          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 191          $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
 192          $cs = [];
 193  
 194          foreach ([$f1, $f2, $f3] as $f) {
 195              $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 196          }
 197  
 198          // Test recovery through template context.
 199          $this->setUser($u1);
 200          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 201          $t1 = $ccg->create_template();
 202          $contextlist = provider::get_contexts_for_userid($u1->id);
 203          $this->assert_contextlist($contextlist, [$sysctx]);
 204          $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
 205          $contextlist = provider::get_contexts_for_userid($u1->id);
 206          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 207  
 208          // Test recovery of category context alone.
 209          $this->setUser($u2);
 210          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 211          $ccg->create_template(['contextid' => $cat2ctx->id]);
 212          $contextlist = provider::get_contexts_for_userid($u2->id);
 213          $this->assert_contextlist($contextlist, [$cat2ctx]);
 214  
 215          // Test recovery through template competency.
 216          $this->setUser($u3);
 217          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 218          $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]);
 219          $contextlist = provider::get_contexts_for_userid($u3->id);
 220          $this->assert_contextlist($contextlist, [$sysctx]);
 221          $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]);
 222          $contextlist = provider::get_contexts_for_userid($u3->id);
 223          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 224  
 225          // Test recovery through template cohort.
 226          $this->setUser($u4);
 227          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 228          $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]);
 229          $contextlist = provider::get_contexts_for_userid($u4->id);
 230          $this->assert_contextlist($contextlist, [$sysctx]);
 231          $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]);
 232          $contextlist = provider::get_contexts_for_userid($u4->id);
 233          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 234      }
 235  
 236      public function test_get_users_in_context_with_usermodified_for_template() {
 237          $dg = $this->getDataGenerator();
 238          $ccg = $dg->get_plugin_generator('core_competency');
 239  
 240          $cat1 = $dg->create_category();
 241          $cat2 = $dg->create_category();
 242          $u1 = $dg->create_user();
 243          $u2 = $dg->create_user();
 244          $u3 = $dg->create_user();
 245          $u4 = $dg->create_user();
 246          $cohort = $dg->create_cohort();
 247  
 248          $sysctx = \context_system::instance();
 249          $cat1ctx = \context_coursecat::instance($cat1->id);
 250          $cat2ctx = \context_coursecat::instance($cat2->id);
 251  
 252          $f1 = $ccg->create_framework();
 253          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 254          $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
 255          $cs = [];
 256  
 257          foreach ([$f1, $f2, $f3] as $f) {
 258              $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 259          }
 260  
 261          // Create template context.
 262          $this->setUser($u1);
 263          $t1 = $ccg->create_template();
 264          $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
 265  
 266          // Add to category context.
 267          $this->setUser($u2);
 268          $ccg->create_template(['contextid' => $cat2ctx->id]);
 269  
 270          // Create template competencies.
 271          $this->setUser($u3);
 272          $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]);
 273          $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]);
 274  
 275          // Create template cohorts.
 276          $this->setUser($u4);
 277          $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]);
 278          $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]);
 279  
 280          // Test correct users appear in each context.
 281          $component = 'core_competency';
 282  
 283          $userlist = new userlist($sysctx, $component);
 284          provider::get_users_in_context($userlist);
 285          $expected = [$u1->id, $u3->id, $u4->id];
 286          $this->assert_array_match($expected, $userlist->get_userids());
 287  
 288          $userlist = new userlist($cat1ctx, $component);
 289          provider::get_users_in_context($userlist);
 290          $expected = [$u1->id, $u3->id, $u4->id];
 291          $this->assert_array_match($expected, $userlist->get_userids());
 292  
 293          $userlist = new userlist($cat2ctx, $component);
 294          provider::get_users_in_context($userlist);
 295          $expected = [$u2->id];
 296          $this->assert_array_match($expected, $userlist->get_userids());
 297      }
 298  
 299      public function test_get_contexts_for_userid_with_usermodified_for_course() {
 300          $dg = $this->getDataGenerator();
 301          $ccg = $dg->get_plugin_generator('core_competency');
 302          $c1 = $dg->create_course();
 303          $c2 = $dg->create_course();
 304          $u0 = $dg->create_user();
 305          $u1 = $dg->create_user();
 306          $u2 = $dg->create_user();
 307          $u3 = $dg->create_user();
 308          $u4 = $dg->create_user();
 309          $c1ctx = \context_course::instance($c1->id);
 310          $c2ctx = \context_course::instance($c2->id);
 311  
 312          $f = $ccg->create_framework();
 313          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 314          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 315  
 316          $this->setUser($u1);
 317          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 318          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 319          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]);
 320          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 321          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 322  
 323          $this->setUser($u2);
 324          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 325          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 326          $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]);
 327          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 328          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c2ctx]);
 329          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]);
 330          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 331          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 332  
 333          $this->setUser($u3);
 334          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 335          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
 336          $ccs->create();
 337          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 338          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 339          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 340  
 341          $this->setUser($u4);
 342          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 343          $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]);
 344          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 345          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 346          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 347          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$c2ctx]);
 348      }
 349  
 350      public function test_get_users_in_context_with_usermodified_for_course() {
 351          $dg = $this->getDataGenerator();
 352          $ccg = $dg->get_plugin_generator('core_competency');
 353          $c1 = $dg->create_course();
 354          $c2 = $dg->create_course();
 355          $u0 = $dg->create_user();
 356          $u1 = $dg->create_user();
 357          $u2 = $dg->create_user();
 358          $u3 = $dg->create_user();
 359          $u4 = $dg->create_user();
 360          $c1ctx = \context_course::instance($c1->id);
 361          $c2ctx = \context_course::instance($c2->id);
 362  
 363          $f = $ccg->create_framework();
 364          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 365          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 366  
 367          $this->setUser($u1);
 368          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]);
 369  
 370          $this->setUser($u2);
 371          $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]);
 372          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]);
 373  
 374          $this->setUser($u3);
 375          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
 376          $ccs->create();
 377  
 378          $this->setUser($u4);
 379          $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]);
 380  
 381          // Test correct users appear in each context.
 382          $component = 'core_competency';
 383  
 384          $userlist = new userlist($c1ctx, $component);
 385          provider::get_users_in_context($userlist);
 386          $expected = [$u1->id, $u2->id, $u3->id];
 387          $this->assert_array_match($expected, $userlist->get_userids());
 388  
 389          $userlist = new userlist($c2ctx, $component);
 390          provider::get_users_in_context($userlist);
 391          $expected = [$u0->id, $u2->id, $u4->id];
 392          $this->assert_array_match($expected, $userlist->get_userids());
 393      }
 394  
 395      public function test_get_contexts_for_userid_with_usermodified_for_module() {
 396          $dg = $this->getDataGenerator();
 397          $ccg = $dg->get_plugin_generator('core_competency');
 398          $c1 = $dg->create_course();
 399          $m1 = $dg->create_module('choice', ['course' => $c1]);
 400          $m2 = $dg->create_module('choice', ['course' => $c1]);
 401          $u1 = $dg->create_user();
 402          $u2 = $dg->create_user();
 403          $m1ctx = \context_module::instance($m1->cmid);
 404          $m2ctx = \context_module::instance($m2->cmid);
 405  
 406          $f = $ccg->create_framework();
 407          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 408          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 409  
 410          $this->setUser($u1);
 411          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 412          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 413          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]);
 414          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 415          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 416  
 417          $this->setUser($u2);
 418          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 419          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 420          $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]);
 421          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 422          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m2ctx]);
 423          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]);
 424          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 425          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m1ctx, $m2ctx]);
 426      }
 427  
 428      public function test_get_users_in_context_with_usermodified_for_module() {
 429          $dg = $this->getDataGenerator();
 430          $ccg = $dg->get_plugin_generator('core_competency');
 431          $c1 = $dg->create_course();
 432          $m1 = $dg->create_module('choice', ['course' => $c1]);
 433          $m2 = $dg->create_module('choice', ['course' => $c1]);
 434          $u1 = $dg->create_user();
 435          $u2 = $dg->create_user();
 436          $m1ctx = \context_module::instance($m1->cmid);
 437          $m2ctx = \context_module::instance($m2->cmid);
 438  
 439          $f = $ccg->create_framework();
 440          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 441          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 442  
 443          $this->setUser($u1);
 444          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]);
 445  
 446          $this->setUser($u2);
 447          $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]);
 448          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]);
 449  
 450          // Test correct users appear in each context.
 451          $component = 'core_competency';
 452  
 453          $userlist = new userlist($m1ctx, $component);
 454          provider::get_users_in_context($userlist);
 455          $expected = [$u1->id, $u2->id];
 456          $this->assert_array_match($expected, $userlist->get_userids());
 457  
 458          $userlist = new userlist($m2ctx, $component);
 459          provider::get_users_in_context($userlist);
 460          $expected = [$u2->id];
 461          $this->assert_array_match($expected, $userlist->get_userids());
 462      }
 463  
 464      public function test_get_contexts_for_userid_with_usermodified_for_plan() {
 465          $dg = $this->getDataGenerator();
 466          $ccg = $dg->get_plugin_generator('core_competency');
 467          $u0 = $dg->create_user();
 468          $u1 = $dg->create_user();
 469          $u2 = $dg->create_user();
 470          $u3 = $dg->create_user();
 471          $u0ctx = \context_user::instance($u0->id);
 472  
 473          $f = $ccg->create_framework();
 474          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 475          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 476  
 477          $this->setUser($u1);
 478          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 479          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 480          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 481          $plan = $ccg->create_plan(['userid' => $u0->id]);
 482          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 483          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 484          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 485  
 486          $this->setUser($u2);
 487          $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]);
 488          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 489          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 490          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 491  
 492          $this->setUser($u3);
 493          $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'),
 494              'userid' => $u0->id]);
 495          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 496          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 497          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 498      }
 499  
 500      public function test_get_users_in_context_with_usermodified_for_plan() {
 501          $dg = $this->getDataGenerator();
 502          $ccg = $dg->get_plugin_generator('core_competency');
 503          $u0 = $dg->create_user();
 504          $u1 = $dg->create_user();
 505          $u2 = $dg->create_user();
 506          $u3 = $dg->create_user();
 507          $u0ctx = \context_user::instance($u0->id);
 508  
 509          $f = $ccg->create_framework();
 510          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 511          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 512  
 513          $this->setUser($u1);
 514          $plan = $ccg->create_plan(['userid' => $u0->id]);
 515  
 516          $this->setUser($u2);
 517          $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]);
 518  
 519          $this->setUser($u3);
 520          $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'),
 521              'userid' => $u0->id]);
 522  
 523          // Test correct users appear in the context.
 524          $component = 'core_competency';
 525  
 526          $userlist = new userlist($u0ctx, $component);
 527          provider::get_users_in_context($userlist);
 528          $expected = [$u0->id, $u1->id, $u2->id, $u3->id];
 529          $this->assert_array_match($expected, $userlist->get_userids());
 530      }
 531  
 532      public function test_get_contexts_for_userid_with_usermodified_for_competency_data() {
 533          $dg = $this->getDataGenerator();
 534          $ccg = $dg->get_plugin_generator('core_competency');
 535          $u0 = $dg->create_user();
 536          $u1 = $dg->create_user();
 537          $u2 = $dg->create_user();
 538          $u3 = $dg->create_user();
 539          $u4 = $dg->create_user();
 540          $u5 = $dg->create_user();
 541          $u6 = $dg->create_user();
 542          $u7 = $dg->create_user();
 543          $u8 = $dg->create_user();
 544          $u0ctx = \context_user::instance($u0->id);
 545  
 546          $f = $ccg->create_framework();
 547          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 548          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 549  
 550          $this->setUser($u1);
 551          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 552          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 553          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 554          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 555          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
 556          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), []);
 557          $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'),
 558              'reviewerid' => $u6->id]);
 559          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 560          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 561          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 562          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 563          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
 564          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 565  
 566          $this->setUser($u2);
 567          $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]);
 568          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 569          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 570          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 571          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 572          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 573          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 574  
 575          $this->setUser($u3);
 576          $ccg->create_user_evidence(['userid' => $u0->id]);
 577          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 578          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 579          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 580          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 581          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 582          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 583  
 584          $this->setUser($u4);
 585          $ccg->create_user_evidence(['userid' => $u0->id]);
 586          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 587          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 588          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 589          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u0ctx]);
 590          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 591          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 592  
 593          // Comment on competency.
 594          $this->allow_anyone_to_comment_anywhere();
 595          $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), []);
 596          $this->setUser($u7);
 597          $comments = $uc->get_comment_object();
 598          $comments->add('Hello there!');
 599          $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), [$u0ctx]);
 600  
 601          // Comment on plan.
 602          $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), []);
 603          $this->setUser($u8);
 604          $plan = $ccg->create_plan(['userid' => $u0->id]);
 605          $comments = $plan->get_comment_object();
 606          $comments->add('Hi, planet!');
 607          $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), [$u0ctx]);
 608      }
 609  
 610      public function test_get_users_in_context_with_usermodified_for_competency_data() {
 611          $dg = $this->getDataGenerator();
 612          $ccg = $dg->get_plugin_generator('core_competency');
 613          $u0 = $dg->create_user();
 614          $u1 = $dg->create_user();
 615          $u2 = $dg->create_user();
 616          $u3 = $dg->create_user();
 617          $u4 = $dg->create_user();
 618          $u5 = $dg->create_user();
 619          $u6 = $dg->create_user();
 620          $u7 = $dg->create_user();
 621          $u8 = $dg->create_user();
 622          $u0ctx = \context_user::instance($u0->id);
 623  
 624          $f = $ccg->create_framework();
 625          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 626  
 627          $this->setUser($u1);
 628          $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'),
 629              'reviewerid' => $u6->id]);
 630  
 631          $this->setUser($u2);
 632          $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]);
 633  
 634          $this->setUser($u3);
 635          $ccg->create_user_evidence(['userid' => $u0->id]);
 636  
 637          $this->setUser($u4);
 638          $ccg->create_user_evidence(['userid' => $u0->id]);
 639  
 640          // Comment on competency.
 641          $this->allow_anyone_to_comment_anywhere();
 642          $this->setUser($u7);
 643          $comments = $uc->get_comment_object();
 644          $comments->add('Hello there!');
 645  
 646          // Comment on plan.
 647          $this->setUser($u8);
 648          $plan = $ccg->create_plan(['userid' => $u0->id]);
 649          $comments = $plan->get_comment_object();
 650          $comments->add('Hi, planet!');
 651  
 652          // Test correct users appear in the context.
 653          $component = 'core_competency';
 654  
 655          $userlist = new userlist($u0ctx, $component);
 656          provider::get_users_in_context($userlist);
 657          $expected = [$u0->id, $u1->id, $u2->id, $u3->id, $u4->id, $u5->id, $u6->id, $u7->id, $u8->id];
 658          $this->assert_array_match($expected, $userlist->get_userids());
 659      }
 660  
 661      public function test_get_contexts_for_userid_with_actual_data_and_actual_data_is_goooood() {
 662          $dg = $this->getDataGenerator();
 663          $ccg = $dg->get_plugin_generator('core_competency');
 664          $c1 = $dg->create_course();
 665          $u1 = $dg->create_user();
 666          $u2 = $dg->create_user();
 667          $u3 = $dg->create_user();
 668          $u4 = $dg->create_user();
 669  
 670          $c1ctx = \context_course::instance($c1->id);
 671          $u1ctx = \context_user::instance($u1->id);
 672          $u2ctx = \context_user::instance($u2->id);
 673          $u3ctx = \context_user::instance($u3->id);
 674          $u4ctx = \context_user::instance($u4->id);
 675  
 676          $f = $ccg->create_framework();
 677          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 678          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 679  
 680          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 681          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 682          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 683          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 684  
 685          $ccg->create_plan(['userid' => $u1->id]);
 686          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 687          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 688          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 689          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 690  
 691          $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
 692          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 693          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 694          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 695          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 696  
 697          $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 698          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 699          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 700          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 701          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 702  
 703          $ccg->create_user_evidence(['userid' => $u4->id]);
 704          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 705          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 706          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 707          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u4ctx]);
 708      }
 709  
 710      public function test_get_users_in_context_with_actual_data_and_actual_data_is_goooood() {
 711          $dg = $this->getDataGenerator();
 712          $ccg = $dg->get_plugin_generator('core_competency');
 713          $c1 = $dg->create_course();
 714          $u1 = $dg->create_user();
 715          $u2 = $dg->create_user();
 716          $u3 = $dg->create_user();
 717          $u4 = $dg->create_user();
 718  
 719          $c1ctx = \context_course::instance($c1->id);
 720          $u1ctx = \context_user::instance($u1->id);
 721          $u2ctx = \context_user::instance($u2->id);
 722          $u3ctx = \context_user::instance($u3->id);
 723          $u4ctx = \context_user::instance($u4->id);
 724  
 725          $f = $ccg->create_framework();
 726          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 727          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 728  
 729          $ccg->create_plan(['userid' => $u1->id]);
 730  
 731          $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
 732  
 733          $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 734  
 735          $ccg->create_user_evidence(['userid' => $u4->id]);
 736  
 737          // Test correct users appear in each context.
 738          $component = 'core_competency';
 739  
 740          $userlist = new userlist($u1ctx, $component);
 741          provider::get_users_in_context($userlist);
 742          $this->assert_array_match([$u1->id], $userlist->get_userids());
 743  
 744          $userlist = new userlist($u2ctx, $component);
 745          provider::get_users_in_context($userlist);
 746          $this->assert_array_match([$u2->id], $userlist->get_userids());
 747  
 748          $userlist = new userlist($c1ctx, $component);
 749          provider::get_users_in_context($userlist);
 750          $this->assert_array_match([$u3->id], $userlist->get_userids());
 751  
 752          $userlist = new userlist($u4ctx, $component);
 753          provider::get_users_in_context($userlist);
 754          $this->assert_array_match([$u4->id], $userlist->get_userids());
 755      }
 756  
 757      public function test_delete_data_for_user() {
 758          $dg = $this->getDataGenerator();
 759          $ccg = $dg->get_plugin_generator('core_competency');
 760  
 761          $c1 = $dg->create_course();
 762          $c2 = $dg->create_course();
 763          $u1 = $dg->create_user();
 764          $u2 = $dg->create_user();
 765  
 766          $c1ctx = \context_course::instance($c1->id);
 767          $u1ctx = \context_user::instance($u1->id);
 768          $u2ctx = \context_user::instance($u2->id);
 769  
 770          $f = $ccg->create_framework();
 771          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 772          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 773  
 774          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
 775          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
 776          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
 777          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
 778              'competencyid' => $comp1->get('id')]);
 779          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
 780              'competencyid' => $comp2->get('id')]);
 781          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
 782              'competencyid' => $comp1->get('id')]);
 783  
 784          $p1a = $ccg->create_plan(['userid' => $u1->id]);
 785          $p1b = $ccg->create_plan(['userid' => $u1->id]);
 786          $p2 = $ccg->create_plan(['userid' => $u2->id]);
 787          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
 788          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
 789          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
 790          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
 791              'competencyid' => $comp1->get('id')]);
 792          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
 793              'competencyid' => $comp2->get('id')]);
 794          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
 795              'competencyid' => $comp1->get('id')]);
 796  
 797          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
 798          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
 799          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
 800          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
 801          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
 802          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
 803  
 804          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
 805              'competencyid' => $comp1->get('id')]);
 806          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
 807              'competencyid' => $comp1->get('id')]);
 808          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
 809              'competencyid' => $comp1->get('id')]);
 810  
 811          // User 1 comments on both plans.
 812          $this->allow_anyone_to_comment_anywhere();
 813          $this->setUser($u1);
 814          $p1a->get_comment_object()->add('Hi...');
 815          $p1a->get_comment_object()->add('mister');
 816          $p2->get_comment_object()->add('Ahoy!');
 817  
 818          // User 2 comments on both competencies.
 819          $this->setUser($u2);
 820          $uc1a->get_comment_object()->add('Hi, too!');
 821          $uc1a->get_comment_object()->add('How are you?');
 822          $uc2->get_comment_object()->add('Ahoy, too!');
 823  
 824          $p1acommentobj = $p1a->get_comment_object();
 825          $p2commentobj = $p2->get_comment_object();
 826          $uc1acommentobj = $uc1a->get_comment_object();
 827          $uc2commentobj = $uc2->get_comment_object();
 828  
 829          $this->setAdminUser();
 830          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 831          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
 832          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 833          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 834          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
 835          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 836          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 837          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
 838          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 839          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 840          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
 841          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 842          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 843          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
 844          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 845          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 846          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
 847          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 848          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 849          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
 850          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 851          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 852          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 853          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 854          $this->assert_has_comments($p1acommentobj);
 855          $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id));
 856          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
 857          $this->assert_has_comments($p2commentobj);
 858          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
 859          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
 860          $this->assert_has_comments($uc1acommentobj);
 861          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
 862          $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id));
 863          $this->assert_has_comments($uc2commentobj);
 864          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
 865          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
 866  
 867          // Deleting user context only.
 868          $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id]);
 869          provider::delete_data_for_user($appctx);
 870  
 871          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 872          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
 873          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 874          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
 875          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
 876          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
 877          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 878          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
 879          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 880          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
 881          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
 882          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
 883          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
 884          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
 885  
 886          $this->assert_has_no_comments($p1acommentobj);
 887          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id));
 888          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
 889          $this->assert_has_no_comments($uc1acommentobj);
 890          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
 891          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id));
 892  
 893          // This should not have been affected.
 894          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 895          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 896  
 897          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 898          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 899          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 900          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 901          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 902          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 903          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 904          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 905          $this->assert_has_comments($p2commentobj);
 906          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
 907          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
 908          $this->assert_has_comments($uc2commentobj);
 909          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
 910          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
 911  
 912          // Deleting course context as well.
 913          $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $c1ctx->id]);
 914          provider::delete_data_for_user($appctx);
 915  
 916          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 917  
 918          // The rest belongs to another course, or the other user.
 919          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 920          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 921          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 922          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 923          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 924          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 925          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 926          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 927          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 928      }
 929  
 930      public function test_delete_data_for_user_with_other_user_context() {
 931          $dg = $this->getDataGenerator();
 932          $ccg = $dg->get_plugin_generator('core_competency');
 933  
 934          $u1 = $dg->create_user();
 935          $u2 = $dg->create_user();
 936  
 937          $u1ctx = \context_user::instance($u1->id);
 938          $u2ctx = \context_user::instance($u2->id);
 939  
 940          $f = $ccg->create_framework();
 941          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 942  
 943          // Create a bunch of data for user 1.
 944          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
 945          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
 946              'competencyid' => $comp1->get('id')]);
 947          $p1a = $ccg->create_plan(['userid' => $u1->id]);
 948          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
 949          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
 950              'competencyid' => $comp1->get('id')]);
 951          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
 952          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
 953  
 954          $p2a = $ccg->create_plan(['userid' => $u2->id]);
 955  
 956          // User 2 comments.
 957          $this->allow_anyone_to_comment_anywhere();
 958          $this->setUser($u2);
 959          $p1a->get_comment_object()->add('Hi...');
 960          $p2a->get_comment_object()->add('Hi, hi!');
 961          $uc1a->get_comment_object()->add('Hi, too!');
 962  
 963          // Confirm state.
 964          $this->setAdminUser();
 965          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 966          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 967          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 968          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 969          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 970          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 971          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 972          $this->assert_has_comments($p1a->get_comment_object());
 973          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
 974          $this->assert_has_comments($p2a->get_comment_object());
 975          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
 976          $this->assert_has_comments($uc1a->get_comment_object());
 977          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
 978  
 979          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
 980  
 981          // Delete for user 2, but we pass u1 context.
 982          provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id]));
 983  
 984          // Nothing should have happened.
 985          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 986          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 987          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 988          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 989          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 990          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 991          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 992          $this->assert_has_comments($p1a->get_comment_object());
 993          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
 994          $this->assert_has_comments($p2a->get_comment_object());
 995          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
 996          $this->assert_has_comments($uc1a->get_comment_object());
 997          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
 998  
 999          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1000  
1001          // Delete for user 2, but we pass u1 and u2 context.
1002          $p2acommentobj = $p2a->get_comment_object();
1003          provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1004  
1005          // The plan got deleted.
1006          $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id')));
1007          $this->assert_has_no_comments($p2acommentobj);
1008  
1009          // Nothing should have happened for u1.
1010          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1011          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1012          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1013          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1014          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1015          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1016          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1017          $this->assert_has_comments($p1a->get_comment_object());
1018          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1019          $this->assert_has_comments($uc1a->get_comment_object());
1020          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1021      }
1022  
1023      public function test_delete_data_for_users() {
1024          $dg = $this->getDataGenerator();
1025          $ccg = $dg->get_plugin_generator('core_competency');
1026  
1027          $c1 = $dg->create_course();
1028          $c2 = $dg->create_course();
1029          $u1 = $dg->create_user();
1030          $u2 = $dg->create_user();
1031  
1032          $c1ctx = \context_course::instance($c1->id);
1033          $u1ctx = \context_user::instance($u1->id);
1034  
1035          $f = $ccg->create_framework();
1036          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1037          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1038  
1039          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1040          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
1041          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
1042          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1043              'competencyid' => $comp1->get('id')]);
1044          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
1045              'competencyid' => $comp2->get('id')]);
1046          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
1047              'competencyid' => $comp1->get('id')]);
1048  
1049          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1050          $p1b = $ccg->create_plan(['userid' => $u1->id]);
1051          $p2 = $ccg->create_plan(['userid' => $u2->id]);
1052          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1053          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
1054          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
1055          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1056              'competencyid' => $comp1->get('id')]);
1057          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
1058              'competencyid' => $comp2->get('id')]);
1059          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
1060              'competencyid' => $comp1->get('id')]);
1061  
1062          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1063          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
1064          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
1065          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1066          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
1067          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
1068  
1069          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
1070              'competencyid' => $comp1->get('id')]);
1071          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
1072              'competencyid' => $comp1->get('id')]);
1073          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
1074              'competencyid' => $comp1->get('id')]);
1075  
1076          // User 1 comments on both plans.
1077          $this->allow_anyone_to_comment_anywhere();
1078          $this->setUser($u1);
1079          $p1a->get_comment_object()->add('Hi...');
1080          $p1a->get_comment_object()->add('mister');
1081          $p2->get_comment_object()->add('Ahoy!');
1082  
1083          // User 2 comments on both competencies.
1084          $this->setUser($u2);
1085          $uc1a->get_comment_object()->add('Hi, too!');
1086          $uc1a->get_comment_object()->add('How are you?');
1087          $uc2->get_comment_object()->add('Ahoy, too!');
1088  
1089          $p1acommentobj = $p1a->get_comment_object();
1090          $p2commentobj = $p2->get_comment_object();
1091          $uc1acommentobj = $uc1a->get_comment_object();
1092          $uc2commentobj = $uc2->get_comment_object();
1093  
1094          $this->setAdminUser();
1095          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1096          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1097          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1098          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1099          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1100          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1101          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1102          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1103          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1104          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1105          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1106          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1107          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1108          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1109          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1110          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1111          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1112          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1113          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1114          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1115          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1116          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1117          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1118          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1119          $this->assert_has_comments($p1acommentobj);
1120          $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id));
1121          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
1122          $this->assert_has_comments($p2commentobj);
1123          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
1124          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
1125          $this->assert_has_comments($uc1acommentobj);
1126          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
1127          $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id));
1128          $this->assert_has_comments($uc2commentobj);
1129          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
1130          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
1131  
1132          // Deleting user context.
1133          $userlist = new approved_userlist($u1ctx, 'core_competency', [$u1->id, $u2->id]);
1134          provider::delete_data_for_users($userlist);
1135  
1136          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1137          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1138          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1139          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1140          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
1141          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
1142          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1143          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1144          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1145          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1146          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
1147          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
1148          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
1149          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
1150  
1151          $this->assert_has_no_comments($p1acommentobj);
1152          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id));
1153          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
1154          $this->assert_has_no_comments($uc1acommentobj);
1155          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
1156          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id));
1157  
1158          // This should not have been affected.
1159          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1160          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1161  
1162          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1163          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1164          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1165          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1166          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1167          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1168          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1169          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1170          $this->assert_has_comments($p2commentobj);
1171          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
1172          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
1173          $this->assert_has_comments($uc2commentobj);
1174          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
1175          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
1176  
1177          // Deleting course context as well.
1178          $userlist = new approved_userlist($c1ctx, 'core_competency', [$u1->id]);
1179          provider::delete_data_for_users($userlist);
1180  
1181          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1182  
1183          // The rest belongs to another course, or the other user.
1184          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1185          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1186          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1187          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1188          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1189          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1190          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1191          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1192          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1193      }
1194  
1195      public function test_delete_data_for_users_with_other_user_context() {
1196          $dg = $this->getDataGenerator();
1197          $ccg = $dg->get_plugin_generator('core_competency');
1198  
1199          $u1 = $dg->create_user();
1200          $u2 = $dg->create_user();
1201  
1202          $u1ctx = \context_user::instance($u1->id);
1203          $u2ctx = \context_user::instance($u2->id);
1204  
1205          $f = $ccg->create_framework();
1206          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1207  
1208          // Create a bunch of data for user 1.
1209          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1210          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1211              'competencyid' => $comp1->get('id')]);
1212          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1213          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1214          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1215              'competencyid' => $comp1->get('id')]);
1216          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1217          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1218  
1219          $p2a = $ccg->create_plan(['userid' => $u2->id]);
1220  
1221          // User 2 comments.
1222          $this->allow_anyone_to_comment_anywhere();
1223          $this->setUser($u2);
1224          $p1a->get_comment_object()->add('Hi...');
1225          $p2a->get_comment_object()->add('Hi, hi!');
1226          $uc1a->get_comment_object()->add('Hi, too!');
1227  
1228          // Confirm state.
1229          $this->setAdminUser();
1230          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1231          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1232          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1233          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1234          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1235          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1236          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1237          $this->assert_has_comments($p1a->get_comment_object());
1238          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1239          $this->assert_has_comments($p2a->get_comment_object());
1240          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
1241          $this->assert_has_comments($uc1a->get_comment_object());
1242          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1243  
1244          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1245  
1246          // Delete for user 2, but we pass u1 context.
1247          $userlist = new approved_userlist($u1ctx, 'core_competency', [$u2->id]);
1248          provider::delete_data_for_users($userlist);
1249  
1250          // Nothing should have happened.
1251          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1252          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1253          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1254          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1255          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1256          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1257          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1258          $this->assert_has_comments($p1a->get_comment_object());
1259          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1260          $this->assert_has_comments($p2a->get_comment_object());
1261          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
1262          $this->assert_has_comments($uc1a->get_comment_object());
1263          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1264  
1265          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1266  
1267          // Delete for user 2, in user 2 context.
1268          $p2acommentobj = $p2a->get_comment_object();
1269          $userlist = new approved_userlist($u2ctx, 'core_competency', [$u2->id]);
1270          provider::delete_data_for_users($userlist);
1271  
1272          // The plan got deleted.
1273          $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id')));
1274          $this->assert_has_no_comments($p2acommentobj);
1275  
1276          // Nothing should have happened for u1.
1277          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1278          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1279          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1280          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1281          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1282          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1283          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1284          $this->assert_has_comments($p1a->get_comment_object());
1285          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1286          $this->assert_has_comments($uc1a->get_comment_object());
1287          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1288      }
1289  
1290      public function test_delete_data_for_all_users_in_context() {
1291          $dg = $this->getDataGenerator();
1292          $ccg = $dg->get_plugin_generator('core_competency');
1293  
1294          $c1 = $dg->create_course();
1295          $c2 = $dg->create_course();
1296          $u1 = $dg->create_user();
1297          $u2 = $dg->create_user();
1298  
1299          $c1ctx = \context_course::instance($c1->id);
1300          $u1ctx = \context_user::instance($u1->id);
1301          $u2ctx = \context_user::instance($u2->id);
1302  
1303          $f = $ccg->create_framework();
1304          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1305          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1306  
1307          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1308          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
1309          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
1310          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1311              'competencyid' => $comp1->get('id')]);
1312          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
1313              'competencyid' => $comp2->get('id')]);
1314          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
1315              'competencyid' => $comp1->get('id')]);
1316  
1317          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1318          $p1b = $ccg->create_plan(['userid' => $u1->id]);
1319          $p2 = $ccg->create_plan(['userid' => $u2->id]);
1320          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1321          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
1322          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
1323          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1324              'competencyid' => $comp1->get('id')]);
1325          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
1326              'competencyid' => $comp2->get('id')]);
1327          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
1328              'competencyid' => $comp1->get('id')]);
1329  
1330          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1331          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
1332          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
1333          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1334          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
1335          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
1336  
1337          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
1338              'competencyid' => $comp1->get('id')]);
1339          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
1340              'competencyid' => $comp1->get('id')]);
1341          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
1342              'competencyid' => $comp1->get('id')]);
1343  
1344          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1345          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1346          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1347          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1348          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1349          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1350          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1351          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1352          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1353          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1354          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1355          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1356          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1357          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1358          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1359          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1360          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1361          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1362          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1363          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1364          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1365          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1366          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1367          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1368  
1369          // Deleting the course 1 context.
1370          provider::delete_data_for_all_users_in_context($c1ctx);
1371          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1372          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1373  
1374          // Not affected.
1375          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1376          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1377          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1378          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1379          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1380          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1381          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1382          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1383          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1384          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1385          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1386          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1387          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1388          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1389          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1390          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1391          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1392          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1393          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1394          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1395          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1396          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1397  
1398          // Deleting the user 1 context.
1399          provider::delete_data_for_all_users_in_context($u1ctx);
1400          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1401          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1402          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1403          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1404          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
1405          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
1406          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1407          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1408          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1409          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1410          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
1411          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
1412          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
1413          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
1414  
1415          // Not affected.
1416          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1417          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1418          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1419          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1420          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1421          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1422          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1423          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1424      }
1425  
1426      public function test_export_data_for_user_in_module_context_where_usermodified_matches() {
1427          $dg = $this->getDataGenerator();
1428          $ccg = $dg->get_plugin_generator('core_competency');
1429  
1430          $c1 = $dg->create_course();
1431          $u1 = $dg->create_user();
1432          $u2 = $dg->create_user();
1433          $m1 = $dg->create_module('page', ['course' => $c1]);
1434          $m2 = $dg->create_module('page', ['course' => $c1]);
1435  
1436          $m1ctx = \context_module::instance($m1->cmid);
1437          $m2ctx = \context_module::instance($m2->cmid);
1438  
1439          $f = $ccg->create_framework();
1440          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1441          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1442          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1443          $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m1->cmid]);
1444  
1445          $this->setUser($u1);
1446          $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $m1->cmid]);
1447          $ccg->create_course_module_competency(['competencyid' => $comp2->get('id'), 'cmid' => $m2->cmid]);
1448  
1449          $this->setUser($u2);
1450          $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m2->cmid]);
1451  
1452          // Export.
1453          $this->setAdminUser();
1454          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id]));
1455  
1456          // Check exported context 1.
1457          $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
1458          $this->assertCount(1, $data->associations);
1459          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1460  
1461          // Check exported context 2.
1462          $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
1463          $this->assertEmpty($data);
1464  
1465          // Export both contexts.
1466          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id, $m2ctx->id]));
1467  
1468          // Check exported context 1.
1469          $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
1470          $this->assertCount(1, $data->associations);
1471          $this->assertEquals($comp1->get('shortname'), $data->associations[0]['name']);
1472          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1473  
1474          // Check exported context 2.
1475          $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
1476          $this->assertCount(1, $data->associations);
1477          $this->assertEquals($comp2->get('shortname'), $data->associations[0]['name']);
1478          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1479      }
1480  
1481      public function test_export_data_for_user_in_course_context_where_usermodified_matches() {
1482          $dg = $this->getDataGenerator();
1483          $ccg = $dg->get_plugin_generator('core_competency');
1484  
1485          $c1 = $dg->create_course();
1486          $c2 = $dg->create_course();
1487          $u0 = $dg->create_user();
1488          $u1 = $dg->create_user();
1489          $u2 = $dg->create_user();
1490          $u3 = $dg->create_user();
1491          $u4 = $dg->create_user();
1492  
1493          $c1ctx = \context_course::instance($c1->id);
1494          $c2ctx = \context_course::instance($c2->id);
1495  
1496          $f = $ccg->create_framework();
1497          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1498          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1499          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1500          $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1501          $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id]);
1502          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1503  
1504          $this->setUser($u1);
1505          $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
1506          $ccg->create_course_competency(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id]);
1507          $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
1508          $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1509          $ccg->create_user_competency_course(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1510          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
1511          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
1512          $ccs->create();
1513  
1514          $this->setUser($u2);
1515          $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id]);
1516          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
1517          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c2->id]);
1518          $ccs->create();
1519  
1520          // Export.
1521          $this->setAdminUser();
1522          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
1523  
1524          // Check exported context 1.
1525          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1526          $this->assertCount(2, $data->competencies);
1527          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1528          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1529          $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
1530          $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
1531          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1532          $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
1533          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1534          $this->assertCount(2, $data->ratings);
1535          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1536          $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
1537  
1538          // Check exported context 2.
1539          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1540          $this->assertEmpty($data);
1541          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1542          $this->assertEmpty($data);
1543          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1544          $this->assertEmpty($data);
1545  
1546          // Export both contexts.
1547          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id, $c2ctx->id]));
1548  
1549          // Check exported context 1.
1550          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1551          $this->assertCount(2, $data->competencies);
1552          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1553          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1554          $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
1555          $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
1556          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1557          $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
1558          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1559          $this->assertCount(2, $data->ratings);
1560          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1561          $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
1562  
1563          // Check exported context 2.
1564          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1565          $this->assertCount(1, $data->competencies);
1566          $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
1567          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1568          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1569          $this->assertEmpty($data);
1570          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1571          $this->assertCount(1, $data->ratings);
1572          $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
1573      }
1574  
1575      public function test_export_data_for_user_in_course_context_with_real_data() {
1576          $dg = $this->getDataGenerator();
1577          $ccg = $dg->get_plugin_generator('core_competency');
1578  
1579          $c1 = $dg->create_course();
1580          $c2 = $dg->create_course();
1581          $u1 = $dg->create_user();
1582          $u2 = $dg->create_user();
1583  
1584          $c1ctx = \context_course::instance($c1->id);
1585          $c2ctx = \context_course::instance($c2->id);
1586  
1587          $f = $ccg->create_framework();
1588          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1589          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1590          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1591  
1592          $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
1593              'userid' => $u1->id, 'grade' => 1, 'proficiency' => true]);
1594          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
1595              'userid' => $u1->id, 'grade' => 2, 'proficiency' => false]);
1596          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
1597              'userid' => $u1->id, 'grade' => 3, 'proficiency' => false]);
1598          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id,
1599              'userid' => $u1->id]);
1600  
1601          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u2->id]);
1602          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u2->id]);
1603  
1604          // Export user 1, in course 1.
1605          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
1606  
1607          // Check course 1.
1608          $data = writer::with_context($c1ctx)->get_data([get_string('competencies', 'core_competency')]);
1609          $this->assertCount(2, $data->ratings);
1610          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1611          $this->assertEquals('A', $data->ratings[0]['rating']['rating']);
1612          $this->assertEquals(transform::yesno(true), $data->ratings[0]['rating']['proficient']);
1613          $this->assertEquals($comp2->get('shortname'), $data->ratings[1]['name']);
1614          $this->assertEquals('B', $data->ratings[1]['rating']['rating']);
1615          $this->assertEquals(transform::yesno(false), $data->ratings[1]['rating']['proficient']);
1616  
1617          // Check course 2.
1618          $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1619          $this->assertEmpty($data);
1620  
1621          // Export user 1, in course 2.
1622          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c2ctx->id]));
1623          $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1624          $this->assertCount(2, $data->ratings);
1625          $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
1626          $this->assertEquals('C', $data->ratings[0]['rating']['rating']);
1627          $this->assertEquals(transform::yesno(false), $data->ratings[0]['rating']['proficient']);
1628          $this->assertEquals($comp3->get('shortname'), $data->ratings[1]['name']);
1629          $this->assertEquals('-', $data->ratings[1]['rating']['rating']);
1630          $this->assertEquals('-', $data->ratings[1]['rating']['proficient']);
1631      }
1632  
1633      public function test_export_data_for_user_in_system_and_category_contexts() {
1634          $dg = $this->getDataGenerator();
1635          $ccg = $dg->get_plugin_generator('core_competency');
1636  
1637          $c1 = $dg->create_cohort();
1638          $c2 = $dg->create_cohort();
1639          $cat1 = $dg->create_category();
1640          $cat2 = $dg->create_category();
1641  
1642          $cat1ctx = \context_coursecat::instance($cat1->id);
1643          $cat2ctx = \context_coursecat::instance($cat2->id);
1644          $sysctx = \context_system::instance();
1645  
1646          $u1 = $dg->create_user();
1647          $u2 = $dg->create_user();
1648          $u3 = $dg->create_user();
1649          $u4 = $dg->create_user();
1650          $u2 = $dg->create_user();
1651  
1652          $this->setUser($u1);
1653