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.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * Auth external functions tests.
  19   *
  20   * @package    core_auth
  21   * @category   external
  22   * @copyright  2016 Juan Leyva
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.2
  25   */
  26  
  27  namespace core_auth\external;
  28  
  29  use auth_email_external;
  30  use core_auth_external;
  31  use core_external\external_api;
  32  use externallib_advanced_testcase;
  33  
  34  defined('MOODLE_INTERNAL') || die();
  35  
  36  global $CFG;
  37  
  38  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  39  
  40  /**
  41   * External auth API tests.
  42   *
  43   * @package     core_auth
  44   * @copyright   2016 Juan Leyva
  45   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   * @since       Moodle 3.2
  47   */
  48  class external_test extends externallib_advanced_testcase {
  49  
  50      /** @var string Original error log */
  51      protected $oldlog;
  52  
  53      /**
  54       * Set up for every test
  55       */
  56      public function setUp(): void {
  57          global $CFG;
  58  
  59          $this->resetAfterTest(true);
  60          $CFG->registerauth = 'email';
  61  
  62          // Discard error logs.
  63          $this->oldlog = ini_get('error_log');
  64          ini_set('error_log', "$CFG->dataroot/testlog.log");
  65      }
  66  
  67      /**
  68       * Tear down to restore old logging..
  69       */
  70      protected function tearDown(): void {
  71          ini_set('error_log', $this->oldlog);
  72          parent::tearDown();
  73      }
  74  
  75      /**
  76       * Test confirm_user
  77       */
  78      public function test_confirm_user() {
  79          global $DB;
  80  
  81          $username = 'pepe';
  82          $password = 'abcdefAª.ªª!!3';
  83          $firstname = 'Pepe';
  84          $lastname = 'Pérez';
  85          $email = 'myemail@no.zbc';
  86  
  87          // Create new user.
  88          $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
  89          $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
  90          $this->assertTrue($result['success']);
  91          $this->assertEmpty($result['warnings']);
  92          $secret = $DB->get_field('user', 'secret', array('username' => $username));
  93  
  94          // Confirm the user.
  95          $result = core_auth_external::confirm_user($username, $secret);
  96          $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
  97          $this->assertTrue($result['success']);
  98          $this->assertEmpty($result['warnings']);
  99          $confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
 100          $this->assertEquals(1, $confirmed);
 101  
 102          // Try to confirm the user again.
 103          $result = core_auth_external::confirm_user($username, $secret);
 104          $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
 105          $this->assertFalse($result['success']);
 106          $this->assertCount(1, $result['warnings']);
 107          $this->assertEquals('alreadyconfirmed', $result['warnings'][0]['warningcode']);
 108  
 109          // Try to use an invalid secret.
 110          $this->expectException('\moodle_exception');
 111          $this->expectExceptionMessage(get_string('invalidconfirmdata', 'error'));
 112          $result = core_auth_external::confirm_user($username, 'zzZZzz');
 113      }
 114  
 115      /**
 116       * Test age digital consent not enabled.
 117       */
 118      public function test_age_digital_consent_verification_is_not_enabled() {
 119          global $CFG;
 120  
 121          $CFG->agedigitalconsentverification = 0;
 122          $result = core_auth_external::is_age_digital_consent_verification_enabled();
 123          $result = external_api::clean_returnvalue(
 124              core_auth_external::is_age_digital_consent_verification_enabled_returns(), $result);
 125          $this->assertFalse($result['status']);
 126      }
 127  
 128      /**
 129       * Test age digital consent is enabled.
 130       */
 131      public function test_age_digital_consent_verification_is_enabled() {
 132          global $CFG;
 133  
 134          $CFG->agedigitalconsentverification = 1;
 135          $result = core_auth_external::is_age_digital_consent_verification_enabled();
 136          $result = external_api::clean_returnvalue(
 137              core_auth_external::is_age_digital_consent_verification_enabled_returns(), $result);
 138          $this->assertTrue($result['status']);
 139      }
 140  
 141      /**
 142       * Test resend_confirmation_email.
 143       */
 144      public function test_resend_confirmation_email() {
 145          global $DB;
 146  
 147          $username = 'pepe';
 148          $password = 'abcdefAª.ªª!!3';
 149          $firstname = 'Pepe';
 150          $lastname = 'Pérez';
 151          $email = 'myemail@no.zbc';
 152  
 153          // Create new user.
 154          $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
 155          $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
 156          $this->assertTrue($result['success']);
 157          $this->assertEmpty($result['warnings']);
 158  
 159          $result = core_auth_external::resend_confirmation_email($username, $password);
 160          $result = external_api::clean_returnvalue(core_auth_external::resend_confirmation_email_returns(), $result);
 161          $this->assertTrue($result['status']);
 162          $this->assertEmpty($result['warnings']);
 163          $confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
 164          $this->assertEquals(0, $confirmed);
 165      }
 166  
 167      /**
 168       * Test resend_confirmation_email invalid username.
 169       */
 170      public function test_resend_confirmation_email_invalid_username() {
 171  
 172          $username = 'pepe';
 173          $password = 'abcdefAª.ªª!!3';
 174          $firstname = 'Pepe';
 175          $lastname = 'Pérez';
 176          $email = 'myemail@no.zbc';
 177  
 178          // Create new user.
 179          $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
 180          $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
 181          $this->assertTrue($result['success']);
 182          $this->assertEmpty($result['warnings']);
 183  
 184          $_SERVER['HTTP_USER_AGENT'] = 'no browser'; // Hack around missing user agent in CLI scripts.
 185          $this->expectException('\moodle_exception');
 186          $this->expectExceptionMessage('error/invalidlogin');
 187          $result = core_auth_external::resend_confirmation_email('abc', $password);
 188      }
 189  
 190      /**
 191       * Test resend_confirmation_email invalid password.
 192       */
 193      public function test_resend_confirmation_email_invalid_password() {
 194  
 195          $username = 'pepe';
 196          $password = 'abcdefAª.ªª!!3';
 197          $firstname = 'Pepe';
 198          $lastname = 'Pérez';
 199          $email = 'myemail@no.zbc';
 200  
 201          // Create new user.
 202          $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
 203          $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
 204          $this->assertTrue($result['success']);
 205          $this->assertEmpty($result['warnings']);
 206  
 207          $_SERVER['HTTP_USER_AGENT'] = 'no browser'; // Hack around missing user agent in CLI scripts.
 208          $this->expectException('\moodle_exception');
 209          $this->expectExceptionMessage('error/invalidlogin');
 210          $result = core_auth_external::resend_confirmation_email($username, 'abc');
 211      }
 212  
 213      /**
 214       * Test resend_confirmation_email already confirmed user.
 215       */
 216      public function test_resend_confirmation_email_already_confirmed_user() {
 217          global $DB;
 218  
 219          $username = 'pepe';
 220          $password = 'abcdefAª.ªª!!3';
 221          $firstname = 'Pepe';
 222          $lastname = 'Pérez';
 223          $email = 'myemail@no.zbc';
 224  
 225          // Create new user.
 226          $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
 227          $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
 228          $this->assertTrue($result['success']);
 229          $this->assertEmpty($result['warnings']);
 230          $secret = $DB->get_field('user', 'secret', array('username' => $username));
 231  
 232          // Confirm the user.
 233          $result = core_auth_external::confirm_user($username, $secret);
 234          $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
 235          $this->assertTrue($result['success']);
 236  
 237          $this->expectException('\moodle_exception');
 238          $this->expectExceptionMessage('error/alreadyconfirmed');
 239          core_auth_external::resend_confirmation_email($username, $password);
 240      }
 241  }