Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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