Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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