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.
   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_file.
  19   *
  20   * @package    assignsubmission_file
  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 assignsubmission_file\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/assign/tests/privacy/provider_test.php');
  30  
  31  /**
  32   * Unit tests for mod/assign/submission/file/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 provider_test extends \mod_assign\privacy\provider_test {
  38  
  39      /**
  40       * Convenience function for creating feedback data.
  41       *
  42       * @param  object   $assign         assign object
  43       * @param  stdClass $student        user object
  44       * @param  string   $filename       filename for the file submission
  45       * @return array   Submission plugin object and the submission object.
  46       */
  47      protected function create_file_submission($assign, $student, $filename) {
  48          global $CFG;
  49          // Create a file submission with the test pdf.
  50          $submission = $assign->get_user_submission($student->id, true);
  51  
  52          $this->setUser($student->id);
  53  
  54          $fs = get_file_storage();
  55          $pdfsubmission = (object) array(
  56              'contextid' => $assign->get_context()->id,
  57              'component' => 'assignsubmission_file',
  58              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  59              'itemid' => $submission->id,
  60              'filepath' => '/',
  61              'filename' => $filename
  62          );
  63          $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
  64          $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
  65  
  66          $data = new \stdClass();
  67          $plugin = $assign->get_submission_plugin_by_type('file');
  68          $plugin->save($submission, $data);
  69  
  70          return [$plugin, $submission];
  71      }
  72  
  73      /**
  74       * Quick test to make sure that get_metadata returns something.
  75       */
  76      public function test_get_metadata() {
  77          $collection = new \core_privacy\local\metadata\collection('assignsubmission_file');
  78          $collection = \assignsubmission_file\privacy\provider::get_metadata($collection);
  79          $this->assertNotEmpty($collection);
  80      }
  81  
  82      /**
  83       * Test that submission files are exported for a user.
  84       */
  85      public function test_export_submission_user_data() {
  86          $this->resetAfterTest();
  87          // Create course, assignment, submission, and then a feedback comment.
  88          $course = $this->getDataGenerator()->create_course();
  89          // Student.
  90          $user1 = $this->getDataGenerator()->create_user();
  91          // Teacher.
  92          $user2 = $this->getDataGenerator()->create_user();
  93          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  94          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
  95          $assign = $this->create_instance(['course' => $course]);
  96  
  97          $context = $assign->get_context();
  98  
  99          $studentfilename = 'user1file.pdf';
 100          list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
 101  
 102          $writer = \core_privacy\local\request\writer::with_context($context);
 103          $this->assertFalse($writer->has_any_data());
 104  
 105          // The student should have a file submission.
 106          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, ['Attempt 1']);
 107          \assignsubmission_file\privacy\provider::export_submission_user_data($exportdata);
 108          // print_object($writer);
 109          $storedfile = $writer->get_files(['Attempt 1'])['user1file.pdf'];
 110          $this->assertInstanceOf('stored_file', $storedfile);
 111          $this->assertEquals($studentfilename, $storedfile->get_filename());
 112      }
 113  
 114      /**
 115       * Test that all submission files are deleted for this context.
 116       */
 117      public function test_delete_submission_for_context() {
 118          $this->resetAfterTest();
 119          // Create course, assignment, submission, and then a feedback comment.
 120          $course = $this->getDataGenerator()->create_course();
 121          // Student.
 122          $user1 = $this->getDataGenerator()->create_user();
 123          $user2 = $this->getDataGenerator()->create_user();
 124  
 125          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 126          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 127  
 128          $assign = $this->create_instance(['course' => $course]);
 129  
 130          $context = $assign->get_context();
 131  
 132          $studentfilename = 'user1file.pdf';
 133          list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
 134          $student2filename = 'user2file.pdf';
 135          list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
 136  
 137          // Only need the context and assign object in this plugin for this operation.
 138          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 139          \assignsubmission_file\privacy\provider::delete_submission_for_context($requestdata);
 140          // This checks that there are no files in this submission.
 141          $this->assertTrue($plugin->is_empty($submission));
 142          $this->assertTrue($plugin2->is_empty($submission2));
 143      }
 144  
 145      /**
 146       * Test that the comments for a user are deleted.
 147       */
 148      public function test_delete_submission_for_userid() {
 149          $this->resetAfterTest();
 150          // Create course, assignment, submission, and then a feedback comment.
 151          $course = $this->getDataGenerator()->create_course();
 152          // Student.
 153          $user1 = $this->getDataGenerator()->create_user();
 154          $user2 = $this->getDataGenerator()->create_user();
 155  
 156          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 157          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 158  
 159          $assign = $this->create_instance(['course' => $course]);
 160  
 161          $context = $assign->get_context();
 162  
 163          $studentfilename = 'user1file.pdf';
 164          list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
 165          $student2filename = 'user2file.pdf';
 166          list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
 167  
 168          // Only need the context and assign object in this plugin for this operation.
 169          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, [], $user1);
 170          \assignsubmission_file\privacy\provider::delete_submission_for_userid($requestdata);
 171          // This checks that there are no files in this submission.
 172          $this->assertTrue($plugin->is_empty($submission));
 173          // There should be files here.
 174          $this->assertFalse($plugin2->is_empty($submission2));
 175      }
 176  
 177      /**
 178       * Test deletion of bulk submissions for a context.
 179       */
 180      public function test_delete_submissions() {
 181          global $DB;
 182  
 183          $this->resetAfterTest();
 184          // Create course, assignment, submission, and then a feedback comment.
 185          $course = $this->getDataGenerator()->create_course();
 186          // Student.
 187          $user1 = $this->getDataGenerator()->create_user();
 188          $user2 = $this->getDataGenerator()->create_user();
 189          $user3 = $this->getDataGenerator()->create_user();
 190          $user4 = $this->getDataGenerator()->create_user();
 191  
 192          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 193          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 194          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 195          $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 196  
 197          $assign1 = $this->create_instance(['course' => $course]);
 198          $assign2 = $this->create_instance(['course' => $course]);
 199  
 200          $context1 = $assign1->get_context();
 201          $context2 = $assign2->get_context();
 202  
 203          $student1filename = 'user1file.pdf';
 204          list($plugin1, $submission1) = $this->create_file_submission($assign1, $user1, $student1filename);
 205          $student2filename = 'user2file.pdf';
 206          list($plugin2, $submission2) = $this->create_file_submission($assign1, $user2, $student2filename);
 207          $student3filename = 'user3file.pdf';
 208          list($plugin3, $submission3) = $this->create_file_submission($assign1, $user3, $student3filename);
 209          $student4filename = 'user4file.pdf';
 210          list($plugin4, $submission4) = $this->create_file_submission($assign2, $user4, $student4filename);
 211          $student5filename = 'user5file.pdf';
 212          list($plugin5, $submission5) = $this->create_file_submission($assign2, $user3, $student5filename);
 213  
 214          $submissionids = [
 215              $submission1->id,
 216              $submission3->id
 217          ];
 218  
 219          $userids = [
 220              $user1->id,
 221              $user3->id
 222          ];
 223  
 224          $data = $DB->get_records('files', ['contextid' => $context1->id, 'component' => 'assignsubmission_file']);
 225          $this->assertCount(6, $data);
 226  
 227          $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign1->get_instance()->id]);
 228          $this->assertCount(3, $data);
 229  
 230          // Records in the second assignment (not being touched).
 231          $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign2->get_instance()->id]);
 232          $this->assertCount(2, $data);
 233  
 234          $deletedata = new \mod_assign\privacy\assign_plugin_request_data($context1, $assign1);
 235          $deletedata->set_userids($userids);
 236          $deletedata->populate_submissions_and_grades();
 237          \assignsubmission_file\privacy\provider::delete_submissions($deletedata);
 238          $data = $DB->get_records('files', ['contextid' => $context1->id, 'component' => 'assignsubmission_file']);
 239          $this->assertCount(2, $data);
 240  
 241          // Submission 1 and 3 have been removed. We should be left with submission2.
 242          $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign1->get_instance()->id]);
 243          $this->assertCount(1, $data);
 244  
 245          // This should be untouched.
 246          $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign2->get_instance()->id]);
 247          $this->assertCount(2, $data);
 248      }
 249  }