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.

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 assignsubmission_comments.
  19   *
  20   * @package    assignsubmission_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/tests/privacy_test.php');
  29  
  30  /**
  31   * Unit tests for mod/assign/submission/comments/classes/privacy/
  32   *
  33   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class assignsubmission_comments_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
  37  
  38      /**
  39       * Convenience function for creating feedback data.
  40       *
  41       * @param  object   $assign         assign object
  42       * @param  stdClass $student        user object
  43       * @param  string   $submissiontext Submission text
  44       * @return array   Submission plugin object and the submission object and the comment object.
  45       */
  46      protected function create_comment_submission($assign, $student, $submissiontext) {
  47  
  48          $submission = $assign->get_user_submission($student->id, true);
  49  
  50          $plugin = $assign->get_submission_plugin_by_type('comments');
  51  
  52          $context = $assign->get_context();
  53          $options = new stdClass();
  54          $options->area = 'submission_comments';
  55          $options->course = $assign->get_course();
  56          $options->context = $context;
  57          $options->itemid = $submission->id;
  58          $options->component = 'assignsubmission_comments';
  59          $options->showcount = true;
  60          $options->displaycancel = true;
  61  
  62          $comment = new comment($options);
  63          $comment->set_post_permission(true);
  64  
  65          $this->setUser($student);
  66  
  67          $comment->add($submissiontext);
  68  
  69          return [$plugin, $submission, $comment];
  70      }
  71  
  72      /**
  73       * Quick test to make sure that get_metadata returns something.
  74       */
  75      public function test_get_metadata() {
  76          $collection = new \core_privacy\local\metadata\collection('assignsubmission_comments');
  77          $collection = \assignsubmission_comments\privacy\provider::get_metadata($collection);
  78          $this->assertNotEmpty($collection);
  79      }
  80  
  81      /**
  82       * Test returning the context for a user who has made a comment in an assignment.
  83       */
  84      public function test_get_context_for_userid_within_submission() {
  85          $this->resetAfterTest();
  86          // Create course, assignment, submission, and then a feedback comment.
  87          $course = $this->getDataGenerator()->create_course();
  88          // Student.
  89          $user1 = $this->getDataGenerator()->create_user();
  90          // Teacher.
  91          $user2 = $this->getDataGenerator()->create_user();
  92          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  93          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
  94          $assign = $this->create_instance(['course' => $course]);
  95  
  96          $context = $assign->get_context();
  97  
  98          $studentcomment = 'Comment from user 1';
  99          list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
 100          $teachercomment = 'From the teacher';
 101          $this->setUser($user2);
 102          $comment->add($teachercomment);
 103  
 104          $contextlist = new \core_privacy\local\request\contextlist();
 105          \assignsubmission_comments\privacy\provider::get_context_for_userid_within_submission($user2->id, $contextlist);
 106          $this->assertEquals($context->id, $contextlist->get_contextids()[0]);
 107      }
 108  
 109      /**
 110       * Test returning student ids given a user ID.
 111       */
 112      public function test_get_student_user_ids() {
 113          $this->resetAfterTest();
 114          // Create course, assignment, submission, and then a feedback comment.
 115          $course = $this->getDataGenerator()->create_course();
 116          // Student.
 117          $user1 = $this->getDataGenerator()->create_user();
 118          // Teacher.
 119          $user2 = $this->getDataGenerator()->create_user();
 120          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 121          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
 122          $assign = $this->create_instance(['course' => $course]);
 123  
 124          $context = $assign->get_context();
 125  
 126          $studentcomment = 'Comment from user 1';
 127          list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
 128          $teachercomment = 'From the teacher';
 129          $this->setUser($user2);
 130          $comment->add($teachercomment);
 131  
 132          $useridlist = new mod_assign\privacy\useridlist($user2->id, $assign->get_instance()->id);
 133          \assignsubmission_comments\privacy\provider::get_student_user_ids($useridlist);
 134          $this->assertEquals($user1->id, $useridlist->get_userids()[0]->id);
 135      }
 136  
 137      /**
 138       * Test returning users related to a given context.
 139       */
 140      public function test_get_userids_from_context() {
 141          // Get a bunch of users making comments.
 142          // Some in one context some in another.
 143          $this->resetAfterTest();
 144          $course = $this->getDataGenerator()->create_course();
 145          // Only in first context.
 146          $user1 = $this->getDataGenerator()->create_user();
 147          $user2 = $this->getDataGenerator()->create_user();
 148          // First and second context.
 149          $user3 = $this->getDataGenerator()->create_user();
 150          // Second context only.
 151          $user4 = $this->getDataGenerator()->create_user();
 152          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 153          $assign1 = $this->create_instance(['course' => $course]);
 154          $assign2 = $this->create_instance(['course' => $course]);
 155  
 156          $assigncontext1 = $assign1->get_context();
 157          $assigncontext2 = $assign2->get_context();
 158  
 159          $user1comment = 'Comment from user 1';
 160          list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
 161          $user2comment = 'From user 2';
 162          $this->setUser($user2);
 163          $comment->add($user2comment);
 164          $user3comment = 'User 3 comment';
 165          $this->setUser($user3);
 166          $comment->add($user3comment);
 167          $user4comment = 'Comment from user 4';
 168          list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
 169          $user3secondcomment = 'Comment on user 4 post.';
 170          $this->setUser($user3);
 171          $comment->add($user3comment);
 172  
 173          $userlist = new \core_privacy\local\request\userlist($assigncontext1, 'assignsubmission_comments');
 174          \assignsubmission_comments\privacy\provider::get_userids_from_context($userlist);
 175          $userids = $userlist->get_userids();
 176          $this->assertCount(3, $userids);
 177          // User 1,2 and 3 are the expected ones in the array. User 4 isn't.
 178          $this->assertContains($user1->id, $userids);
 179          $this->assertContains($user2->id, $userids);
 180          $this->assertContains($user3->id, $userids);
 181          $this->assertNotContains($user4->id, $userids);
 182      }
 183  
 184      /**
 185       * Test that comments are exported for a user.
 186       */
 187      public function test_export_submission_user_data() {
 188          $this->resetAfterTest();
 189          // Create course, assignment, submission, and then a feedback comment.
 190          $course = $this->getDataGenerator()->create_course();
 191          // Student.
 192          $user1 = $this->getDataGenerator()->create_user();
 193          // Teacher.
 194          $user2 = $this->getDataGenerator()->create_user();
 195          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 196          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
 197          $assign = $this->create_instance(['course' => $course]);
 198  
 199          $context = $assign->get_context();
 200  
 201          $studentcomment = 'Comment from user 1';
 202          list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
 203          $teachercomment = 'From the teacher';
 204          $this->setUser($user2);
 205          $comment->add($teachercomment);
 206  
 207          $writer = \core_privacy\local\request\writer::with_context($context);
 208          $this->assertFalse($writer->has_any_data());
 209  
 210          // The student should be able to see the teachers feedback.
 211          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission);
 212          \assignsubmission_comments\privacy\provider::export_submission_user_data($exportdata);
 213          $exportedcomments = $writer->get_data(['Comments']);
 214  
 215          // Can't rely on these comments coming out in order.
 216          if ($exportedcomments->comments[0]->userid == $user1->id) {
 217              $exportedstudentcomment = $exportedcomments->comments[0]->content;
 218              $exportedteachercomment = $exportedcomments->comments[1]->content;
 219          } else {
 220              $exportedstudentcomment = $exportedcomments->comments[1]->content;
 221              $exportedteachercomment = $exportedcomments->comments[0]->content;
 222          }
 223          $this->assertCount(2, $exportedcomments->comments);
 224          $this->assertStringContainsString($studentcomment, $exportedstudentcomment);
 225          $this->assertStringContainsString($teachercomment, $exportedteachercomment);
 226      }
 227  
 228      /**
 229       * Test that all comments are deleted for this context.
 230       */
 231      public function test_delete_submission_for_context() {
 232          global $DB;
 233          $this->resetAfterTest();
 234  
 235          // Create course, assignment, submission, and then a feedback comment.
 236          $course = $this->getDataGenerator()->create_course();
 237          // Student.
 238          $user1 = $this->getDataGenerator()->create_user();
 239          $user2 = $this->getDataGenerator()->create_user();
 240          // Teacher.
 241          $user3 = $this->getDataGenerator()->create_user();
 242          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 243          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 244          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 245          $assign = $this->create_instance(['course' => $course]);
 246  
 247          $context = $assign->get_context();
 248  
 249          $studentcomment = 'Comment from user 1';
 250          list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
 251          $studentcomment = 'Comment from user 2';
 252          list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
 253          $teachercomment1 = 'From the teacher';
 254          $teachercomment2 = 'From the teacher for second student.';
 255          $this->setUser($user3);
 256          $comment->add($teachercomment1);
 257          $comment2->add($teachercomment2);
 258  
 259          // Only need the context in this plugin for this operation.
 260          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 261          \assignsubmission_comments\privacy\provider::delete_submission_for_context($requestdata);
 262  
 263          $results = $DB->get_records('comments', ['contextid' => $context->id]);
 264          $this->assertEmpty($results);
 265      }
 266  
 267      /**
 268       * Test that the comments for a user are deleted.
 269       */
 270      public function test_delete_submission_for_userid() {
 271          global $DB;
 272          $this->resetAfterTest();
 273          // Create course, assignment, submission, and then a feedback comment.
 274          $course = $this->getDataGenerator()->create_course();
 275          // Student.
 276          $user1 = $this->getDataGenerator()->create_user();
 277          $user2 = $this->getDataGenerator()->create_user();
 278          // Teacher.
 279          $user3 = $this->getDataGenerator()->create_user();
 280          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 281          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 282          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 283          $assign = $this->create_instance(['course' => $course]);
 284  
 285          $context = $assign->get_context();
 286  
 287          $studentcomment = 'Comment from user 1';
 288          list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
 289          $studentcomment = 'Comment from user 2';
 290          list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
 291          $teachercomment1 = 'From the teacher';
 292          $teachercomment2 = 'From the teacher for second student.';
 293          $this->setUser($user3);
 294          $comment->add($teachercomment1);
 295          $comment2->add($teachercomment2);
 296  
 297          // Provide full details to delete the comments.
 298          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, null, [], $user1);
 299          \assignsubmission_comments\privacy\provider::delete_submission_for_userid($requestdata);
 300  
 301          $results = $DB->get_records('comments', ['contextid' => $context->id]);
 302          // We are only deleting the comments for user1 (one comment) so we should have three left.
 303          $this->assertCount(3, $results);
 304          foreach ($results as $result) {
 305              // Check that none of the comments are from user1.
 306              $this->assertNotEquals($user1->id, $result->userid);
 307          }
 308      }
 309  
 310      /**
 311       * Test deletion of all submissions for a context works.
 312       */
 313      public function test_delete_submissions() {
 314          global $DB;
 315          // Get a bunch of users making comments.
 316          // Some in one context some in another.
 317          $this->resetAfterTest();
 318          $course = $this->getDataGenerator()->create_course();
 319          // Only in first context.
 320          $user1 = $this->getDataGenerator()->create_user();
 321          $user2 = $this->getDataGenerator()->create_user();
 322          // First and second context.
 323          $user3 = $this->getDataGenerator()->create_user();
 324          // Second context only.
 325          $user4 = $this->getDataGenerator()->create_user();
 326          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 327          $assign1 = $this->create_instance(['course' => $course]);
 328          $assign2 = $this->create_instance(['course' => $course]);
 329  
 330          $assigncontext1 = $assign1->get_context();
 331          $assigncontext2 = $assign2->get_context();
 332  
 333          $user1comment = 'Comment from user 1';
 334          list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
 335          $user2comment = 'From user 2';
 336          $this->setUser($user2);
 337          $comment->add($user2comment);
 338          $user3comment = 'User 3 comment';
 339          $this->setUser($user3);
 340          $comment->add($user3comment);
 341          $user4comment = 'Comment from user 4';
 342          list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
 343          $user3secondcomment = 'Comment on user 4 post.';
 344          $this->setUser($user3);
 345          $comment->add($user3comment);
 346  
 347          // There should be three entries. One for the first three users.
 348          $results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
 349          $this->assertCount(3, $results);
 350  
 351          $deletedata = new \mod_assign\privacy\assign_plugin_request_data($assigncontext1, $assign1);
 352          $deletedata->set_userids([$user1->id, $user3->id]);
 353          \assignsubmission_comments\privacy\provider::delete_submissions($deletedata);
 354  
 355          // We should be left with just a comment from user 2.
 356          $results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
 357          $this->assertCount(1, $results);
 358          $this->assertEquals($user2comment, current($results)->content);
 359      }
 360  }