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