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 assignfeedback_editpdf.
  19   *
  20   * @package    assignfeedback_editpdf
  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 \assignfeedback_editpdf\page_editor;
  32  use \mod_assign\privacy\assign_plugin_request_data;
  33  
  34  /**
  35   * Unit tests for mod/assign/feedback/editpdf/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 assignfeedback_editpdf_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
  41  
  42      public function setUp(): void {
  43          // Skip this test if ghostscript is not supported.
  44          $result = \assignfeedback_editpdf\pdf::test_gs_path(false);
  45          if ($result->status !== \assignfeedback_editpdf\pdf::GSPATH_OK) {
  46              $this->markTestSkipped('Ghostscript not setup');
  47              return;
  48          }
  49          parent::setUp();
  50      }
  51  
  52      /**
  53       * Convenience function for creating feedback data.
  54       *
  55       * @param  object   $assign         assign object
  56       * @param  stdClass $student        user object
  57       * @param  stdClass $teacher        user object
  58       * @return array   Feedback plugin object and the grade object.
  59       */
  60      protected function create_feedback($assign, $student, $teacher) {
  61          global $CFG;
  62  
  63          // Create a file submission with the test pdf.
  64          $submission = $assign->get_user_submission($student->id, true);
  65  
  66          $this->setUser($student->id);
  67  
  68          $fs = get_file_storage();
  69          $pdfsubmission = (object) array(
  70              'contextid' => $assign->get_context()->id,
  71              'component' => 'assignsubmission_file',
  72              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  73              'itemid' => $submission->id,
  74              'filepath' => '/',
  75              'filename' => 'submission.pdf'
  76          );
  77          $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
  78          $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
  79  
  80          $data = new \stdClass();
  81          $plugin = $assign->get_submission_plugin_by_type('file');
  82          $plugin->save($submission, $data);
  83  
  84          $this->setUser($teacher->id);
  85  
  86          $plugin = $assign->get_feedback_plugin_by_type('editpdf');
  87  
  88          $grade = $assign->get_user_grade($student->id, true);
  89  
  90          $comment = new \assignfeedback_editpdf\comment();
  91  
  92          $comment->rawtext = 'Comment text';
  93          $comment->width = 100;
  94          $comment->x = 100;
  95          $comment->y = 100;
  96          $comment->colour = 'red';
  97          page_editor::set_comments($grade->id, 0, [$comment]);
  98  
  99          $annotation = new \assignfeedback_editpdf\annotation();
 100  
 101          $annotation->path = '';
 102          $annotation->x = 100;
 103          $annotation->y = 100;
 104          $annotation->endx = 200;
 105          $annotation->endy = 200;
 106          $annotation->type = 'line';
 107          $annotation->colour = 'red';
 108  
 109          page_editor::set_annotations($grade->id, 0, [$annotation]);
 110  
 111          $comments = page_editor::get_comments($grade->id, 0, true);
 112          $annotations = page_editor::get_annotations($grade->id, 0, false);
 113          page_editor::release_drafts($grade->id);
 114          $storedfile = \assignfeedback_editpdf\document_services::generate_feedback_document($assign->get_instance()->id, $student->id,
 115                  $grade->attemptnumber);
 116  
 117          return [$plugin, $grade, $storedfile];
 118      }
 119  
 120      /**
 121       * Quick test to make sure that get_metadata returns something.
 122       */
 123      public function test_get_metadata() {
 124          $collection = new \core_privacy\local\metadata\collection('assignfeedback_editpdf');
 125          $collection = \assignfeedback_editpdf\privacy\provider::get_metadata($collection);
 126          $this->assertNotEmpty($collection);
 127      }
 128  
 129      /**
 130       * Test that feedback comments are exported for a user.
 131       */
 132      public function test_export_feedback_user_data() {
 133          $this->resetAfterTest();
 134          // Create course, assignment, submission, and then a feedback comment.
 135          $course = $this->getDataGenerator()->create_course();
 136          // Student.
 137          $user1 = $this->getDataGenerator()->create_user();
 138          // Teacher.
 139          $user2 = $this->getDataGenerator()->create_user();
 140          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 141          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
 142          $assign = $this->create_instance(['course' => $course,
 143                  'assignsubmission_file_enabled' => 1,
 144                  'assignsubmission_file_maxfiles' => 1,
 145                  'assignfeedback_editpdf_enabled' => 1,
 146                  'assignsubmission_file_maxsizebytes' => 1000000]);
 147  
 148          $context = $assign->get_context();
 149  
 150          list($plugin, $grade, $storedfile) = $this->create_feedback($assign, $user1, $user2);
 151  
 152          // Check that we have data.
 153          $this->assertFalse($plugin->is_empty($grade));
 154  
 155          $writer = \core_privacy\local\request\writer::with_context($context);
 156          $this->assertFalse($writer->has_any_data());
 157  
 158          // The student should be able to see the teachers feedback.
 159          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
 160          \assignfeedback_editpdf\privacy\provider::export_feedback_user_data($exportdata);
 161          // print_object($writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')]));
 162          // print_object($writer->get_files(['PDF feedback', $storedfile->get_filename()]));
 163          $pdffile = $writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')])[$storedfile->get_filename()];
 164          // The writer should have returned a stored file.
 165          $this->assertInstanceOf('stored_file', $pdffile);
 166      }
 167  
 168      /**
 169       * Test that all feedback is deleted for a context.
 170       */
 171      public function test_delete_feedback_for_context() {
 172          $this->resetAfterTest();
 173          // Create course, assignment, submission, and then a feedback comment.
 174          $course = $this->getDataGenerator()->create_course();
 175          // Students.
 176          $user1 = $this->getDataGenerator()->create_user();
 177          $user2 = $this->getDataGenerator()->create_user();
 178          // Teacher.
 179          $user3 = $this->getDataGenerator()->create_user();
 180          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 181          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 182          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 183          $assign = $this->create_instance(['course' => $course,
 184                  'assignsubmission_file_enabled' => 1,
 185                  'assignsubmission_file_maxfiles' => 1,
 186                  'assignfeedback_editpdf_enabled' => 1,
 187                  'assignsubmission_file_maxsizebytes' => 1000000]);
 188  
 189          $context = $assign->get_context();
 190  
 191          list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
 192          list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
 193  
 194          // Check that we have data.
 195          $this->assertFalse($plugin1->is_empty($grade1));
 196          $this->assertFalse($plugin2->is_empty($grade2));
 197  
 198          $requestdata = new assign_plugin_request_data($context, $assign);
 199          \assignfeedback_editpdf\privacy\provider::delete_feedback_for_context($requestdata);
 200  
 201          // Check that we now have no data.
 202          $this->assertTrue($plugin1->is_empty($grade1));
 203          $this->assertTrue($plugin2->is_empty($grade2));
 204      }
 205  
 206      /**
 207       * Test that a grade item is deleted for a user.
 208       */
 209      public function test_delete_feedback_for_grade() {
 210          $this->resetAfterTest();
 211          // Create course, assignment, submission, and then a feedback comment.
 212          $course = $this->getDataGenerator()->create_course();
 213          // Students.
 214          $user1 = $this->getDataGenerator()->create_user();
 215          $user2 = $this->getDataGenerator()->create_user();
 216          // Teacher.
 217          $user3 = $this->getDataGenerator()->create_user();
 218          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 219          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 220          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 221          $assign = $this->create_instance(['course' => $course,
 222                  'assignsubmission_file_enabled' => 1,
 223                  'assignsubmission_file_maxfiles' => 1,
 224                  'assignfeedback_editpdf_enabled' => 1,
 225                  'assignsubmission_file_maxsizebytes' => 1000000]);
 226  
 227          $context = $assign->get_context();
 228  
 229          list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
 230          list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
 231  
 232          // Check that we have data.
 233          $this->assertFalse($plugin1->is_empty($grade1));
 234          $this->assertFalse($plugin2->is_empty($grade2));
 235  
 236          $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
 237          \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grade($requestdata);
 238  
 239          // Check that we now have no data for user 1.
 240          $this->assertTrue($plugin1->is_empty($grade1));
 241          // Check that user 2 data is still there.
 242          $this->assertFalse($plugin2->is_empty($grade2));
 243      }
 244  
 245      /**
 246       * Test that a grade item is deleted for a user.
 247       */
 248      public function test_delete_feedback_for_grades() {
 249          global $DB;
 250  
 251          $this->resetAfterTest();
 252          // Create course, assignment, submission, and then a feedback comment.
 253          $course = $this->getDataGenerator()->create_course();
 254          // Students.
 255          $user1 = $this->getDataGenerator()->create_user();
 256          $user2 = $this->getDataGenerator()->create_user();
 257          $user3 = $this->getDataGenerator()->create_user();
 258          $user4 = $this->getDataGenerator()->create_user();
 259          // Teacher.
 260          $user5 = $this->getDataGenerator()->create_user();
 261          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 262          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 263          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 264          $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 265          $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
 266          $assign1 = $this->create_instance(['course' => $course,
 267                  'assignsubmission_file_enabled' => 1,
 268                  'assignsubmission_file_maxfiles' => 1,
 269                  'assignfeedback_editpdf_enabled' => 1,
 270                  'assignsubmission_file_maxsizebytes' => 1000000]);
 271  
 272          $assign2 = $this->create_instance(['course' => $course,
 273                  'assignsubmission_file_enabled' => 1,
 274                  'assignsubmission_file_maxfiles' => 1,
 275                  'assignfeedback_editpdf_enabled' => 1,
 276                  'assignsubmission_file_maxsizebytes' => 1000000]);
 277  
 278          $context = $assign1->get_context();
 279  
 280          list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign1, $user1, $user5);
 281          list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign1, $user2, $user5);
 282          list($plugin3, $grade3, $storedfile3) = $this->create_feedback($assign1, $user3, $user5);
 283          list($plugin4, $grade4, $storedfile4) = $this->create_feedback($assign2, $user3, $user5);
 284          list($plugin5, $grade5, $storedfile5) = $this->create_feedback($assign2, $user4, $user5);
 285  
 286          // Check that we have data.
 287          $this->assertFalse($plugin1->is_empty($grade1));
 288          $this->assertFalse($plugin2->is_empty($grade2));
 289          $this->assertFalse($plugin3->is_empty($grade3));
 290          $this->assertFalse($plugin4->is_empty($grade4));
 291          $this->assertFalse($plugin5->is_empty($grade5));
 292  
 293          // Check that there are also files generated.
 294          $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
 295          $this->assertCount(10, $files);
 296  
 297          $deletedata = new assign_plugin_request_data($context, $assign1);
 298          $deletedata->set_userids([$user1->id, $user3->id]);
 299          $deletedata->populate_submissions_and_grades();
 300          \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grades($deletedata);
 301  
 302          // Check that we now have no data for user 1.
 303          $this->assertTrue($plugin1->is_empty($grade1));
 304          // Check that user 2 data is still there.
 305          $this->assertFalse($plugin2->is_empty($grade2));
 306          // User 3 in assignment 1 should be gone.
 307          $this->assertTrue($plugin3->is_empty($grade3));
 308          // User 3 in assignment 2 should still be here.
 309          $this->assertFalse($plugin4->is_empty($grade4));
 310          // User 4 in assignment 2 should also still be here.
 311          $this->assertFalse($plugin5->is_empty($grade5));
 312  
 313          // Check the files as well.
 314          $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
 315          // We should now only have six records here.
 316          $this->assertCount(6, $files);
 317      }
 318  }