Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Testcase for providers implementing parts of the core_privacy subsystem.
  19   *
  20   * @package    core_privacy
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_privacy\tests;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  /**
  32   * Testcase for providers implementing parts of the core_privacy subsystem.
  33   *
  34   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  abstract class provider_testcase extends \advanced_testcase {
  38  
  39      /**
  40       * Test tearDown.
  41       */
  42      public function tearDown(): void {
  43          \core_privacy\local\request\writer::reset();
  44      }
  45  
  46      /**
  47       * Export all data for a component for the specified user.
  48       *
  49       * @param   int         $userid     The userid of the user to fetch.
  50       * @param   string      $component  The component to get context data for.
  51       * @return  \core_privacy\local\request\contextlist
  52       */
  53      public function get_contexts_for_userid(int $userid, string $component) {
  54          $classname = $this->get_provider_classname($component);
  55  
  56          return $classname::get_contexts_for_userid($userid);
  57      }
  58  
  59      /**
  60       * Export all data for a component for the specified user.
  61       *
  62       * @param   int         $userid     The userid of the user to fetch.
  63       * @param   string      $component  The component to get export data for.
  64       */
  65      public function export_all_data_for_user(int $userid, string $component) {
  66          $contextlist = $this->get_contexts_for_userid($userid, $component);
  67  
  68          $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist(
  69              \core_user::get_user($userid),
  70              $component,
  71              $contextlist->get_contextids()
  72          );
  73  
  74          $classname = $this->get_provider_classname($component);
  75          $classname::export_user_data($approvedcontextlist);
  76      }
  77  
  78      /**
  79       * Export all daa within a context for a component for the specified user.
  80       *
  81       * @param   int         $userid     The userid of the user to fetch.
  82       * @param   \context    $context    The context to export data for.
  83       * @param   string      $component  The component to get export data for.
  84       */
  85      public function export_context_data_for_user(int $userid, \context $context, string $component) {
  86          $contextlist = new \core_privacy\tests\request\approved_contextlist(
  87              \core_user::get_user($userid),
  88              $component,
  89              [$context->id]
  90          );
  91  
  92          $classname = $this->get_provider_classname($component);
  93          $classname::export_user_data($contextlist);
  94      }
  95  
  96      /**
  97       * Determine the classname and ensure that it is a provider.
  98       *
  99       * @param   string      $component      The classname.
 100       * @return  string
 101       */
 102      protected function get_provider_classname($component) {
 103          $classname = "\\$component}\\privacy\\provider";
 104  
 105          if (!class_exists($classname)) {
 106              throw new \coding_exception("{$component} does not implement any provider");
 107          }
 108  
 109          $rc = new \ReflectionClass($classname);
 110          if (!$rc->implementsInterface(\core_privacy\local\metadata\provider::class)) {
 111              throw new \coding_exception("{$component} does not implement metadata provider");
 112          }
 113  
 114          if (!$rc->implementsInterface(\core_privacy\local\request\core_user_data_provider::class)) {
 115              throw new \coding_exception("{$component} does not declare that it provides any user data");
 116          }
 117  
 118          return $classname;
 119      }
 120  }