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 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Collection unit tests.
  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\metadata\collection;
  31  use \core_privacy\local\metadata\types;
  32  
  33  /**
  34   * Tests for the \core_privacy API's collection 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\metadata\collection
  39   */
  40  class core_privacy_metadata_collection extends advanced_testcase {
  41  
  42      /**
  43       * Test that adding an unknown type causes the type to be added to the collection.
  44       *
  45       * @covers ::add_type
  46       */
  47      public function test_add_type_generic_type() {
  48          $collection = new collection('core_privacy');
  49  
  50          // Mock a new types\type.
  51          $mockedtype = $this->createMock(types\type::class);
  52          $collection->add_type($mockedtype);
  53  
  54          $items = $collection->get_collection();
  55          $this->assertCount(1, $items);
  56          $this->assertEquals($mockedtype, reset($items));
  57      }
  58  
  59      /**
  60       * Test that adding a known type works as anticipated.
  61       *
  62       * @covers ::add_type
  63       */
  64      public function test_add_type_known_type() {
  65          $collection = new collection('core_privacy');
  66  
  67          $linked = new types\subsystem_link('example', [], 'langstring');
  68          $collection->add_type($linked);
  69  
  70          $items = $collection->get_collection();
  71          $this->assertCount(1, $items);
  72          $this->assertEquals($linked, reset($items));
  73      }
  74  
  75      /**
  76       * Test that adding multiple types returns them all.
  77       *
  78       * @covers ::add_type
  79       */
  80      public function test_add_type_multiple() {
  81          $collection = new collection('core_privacy');
  82  
  83          $a = new types\subsystem_link('example', [], 'langstring');
  84          $collection->add_type($a);
  85  
  86          $b = new types\subsystem_link('example', [], 'langstring');
  87          $collection->add_type($b);
  88  
  89          $items = $collection->get_collection();
  90          $this->assertCount(2, $items);
  91      }
  92  
  93      /**
  94       * Test that the add_database_table function adds a database table.
  95       *
  96       * @covers ::add_database_table
  97       */
  98      public function test_add_database_table() {
  99          $collection = new collection('core_privacy');
 100  
 101          $name = 'example';
 102          $fields = ['field' => 'description'];
 103          $summary = 'summarisation';
 104  
 105          $collection->add_database_table($name, $fields, $summary);
 106  
 107          $items = $collection->get_collection();
 108          $this->assertCount(1, $items);
 109          $item = reset($items);
 110          $this->assertInstanceOf(types\database_table::class, $item);
 111          $this->assertEquals($name, $item->get_name());
 112          $this->assertEquals($fields, $item->get_privacy_fields());
 113          $this->assertEquals($summary, $item->get_summary());
 114      }
 115  
 116      /**
 117       * Test that the add_user_preference function adds a single user preference.
 118       *
 119       * @covers ::add_user_preference
 120       */
 121      public function test_add_user_preference() {
 122          $collection = new collection('core_privacy');
 123  
 124          $name = 'example';
 125          $summary = 'summarisation';
 126  
 127          $collection->add_user_preference($name, $summary);
 128  
 129          $items = $collection->get_collection();
 130          $this->assertCount(1, $items);
 131          $item = reset($items);
 132          $this->assertInstanceOf(types\user_preference::class, $item);
 133          $this->assertEquals($name, $item->get_name());
 134          $this->assertEquals($summary, $item->get_summary());
 135      }
 136  
 137      /**
 138       * Test that the link_external_location function links an external location.
 139       *
 140       * @covers ::link_external_location
 141       */
 142      public function test_link_external_location() {
 143          $collection = new collection('core_privacy');
 144  
 145          $name = 'example';
 146          $fields = ['field' => 'description'];
 147          $summary = 'summarisation';
 148  
 149          $collection->link_external_location($name, $fields, $summary);
 150  
 151          $items = $collection->get_collection();
 152          $this->assertCount(1, $items);
 153          $item = reset($items);
 154          $this->assertInstanceOf(types\external_location::class, $item);
 155          $this->assertEquals($name, $item->get_name());
 156          $this->assertEquals($fields, $item->get_privacy_fields());
 157          $this->assertEquals($summary, $item->get_summary());
 158      }
 159  
 160      /**
 161       * Test that the link_subsystem function links the subsystem.
 162       *
 163       * @covers ::link_subsystem
 164       */
 165      public function test_link_subsystem() {
 166          $collection = new collection('core_privacy');
 167  
 168          $name = 'example';
 169          $summary = 'summarisation';
 170  
 171          $collection->link_subsystem($name, $summary);
 172  
 173          $items = $collection->get_collection();
 174          $this->assertCount(1, $items);
 175          $item = reset($items);
 176          $this->assertInstanceOf(types\subsystem_link::class, $item);
 177          $this->assertEquals($name, $item->get_name());
 178          $this->assertEquals($summary, $item->get_summary());
 179      }
 180  
 181      /**
 182       * Test that the link_plugintype function links the plugin.
 183       *
 184       * @covers ::link_plugintype
 185       */
 186      public function test_link_plugintype() {
 187          $collection = new collection('core_privacy');
 188  
 189          $name = 'example';
 190          $summary = 'summarisation';
 191  
 192          $collection->link_plugintype($name, $summary);
 193  
 194          $items = $collection->get_collection();
 195          $this->assertCount(1, $items);
 196          $item = reset($items);
 197          $this->assertInstanceOf(types\plugintype_link::class, $item);
 198          $this->assertEquals($name, $item->get_name());
 199          $this->assertEquals($summary, $item->get_summary());
 200      }
 201  
 202      /**
 203       * Data provider to supply a list of valid components.
 204       *
 205       * @return  array
 206       */
 207      public function component_list_provider() {
 208          return [
 209              ['core_privacy'],
 210              ['mod_forum'],
 211          ];
 212      }
 213  
 214      /**
 215       * Test that we can get the component correctly.
 216       *
 217       * The component will be used for string translations.
 218       *
 219       * @dataProvider component_list_provider
 220       * @param   string  $component The component to test
 221       * @covers ::get_component
 222       */
 223      public function test_get_component($component) {
 224          $collection = new collection($component);
 225  
 226          $this->assertEquals($component, $collection->get_component());
 227      }
 228  }