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.

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

   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  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  23  
  24  /**
  25   * Competency rule testcase.
  26   *
  27   * @package    core_competency
  28   * @copyright  2015 Frédéric Massart - FMCorz.net
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class competency_rule_test extends \externallib_advanced_testcase {
  32  
  33      public function test_rule_all_matching() {
  34          $this->resetAfterTest(true);
  35  
  36          $this->setAdminUser();
  37          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
  38          $u1 = $this->getDataGenerator()->create_user();
  39  
  40          // Set up the framework and competencies.
  41          $framework = $lpg->create_framework();
  42          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'),
  43              'ruletype' => 'core_competency\competency_rule_all'));
  44          $c11 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id'),
  45              'ruletype' => 'core_competency\competency_rule_all'));
  46          $c111 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c11->get('id'),
  47              'ruletype' => 'core_competency\competency_rule_all'));
  48          $c112 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c11->get('id'),
  49              'ruletype' => 'core_competency\competency_rule_all'));
  50          $c12 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id'),
  51              'ruletype' => 'core_competency\competency_rule_all'));
  52          $c13 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id'),
  53              'ruletype' => 'core_competency\competency_rule_all'));
  54          $c131 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c13->get('id'),
  55              'ruletype' => 'core_competency\competency_rule_all'));
  56  
  57          // Create some user competency records.
  58          $uc1 = $lpg->create_user_competency(array('competencyid' => $c1->get('id'), 'userid' => $u1->id));
  59          $uc11 = $lpg->create_user_competency(array('competencyid' => $c11->get('id'), 'userid' => $u1->id,
  60              'grade' => 1, 'proficiency' => 1));
  61          $uc111 = $lpg->create_user_competency(array('competencyid' => $c111->get('id'), 'userid' => $u1->id,
  62              'grade' => 1, 'proficiency' => 1));
  63          $uc112 = $lpg->create_user_competency(array('competencyid' => $c112->get('id'), 'userid' => $u1->id,
  64              'grade' => 1, 'proficiency' => 1));
  65          $uc12 = $lpg->create_user_competency(array('competencyid' => $c12->get('id'), 'userid' => $u1->id));
  66          $uc13 = new user_competency(0, (object) array('userid' => $u1->id, 'competencyid' => $c13->get('id')));
  67  
  68          // Not all children are met.
  69          $cr = new competency_rule_all($c1);
  70          $this->assertFalse($cr->matches($uc1));
  71  
  72          // All children are met.
  73          $cr = new competency_rule_all($c11);
  74          $this->assertTrue($cr->matches($uc11));
  75  
  76          // The competency doesn't have any children.
  77          $cr = new competency_rule_all($c12);
  78          $this->assertFalse($cr->matches($uc12));
  79  
  80          // The competency doesn't have saved user competency records.
  81          $cr = new competency_rule_all($c13);
  82          $this->assertFalse($cr->matches($uc13));
  83      }
  84  
  85      public function test_rule_points_validation() {
  86          $this->resetAfterTest(true);
  87          $this->setAdminUser();
  88  
  89          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
  90          $framework = $lpg->create_framework();
  91          $framework2 = $lpg->create_framework();
  92          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
  93          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
  94          $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
  95          $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
  96          $cx = $lpg->create_competency(array('competencyframeworkid' => $framework2->get('id')));
  97  
  98          $c1->set('ruletype', 'core_competency\competency_rule_points');
  99          $rule = new competency_rule_points($c1);
 100  
 101          // Invalid config.
 102          $config = json_encode(array());
 103          $this->assertFalse($rule->validate_config($config));
 104  
 105          // Missing required points.
 106          $config = json_encode(array(
 107              'base' => array(),
 108              'competencies' => array(
 109                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 110                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 111              )
 112          ));
 113          $this->assertFalse($rule->validate_config($config));
 114  
 115          // Invalid required points.
 116          $config = json_encode(array(
 117              'base' => array('points' => 'abc'),
 118              'competencies' => array(
 119                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 120                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 121              )
 122          ));
 123          $this->assertFalse($rule->validate_config($config));
 124  
 125          // Less than 1 required points.
 126          $config = json_encode(array(
 127              'base' => array('points' => 0),
 128              'competencies' => array(
 129                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 130                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 131              )
 132          ));
 133          $this->assertFalse($rule->validate_config($config));
 134  
 135          // Not enough required points.
 136          $config = json_encode(array(
 137              'base' => array('points' => 3),
 138              'competencies' => array(
 139                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 140                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 141              )
 142          ));
 143          $this->assertFalse($rule->validate_config($config));
 144  
 145          // Duplicate competency.
 146          $config = json_encode(array(
 147              'base' => array('points' => 1),
 148              'competencies' => array(
 149                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 150                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 151              )
 152          ));
 153          $this->assertFalse($rule->validate_config($config));
 154  
 155          // Competency includes itself.
 156          $config = json_encode(array(
 157              'base' => array('points' => 1),
 158              'competencies' => array(
 159                  array('id' => $c1->get('id'), 'points' => 1, 'required' => 0),
 160                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 161              )
 162          ));
 163          $this->assertFalse($rule->validate_config($config));
 164  
 165          // Cannot use negative points.
 166          $config = json_encode(array(
 167              'base' => array('points' => 1),
 168              'competencies' => array(
 169                  array('id' => $c2->get('id'), 'points' => -1, 'required' => 0),
 170                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 171              )
 172          ));
 173          $this->assertFalse($rule->validate_config($config));
 174  
 175          // Not competencies set.
 176          $config = json_encode(array(
 177              'base' => array('points' => 1),
 178              'competencies' => array(
 179              )
 180          ));
 181          $this->assertFalse($rule->validate_config($config));
 182  
 183          // There is a competency that is not a child.
 184          $config = json_encode(array(
 185              'base' => array('points' => 1),
 186              'competencies' => array(
 187                  array('id' => $c1->get('id'), 'points' => 1, 'required' => 0),
 188                  array('id' => $c2->get('id'), 'points' => 1, 'required' => 0),
 189                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 190              )
 191          ));
 192          $this->assertFalse($rule->validate_config($config));
 193  
 194          // There is a competency from another framework in there.
 195          $config = json_encode(array(
 196              'base' => array('points' => 1),
 197              'competencies' => array(
 198                  array('id' => $cx->get('id'), 'points' => 1, 'required' => 0),
 199                  array('id' => $c3->get('id'), 'points' => 1, 'required' => 0),
 200              )
 201          ));
 202          $this->assertFalse($rule->validate_config($config));
 203  
 204          // A normal config.
 205          $config = json_encode(array(
 206              'base' => array('points' => 4),
 207              'competencies' => array(
 208                  array('id' => $c2->get('id'), 'points' => 3, 'required' => 0),
 209                  array('id' => $c3->get('id'), 'points' => 2, 'required' => 1),
 210              )
 211          ));
 212          $this->assertTrue($rule->validate_config($config));
 213      }
 214  
 215      public function test_rule_points_matching() {
 216          $this->resetAfterTest(true);
 217  
 218          $this->setAdminUser();
 219          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 220          $u1 = $this->getDataGenerator()->create_user();
 221  
 222          // Set up the framework and competencies.
 223          $framework = $lpg->create_framework();
 224          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
 225          $c1->set('ruletype', 'core_competency\competency_rule_points');
 226          $comprule = new competency_rule_points($c1);
 227          $c11 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
 228          $c12 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
 229          $c13 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
 230          $c14 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
 231  
 232          // Create some user competency records.
 233          $uc1 = $lpg->create_user_competency(array('competencyid' => $c1->get('id'), 'userid' => $u1->id));
 234          $uc11 = $lpg->create_user_competency(array('competencyid' => $c11->get('id'), 'userid' => $u1->id,
 235              'grade' => 1, 'proficiency' => 1));
 236          $uc12 = $lpg->create_user_competency(array('competencyid' => $c12->get('id'), 'userid' => $u1->id,
 237              'grade' => 1, 'proficiency' => 1));
 238          $uc13 = $lpg->create_user_competency(array('competencyid' => $c13->get('id'), 'userid' => $u1->id));
 239  
 240          // Enough points.
 241          $rule = array(
 242              'base' => array('points' => 8),
 243              'competencies' => array(
 244                  array(
 245                      'id' => $c11->get('id'),
 246                      'points' => 4,
 247                      'required' => 0
 248                  ),
 249                  array(
 250                      'id' => $c12->get('id'),
 251                      'points' => 4,
 252                      'required' => 0
 253                  ),
 254              )
 255          );
 256          $c1->set('ruleconfig', json_encode($rule));
 257          $c1->update();
 258          $this->assertTrue($comprule->matches($uc1));
 259  
 260          // Not enough points.
 261          $rule = array(
 262              'base' => array('points' => 8),
 263              'competencies' => array(
 264                  array(
 265                      'id' => $c11->get('id'),
 266                      'points' => 4,
 267                      'required' => 0
 268                  ),
 269                  array(
 270                      'id' => $c13->get('id'),
 271                      'points' => 4,
 272                      'required' => 0
 273                  ),
 274              )
 275          );
 276          $c1->set('ruleconfig', json_encode($rule));
 277          $c1->update();
 278          $this->assertFalse($comprule->matches($uc1));
 279  
 280          // One required that is not met but points were OK.
 281          $rule = array(
 282              'base' => array('points' => 8),
 283              'competencies' => array(
 284                  array(
 285                      'id' => $c11->get('id'),
 286                      'points' => 4,
 287                      'required' => 0
 288                  ),
 289                  array(
 290                      'id' => $c12->get('id'),
 291                      'points' => 4,
 292                      'required' => 0
 293                  ),
 294                  array(
 295                      'id' => $c13->get('id'),
 296                      'points' => 4,
 297                      'required' => 1
 298                  ),
 299              )
 300          );
 301          $c1->set('ruleconfig', json_encode($rule));
 302          $c1->update();
 303          $this->assertFalse($comprule->matches($uc1));
 304  
 305          // One required, one not, should match.
 306          $rule = array(
 307              'base' => array('points' => 8),
 308              'competencies' => array(
 309                  array(
 310                      'id' => $c11->get('id'),
 311                      'points' => 4,
 312                      'required' => 0
 313                  ),
 314                  array(
 315                      'id' => $c12->get('id'),
 316                      'points' => 4,
 317                      'required' => 1
 318                  ),
 319              )
 320          );
 321          $c1->set('ruleconfig', json_encode($rule));
 322          $c1->update();
 323          $this->assertTrue($comprule->matches($uc1));
 324  
 325          // All required and should match.
 326          $rule = array(
 327              'base' => array('points' => 8),
 328              'competencies' => array(
 329                  array(
 330                      'id' => $c11->get('id'),
 331                      'points' => 4,
 332                      'required' => 1
 333                  ),
 334                  array(
 335                      'id' => $c12->get('id'),
 336                      'points' => 4,
 337                      'required' => 1
 338                  ),
 339              )
 340          );
 341          $c1->set('ruleconfig', json_encode($rule));
 342          $c1->update();
 343          $this->assertTrue($comprule->matches($uc1));
 344  
 345          // All required, but one doesn't have a user record.
 346          $rule = array(
 347              'base' => array('points' => 4),
 348              'competencies' => array(
 349                  array(
 350                      'id' => $c12->get('id'),
 351                      'points' => 4,
 352                      'required' => 1
 353                  ),
 354                  array(
 355                      'id' => $c14->get('id'),
 356                      'points' => 4,
 357                      'required' => 1
 358                  ),
 359              )
 360          );
 361          $c1->set('ruleconfig', json_encode($rule));
 362          $c1->update();
 363          $this->assertFalse($comprule->matches($uc1));
 364      }
 365  
 366  }