Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 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   * Data provider tests.
  19   *
  20   * @package    core_auth
  21   * @category   test
  22   * @copyright  2018 Frédéric Massart
  23   * @author     Frédéric Massart <fred@branchup.tech>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  global $CFG;
  29  
  30  use core_privacy\tests\provider_testcase;
  31  use core_privacy\local\request\transform;
  32  use core_privacy\local\request\writer;
  33  use core_auth\privacy\provider;
  34  
  35  /**
  36   * Data provider testcase class.
  37   *
  38   * @package    core_auth
  39   * @category   test
  40   * @copyright  2018 Frédéric Massart
  41   * @author     Frédéric Massart <fred@branchup.tech>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class core_auth_privacy_testcase extends provider_testcase {
  45  
  46      public function setUp() {
  47          $this->resetAfterTest();
  48      }
  49  
  50      public function test_export_user_preferences() {
  51          $dg = $this->getDataGenerator();
  52          $u1 = $dg->create_user();
  53          $u2 = $dg->create_user();
  54          $sysctx = context_system::instance();
  55          $now = time();
  56  
  57          // Check nothing is there.
  58          writer::reset();
  59          provider::export_user_preferences($u1->id);
  60          $prefs = writer::with_context($sysctx)->get_user_preferences('core_auth');
  61          $this->assertEmpty((array) $prefs);
  62  
  63          // Set some preferences.
  64          set_user_preference('auth_forcepasswordchange', 1, $u1);
  65          set_user_preference('create_password', 1, $u1);
  66          set_user_preference('login_failed_count', 18, $u1);
  67          set_user_preference('login_failed_count_since_success', 7, $u1);
  68          set_user_preference('login_failed_last', $now - DAYSECS, $u1);
  69          set_user_preference('login_lockout', $now - HOURSECS, $u1);
  70          set_user_preference('login_lockout_ignored', 0, $u1);
  71          set_user_preference('login_lockout_secret', 'Hello world!', $u1);
  72  
  73          set_user_preference('auth_forcepasswordchange', 0, $u2);
  74          set_user_preference('create_password', 0, $u2);
  75          set_user_preference('login_lockout_ignored', 1, $u2);
  76  
  77          // Check user 1.
  78          writer::reset();
  79          provider::export_user_preferences($u1->id);
  80          $prefs = writer::with_context($sysctx)->get_user_preferences('core_auth');
  81          $this->assertEquals(transform::yesno(true), $prefs->auth_forcepasswordchange->value);
  82          $this->assertEquals(transform::yesno(true), $prefs->create_password->value);
  83          $this->assertEquals(18, $prefs->login_failed_count->value);
  84          $this->assertEquals(7, $prefs->login_failed_count_since_success->value);
  85          $this->assertEquals(transform::datetime($now - DAYSECS), $prefs->login_failed_last->value);
  86          $this->assertEquals(transform::datetime($now - HOURSECS), $prefs->login_lockout->value);
  87          $this->assertEquals(transform::yesno(false), $prefs->login_lockout_ignored->value);
  88          $this->assertEquals('Hello world!', $prefs->login_lockout_secret->value);
  89  
  90          // Check user 2.
  91          writer::reset();
  92          provider::export_user_preferences($u2->id);
  93          $prefs = writer::with_context($sysctx)->get_user_preferences('core_auth');
  94          $this->assertEquals(transform::yesno(false), $prefs->auth_forcepasswordchange->value);
  95          $this->assertEquals(transform::yesno(false), $prefs->create_password->value);
  96          $this->assertObjectNotHasAttribute('login_failed_count', $prefs);
  97          $this->assertObjectNotHasAttribute('login_failed_count_since_success', $prefs);
  98          $this->assertObjectNotHasAttribute('login_failed_last', $prefs);
  99          $this->assertObjectNotHasAttribute('login_lockout', $prefs);
 100          $this->assertEquals(transform::yesno(true), $prefs->login_lockout_ignored->value);
 101          $this->assertObjectNotHasAttribute('login_lockout_secret', $prefs);
 102      }
 103  }