Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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