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