Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 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   * Unit tests for assignfeedback_comments.
  19   *
  20   * @package    assignfeedback_comments
  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  /**
  32   * Unit tests for mod/assign/feedback/comments/classes/privacy/
  33   *
  34   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class assignfeedback_comments_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
  38  
  39      /**
  40       * Convenience function for creating feedback data.
  41       *
  42       * @param  object   $assign         assign object
  43       * @param  stdClass $student        user object
  44       * @param  stdClass $teacher        user object
  45       * @param  string   $submissiontext Submission text
  46       * @param  string   $feedbacktext   Feedback text
  47       * @return array   Feedback plugin object and the grade object.
  48       */
  49      protected function create_feedback($assign, $student, $teacher, $submissiontext, $feedbacktext) {
  50          global $CFG;
  51  
  52          $submission = new \stdClass();
  53          $submission->assignment = $assign->get_instance()->id;
  54          $submission->userid = $student->id;
  55          $submission->timecreated = time();
  56          $submission->onlinetext_editor = ['text' => $submissiontext,
  57                                           'format' => FORMAT_MOODLE];
  58  
  59          $this->setUser($student);
  60          $notices = [];
  61          $assign->save_submission($submission, $notices);
  62  
  63          $grade = $assign->get_user_grade($student->id, true);
  64  
  65          $this->setUser($teacher);
  66  
  67          $context = context_user::instance($teacher->id);
  68  
  69          $draftitemid = file_get_unused_draft_itemid();
  70          file_prepare_draft_area($draftitemid, $context->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
  71              ASSIGNFEEDBACK_COMMENTS_FILEAREA, $grade->id);
  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          $fs->create_file_from_string($dummy, $feedbacktext);
  84  
  85          $feedbacktext = $feedbacktext .
  86              " <img src='{$CFG->wwwroot}/draftfile.php/{$context->id}/user/draft/{$draftitemid}/feedback1.txt.png>";
  87  
  88          $plugin = $assign->get_feedback_plugin_by_type('comments');
  89          $feedbackdata = new \stdClass();
  90          $feedbackdata->assignfeedbackcomments_editor = [
  91              'text' => $feedbacktext,
  92              'format' => FORMAT_HTML,
  93              'itemid' => $draftitemid
  94          ];
  95  
  96          $plugin->save($grade, $feedbackdata);
  97          return [$plugin, $grade];
  98      }
  99  
 100      /**
 101       * Quick test to make sure that get_metadata returns something.
 102       */
 103      public function test_get_metadata() {
 104          $collection = new \core_privacy\local\metadata\collection('assignfeedback_comments');
 105          $collection = \assignfeedback_comments\privacy\provider::get_metadata($collection);
 106          $this->assertNotEmpty($collection);
 107      }
 108  
 109      /**
 110       * Test that feedback comments are exported for a user.
 111       */
 112      public function test_export_feedback_user_data() {
 113          $this->resetAfterTest();
 114  
 115          // Create course, assignment, submission, and then a feedback comment.
 116          $course = $this->getDataGenerator()->create_course();
 117          // Student.
 118          $user1 = $this->getDataGenerator()->create_user();
 119          // Teacher.
 120          $user2 = $this->getDataGenerator()->create_user();
 121          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 122          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
 123          $assign = $this->create_instance(['course' => $course]);
 124  
 125          $context = $assign->get_context();
 126  
 127          $feedbacktext = '<p>first comment for this test</p>';
 128          list($plugin, $grade) = $this->create_feedback($assign, $user1, $user2, 'Submission text', $feedbacktext);
 129  
 130          $writer = \core_privacy\local\request\writer::with_context($context);
 131          $this->assertFalse($writer->has_any_data());
 132  
 133          // The student should be able to see the teachers feedback.
 134          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
 135          \assignfeedback_comments\privacy\provider::export_feedback_user_data($exportdata);
 136          $this->assertContains($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext);
 137  
 138          $filespath = [];
 139          $filespath[] = 'Feedback comments';
 140          $feedbackfile = $writer->get_files($filespath)['feedback1.txt'];
 141  
 142          $this->assertInstanceOf('stored_file', $feedbackfile);
 143          $this->assertEquals('feedback1.txt', $feedbackfile->get_filename());
 144  
 145          // The teacher should also be able to see the feedback that they provided.
 146          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user2);
 147          \assignfeedback_comments\privacy\provider::export_feedback_user_data($exportdata);
 148          $this->assertContains($feedbacktext, $writer->get_data(['Feedback comments'])->commenttext);
 149  
 150          $feedbackfile = $writer->get_files($filespath)['feedback1.txt'];
 151  
 152          $this->assertInstanceOf('stored_file', $feedbackfile);
 153          $this->assertEquals('feedback1.txt', $feedbackfile->get_filename());
 154      }
 155  
 156      /**
 157       * Test that all feedback is deleted for a context.
 158       */
 159      public function test_delete_feedback_for_context() {
 160          $this->resetAfterTest();
 161          // Create course, assignment, submission, and then a feedback comment.
 162          $course = $this->getDataGenerator()->create_course();
 163          // Student.
 164          $user1 = $this->getDataGenerator()->create_user();
 165          $user2 = $this->getDataGenerator()->create_user();
 166          // Teacher.
 167          $user3 = $this->getDataGenerator()->create_user();
 168          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 169          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 170          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 171          $assign = $this->create_instance(['course' => $course]);
 172  
 173          $context = $assign->get_context();
 174  
 175          $feedbacktext = '<p>first comment for this test</p>';
 176          list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 177          $feedbacktext = '<p>Comment for second student.</p>';
 178          list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 179  
 180          // Check that we have data.
 181          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 182          $this->assertNotEmpty($feedbackcomments);
 183          $feedbackcomments = $plugin1->get_feedback_comments($grade2->id);
 184          $this->assertNotEmpty($feedbackcomments);
 185  
 186          $fs = new file_storage();
 187          $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
 188              ASSIGNFEEDBACK_COMMENTS_FILEAREA);
 189          // 4 including directories.
 190          $this->assertEquals(4, count($files));
 191  
 192          // Delete all comments for this context.
 193          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 194          assignfeedback_comments\privacy\provider::delete_feedback_for_context($requestdata);
 195  
 196          // Check that the data is now gone.
 197          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 198          $this->assertEmpty($feedbackcomments);
 199          $feedbackcomments = $plugin1->get_feedback_comments($grade2->id);
 200          $this->assertEmpty($feedbackcomments);
 201  
 202          $fs = new file_storage();
 203          $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
 204              ASSIGNFEEDBACK_COMMENTS_FILEAREA);
 205          $this->assertEquals(0, count($files));
 206  
 207      }
 208  
 209      /**
 210       * Test that a grade item is deleted for a user.
 211       */
 212      public function test_delete_feedback_for_grade() {
 213          $this->resetAfterTest();
 214          // Create course, assignment, submission, and then a feedback comment.
 215          $course = $this->getDataGenerator()->create_course();
 216          // Student.
 217          $user1 = $this->getDataGenerator()->create_user();
 218          $user2 = $this->getDataGenerator()->create_user();
 219          // Teacher.
 220          $user3 = $this->getDataGenerator()->create_user();
 221          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 222          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 223          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 224          $assign = $this->create_instance(['course' => $course]);
 225  
 226          $context = $assign->get_context();
 227  
 228          $feedbacktext = '<p>first comment for this test</p>';
 229          list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 230          $feedbacktext = '<p>Comment for second student.</p>';
 231          list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 232  
 233          // Check that we have data.
 234          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 235          $this->assertNotEmpty($feedbackcomments);
 236          $feedbackcomments = $plugin1->get_feedback_comments($grade2->id);
 237          $this->assertNotEmpty($feedbackcomments);
 238  
 239          $fs = new file_storage();
 240          $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
 241              ASSIGNFEEDBACK_COMMENTS_FILEAREA);
 242          // 4 including directories.
 243          $this->assertEquals(4, count($files));
 244  
 245          // Delete all comments for this grade object.
 246          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade1, [], $user1);
 247          assignfeedback_comments\privacy\provider::delete_feedback_for_grade($requestdata);
 248  
 249          // These comments should be empty.
 250          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 251          $this->assertEmpty($feedbackcomments);
 252  
 253          // These comments should not.
 254          $feedbackcomments = $plugin1->get_feedback_comments($grade2->id);
 255          $this->assertNotEmpty($feedbackcomments);
 256  
 257          $fs = new file_storage();
 258          $files = $fs->get_area_files($assign->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
 259              ASSIGNFEEDBACK_COMMENTS_FILEAREA);
 260          // 2 files that were not deleted.
 261          $this->assertEquals(2, count($files));
 262  
 263          array_shift($files);
 264          $file = array_shift($files);
 265  
 266          $this->assertInstanceOf('stored_file', $file);
 267          $this->assertEquals('feedback1.txt', $file->get_filename());
 268          $this->assertEquals($grade2->id, $file->get_itemid());
 269      }
 270  
 271      /**
 272       * Test that a grade item is deleted for a user.
 273       */
 274      public function test_delete_feedback_for_grades() {
 275          $this->resetAfterTest();
 276          // Create course, assignment, submission, and then a feedback comment.
 277          $course = $this->getDataGenerator()->create_course();
 278          // Student.
 279          $user1 = $this->getDataGenerator()->create_user();
 280          $user2 = $this->getDataGenerator()->create_user();
 281          $user3 = $this->getDataGenerator()->create_user();
 282          $user4 = $this->getDataGenerator()->create_user();
 283          // Teacher.
 284          $user5 = $this->getDataGenerator()->create_user();
 285          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 286          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 287          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 288          $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 289          $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
 290          $assign1 = $this->create_instance(['course' => $course]);
 291          $assign2 = $this->create_instance(['course' => $course]);
 292  
 293          $feedbacktext = '<p>first comment for this test</p>';
 294          list($plugin1, $grade1) = $this->create_feedback($assign1, $user1, $user5, 'Submission text', $feedbacktext);
 295          $feedbacktext = '<p>Comment for second student.</p>';
 296          list($plugin2, $grade2) = $this->create_feedback($assign1, $user2, $user5, 'Submission text', $feedbacktext);
 297          $feedbacktext = '<p>Comment for third student.</p>';
 298          list($plugin3, $grade3) = $this->create_feedback($assign1, $user3, $user5, 'Submission text', $feedbacktext);
 299          $feedbacktext = '<p>Comment for third student in the second assignment.</p>';
 300          list($plugin4, $grade4) = $this->create_feedback($assign2, $user3, $user5, 'Submission text', $feedbacktext);
 301          $feedbacktext = '<p>Comment for fourth student in the second assignment.</p>';
 302          list($plugin5, $grade5) = $this->create_feedback($assign2, $user4, $user5, 'Submission text', $feedbacktext);
 303  
 304          // Check that we have data.
 305          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 306          $this->assertNotEmpty($feedbackcomments);
 307          $feedbackcomments = $plugin2->get_feedback_comments($grade2->id);
 308          $this->assertNotEmpty($feedbackcomments);
 309          $feedbackcomments = $plugin3->get_feedback_comments($grade3->id);
 310          $this->assertNotEmpty($feedbackcomments);
 311          $feedbackcomments = $plugin4->get_feedback_comments($grade4->id);
 312          $this->assertNotEmpty($feedbackcomments);
 313          $feedbackcomments = $plugin5->get_feedback_comments($grade5->id);
 314          $this->assertNotEmpty($feedbackcomments);
 315  
 316          $fs = new file_storage();
 317          // 6 including directories for assign 1.
 318          // 4 including directories for assign 2.
 319          $this->assertCount(6, $fs->get_area_files($assign1->get_context()->id,
 320                  ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA));
 321          $this->assertCount(4, $fs->get_area_files($assign2->get_context()->id,
 322                  ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA));
 323  
 324          $deletedata = new \mod_assign\privacy\assign_plugin_request_data($assign1->get_context(), $assign1);
 325          $deletedata->set_userids([$user1->id, $user3->id]);
 326          $deletedata->populate_submissions_and_grades();
 327          assignfeedback_comments\privacy\provider::delete_feedback_for_grades($deletedata);
 328  
 329          // Check that grade 1 and grade 3 have been removed.
 330          $feedbackcomments = $plugin1->get_feedback_comments($grade1->id);
 331          $this->assertEmpty($feedbackcomments);
 332          $feedbackcomments = $plugin2->get_feedback_comments($grade2->id);
 333          $this->assertNotEmpty($feedbackcomments);
 334          $feedbackcomments = $plugin3->get_feedback_comments($grade3->id);
 335          $this->assertEmpty($feedbackcomments);
 336          $feedbackcomments = $plugin4->get_feedback_comments($grade4->id);
 337          $this->assertNotEmpty($feedbackcomments);
 338          $feedbackcomments = $plugin5->get_feedback_comments($grade5->id);
 339          $this->assertNotEmpty($feedbackcomments);
 340  
 341          // We have deleted two from assign 1, and none from assign 2.
 342          // 2 including directories for assign 1.
 343          // 4 including directories for assign 2.
 344          $this->assertCount(2, $fs->get_area_files($assign1->get_context()->id,
 345                  ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA));
 346          $this->assertCount(4, $fs->get_area_files($assign2->get_context()->id,
 347                  ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA));
 348      }
 349  }