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