Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  /**
  18   * Unit Tests for the approved userlist Class
  19   *
  20   * @package     core_privacy
  21   * @category    test
  22   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  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  
  30  use \core_privacy\local\request\approved_userlist;
  31  use \core_privacy\local\request\userlist;
  32  
  33  /**
  34   * Tests for the \core_privacy API's approved userlist functionality.
  35   *
  36   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @coversDefaultClass \core_privacy\local\request\approved_userlist
  39   */
  40  class approved_userlist_test extends advanced_testcase {
  41      /**
  42       * The approved userlist should not be modifiable once set.
  43       *
  44       * @covers ::__construct
  45       * @covers \core_privacy\local\request\approved_userlist<extended>
  46       */
  47      public function test_default_values_set() {
  48          $this->resetAfterTest();
  49  
  50          $u1 = $this->getDataGenerator()->create_user();
  51          $u2 = $this->getDataGenerator()->create_user();
  52          $u3 = $this->getDataGenerator()->create_user();
  53          $u4 = $this->getDataGenerator()->create_user();
  54  
  55          $context = \context_system::instance();
  56          $component = 'core_privacy';
  57  
  58          $uut = new approved_userlist($context, $component, [$u1->id, $u2->id]);
  59  
  60          $this->assertEquals($context, $uut->get_context());
  61          $this->assertEquals($component, $uut->get_component());
  62  
  63          $expected = [
  64              $u1->id,
  65              $u2->id,
  66          ];
  67          sort($expected);
  68  
  69          $result = $uut->get_userids();
  70          sort($result);
  71  
  72          $this->assertEquals($expected, $result);
  73      }
  74  
  75      /**
  76       * @covers ::create_from_userlist
  77       * @covers \core_privacy\local\request\approved_userlist<extended>
  78       */
  79      public function test_create_from_userlist() {
  80          $this->resetAfterTest();
  81  
  82          $u1 = $this->getDataGenerator()->create_user();
  83          $u2 = $this->getDataGenerator()->create_user();
  84          $u3 = $this->getDataGenerator()->create_user();
  85          $u4 = $this->getDataGenerator()->create_user();
  86  
  87          $context = \context_system::instance();
  88          $component = 'core_privacy';
  89  
  90          $sourcelist = new userlist($context, $component);
  91          $sourcelist->add_users([$u1->id, $u3->id]);
  92  
  93          $expected = [
  94              $u1->id,
  95              $u3->id,
  96          ];
  97          sort($expected);
  98  
  99          $approvedlist = approved_userlist::create_from_userlist($sourcelist);
 100  
 101          $this->assertEquals($component, $approvedlist->get_component());
 102          $this->assertEquals($context, $approvedlist->get_context());
 103  
 104          $result = $approvedlist->get_userids();
 105          sort($result);
 106          $this->assertEquals($expected, $result);
 107      }
 108  }