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.
   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 abstract 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\userlist_base;
  31  
  32  /**
  33   * Tests for the \core_privacy API's userlist base functionality.
  34   *
  35   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @coversDefaultClass \core_privacy\local\request\userlist_base
  38   */
  39  class userlist_base_test extends advanced_testcase {
  40      /**
  41       * Ensure that get_userids returns the list of unique userids.
  42       *
  43       * @dataProvider    get_userids_provider
  44       * @param   array   $input List of user IDs
  45       * @param   array   $expected list of userids
  46       * @param   int     $count Expected count
  47       * @covers ::get_userids
  48       */
  49      public function test_get_userids($input, $expected, $count) {
  50          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
  51          $uut->set_userids($input);
  52  
  53          $result = $uut->get_userids();
  54          $this->assertCount($count, $result);
  55  
  56          // Note: Array order is not guaranteed and should not matter.
  57          foreach ($expected as $userid) {
  58              $this->assertNotFalse(array_search($userid, $result));
  59          }
  60      }
  61  
  62      /**
  63       * Provider for the list of userids.
  64       *
  65       * @return array
  66       */
  67      public function get_userids_provider() {
  68          return [
  69              'basic' => [
  70                  [1, 2, 3, 4, 5],
  71                  [1, 2, 3, 4, 5],
  72                  5,
  73              ],
  74              'duplicates' => [
  75                  [1, 1, 2, 2, 3, 4, 5],
  76                  [1, 2, 3, 4, 5],
  77                  5,
  78              ],
  79              'Mixed order with duplicates' => [
  80                  [5, 4, 2, 5, 4, 1, 3, 4, 1, 5, 5, 5, 2, 4, 1, 2],
  81                  [1, 2, 3, 4, 5],
  82                  5,
  83              ],
  84          ];
  85      }
  86  
  87      /**
  88       * Ensure that get_users returns the correct list of users.
  89       *
  90       * @covers ::get_users
  91       */
  92      public function test_get_users() {
  93          $this->resetAfterTest();
  94  
  95          $users = [];
  96          $user = $this->getDataGenerator()->create_user();
  97          $users[$user->id] = $user;
  98  
  99          $user = $this->getDataGenerator()->create_user();
 100          $users[$user->id] = $user;
 101  
 102          $user = $this->getDataGenerator()->create_user();
 103          $users[$user->id] = $user;
 104  
 105          $otheruser = $this->getDataGenerator()->create_user();
 106  
 107          $ids = array_keys($users);
 108  
 109          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 110          $uut->set_userids($ids);
 111  
 112          $result = $uut->get_users();
 113  
 114          sort($users);
 115          sort($result);
 116  
 117          $this->assertCount(3, $result);
 118          $this->assertEquals($users, $result);
 119      }
 120  
 121      /**
 122       * Ensure that the userlist_base is countable.
 123       *
 124       * @dataProvider    get_userids_provider
 125       * @param   array   $input List of user IDs
 126       * @param   array   $expected list of userids
 127       * @param   int     $count Expected count
 128       * @covers ::count
 129       */
 130      public function test_countable($input, $expected, $count) {
 131          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 132          $uut->set_userids($input);
 133  
 134          $this->assertCount($count, $uut);
 135      }
 136  
 137      /**
 138       * Ensure that the userlist_base iterates over the set of users.
 139       *
 140       * @covers ::current
 141       * @covers ::key
 142       * @covers ::next
 143       * @covers ::rewind
 144       * @covers ::valid
 145       */
 146      public function test_user_iteration() {
 147          $this->resetAfterTest();
 148  
 149          $users = [];
 150          $user = $this->getDataGenerator()->create_user();
 151          $users[$user->id] = $user;
 152  
 153          $user = $this->getDataGenerator()->create_user();
 154          $users[$user->id] = $user;
 155  
 156          $user = $this->getDataGenerator()->create_user();
 157          $users[$user->id] = $user;
 158  
 159          $otheruser = $this->getDataGenerator()->create_user();
 160  
 161          $ids = array_keys($users);
 162  
 163          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 164          $uut->set_userids($ids);
 165  
 166          foreach ($uut as $key => $user) {
 167              $this->assertTrue(isset($users[$user->id]));
 168              $this->assertEquals($users[$user->id], $user);
 169          }
 170      }
 171  
 172      /**
 173       * Test that a deleted user is still returned.
 174       * If a user has data then it still must be deleted, even if they are deleted.
 175       *
 176       * @covers ::count
 177       */
 178      public function test_current_user_one_user() {
 179          $this->resetAfterTest();
 180  
 181          $user = $this->getDataGenerator()->create_user();
 182  
 183          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 184          $uut->set_userids([$user->id]);
 185  
 186          $this->assertCount(1, $uut);
 187          $this->assertEquals($user, $uut->current());
 188  
 189          delete_user($user);
 190          $u = $uut->current();
 191          $this->assertEquals($user->id, $u->id);
 192      }
 193  
 194      /**
 195       * Test that an invalid user returns no entry.
 196       *
 197       * @covers ::count
 198       */
 199      public function test_current_user_invalid() {
 200          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 201          $uut->set_userids([-100]);
 202  
 203          $this->assertCount(1, $uut);
 204          $this->assertNull($uut->current());
 205      }
 206  
 207      /**
 208       * Test that where an invalid user is listed, the next user in the list is returned instead.
 209       *
 210       * @covers ::count
 211       */
 212      public function test_current_user_two_users() {
 213          $this->resetAfterTest();
 214  
 215          $u1 = $this->getDataGenerator()->create_user();
 216  
 217          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 218          $uut->set_userids([-100, $u1->id]);
 219  
 220          $this->assertCount(2, $uut);
 221          $this->assertEquals($u1, $uut->current());
 222      }
 223  
 224      /**
 225       * Ensure that the component specified in the constructor is used and available.
 226       *
 227       * @covers ::set_component
 228       */
 229      public function test_set_component_in_constructor() {
 230          $uut = new test_userlist_base(\context_system::instance(), 'core_tests');
 231          $this->assertEquals('core_tests', $uut->get_component());
 232      }
 233  
 234      /**
 235       * Ensure that the context specified in the constructor is available.
 236       *
 237       * @covers ::__construct
 238       */
 239      public function test_set_context_in_constructor() {
 240          $context = \context_user::instance(\core_user::get_user_by_username('admin')->id);
 241  
 242          $uut = new test_userlist_base($context, 'core_tests');
 243          $this->assertEquals($context, $uut->get_context());
 244      }
 245  }
 246  
 247  /**
 248   * A test class extending the userlist_base allowing setting of the userids.
 249   *
 250   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
 251   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 252   */
 253  class test_userlist_base extends userlist_base {
 254      /**
 255       * Set the contextids for the test class.
 256       *
 257       * @param   int[]   $contexids  The list of contextids to use.
 258       */
 259      public function set_userids(array $userids) : userlist_base {
 260          return parent::set_userids($userids);
 261      }
 262  }