Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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