Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  namespace factor_role;
  18  
  19  /**
  20   * Tests for role factor.
  21   *
  22   * @covers      \factor_role\factor
  23   * @package     factor_role
  24   * @copyright   2023 Stevani Andolo <stevani@hotmail.com.au>
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class factor_test extends \advanced_testcase {
  28  
  29      /**
  30       * Tests getting the summary condition
  31       *
  32       * @covers ::get_summary_condition
  33       * @covers ::get_roles
  34       */
  35      public function test_get_summary_condition() {
  36          global $DB;
  37  
  38          $this->resetAfterTest();
  39  
  40          $managerrole = $DB->get_record('role', ['shortname' => 'manager']);
  41          $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
  42          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  43          $adminrolename = get_string('administrator');
  44          $managerrolename = role_get_name($managerrole);
  45          $teacherrolename = role_get_name($teacherrole);
  46          $studentrolename = role_get_name($studentrole);
  47  
  48          set_config('enabled', 1, 'factor_role');
  49          $rolefactor = \tool_mfa\plugininfo\factor::get_factor('role');
  50  
  51          // Admin is disabled by default in this factor.
  52          $selectedroles = get_config('factor_role', 'roles');
  53          $selectedroles = $rolefactor->get_roles(explode(',', $selectedroles));
  54          $this->assertContains($adminrolename, $selectedroles);
  55          $this->assertNotContains($managerrolename, $selectedroles);
  56          $this->assertNotContains($teacherrolename, $selectedroles);
  57          $this->assertNotContains($studentrolename, $selectedroles);
  58          $this->assertStringContainsString(
  59              implode(', ', $selectedroles),
  60              $rolefactor->get_summary_condition()
  61          );
  62  
  63          // Disabled role factor for managers.
  64          set_config('roles', $managerrole->id, 'factor_role');
  65  
  66          $selectedroles = get_config('factor_role', 'roles');
  67          $selectedroles = $rolefactor->get_roles(explode(',', $selectedroles));
  68          $this->assertNotContains($adminrolename, $selectedroles);
  69          $this->assertContains($managerrolename, $selectedroles);
  70          $this->assertNotContains($teacherrolename, $selectedroles);
  71          $this->assertNotContains($studentrolename, $selectedroles);
  72          $this->assertStringContainsString(
  73              implode(', ', $selectedroles),
  74              $rolefactor->get_summary_condition()
  75          );
  76  
  77          // Disabled role factor for teachers.
  78          set_config('roles', $teacherrole->id, 'factor_role');
  79  
  80          $selectedroles = get_config('factor_role', 'roles');
  81          $selectedroles = $rolefactor->get_roles(explode(',', $selectedroles));
  82          $this->assertNotContains($adminrolename, $selectedroles);
  83          $this->assertNotContains($managerrolename, $selectedroles);
  84          $this->assertContains($teacherrolename, $selectedroles);
  85          $this->assertNotContains($studentrolename, $selectedroles);
  86          $this->assertStringContainsString(
  87              implode(', ', $selectedroles),
  88              $rolefactor->get_summary_condition()
  89          );
  90  
  91          // Disabled role factor for students.
  92          set_config('roles', $studentrole->id, 'factor_role');
  93  
  94          $selectedroles = get_config('factor_role', 'roles');
  95          $selectedroles = $rolefactor->get_roles(explode(',', $selectedroles));
  96          $this->assertNotContains($adminrolename, $selectedroles);
  97          $this->assertNotContains($managerrolename, $selectedroles);
  98          $this->assertNotContains($teacherrolename, $selectedroles);
  99          $this->assertContains($studentrolename, $selectedroles);
 100          $this->assertStringContainsString(
 101              implode(', ', $selectedroles),
 102              $rolefactor->get_summary_condition()
 103          );
 104  
 105          // Disabled role factor for admins, managers, teachers and students.
 106          set_config('roles', "admin,$managerrole->id,$teacherrole->id,$studentrole->id", 'factor_role');
 107  
 108          $selectedroles = get_config('factor_role', 'roles');
 109          $selectedroles = $rolefactor->get_roles(explode(',', $selectedroles));
 110          $this->assertContains($adminrolename, $selectedroles);
 111          $this->assertContains($managerrolename, $selectedroles);
 112          $this->assertContains($teacherrolename, $selectedroles);
 113          $this->assertContains($studentrolename, $selectedroles);
 114          $this->assertStringContainsString(
 115              implode(', ', $selectedroles),
 116              $rolefactor->get_summary_condition()
 117          );
 118  
 119          // Enable all roles.
 120          unset_config('roles', 'factor_role');
 121          $this->assertEquals(
 122              get_string('summarycondition', 'factor_role', get_string('none')),
 123              $rolefactor->get_summary_condition()
 124          );
 125      }
 126  }