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   * Contains the event tests for the plugin.
  19   *
  20   * @package   assignsubmission_file
  21   * @copyright 2013 Frédéric Massart
  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/generator.php');
  29  
  30  class assignsubmission_file_events_testcase extends advanced_testcase {
  31  
  32      // Use the generator helper.
  33      use mod_assign_test_generator;
  34  
  35      /**
  36       * Test that the assessable_uploaded event is fired when a file submission has been made.
  37       */
  38      public function test_assessable_uploaded() {
  39          $this->resetAfterTest();
  40  
  41          $course = $this->getDataGenerator()->create_course();
  42          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  43          $assign = $this->create_instance($course);
  44          $context = $assign->get_context();
  45          $cm = $assign->get_course_module();
  46  
  47          $this->setUser($student->id);
  48          $submission = $assign->get_user_submission($student->id, true);
  49  
  50          $fs = get_file_storage();
  51          $dummy = (object) array(
  52              'contextid' => $context->id,
  53              'component' => 'assignsubmission_file',
  54              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  55              'itemid' => $submission->id,
  56              'filepath' => '/',
  57              'filename' => 'myassignmnent.pdf'
  58          );
  59          $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  60          $dummy = (object) array(
  61              'contextid' => $context->id,
  62              'component' => 'assignsubmission_file',
  63              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  64              'itemid' => $submission->id,
  65              'filepath' => '/',
  66              'filename' => 'myassignmnent.png'
  67          );
  68          $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  69          $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
  70              $submission->id, 'id', false);
  71  
  72          $data = new stdClass();
  73          $plugin = $assign->get_submission_plugin_by_type('file');
  74          $sink = $this->redirectEvents();
  75          $plugin->save($submission, $data);
  76          $events = $sink->get_events();
  77  
  78          $this->assertCount(2, $events);
  79          $event = reset($events);
  80          $this->assertInstanceOf('\assignsubmission_file\event\assessable_uploaded', $event);
  81          $this->assertEquals($context->id, $event->contextid);
  82          $this->assertEquals($submission->id, $event->objectid);
  83          $this->assertCount(2, $event->other['pathnamehashes']);
  84          $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
  85          $this->assertEquals($fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
  86          $expected = new stdClass();
  87          $expected->modulename = 'assign';
  88          $expected->cmid = $cm->id;
  89          $expected->itemid = $submission->id;
  90          $expected->courseid = $course->id;
  91          $expected->userid = $student->id;
  92          $expected->file = $files;
  93          $expected->files = $files;
  94          $expected->pathnamehashes = array($fi->get_pathnamehash(), $fi2->get_pathnamehash());
  95          $this->assertEventLegacyData($expected, $event);
  96          $this->assertEventContextNotUsed($event);
  97      }
  98  
  99      /**
 100       * Test that the submission_created event is fired when a file submission is saved.
 101       */
 102      public function test_submission_created() {
 103          $this->resetAfterTest();
 104  
 105          $course = $this->getDataGenerator()->create_course();
 106          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 107          $assign = $this->create_instance($course);
 108          $context = $assign->get_context();
 109  
 110          $this->setUser($student->id);
 111          $submission = $assign->get_user_submission($student->id, true);
 112  
 113          $fs = get_file_storage();
 114          $dummy = (object) array(
 115              'contextid' => $context->id,
 116              'component' => 'assignsubmission_file',
 117              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
 118              'itemid' => $submission->id,
 119              'filepath' => '/',
 120              'filename' => 'myassignmnent.pdf'
 121          );
 122          $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
 123          $dummy = (object) array(
 124              'contextid' => $context->id,
 125              'component' => 'assignsubmission_file',
 126              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
 127              'itemid' => $submission->id,
 128              'filepath' => '/',
 129              'filename' => 'myassignmnent.png'
 130          );
 131          $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
 132          $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
 133              $submission->id, 'id', false);
 134  
 135          $data = new stdClass();
 136          $plugin = $assign->get_submission_plugin_by_type('file');
 137          $sink = $this->redirectEvents();
 138          $plugin->save($submission, $data);
 139          $events = $sink->get_events();
 140  
 141          $this->assertCount(2, $events);
 142          // We want to test the last event fired.
 143          $event = $events[1];
 144          $this->assertInstanceOf('\assignsubmission_file\event\submission_created', $event);
 145          $this->assertEquals($context->id, $event->contextid);
 146          $this->assertEquals($course->id, $event->courseid);
 147          $this->assertEquals($submission->id, $event->other['submissionid']);
 148          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 149          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 150          $this->assertEquals($submission->userid, $event->relateduserid);
 151      }
 152  
 153      /**
 154       * Test that the submission_updated event is fired when a file submission is saved when an existing submission already exists.
 155       */
 156      public function test_submission_updated() {
 157          $this->resetAfterTest();
 158  
 159          $course = $this->getDataGenerator()->create_course();
 160          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 161          $assign = $this->create_instance($course);
 162          $context = $assign->get_context();
 163  
 164          $this->setUser($student->id);
 165          $submission = $assign->get_user_submission($student->id, true);
 166  
 167          $fs = get_file_storage();
 168          $dummy = (object) array(
 169              'contextid' => $context->id,
 170              'component' => 'assignsubmission_file',
 171              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
 172              'itemid' => $submission->id,
 173              'filepath' => '/',
 174              'filename' => 'myassignmnent.pdf'
 175          );
 176          $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
 177          $dummy = (object) array(
 178              'contextid' => $context->id,
 179              'component' => 'assignsubmission_file',
 180              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
 181              'itemid' => $submission->id,
 182              'filepath' => '/',
 183              'filename' => 'myassignmnent.png'
 184          );
 185          $fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
 186          $files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
 187              $submission->id, 'id', false);
 188  
 189          $data = new stdClass();
 190          $plugin = $assign->get_submission_plugin_by_type('file');
 191          $sink = $this->redirectEvents();
 192          // Create a submission.
 193          $plugin->save($submission, $data);
 194          // Update a submission.
 195          $plugin->save($submission, $data);
 196          $events = $sink->get_events();
 197  
 198          $this->assertCount(4, $events);
 199          // We want to test the last event fired.
 200          $event = $events[3];
 201          $this->assertInstanceOf('\assignsubmission_file\event\submission_updated', $event);
 202          $this->assertEquals($context->id, $event->contextid);
 203          $this->assertEquals($course->id, $event->courseid);
 204          $this->assertEquals($submission->id, $event->other['submissionid']);
 205          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 206          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 207          $this->assertEquals($submission->userid, $event->relateduserid);
 208      }
 209  
 210  }