Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 for mod_assign.
  19   *
  20   * @package     mod_assign
  21   * @category    test
  22   * @copyright   2018 Adrian Greeve <adriangreeve.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_assign\privacy;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/mod/assign/feedbackplugin.php');
  32  require_once($CFG->dirroot . '/mod/assign/feedback/comments/locallib.php');
  33  
  34  /**
  35   * Unit tests for the assignment feedback subplugins API's privacy legacy_polyfill.
  36   *
  37   * @package     mod_assign
  38   * @category    test
  39   * @copyright   2018 Adrian Greeve <adriangreeve.com>
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class feedback_legacy_polyfill_test extends \advanced_testcase {
  43  
  44      /**
  45       * Convenience function to create an instance of an assignment.
  46       *
  47       * @param array $params Array of parameters to pass to the generator
  48       * @return assign The assign class.
  49       */
  50      protected function create_instance($params = array()) {
  51          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  52          $instance = $generator->create_instance($params);
  53          $cm = get_coursemodule_from_instance('assign', $instance->id);
  54          $context = \context_module::instance($cm->id);
  55          return new \assign($context, $cm, $params['course']);
  56      }
  57  
  58      /**
  59       * Test the get_context_for_userid_within_feedback shim.
  60       */
  61      public function test_get_context_for_userid_within_feedback() {
  62          $userid = 21;
  63          $contextlist = new \core_privacy\local\request\contextlist();
  64          $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
  65          $mock->expects($this->once())
  66              ->method('get_return_value')
  67              ->with('_get_context_for_userid_within_feedback', [$userid, $contextlist]);
  68          test_legacy_polyfill_feedback_provider::$mock = $mock;
  69          test_legacy_polyfill_feedback_provider::get_context_for_userid_within_feedback($userid, $contextlist);
  70      }
  71  
  72      /**
  73       * Test the get_student_user_ids shim.
  74       */
  75      public function test_get_student_user_ids() {
  76          $teacherid = 107;
  77          $assignid = 15;
  78          $useridlist = new \mod_assign\privacy\useridlist($teacherid, $assignid);
  79          $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
  80          $mock->expects($this->once())
  81              ->method('get_return_value')
  82              ->with('_get_student_user_ids', [$useridlist]);
  83          test_legacy_polyfill_feedback_provider::$mock = $mock;
  84          test_legacy_polyfill_feedback_provider::get_student_user_ids($useridlist);
  85      }
  86  
  87      /**
  88       * Test the export_feedback_user_data shim.
  89       */
  90      public function test_export_feedback_user_data() {
  91          $this->resetAfterTest();
  92          $course = $this->getDataGenerator()->create_course();
  93          $assign = $this->create_instance(['course' => $course]);
  94          $context = \context_system::instance();
  95          $subplugin = new \assign_feedback_comments($assign, 'comments');
  96          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
  97          $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
  98          $mock->expects($this->once())
  99              ->method('get_return_value')
 100              ->with('_export_feedback_user_data', [$requestdata]);
 101          test_legacy_polyfill_feedback_provider::$mock = $mock;
 102          test_legacy_polyfill_feedback_provider::export_feedback_user_data($requestdata);
 103      }
 104  
 105      /**
 106       * Test the delete_feedback_for_context shim.
 107       */
 108      public function test_delete_feedback_for_context() {
 109          $this->resetAfterTest();
 110          $course = $this->getDataGenerator()->create_course();
 111          $assign = $this->create_instance(['course' => $course]);
 112          $context = \context_system::instance();
 113          $subplugin = new \assign_feedback_comments($assign, 'comments');
 114          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
 115          $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
 116          $mock->expects($this->once())
 117              ->method('get_return_value')
 118              ->with('_delete_feedback_for_context', [$requestdata]);
 119          test_legacy_polyfill_feedback_provider::$mock = $mock;
 120          test_legacy_polyfill_feedback_provider::delete_feedback_for_context($requestdata);
 121      }
 122  
 123      /**
 124       * Test the delete feedback for grade shim.
 125       */
 126      public function test_delete_feedback_for_grade() {
 127          $this->resetAfterTest();
 128          $course = $this->getDataGenerator()->create_course();
 129          $assign = $this->create_instance(['course' => $course]);
 130          $context = \context_system::instance();
 131          $subplugin = new \assign_feedback_comments($assign, 'comments');
 132          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context,$assign);
 133          $mock = $this->createMock(test_assignfeedback_legacy_polyfill_mock_wrapper::class);
 134          $mock->expects($this->once())
 135              ->method('get_return_value')
 136              ->with('_delete_feedback_for_grade', [$requestdata]);
 137          test_legacy_polyfill_feedback_provider::$mock = $mock;
 138          test_legacy_polyfill_feedback_provider::delete_feedback_for_grade($requestdata);
 139      }
 140  }
 141  /**
 142   * Legacy polyfill test class for the assignfeedback_provider.
 143   *
 144   * @copyright   2018 Adrian Greeve <adriangreeve.com>
 145   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 146   */
 147  class test_legacy_polyfill_feedback_provider implements \mod_assign\privacy\assignfeedback_provider {
 148      use \mod_assign\privacy\feedback_legacy_polyfill;
 149      /**
 150       * @var test_legacy_polyfill_feedback_provider $mock.
 151       */
 152      public static $mock = null;
 153  
 154      /**
 155       * Retrieves the contextids associated with the provided userid for this subplugin.
 156       * NOTE if your subplugin must have an entry in the assign_grade table to work, then this
 157       * method can be empty.
 158       *
 159       * @param  int $userid The user ID to get context IDs for.
 160       * @param  contextlist $contextlist Use add_from_sql with this object to add your context IDs.
 161       */
 162      public static function _get_context_for_userid_within_feedback(int $userid,
 163              \core_privacy\local\request\contextlist $contextlist) {
 164          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 165      }
 166  
 167      /**
 168       * Returns student user ids related to the provided teacher ID. If an entry must be present in the assign_grade table for
 169       * your plugin to work then there is no need to fill in this method. If you filled in get_context_for_userid_within_feedback()
 170       * then you probably have to fill this in as well.
 171       *
 172       * @param  useridlist $useridlist A list of user IDs of students graded by this user.
 173       */
 174      public static function _get_student_user_ids(\mod_assign\privacy\useridlist $useridlist) {
 175          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 176      }
 177  
 178      /**
 179       * Export feedback data with the available grade and userid information provided.
 180       * assign_plugin_request_data contains:
 181       * - context
 182       * - grade object
 183       * - current path (subcontext)
 184       * - user object
 185       *
 186       * @param  assign_plugin_request_data $exportdata Contains data to help export the user information.
 187       */
 188      public static function _export_feedback_user_data(\mod_assign\privacy\assign_plugin_request_data $exportdata) {
 189          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 190      }
 191  
 192      /**
 193       * Any call to this method should delete all user data for the context defined in the deletion_criteria.
 194       * assign_plugin_request_data contains:
 195       * - context
 196       * - assign object
 197       *
 198       * @param  assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
 199       */
 200      public static function _delete_feedback_for_context(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
 201          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 202      }
 203  
 204      /**
 205       * Calling this function should delete all user data associated with this grade.
 206       * assign_plugin_request_data contains:
 207       * - context
 208       * - grade object
 209       * - user object
 210       * - assign object
 211       *
 212       * @param  assign_plugin_request_data $requestdata Data useful for deleting user data.
 213       */
 214      public static function _delete_feedback_for_grade(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
 215          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 216      }
 217  }
 218  /**
 219   * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
 220   *
 221   * @copyright   2018 Adrian Greeve <adriangreeve.com>
 222   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 223   */
 224  class test_assignfeedback_legacy_polyfill_mock_wrapper {
 225      /**
 226       * Get the return value for the specified item.
 227       */
 228      public function get_return_value() {
 229      }
 230  }