Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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_onlinetext
  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_onlinetext\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 an online text submission is saved.
  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  
  53          $submission = $assign->get_user_submission($student->id, true);
  54          $data = (object) [
  55              'onlinetext_editor' => [
  56                  'itemid' => file_get_unused_draft_itemid(),
  57                  'text' => 'Submission text',
  58                  'format' => FORMAT_PLAIN,
  59              ],
  60          ];
  61  
  62          $sink = $this->redirectEvents();
  63          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  64          $plugin->save($submission, $data);
  65          $events = $sink->get_events();
  66  
  67          $this->assertCount(2, $events);
  68          $event = reset($events);
  69          $this->assertInstanceOf('\assignsubmission_onlinetext\event\assessable_uploaded', $event);
  70          $this->assertEquals($context->id, $event->contextid);
  71          $this->assertEquals($submission->id, $event->objectid);
  72          $this->assertEquals(array(), $event->other['pathnamehashes']);
  73          $this->assertEquals(FORMAT_PLAIN, $event->other['format']);
  74          $this->assertEquals('Submission text', $event->other['content']);
  75          $expected = new \stdClass();
  76          $expected->modulename = 'assign';
  77          $expected->cmid = $cm->id;
  78          $expected->itemid = $submission->id;
  79          $expected->courseid = $course->id;
  80          $expected->userid = $student->id;
  81          $expected->content = 'Submission text';
  82          $this->assertEventContextNotUsed($event);
  83      }
  84  
  85      /**
  86       * Test that the submission_created event is fired when an onlinetext submission is saved.
  87       */
  88      public function test_submission_created() {
  89          $this->resetAfterTest();
  90  
  91          $course = $this->getDataGenerator()->create_course();
  92          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  93          $assign = $this->create_instance($course);
  94          $context = $assign->get_context();
  95  
  96          $this->setUser($student->id);
  97  
  98          $submission = $assign->get_user_submission($student->id, true);
  99          $data = (object) [
 100              'onlinetext_editor' => [
 101                  'itemid' => file_get_unused_draft_itemid(),
 102                  'text' => 'Submission text',
 103                  'format' => FORMAT_PLAIN,
 104              ],
 105          ];
 106  
 107          $sink = $this->redirectEvents();
 108          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
 109          $plugin->save($submission, $data);
 110          $events = $sink->get_events();
 111  
 112          $this->assertCount(2, $events);
 113          $event = $events[1];
 114          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_created', $event);
 115          $this->assertEquals($context->id, $event->contextid);
 116          $this->assertEquals($course->id, $event->courseid);
 117          $this->assertEquals($submission->id, $event->other['submissionid']);
 118          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 119          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 120          $this->assertEquals($submission->userid, $event->relateduserid);
 121      }
 122  
 123      /**
 124       * Test that the submission_updated event is fired when an onlinetext
 125       * submission is saved and an existing submission already exists.
 126       */
 127      public function test_submission_updated() {
 128          $this->resetAfterTest();
 129  
 130          $course = $this->getDataGenerator()->create_course();
 131          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 132          $assign = $this->create_instance($course);
 133          $context = $assign->get_context();
 134  
 135          $this->setUser($student->id);
 136  
 137          $submission = $assign->get_user_submission($student->id, true);
 138          $data = (object) [
 139              'onlinetext_editor' => [
 140                  'itemid' => file_get_unused_draft_itemid(),
 141                  'text' => 'Submission text',
 142                  'format' => FORMAT_PLAIN,
 143              ],
 144          ];
 145  
 146          $sink = $this->redirectEvents();
 147          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
 148          $plugin->save($submission, $data);
 149          $sink->clear();
 150  
 151          // Update a submission.
 152          $plugin->save($submission, $data);
 153          $events = $sink->get_events();
 154  
 155          $this->assertCount(2, $events);
 156          $event = $events[1];
 157          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_updated', $event);
 158          $this->assertEquals($context->id, $event->contextid);
 159          $this->assertEquals($course->id, $event->courseid);
 160          $this->assertEquals($submission->id, $event->other['submissionid']);
 161          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 162          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 163          $this->assertEquals($submission->userid, $event->relateduserid);
 164      }
 165  }