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   * Course competency persistent testcase.
  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 course_competency_test extends \advanced_testcase {
  27  
  28      public function test_get_courses_with_competency_and_user() {
  29          global $CFG, $DB;
  30  
  31          $this->resetAfterTest(true);
  32          $dg = $this->getDataGenerator();
  33          $lpg = $dg->get_plugin_generator('core_competency');
  34  
  35          $c1 = $dg->create_course();
  36          $c2 = $dg->create_course();
  37          $c3 = $dg->create_course();
  38          $c4 = $dg->create_course();
  39          $u1 = $dg->create_user();
  40          $u2 = $dg->create_user();
  41          $u3 = $dg->create_user();
  42          $u4 = $dg->create_user();
  43  
  44          $flatfileplugin = enrol_get_plugin('flatfile');
  45          $flatfileinstanceid = $flatfileplugin->add_instance($c2);
  46  
  47          $framework = $lpg->create_framework();
  48          $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));   // In C1, and C2.
  49          $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));   // In C2.
  50          $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));   // In None.
  51          $comp4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));   // In C4.
  52          $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c1->id));
  53          $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c2->id));
  54          $lpg->create_course_competency(array('competencyid' => $comp2->get('id'), 'courseid' => $c2->id));
  55          $lpg->create_course_competency(array('competencyid' => $comp4->get('id'), 'courseid' => $c4->id));
  56  
  57          // Enrol the user 1 in C1, C2, and C3.
  58          $dg->enrol_user($u1->id, $c1->id);
  59          $dg->enrol_user($u1->id, $c2->id);
  60          $dg->enrol_user($u1->id, $c3->id);
  61  
  62          // Enrol the user 2 in C4.
  63          $dg->enrol_user($u2->id, $c4->id);
  64  
  65          // Enrol the user 3 in C1 and C2, but non active in C2.
  66          $dg->enrol_user($u3->id, $c1->id);
  67          $dg->enrol_user($u3->id, $c2->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
  68  
  69          // Enrol the user 4 with a plugin which will be enabled/disabled.
  70          $dg->enrol_user($u4->id, $c2->id, null, 'flatfile');
  71  
  72          // Using the competency that is not used anywhere -> no courses.
  73          $this->assertCount(0, course_competency::get_courses_with_competency_and_user($comp3->get('id'), $u1->id));
  74  
  75          // Using the competency that is used in a course where the user is not enrolled -> no courses.
  76          $this->assertCount(0, course_competency::get_courses_with_competency_and_user($comp4->get('id'), $u1->id));
  77  
  78          // Using the competency that is used in a course where the user is enrolled -> one course.
  79          $courses = course_competency::get_courses_with_competency_and_user($comp2->get('id'), $u1->id);
  80          $this->assertCount(1, $courses);
  81          $this->assertArrayHasKey($c2->id, $courses);
  82  
  83          // Using the competency used multiple times.
  84          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u1->id);
  85          $this->assertCount(2, $courses);
  86          $this->assertArrayHasKey($c1->id, $courses);
  87          $this->assertArrayHasKey($c2->id, $courses);
  88  
  89          // Checking for another user where the competency is used twice, but not for them.
  90          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u2->id);
  91          $this->assertCount(0, $courses);
  92  
  93          // Checking for another user where the competency is used in their course.
  94          $courses = course_competency::get_courses_with_competency_and_user($comp4->get('id'), $u2->id);
  95          $this->assertCount(1, $courses);
  96          $this->assertArrayHasKey($c4->id, $courses);
  97  
  98          // Checking for a user who is suspended in a course.
  99          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u3->id);
 100          $this->assertCount(1, $courses);
 101          $this->assertArrayHasKey($c1->id, $courses);
 102  
 103          // Check for the user with plugin enabled.
 104          $enrolplugins = explode(',', $CFG->enrol_plugins_enabled);
 105          $enrolplugins[] = 'flatfile';
 106          $CFG->enrol_plugins_enabled = implode(',', array_unique($enrolplugins));
 107          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u4->id);
 108          $this->assertCount(1, $courses);
 109          $this->assertArrayHasKey($c2->id, $courses);
 110  
 111          // Check for the user with plugin enabled, but enrolment instance disabled.
 112          $flatfileinstance = $DB->get_record('enrol', array('id' => $flatfileinstanceid));
 113          $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_DISABLED);
 114          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u4->id);
 115          $this->assertCount(0, $courses);
 116          $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_ENABLED);
 117  
 118          // Check for the user with plugin disabled.
 119          $enrolplugins = array_flip(explode(',', $CFG->enrol_plugins_enabled));
 120          unset($enrolplugins['flatfile']);
 121          $CFG->enrol_plugins_enabled = implode(',', array_keys($enrolplugins));
 122          $courses = course_competency::get_courses_with_competency_and_user($comp1->get('id'), $u4->id);
 123          $this->assertCount(0, $courses);
 124      }
 125  
 126  }