Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

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