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_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->assertEventLegacyData($expected, $event);
  83          $this->assertEventContextNotUsed($event);
  84      }
  85  
  86      /**
  87       * Test that the submission_created event is fired when an onlinetext submission is saved.
  88       */
  89      public function test_submission_created() {
  90          $this->resetAfterTest();
  91  
  92          $course = $this->getDataGenerator()->create_course();
  93          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  94          $assign = $this->create_instance($course);
  95          $context = $assign->get_context();
  96  
  97          $this->setUser($student->id);
  98  
  99          $submission = $assign->get_user_submission($student->id, true);
 100          $data = (object) [
 101              'onlinetext_editor' => [
 102                  'itemid' => file_get_unused_draft_itemid(),
 103                  'text' => 'Submission text',
 104                  'format' => FORMAT_PLAIN,
 105              ],
 106          ];
 107  
 108          $sink = $this->redirectEvents();
 109          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
 110          $plugin->save($submission, $data);
 111          $events = $sink->get_events();
 112  
 113          $this->assertCount(2, $events);
 114          $event = $events[1];
 115          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_created', $event);
 116          $this->assertEquals($context->id, $event->contextid);
 117          $this->assertEquals($course->id, $event->courseid);
 118          $this->assertEquals($submission->id, $event->other['submissionid']);
 119          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 120          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 121          $this->assertEquals($submission->userid, $event->relateduserid);
 122      }
 123  
 124      /**
 125       * Test that the submission_updated event is fired when an onlinetext
 126       * submission is saved and an existing submission already exists.
 127       */
 128      public function test_submission_updated() {
 129          $this->resetAfterTest();
 130  
 131          $course = $this->getDataGenerator()->create_course();
 132          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 133          $assign = $this->create_instance($course);
 134          $context = $assign->get_context();
 135  
 136          $this->setUser($student->id);
 137  
 138          $submission = $assign->get_user_submission($student->id, true);
 139          $data = (object) [
 140              'onlinetext_editor' => [
 141                  'itemid' => file_get_unused_draft_itemid(),
 142                  'text' => 'Submission text',
 143                  'format' => FORMAT_PLAIN,
 144              ],
 145          ];
 146  
 147          $sink = $this->redirectEvents();
 148          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
 149          $plugin->save($submission, $data);
 150          $sink->clear();
 151  
 152          // Update a submission.
 153          $plugin->save($submission, $data);
 154          $events = $sink->get_events();
 155  
 156          $this->assertCount(2, $events);
 157          $event = $events[1];
 158          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_updated', $event);
 159          $this->assertEquals($context->id, $event->contextid);
 160          $this->assertEquals($course->id, $event->courseid);
 161          $this->assertEquals($submission->id, $event->other['submissionid']);
 162          $this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
 163          $this->assertEquals($submission->status, $event->other['submissionstatus']);
 164          $this->assertEquals($submission->userid, $event->relateduserid);
 165      }
 166  }