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.

Differences Between: [Versions 401 and 402] [Versions 401 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 assignfeedback_file.
  19   *
  20   * @package    assignfeedback_file
  21   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace assignfeedback_file\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  30  require_once($CFG->dirroot . '/mod/assign/tests/privacy/provider_test.php');
  31  
  32  use mod_assign\privacy\assign_plugin_request_data;
  33  
  34  /**
  35   * Unit tests for mod/assign/feedback/file/classes/privacy/
  36   *
  37   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class provider_test extends \mod_assign\privacy\provider_test {
  41  
  42      /**
  43       * Convenience function for creating feedback data.
  44       *
  45       * @param  object   $assign         assign object
  46       * @param  stdClass $student        user object
  47       * @param  stdClass $teacher        user object
  48       * @param  string   $submissiontext Submission text
  49       * @param  string   $feedbacktext   Feedback text
  50       * @return array   Feedback plugin object and the grade object.
  51       */
  52      protected function create_feedback($assign, $student, $teacher, $submissiontext, $feedbacktext) {
  53  
  54          $submission = new \stdClass();
  55          $submission->assignment = $assign->get_instance()->id;
  56          $submission->userid = $student->id;
  57          $submission->timecreated = time();
  58          $submission->onlinetext_editor = ['text' => $submissiontext,
  59                                           'format' => FORMAT_MOODLE];
  60  
  61          $this->setUser($student);
  62          $notices = [];
  63          $assign->save_submission($submission, $notices);
  64  
  65          $grade = $assign->get_user_grade($student->id, true);
  66  
  67          $this->setUser($teacher);
  68  
  69          $context = \context_user::instance($teacher->id);
  70  
  71          $draftitemid = file_get_unused_draft_itemid();
  72          file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
  73  
  74          $dummy = array(
  75              'contextid' => $context->id,
  76              'component' => 'user',
  77              'filearea' => 'draft',
  78              'itemid' => $draftitemid,
  79              'filepath' => '/',
  80              'filename' => 'feedback1.txt'
  81          );
  82  
  83          $fs = get_file_storage();
  84          $file = $fs->create_file_from_string($dummy, $feedbacktext);
  85  
  86          // Create formdata.
  87          $data = new \stdClass();
  88          $data->{'files_' . $teacher->id . '_filemanager'} = $draftitemid;
  89  
  90          $plugin = $assign->get_feedback_plugin_by_type('file');
  91          // Save the feedback.
  92          $plugin->save($grade, $data);
  93  
  94          return [$plugin, $grade];
  95      }
  96  
  97      /**
  98       * Quick test to make sure that get_metadata returns something.
  99       */
 100      public function test_get_metadata() {
 101          $collection = new \core_privacy\local\metadata\collection('assignfeedback_file');
 102          $collection = \assignfeedback_file\privacy\provider::get_metadata($collection);
 103          $this->assertNotEmpty($collection);
 104      }
 105  
 106      /**
 107       * Test that feedback comments are exported for a user.
 108       */
 109      public function test_export_feedback_user_data() {
 110          $this->resetAfterTest();
 111          // Create course, assignment, submission, and then a feedback comment.
 112          $course = $this->getDataGenerator()->create_course();
 113          // Student.
 114          $user1 = $this->getDataGenerator()->create_user();
 115          // Teacher.
 116          $user2 = $this->getDataGenerator()->create_user();
 117          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 118          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
 119          $assign = $this->create_instance(['course' => $course]);
 120  
 121          $context = $assign->get_context();
 122  
 123          $feedbacktext = '<p>first comment for this test</p>';
 124          list($plugin, $grade) = $this->create_feedback($assign, $user1, $user2, 'Submission text', $feedbacktext);
 125  
 126          $writer = \core_privacy\local\request\writer::with_context($context);
 127          $this->assertFalse($writer->has_any_data());
 128  
 129          // The student should be able to see the teachers feedback.
 130          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
 131          \assignfeedback_file\privacy\provider::export_feedback_user_data($exportdata);
 132          $feedbackfile = $writer->get_files([get_string('privacy:path', 'assignfeedback_file')])['feedback1.txt'];
 133          // Check that we got a stored file.
 134          $this->assertInstanceOf('stored_file', $feedbackfile);
 135          $this->assertEquals('feedback1.txt', $feedbackfile->get_filename());
 136      }
 137  
 138      /**
 139       * Test that all feedback is deleted for a context.
 140       */
 141      public function test_delete_feedback_for_context() {
 142          $this->resetAfterTest();
 143          // Create course, assignment, submission, and then a feedback comment.
 144          $course = $this->getDataGenerator()->create_course();
 145          // Students.
 146          $user1 = $this->getDataGenerator()->create_user();
 147          $user2 = $this->getDataGenerator()->create_user();
 148          // Teacher.
 149          $user3 = $this->getDataGenerator()->create_user();
 150          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 151          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 152          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 153          $assign = $this->create_instance(['course' => $course]);
 154  
 155          $context = $assign->get_context();
 156  
 157          $feedbacktext = '<p>first comment for this test</p>';
 158          list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 159          $feedbacktext = '<p>Comment for second submission.</p>';
 160          list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 161  
 162          // Check that we have data.
 163          $this->assertFalse($plugin1->is_empty($grade1));
 164          $this->assertFalse($plugin2->is_empty($grade2));
 165  
 166          $requestdata = new assign_plugin_request_data($context, $assign);
 167          \assignfeedback_file\privacy\provider::delete_feedback_for_context($requestdata);
 168  
 169          // Check that we now have no data.
 170          $this->assertTrue($plugin1->is_empty($grade1));
 171          $this->assertTrue($plugin2->is_empty($grade2));
 172      }
 173  
 174      /**
 175       * Test that a grade item is deleted for a user.
 176       */
 177      public function test_delete_feedback_for_grade() {
 178          $this->resetAfterTest();
 179          // Create course, assignment, submission, and then a feedback comment.
 180          $course = $this->getDataGenerator()->create_course();
 181          // Students.
 182          $user1 = $this->getDataGenerator()->create_user();
 183          $user2 = $this->getDataGenerator()->create_user();
 184          // Teacher.
 185          $user3 = $this->getDataGenerator()->create_user();
 186          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 187          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 188          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 189          $assign = $this->create_instance(['course' => $course]);
 190  
 191          $context = $assign->get_context();
 192  
 193          $feedbacktext = '<p>first comment for this test</p>';
 194          list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 195          $feedbacktext = '<p>Comment for second submission.</p>';
 196          list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 197  
 198          // Check that we have data.
 199          $this->assertFalse($plugin1->is_empty($grade1));
 200          $this->assertFalse($plugin2->is_empty($grade2));
 201  
 202          $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
 203          \assignfeedback_file\privacy\provider::delete_feedback_for_grade($requestdata);
 204  
 205          // Check that we now have no data.
 206          $this->assertTrue($plugin1->is_empty($grade1));
 207          // User 2's data should still be intact.
 208          $this->assertFalse($plugin2->is_empty($grade2));
 209      }
 210  
 211      /**
 212       * Test that a grade item is deleted for a user.
 213       */
 214      public function test_delete_feedback_for_grades() {
 215          $this->resetAfterTest();
 216          // Create course, assignment, submission, and then a feedback comment.
 217          $course = $this->getDataGenerator()->create_course();
 218          // Students.
 219          $user1 = $this->getDataGenerator()->create_user();
 220          $user2 = $this->getDataGenerator()->create_user();
 221          $user3 = $this->getDataGenerator()->create_user();
 222          $user4 = $this->getDataGenerator()->create_user();
 223          // Teacher.
 224          $user5 = $this->getDataGenerator()->create_user();
 225          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 226          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 227          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 228          $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 229          $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
 230          $assign1 = $this->create_instance(['course' => $course]);
 231          $assign2 = $this->create_instance(['course' => $course]);
 232  
 233          $context = $assign1->get_context();
 234  
 235          $feedbacktext = '<p>first comment for this test</p>';
 236          list($plugin1, $grade1) = $this->create_feedback($assign1, $user1, $user5, 'Submission text', $feedbacktext);
 237          $feedbacktext = '<p>Comment for second submission.</p>';
 238          list($plugin2, $grade2) = $this->create_feedback($assign1, $user2, $user5, 'Submission text', $feedbacktext);
 239          $feedbacktext = '<p>Comment for second submission.</p>';
 240          list($plugin3, $grade3) = $this->create_feedback($assign1, $user3, $user5, 'Submission text', $feedbacktext);
 241          $feedbacktext = '<p>Comment for second submission.</p>';
 242          list($plugin4, $grade4) = $this->create_feedback($assign2, $user3, $user5, 'Submission text', $feedbacktext);
 243          $feedbacktext = '<p>Comment for second submission.</p>';
 244          list($plugin5, $grade5) = $this->create_feedback($assign2, $user4, $user5, 'Submission text', $feedbacktext);
 245  
 246          // Check that we have data.
 247          $this->assertFalse($plugin1->is_empty($grade1));
 248          $this->assertFalse($plugin2->is_empty($grade2));
 249          $this->assertFalse($plugin3->is_empty($grade3));
 250          $this->assertFalse($plugin4->is_empty($grade4));
 251          $this->assertFalse($plugin5->is_empty($grade5));
 252  
 253          $deletedata = new assign_plugin_request_data($context, $assign1);
 254          $deletedata->set_userids([$user1->id, $user3->id]);
 255          $deletedata->populate_submissions_and_grades();
 256          \assignfeedback_file\privacy\provider::delete_feedback_for_grades($deletedata);
 257  
 258          // Check that we now have no data.
 259          $this->assertTrue($plugin1->is_empty($grade1));
 260          // User 2's data should still be intact.
 261          $this->assertFalse($plugin2->is_empty($grade2));
 262          // User 3's data in assignment 1 should be gone.
 263          $this->assertTrue($plugin3->is_empty($grade3));
 264          // User 3's data in assignment 2 should still be intact.
 265          $this->assertFalse($plugin4->is_empty($grade4));
 266          // User 4's data in assignment 2 should still be intact.
 267          $this->assertFalse($plugin5->is_empty($grade5));
 268      }
 269  }