Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Hook tests.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core_competency\course_competency;
  28  use core_competency\course_module_competency;
  29  use core_competency\user_competency_course;
  30  
  31  /**
  32   * Hook tests.
  33   *
  34   * @package    core_competency
  35   * @copyright  2016 Frédéric Massart - FMCorz.net
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_competency_hooks_testcase extends advanced_testcase {
  39  
  40      public function test_hook_course_deleted() {
  41          $this->resetAfterTest();
  42          $dg = $this->getDataGenerator();
  43          $ccg = $dg->get_plugin_generator('core_competency');
  44  
  45          $u1 = $dg->create_user();
  46  
  47          $framework = $ccg->create_framework();
  48          $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
  49          $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
  50  
  51          $c1 = $dg->create_course();
  52          $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
  53          $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
  54          $assign1a = $dg->create_module('assign', ['course' => $c1]);
  55          $assign1b = $dg->create_module('assign', ['course' => $c1]);
  56          $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
  57          $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
  58          $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
  59              'userid' => $u1->id]);
  60          $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
  61              'userid' => $u1->id]);
  62  
  63          $c2 = $dg->create_course();
  64          $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
  65          $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
  66          $assign2a = $dg->create_module('assign', ['course' => $c2]);
  67          $assign2b = $dg->create_module('assign', ['course' => $c2]);
  68          $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
  69          $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
  70          $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
  71              'userid' => $u1->id]);
  72          $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
  73              'userid' => $u1->id]);
  74  
  75          delete_course($c1, false);
  76  
  77          $this->assertEquals(0, course_competency::count_records(['courseid' => $c1->id]));
  78          $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
  79          $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
  80          $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
  81          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
  82          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
  83          $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
  84          $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
  85      }
  86  
  87      public function test_hook_course_module_deleted() {
  88          $this->resetAfterTest();
  89          $dg = $this->getDataGenerator();
  90          $ccg = $dg->get_plugin_generator('core_competency');
  91  
  92          $u1 = $dg->create_user();
  93  
  94          $framework = $ccg->create_framework();
  95          $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
  96          $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
  97  
  98          $c1 = $dg->create_course();
  99          $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 100          $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
 101          $assign1a = $dg->create_module('assign', ['course' => $c1]);
 102          $assign1b = $dg->create_module('assign', ['course' => $c1]);
 103          $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
 104          $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
 105          $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
 106              'userid' => $u1->id]);
 107          $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
 108              'userid' => $u1->id]);
 109  
 110          $c2 = $dg->create_course();
 111          $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
 112          $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
 113          $assign2a = $dg->create_module('assign', ['course' => $c2]);
 114          $assign2b = $dg->create_module('assign', ['course' => $c2]);
 115          $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
 116          $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
 117          $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
 118              'userid' => $u1->id]);
 119          $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
 120              'userid' => $u1->id]);
 121  
 122          course_delete_module($assign1b->cmid);
 123  
 124          $this->assertEquals(2, course_competency::count_records(['courseid' => $c1->id]));
 125          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
 126          $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
 127          $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c1->id]));
 128  
 129          $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
 130          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
 131          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
 132          $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
 133      }
 134  
 135      public function test_hook_course_reset_competency_ratings() {
 136          $this->resetAfterTest();
 137          $dg = $this->getDataGenerator();
 138          $ccg = $dg->get_plugin_generator('core_competency');
 139  
 140          $u1 = $dg->create_user();
 141  
 142          $framework = $ccg->create_framework();
 143          $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
 144          $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
 145  
 146          $c1 = $dg->create_course();
 147          $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 148          $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
 149          $assign1a = $dg->create_module('assign', ['course' => $c1]);
 150          $assign1b = $dg->create_module('assign', ['course' => $c1]);
 151          $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
 152          $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
 153          $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
 154              'userid' => $u1->id]);
 155          $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
 156              'userid' => $u1->id]);
 157  
 158          $c2 = $dg->create_course();
 159          $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
 160          $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
 161          $assign2a = $dg->create_module('assign', ['course' => $c2]);
 162          $assign2b = $dg->create_module('assign', ['course' => $c2]);
 163          $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
 164          $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
 165          $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
 166              'userid' => $u1->id]);
 167          $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
 168              'userid' => $u1->id]);
 169  
 170          reset_course_userdata((object) ['id' => $c1->id, 'reset_competency_ratings' => true]);
 171  
 172          $this->assertEquals(2, course_competency::count_records(['courseid' => $c1->id]));
 173          $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
 174          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
 175          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
 176          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
 177          $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
 178          $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
 179          $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
 180      }
 181  
 182      public function test_hook_cohort_deleted() {
 183          $this->resetAfterTest();
 184          $this->setAdminUser();
 185  
 186          $datagen = $this->getDataGenerator();
 187          $corecompgen = $datagen->get_plugin_generator('core_competency');
 188  
 189          $c1 = $datagen->create_cohort();
 190          $c2 = $datagen->create_cohort();
 191          $t1 = $corecompgen->create_template();
 192          $t2 = $corecompgen->create_template();
 193  
 194          // Create the template cohorts.
 195          core_competency\api::create_template_cohort($t1->get('id'), $c1->id);
 196          core_competency\api::create_template_cohort($t1->get('id'), $c2->id);
 197          core_competency\api::create_template_cohort($t2->get('id'), $c1->id);
 198  
 199          // Check that the association was made.
 200          $this->assertEquals(2, \core_competency\template_cohort::count_records(array('templateid' => $t1->get('id'))));
 201          $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t2->get('id'))));
 202  
 203          // Delete the first cohort.
 204          cohort_delete_cohort($c1);
 205  
 206          // Check that the association was removed.
 207          $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get('id'))));
 208          $this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get('id'))));
 209      }
 210  
 211      public function test_hook_user_deleted() {
 212          $this->resetAfterTest();
 213          $dg = $this->getDataGenerator();
 214          $ccg = $dg->get_plugin_generator('core_competency');
 215  
 216          $u1 = $dg->create_user();
 217  
 218          $framework = $ccg->create_framework();
 219          $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
 220          $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
 221  
 222          $c1 = $dg->create_course();
 223          $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 224          $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
 225          $assign1a = $dg->create_module('assign', ['course' => $c1]);
 226          $assign1b = $dg->create_module('assign', ['course' => $c1]);
 227          $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
 228          $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
 229          $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
 230              'userid' => $u1->id]);
 231          $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
 232              'userid' => $u1->id]);
 233  
 234          $c2 = $dg->create_course();
 235          $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
 236          $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
 237          $assign2a = $dg->create_module('assign', ['course' => $c2]);
 238          $assign2b = $dg->create_module('assign', ['course' => $c2]);
 239          $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
 240          $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
 241          $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
 242              'userid' => $u1->id]);
 243          $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
 244              'userid' => $u1->id]);
 245  
 246          reset_course_userdata((object) ['id' => $c1->id, 'reset_competency_ratings' => true]);
 247  
 248          delete_user($u1);
 249  
 250          // Assert the records don't exist anymore.
 251          $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
 252      }
 253  }