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_onlinetext.
  19   *
  20   * @package    assignsubmission_onlinetext
  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/onlinetext/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_online_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   $text           Submission text.
  44       * @return array   Submission plugin object and the submission object.
  45       */
  46      protected function create_online_submission($assign, $student, $text) {
  47          global $CFG;
  48  
  49          $this->setUser($student->id);
  50          $submission = $assign->get_user_submission($student->id, true);
  51          $data = new stdClass();
  52          $data->onlinetext_editor = array(
  53              'itemid' => file_get_unused_draft_itemid(),
  54              'text' => $text,
  55              'format' => FORMAT_PLAIN
  56          );
  57  
  58          $submission = $assign->get_user_submission($student->id, true);
  59  
  60          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  61          $plugin->save($submission, $data);
  62  
  63          return [$plugin, $submission];
  64      }
  65  
  66      /**
  67       * Quick test to make sure that get_metadata returns something.
  68       */
  69      public function test_get_metadata() {
  70          $collection = new \core_privacy\local\metadata\collection('assignsubmission_onlinetext');
  71          $collection = \assignsubmission_onlinetext\privacy\provider::get_metadata($collection);
  72          $this->assertNotEmpty($collection);
  73      }
  74  
  75      /**
  76       * Test that submission files and text are exported for a user.
  77       */
  78      public function test_export_submission_user_data() {
  79          $this->resetAfterTest();
  80          // Create course, assignment, submission, and then a feedback comment.
  81          $course = $this->getDataGenerator()->create_course();
  82          // Student.
  83          $user1 = $this->getDataGenerator()->create_user();
  84          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  85          $assign = $this->create_instance(['course' => $course]);
  86  
  87          $context = $assign->get_context();
  88  
  89          $submissiontext = 'Just some text';
  90          list($plugin, $submission) = $this->create_online_submission($assign, $user1, $submissiontext);
  91  
  92          $writer = \core_privacy\local\request\writer::with_context($context);
  93          $this->assertFalse($writer->has_any_data());
  94  
  95          // The student should have some text submitted.
  96          $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, ['Attempt 1']);
  97          \assignsubmission_onlinetext\privacy\provider::export_submission_user_data($exportdata);
  98          $this->assertEquals($submissiontext, $writer->get_data(['Attempt 1',
  99                  get_string('privacy:path', 'assignsubmission_onlinetext')])->text);
 100      }
 101  
 102      /**
 103       * Test that all submission files are deleted for this context.
 104       */
 105      public function test_delete_submission_for_context() {
 106          $this->resetAfterTest();
 107          // Create course, assignment, submission, and then a feedback comment.
 108          $course = $this->getDataGenerator()->create_course();
 109          // Student.
 110          $user1 = $this->getDataGenerator()->create_user();
 111          $user2 = $this->getDataGenerator()->create_user();
 112  
 113          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 114          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 115  
 116          $assign = $this->create_instance(['course' => $course]);
 117  
 118          $context = $assign->get_context();
 119  
 120          $studenttext = 'Student one\'s text.';
 121          list($plugin, $submission) = $this->create_online_submission($assign, $user1, $studenttext);
 122          $studenttext2 = 'Student two\'s text.';
 123          list($plugin2, $submission2) = $this->create_online_submission($assign, $user2, $studenttext2);
 124  
 125          // Only need the context and assign object in this plugin for this operation.
 126          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 127          \assignsubmission_onlinetext\privacy\provider::delete_submission_for_context($requestdata);
 128          // This checks that there is no content for these submissions.
 129          $this->assertTrue($plugin->is_empty($submission));
 130          $this->assertTrue($plugin2->is_empty($submission2));
 131      }
 132  
 133      /**
 134       * Test that the comments for a user are deleted.
 135       */
 136      public function test_delete_submission_for_userid() {
 137          $this->resetAfterTest();
 138          // Create course, assignment, submission, and then a feedback comment.
 139          $course = $this->getDataGenerator()->create_course();
 140          // Student.
 141          $user1 = $this->getDataGenerator()->create_user();
 142          $user2 = $this->getDataGenerator()->create_user();
 143  
 144          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 145          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 146  
 147          $assign = $this->create_instance(['course' => $course]);
 148  
 149          $context = $assign->get_context();
 150  
 151          $studenttext = 'Student one\'s text.';
 152          list($plugin, $submission) = $this->create_online_submission($assign, $user1, $studenttext);
 153          $studenttext2 = 'Student two\'s text.';
 154          list($plugin2, $submission2) = $this->create_online_submission($assign, $user2, $studenttext2);
 155  
 156          // Need more data for this operation.
 157          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, [], $user1);
 158          \assignsubmission_onlinetext\privacy\provider::delete_submission_for_userid($requestdata);
 159          // This checks that there is no content for the first submission.
 160          $this->assertTrue($plugin->is_empty($submission));
 161          // But there is for the second submission.
 162          $this->assertFalse($plugin2->is_empty($submission2));
 163      }
 164  
 165      public function test_delete_submissions() {
 166          global $DB;
 167  
 168          $this->resetAfterTest();
 169  
 170          $course = $this->getDataGenerator()->create_course();
 171          $user1 = $this->getDataGenerator()->create_user();
 172          $user2 = $this->getDataGenerator()->create_user();
 173          $user3 = $this->getDataGenerator()->create_user();
 174          // Only makes submissions in the second assignment.
 175          $user4 = $this->getDataGenerator()->create_user();
 176  
 177          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 178          $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 179          $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 180          $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 181  
 182          $assign1 = $this->create_instance(['course' => $course]);
 183          $assign2 = $this->create_instance(['course' => $course]);
 184  
 185          $context1 = $assign1->get_context();
 186          $context2 = $assign2->get_context();
 187  
 188          $student1text = 'Student one\'s text.';
 189          list($plugin1, $submission1) = $this->create_online_submission($assign1, $user1, $student1text);
 190          $student2text = 'Student two\'s text.';
 191          list($plugin2, $submission2) = $this->create_online_submission($assign1, $user2, $student2text);
 192          $student3text = 'Student two\'s text.';
 193          list($plugin3, $submission3) = $this->create_online_submission($assign1, $user3, $student3text);
 194          // Now for submissions in assignment two.
 195          $student3text2 = 'Student two\'s text for the second assignment.';
 196          list($plugin4, $submission4) = $this->create_online_submission($assign2, $user3, $student3text2);
 197          $student4text = 'Student four\'s text.';
 198          list($plugin5, $submission5) = $this->create_online_submission($assign2, $user4, $student4text);
 199  
 200          $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign1->get_instance()->id]);
 201          $this->assertCount(3, $data);
 202          // Delete the submissions for user 1 and 3.
 203          $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context1, $assign1);
 204          $requestdata->set_userids([$user1->id, $user2->id]);
 205          $requestdata->populate_submissions_and_grades();
 206          \assignsubmission_onlinetext\privacy\provider::delete_submissions($requestdata);
 207  
 208          // There should only be one record left for assignment one.
 209          $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign1->get_instance()->id]);
 210          $this->assertCount(1, $data);
 211  
 212          // Check that the second assignment has not been touched.
 213          $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign2->get_instance()->id]);
 214          $this->assertCount(2, $data);
 215      }
 216  }