Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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