Search moodle.org's
Developer Documentation

See Release Notes

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