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.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

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