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   * Unit tests for the privacy legacy polyfill.
  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\request\contextlist;
  32  use \core_privacy\local\request\approved_contextlist;
  33  
  34  /**
  35   * Tests for the \core_privacy API's types\user_preference functionality.
  36   * Unit tests for the Privacy API's legacy_polyfill.
  37   *
  38   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @coversDefaultClass \core_privacy\local\legacy_polyfill
  41   */
  42  class core_privacy_legacy_polyfill_test extends advanced_testcase {
  43      /**
  44       * Test that the null_provider polyfill works and that the static _get_reason can be
  45       * successfully called.
  46       *
  47       * @covers ::get_reason
  48       */
  49      public function test_null_provider() {
  50          $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
  51      }
  52  
  53      /**
  54       * Test that the metdata\provider polyfill works and that the static _get_metadata can be
  55       * successfully called.
  56       *
  57       * @covers ::get_metadata
  58       */
  59      public function test_metadata_provider() {
  60          $collection = new collection('core_privacy');
  61          $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
  62      }
  63  
  64      /**
  65       * Test that the local\request\user_preference_provider polyfill works and that the static
  66       * _export_user_preferences can be successfully called.
  67       *
  68       * @covers ::export_user_preferences
  69       */
  70      public function test_user_preference_provider() {
  71          $userid = 417;
  72  
  73          $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
  74          $mock->expects($this->once())
  75              ->method('get_return_value')
  76              ->with('_export_user_preferences', [$userid]);
  77  
  78          test_legacy_polyfill_user_preference_provider::$mock = $mock;
  79          test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
  80      }
  81  
  82      /**
  83       * Test that the local\request\core_user_preference_provider polyfill works and that the static
  84       * _get_contexts_for_userid can be successfully called.
  85       *
  86       * @covers ::get_contexts_for_userid
  87       */
  88      public function test_get_contexts_for_userid() {
  89          $userid = 417;
  90          $contextlist = new contextlist('core_privacy');
  91  
  92          $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
  93          $mock->expects($this->once())
  94              ->method('get_return_value')
  95              ->with('_get_contexts_for_userid', [$userid])
  96              ->willReturn($contextlist);
  97  
  98          test_legacy_polyfill_request_provider::$mock = $mock;
  99          $result = test_legacy_polyfill_request_provider::get_contexts_for_userid($userid);
 100          $this->assertSame($contextlist, $result);
 101      }
 102  
 103      /**
 104       * Test that the local\request\core_user_preference_provider polyfill works and that the static
 105       * _export_user_data can be successfully called.
 106       *
 107       * @covers ::export_user_data
 108       */
 109      public function test_export_user_data() {
 110          $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
 111  
 112          $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
 113          $mock->expects($this->once())
 114              ->method('get_return_value')
 115              ->with('_export_user_data', [$contextlist]);
 116  
 117          test_legacy_polyfill_request_provider::$mock = $mock;
 118          test_legacy_polyfill_request_provider::export_user_data($contextlist);
 119      }
 120  
 121      /**
 122       * Test that the local\request\core_user_preference_provider polyfill works and that the static
 123       * _delete_data_for_all_users_in_context can be successfully called.
 124       *
 125       * @covers ::delete_data_for_all_users_in_context
 126       */
 127      public function test_delete_data_for_all_users_in_context() {
 128          $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
 129          $mock->expects($this->once())
 130              ->method('get_return_value')
 131              ->with('_delete_data_for_all_users_in_context', [\context_system::instance()]);
 132  
 133          test_legacy_polyfill_request_provider::$mock = $mock;
 134          test_legacy_polyfill_request_provider::delete_data_for_all_users_in_context(\context_system::instance());
 135      }
 136  
 137      /**
 138       * Test that the local\request\core_user_preference_provider polyfill works and that the static
 139       * _delete_data_for_user can be successfully called.
 140       *
 141       * @covers ::delete_data_for_user
 142       */
 143      public function test_delete_data_for_user() {
 144          $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
 145  
 146          $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
 147          $mock->expects($this->once())
 148              ->method('get_return_value')
 149              ->with('_delete_data_for_user', [$contextlist]);
 150  
 151          test_legacy_polyfill_request_provider::$mock = $mock;
 152          test_legacy_polyfill_request_provider::delete_data_for_user($contextlist);
 153      }
 154  }
 155  
 156  /**
 157   * Legacy polyfill test for the null provider.
 158   *
 159   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
 160   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 161   */
 162  class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
 163  
 164      use \core_privacy\local\legacy_polyfill;
 165  
 166      /**
 167       * Test for get_reason
 168       */
 169      protected static function _get_reason() {
 170          return 'thisisareason';
 171      }
 172  }
 173  
 174  /**
 175   * Legacy polyfill test for the metadata provider.
 176   *
 177   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
 178   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 179   */
 180  class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
 181  
 182      use \core_privacy\local\legacy_polyfill;
 183  
 184      /**
 185       * Test for get_metadata.
 186       *
 187       * @param   collection     $collection The initialised collection to add items to.
 188       * @return  collection     A listing of user data stored through this system.
 189       */
 190      protected static function _get_metadata(collection $collection) {
 191          return $collection;
 192      }
 193  }
 194  
 195  /**
 196   * Legacy polyfill test for the metadata provider.
 197   *
 198   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
 199   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 200   */
 201  class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
 202  
 203      use \core_privacy\local\legacy_polyfill;
 204  
 205      /**
 206       * @var test_legacy_polyfill_request_provider $mock
 207       */
 208      public static $mock = null;
 209  
 210      /**
 211       * Export all user preferences for the plugin.
 212       *
 213       * @param   int         $userid The userid of the user whose data is to be exported.
 214       */
 215      protected static function _export_user_preferences($userid) {
 216          return static::$mock->get_return_value(__FUNCTION__, func_get_args());
 217      }
 218  }
 219  
 220  /**
 221   * Legacy polyfill test for the request provider.
 222   *
 223   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
 224   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 225   */
 226  class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
 227  
 228      use \core_privacy\local\legacy_polyfill;
 229  
 230      /**
 231       * @var test_legacy_polyfill_request_provider $mock
 232       */
 233      public static $mock = null;
 234  
 235      /**
 236       * Test for get_contexts_for_userid.
 237       *
 238       * @param   int         $userid     The user to search.
 239       * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
 240       */
 241      protected static function _get_contexts_for_userid($userid) {
 242          return static::$mock->get_return_value(__FUNCTION__, func_get_args());
 243      }
 244  
 245      /**
 246       * Test for export_user_data.
 247       *
 248       * @param   approved_contextlist    $contextlist    The approved contexts to export information for.
 249       */
 250      protected static function _export_user_data(approved_contextlist $contextlist) {
 251          return static::$mock->get_return_value(__FUNCTION__, func_get_args());
 252      }
 253  
 254  
 255      /**
 256       * Delete all use data which matches the specified deletion criteria.
 257       *
 258       * @param   context         $context   The specific context to delete data for.
 259       */
 260      public static function _delete_data_for_all_users_in_context(\context $context) {
 261          return static::$mock->get_return_value(__FUNCTION__, func_get_args());
 262      }
 263  
 264      /**
 265       * Delete all user data for the specified user, in the specified contexts.
 266       *
 267       * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
 268       */
 269      public static function _delete_data_for_user(approved_contextlist $contextlist) {
 270          return static::$mock->get_return_value(__FUNCTION__, func_get_args());
 271      }
 272  }
 273  
 274  class test_legacy_polyfill_mock_wrapper {
 275      /**
 276       * Get the return value for the specified item.
 277       */
 278      public function get_return_value() {}
 279  }