Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Manual authentication tests.
  19   *
  20   * @package    auth_manual
  21   * @category   test
  22   * @copyright  2014 Gilles-Philippe Leblanc <gilles-philippe.leblanc@umontreal.ca>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot.'/auth/manual/auth.php');
  30  
  31  /**
  32   * Manual authentication tests class.
  33   *
  34   * @package    auth_manual
  35   * @category   test
  36   * @copyright  2014 Gilles-Philippe Leblanc <gilles-philippe.leblanc@umontreal.ca>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class auth_manual_testcase extends advanced_testcase {
  40  
  41      /** @var auth_plugin_manual Keeps the authentication plugin. */
  42      protected $authplugin;
  43  
  44      /**
  45       * Setup test data.
  46       */
  47      protected function setUp(): void {
  48          $this->resetAfterTest(true);
  49          $this->authplugin = new auth_plugin_manual();
  50          set_config('expiration', '1', 'auth_manual');
  51          set_config('expiration_warning', '2', 'auth_manual');
  52          set_config('expirationtime', '30', 'auth_manual');
  53          $this->authplugin->config = get_config(auth_plugin_manual::COMPONENT_NAME);
  54      }
  55  
  56      /**
  57       * Test user_update_password method.
  58       */
  59      public function test_user_update_password() {
  60          $user = $this->getDataGenerator()->create_user();
  61          $expectedtime = time();
  62          $passwordisupdated = $this->authplugin->user_update_password($user, 'MyNewPassword*');
  63  
  64          // Assert that the actual time should be equal or a little greater than the expected time.
  65          $this->assertGreaterThanOrEqual($expectedtime, get_user_preferences('auth_manual_passwordupdatetime', 0, $user->id));
  66  
  67          // Assert that the password was successfully updated.
  68          $this->assertTrue($passwordisupdated);
  69      }
  70  
  71      /**
  72       * Test test_password_expire method.
  73       */
  74      public function test_password_expire() {
  75          $userrecord = array();
  76          $expirationtime = 31 * DAYSECS;
  77          $userrecord['timecreated'] = time() - $expirationtime;
  78          $user1 = $this->getDataGenerator()->create_user($userrecord);
  79          $user2 = $this->getDataGenerator()->create_user();
  80  
  81          // The user 1 was created 31 days ago and has not changed his password yet, so the password has expirated.
  82          $this->assertLessThanOrEqual(-1, $this->authplugin->password_expire($user1->username));
  83  
  84          // The user 2 just came to be created and has not changed his password yet, so the password has not expirated.
  85          $this->assertEquals(30, $this->authplugin->password_expire($user2->username));
  86  
  87          $this->authplugin->user_update_password($user1, 'MyNewPassword*');
  88  
  89          // The user 1 just updated his password so the password has not expirated.
  90          $this->assertEquals(30, $this->authplugin->password_expire($user1->username));
  91      }
  92  
  93  }