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 tool_mfa;
  18  use tool_mfa\tool_mfa_trait;
  19  
  20  defined('MOODLE_INTERNAL') || die();
  21  require_once (__DIR__ . '/tool_mfa_trait.php');
  22  
  23  /**
  24   * Tests for base factor implementation methods.
  25   *
  26   * @package     tool_mfa
  27   * @author      Peter Burnett <peterburnett@catalyst-au.net>
  28   * @copyright   2023 Catalyst IT
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class object_factor_base_test extends \advanced_testcase {
  32  
  33      use tool_mfa_trait;
  34  
  35      /**
  36       * Test deleting user's configured factors
  37       *
  38       * @covers ::setup_user_factor
  39       * @return void
  40       */
  41      public function test_revoke_user_factor() {
  42          $this->resetAfterTest();
  43          $user = $this->getDataGenerator()->create_user();
  44          $this->setUser($user);
  45  
  46          $this->set_factor_state('totp', 1, 100);
  47          $totpfactor = \tool_mfa\plugininfo\factor::get_factor('totp');
  48          $totpdata = [
  49              'secret' => 'fakekey',
  50              'devicename' => 'fakedevice',
  51          ];
  52          $factorinstance = $totpfactor->setup_user_factor((object) $totpdata);
  53          $totpdata2 = [
  54              'secret' => 'fakekey2',
  55              'devicename' => 'fakedevice2',
  56          ];
  57          $totpfactor->setup_user_factor((object) $totpdata2);
  58  
  59          $this->assertFalse((bool) $factorinstance->revoked);
  60          $this->assertEquals(2, count($totpfactor->get_active_user_factors($user)));
  61  
  62          // Test that calling the revoke on the generic type revokes all.
  63          $totpfactor->revoke_user_factor();
  64          $this->assertEquals(0, count($totpfactor->get_active_user_factors($user)));
  65  
  66          // Add another factor for testing.
  67          $totpdata3 = [
  68              'secret' => 'fakekey3',
  69              'devicename' => 'fakedevice3',
  70          ];
  71          $factorinstance2 = $totpfactor->setup_user_factor((object) $totpdata3);
  72  
  73          // Now test you can't revoke another users factor.
  74          $user2 = $this->getDataGenerator()->create_user();
  75          $this->setUser($user2);
  76  
  77          $this->assertFalse($totpfactor->revoke_user_factor($factorinstance2->id));
  78          $this->assertEquals(1, count($totpfactor->get_active_user_factors($user)));
  79  
  80          // Now revoke as ourselves.
  81          $this->setUser($user);
  82          $this->assertTrue($totpfactor->revoke_user_factor($factorinstance2->id));
  83          $this->assertEquals(0, count($totpfactor->get_active_user_factors($user)));
  84      }
  85  }