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 a the collection of contextlists 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\contextlist_collection;
  31  use \core_privacy\local\request\contextlist;
  32  use \core_privacy\local\request\approved_contextlist;
  33  
  34  /**
  35   * Tests for the \core_privacy API's contextlist 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\contextlist_collection
  40   */
  41  class contextlist_collection_test extends advanced_testcase {
  42      /**
  43       * A contextlist_collection should support the contextlist type.
  44       *
  45       * @covers ::add_contextlist
  46       */
  47      public function test_supports_contextlist() {
  48          $uit = new contextlist_collection(1);
  49          $contextlist = new contextlist();
  50          $contextlist->set_component('core_privacy');
  51          $uit->add_contextlist($contextlist);
  52  
  53          $this->assertCount(1, $uit->get_contextlists());
  54      }
  55  
  56      /**
  57       * A contextlist_collection should support the approved_contextlist type.
  58       *
  59       * @covers ::add_contextlist
  60       */
  61      public function test_supports_approved_contextlist() {
  62          $uit = new contextlist_collection(1);
  63          $testuser = \core_user::get_user_by_username('admin');
  64          $contextids = [3, 2, 1];
  65          $uit->add_contextlist(new approved_contextlist($testuser, 'core_privacy', $contextids));
  66  
  67          $this->assertCount(1, $uit->get_contextlists());
  68      }
  69  
  70      /**
  71       * Ensure that get_contextlist_for_component returns the correct contextlist.
  72       *
  73       * @covers ::get_contextlist_for_component
  74       */
  75      public function test_get_contextlist_for_component() {
  76          $uit = new contextlist_collection(1);
  77          $coretests = new contextlist();
  78          $coretests->set_component('core_tests');
  79          $uit->add_contextlist($coretests);
  80  
  81          $coreprivacy = new contextlist();
  82          $coreprivacy->set_component('core_privacy');
  83          $uit->add_contextlist($coreprivacy);
  84  
  85          // Note: This uses assertSame rather than assertEquals.
  86          // The former checks the actual object, whilst assertEquals only checks that they look the same.
  87          $this->assertSame($coretests, $uit->get_contextlist_for_component('core_tests'));
  88          $this->assertSame($coreprivacy, $uit->get_contextlist_for_component('core_privacy'));
  89      }
  90  
  91      /**
  92       * Ensure that get_contextlist_for_component does not die horribly when querying a non-existent component.
  93       *
  94       * @covers ::get_contextlist_for_component
  95       */
  96      public function test_get_contextlist_for_component_not_found() {
  97          $uit = new contextlist_collection(1);
  98  
  99          $this->assertNull($uit->get_contextlist_for_component('core_tests'));
 100      }
 101  
 102      /**
 103       * Ensure that a duplicate contextlist in the collection throws an Exception.
 104       *
 105       * @covers ::add_contextlist
 106       */
 107      public function test_duplicate_addition_throws() {
 108          $uit = new contextlist_collection(1);
 109  
 110          $coretests = new contextlist();
 111          $coretests->set_component('core_tests');
 112          $uit->add_contextlist($coretests);
 113  
 114          $this->expectException('moodle_exception');
 115          $uit->add_contextlist($coretests);
 116      }
 117  
 118      /**
 119       * Ensure that the contextlist_collection is countable.
 120       *
 121       * @covers ::count
 122       */
 123      public function test_countable() {
 124          $uit = new contextlist_collection(1);
 125  
 126          $contextlist = new contextlist();
 127          $contextlist->set_component('test_example');
 128          $uit->add_contextlist($contextlist);
 129  
 130          $contextlist = new contextlist();
 131          $contextlist->set_component('test_another');
 132          $uit->add_contextlist($contextlist);
 133  
 134          $this->assertCount(2, $uit);
 135      }
 136  
 137      /**
 138       * Ensure that the contextlist_collection iterates over the set of contextlists.
 139       *
 140       * @covers ::current
 141       * @covers ::key
 142       * @covers ::next
 143       * @covers ::rewind
 144       * @covers ::valid
 145       */
 146      public function test_iteration() {
 147          $uit = new contextlist_collection(1);
 148  
 149          $testdata = [];
 150  
 151          $component = 'test_example';
 152          $contextlist = new contextlist();
 153          $contextlist->set_component($component);
 154          $uit->add_contextlist($contextlist);
 155          $testdata[$component] = $contextlist;
 156  
 157          $component = 'test_another';
 158          $contextlist = new contextlist();
 159          $contextlist->set_component($component);
 160          $uit->add_contextlist($contextlist);
 161          $testdata[$component] = $contextlist;
 162  
 163          $component = 'test_third';
 164          $contextlist = new contextlist();
 165          $contextlist->set_component($component);
 166          $uit->add_contextlist($contextlist);
 167          $testdata[$component] = $contextlist;
 168  
 169          foreach ($uit as $component => $list) {
 170              $this->assertEquals($testdata[$component], $list);
 171          }
 172  
 173          $this->assertCount(3, $uit);
 174      }
 175  
 176      /**
 177       * Test that the userid is correctly returned.
 178       *
 179       * @covers ::get_userid
 180       */
 181      public function test_get_userid() {
 182          $uit = new contextlist_collection(1);
 183  
 184          $this->assertEquals(1, $uit->get_userid());
 185      }
 186  
 187      /**
 188       * Test that an exception is thrown if a contextlist does not contain a component.
 189       */
 190      public function test_add_without_component() {
 191          $uit = new contextlist_collection(1);
 192  
 193          $this->expectException(moodle_exception::class);
 194          $uit->add_contextlist(new contextlist());
 195      }
 196  }