Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 a the collection of userlists 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_collection;
  31  use \core_privacy\local\request\userlist;
  32  use \core_privacy\local\request\approved_userlist;
  33  
  34  /**
  35   * Tests for the \core_privacy API's userlist collection functionality.
  36   *
  37   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @coversDefaultClass \core_privacy\local\request\userlist_collection
  40   */
  41  class userlist_collection_test extends advanced_testcase {
  42  
  43      /**
  44       * A userlist_collection should support the userlist type.
  45       *
  46       * @covers ::add_userlist
  47       */
  48      public function test_supports_userlist() {
  49          $cut = \context_system::instance();
  50          $uut = new userlist_collection($cut);
  51  
  52          $userlist = new userlist($cut, 'core_privacy');
  53          $uut->add_userlist($userlist);
  54  
  55          $this->assertCount(1, $uut->get_userlists());
  56      }
  57  
  58      /**
  59       * A userlist_collection should support the approved_userlist type.
  60       *
  61       * @covers ::add_userlist
  62       */
  63      public function test_supports_approved_userlist() {
  64          $cut = \context_system::instance();
  65          $uut = new userlist_collection($cut);
  66  
  67          $userlist = new approved_userlist($cut, 'core_privacy', [1, 2, 3]);
  68          $uut->add_userlist($userlist);
  69  
  70          $this->assertCount(1, $uut->get_userlists());
  71      }
  72  
  73      /**
  74       * Ensure that get_userlist_for_component returns the correct userlist.
  75       *
  76       * @covers ::get_userlist_for_component
  77       */
  78      public function test_get_userlist_for_component() {
  79          $cut = \context_system::instance();
  80          $uut = new userlist_collection($cut);
  81  
  82          $privacy = new userlist($cut, 'core_privacy');
  83          $uut->add_userlist($privacy);
  84  
  85          $test = new userlist($cut, 'core_tests');
  86          $uut->add_userlist($test);
  87  
  88          // Note: This uses assertSame rather than assertEquals.
  89          // The former checks the actual object, whilst assertEquals only checks that they look the same.
  90          $this->assertSame($privacy, $uut->get_userlist_for_component('core_privacy'));
  91          $this->assertSame($test, $uut->get_userlist_for_component('core_tests'));
  92      }
  93  
  94      /**
  95       * Ensure that get_userlist_for_component does not die horribly when querying a non-existent component.
  96       *
  97       * @covers ::get_userlist_for_component
  98       */
  99      public function test_get_userlist_for_component_not_found() {
 100          $cut = \context_system::instance();
 101          $uut = new userlist_collection($cut);
 102  
 103          $this->assertNull($uut->get_userlist_for_component('core_tests'));
 104      }
 105  
 106      /**
 107       * Ensure that a duplicate userlist in the collection throws an Exception.
 108       *
 109       * @covers ::add_userlist
 110       */
 111      public function test_duplicate_addition_throws() {
 112          $cut = \context_system::instance();
 113          $uut = new userlist_collection($cut);
 114  
 115          $userlist = new userlist($cut, 'core_privacy');
 116          $uut->add_userlist($userlist);
 117  
 118          $this->expectException('moodle_exception');
 119          $uut->add_userlist($userlist);
 120      }
 121  
 122      /**
 123       * Ensure that the userlist_collection is countable.
 124       *
 125       * @covers ::count
 126       */
 127      public function test_countable() {
 128          $cut = \context_system::instance();
 129          $uut = new userlist_collection($cut);
 130  
 131          $uut->add_userlist(new userlist($cut, 'core_privacy'));
 132          $uut->add_userlist(new userlist($cut, 'core_tests'));
 133  
 134          $this->assertCount(2, $uut);
 135      }
 136  
 137      /**
 138       * Ensure that the userlist_collection iterates over the set of userlists.
 139       *
 140       * @covers ::current
 141       * @covers ::key
 142       * @covers ::next
 143       * @covers ::rewind
 144       * @covers ::valid
 145       */
 146      public function test_iteration() {
 147          $cut = \context_system::instance();
 148          $uut = new userlist_collection($cut);
 149  
 150          $testdata = [];
 151  
 152          $privacy = new userlist($cut, 'core_privacy');
 153          $uut->add_userlist($privacy);
 154          $testdata['core_privacy'] = $privacy;
 155  
 156          $test = new userlist($cut, 'core_tests');
 157          $uut->add_userlist($test);
 158          $testdata['core_tests'] = $test;
 159  
 160          $another = new userlist($cut, 'privacy_another');
 161          $uut->add_userlist($another);
 162          $testdata['privacy_another'] = $another;
 163  
 164          foreach ($uut as $component => $list) {
 165              $this->assertEquals($testdata[$component], $list);
 166          }
 167  
 168          $this->assertCount(3, $uut);
 169      }
 170  
 171      /**
 172       * Test that the context is correctly returned.
 173       *
 174       * @covers ::get_context
 175       */
 176      public function test_get_context() {
 177          $cut = \context_system::instance();
 178          $uut = new userlist_collection($cut);
 179  
 180          $this->assertSame($cut, $uut->get_context());
 181      }
 182  }