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/submissionplugin.php');
  32  require_once($CFG->dirroot . '/mod/assign/submission/comments/locallib.php');
  33  
  34  /**
  35   * Unit tests for the assignment submission 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 submission_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_submission shim.
  60       */
  61      public function test_get_context_for_userid_within_submission() {
  62          $userid = 21;
  63          $contextlist = new \core_privacy\local\request\contextlist();
  64          $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
  65          $mock->expects($this->once())
  66              ->method('get_return_value')
  67              ->with('_get_context_for_userid_within_submission', [$userid, $contextlist]);
  68          test_legacy_polyfill_submission_provider::$mock = $mock;
  69          test_legacy_polyfill_submission_provider::get_context_for_userid_within_submission($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_assignsubmission_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_submission_provider::$mock = $mock;
  84          test_legacy_polyfill_submission_provider::get_student_user_ids($useridlist);
  85      }
  86  
  87      /**
  88       * Test the export_submission_user_data shim.
  89       */
  90      public function test_export_submission_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_submission_comments($assign, 'comment');
  96          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
  97          $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
  98          $mock->expects($this->once())
  99              ->method('get_return_value')
 100              ->with('_export_submission_user_data', [$requestdata]);
 101          test_legacy_polyfill_submission_provider::$mock = $mock;
 102          test_legacy_polyfill_submission_provider::export_submission_user_data($requestdata);
 103      }
 104  
 105      /**
 106       * Test the delete_submission_for_context shim.
 107       */
 108      public function test_delete_submission_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_submission_comments($assign, 'comment');
 114          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 115          $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
 116          $mock->expects($this->once())
 117              ->method('get_return_value')
 118              ->with('_delete_submission_for_context', [$requestdata]);
 119          test_legacy_polyfill_submission_provider::$mock = $mock;
 120          test_legacy_polyfill_submission_provider::delete_submission_for_context($requestdata);
 121      }
 122  
 123      /**
 124       * Test the delete submission for grade shim.
 125       */
 126      public function test_delete_submission_for_userid() {
 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_submission_comments($assign, 'comment');
 132          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 133          $mock = $this->createMock(test_assignsubmission_legacy_polyfill_mock_wrapper::class);
 134          $mock->expects($this->once())
 135              ->method('get_return_value')
 136              ->with('_delete_submission_for_userid', [$requestdata]);
 137          test_legacy_polyfill_submission_provider::$mock = $mock;
 138          test_legacy_polyfill_submission_provider::delete_submission_for_userid($requestdata);
 139      }
 140  }
 141  /**
 142   * Legacy polyfill test class for the assignsubmission_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_submission_provider implements \mod_assign\privacy\assignsubmission_provider {
 148      use \mod_assign\privacy\submission_legacy_polyfill;
 149      /**
 150       * @var test_legacy_polyfill_submission_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_submission(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 it is possible that a student ID will not be returned by
 169       * the sql query in \mod_assign\privacy\provider::find_grader_info() Then you need to provide some sql to retrive those
 170       * student IDs. This is highly likely if you had to fill in get_context_for_userid_within_submission above.
 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       * This method is used to export any user data this sub-plugin has using the assign_plugin_request_data object to get the
 180       * context and userid.
 181       * assign_plugin_request_data contains:
 182       * - context
 183       * - grade object
 184       * - current path (subcontext)
 185       * - user object
 186       *
 187       * @param  assign_plugin_request_data $exportdata Contains data to help export the user information.
 188       */
 189      public static function _export_submission_user_data(\mod_assign\privacy\assign_plugin_request_data $exportdata) {
 190          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 191      }
 192  
 193      /**
 194       * Any call to this method should delete all user data for the context defined in the deletion_criteria.
 195       * assign_plugin_request_data contains:
 196       * - context
 197       * - assign object
 198       *
 199       * @param  assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
 200       */
 201      public static function _delete_submission_for_context(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
 202          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 203      }
 204  
 205      /**
 206       * A call to this method should delete user data (where practicle) from the userid and context.
 207       * assign_plugin_request_data contains:
 208       * - context
 209       * - grade object
 210       * - user object
 211       * - assign object
 212       *
 213       * @param  assign_plugin_request_data $requestdata Data useful for deleting user data.
 214       */
 215      public static function _delete_submission_for_userid(\mod_assign\privacy\assign_plugin_request_data $requestdata) {
 216          static::$mock->get_return_value(__FUNCTION__, func_get_args());
 217      }
 218  }
 219  /**
 220   * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
 221   *
 222   * @copyright   2018 Adrian Greeve <adriangreeve.com>
 223   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 224   */
 225  class test_assignsubmission_legacy_polyfill_mock_wrapper {
 226      /**
 227       * Get the return value for the specified item.
 228       */
 229      public function get_return_value() {
 230      }
 231  }