[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/submission/onlinetext/tests/ -> events_test.php (source)

   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/base_test.php');
  29  
  30  class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
  31  
  32      /** @var stdClass $user A user to submit an assignment. */
  33      protected $user;
  34  
  35      /** @var stdClass $course New course created to hold the assignment activity. */
  36      protected $course;
  37  
  38      /** @var stdClass $cm A context module object. */
  39      protected $cm;
  40  
  41      /** @var stdClass $context Context of the assignment activity. */
  42      protected $context;
  43  
  44      /** @var stdClass $assign The assignment object. */
  45      protected $assign;
  46  
  47      /** @var stdClass $submission Submission information. */
  48      protected $submission;
  49  
  50      /** @var stdClass $data General data for the assignment submission. */
  51      protected $data;
  52  
  53      /**
  54       * Setup all the various parts of an assignment activity including creating an onlinetext submission.
  55       */
  56      protected function setUp() {
  57          $this->user = $this->getDataGenerator()->create_user();
  58          $this->course = $this->getDataGenerator()->create_course();
  59          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  60          $params['course'] = $this->course->id;
  61          $instance = $generator->create_instance($params);
  62          $this->cm = get_coursemodule_from_instance('assign', $instance->id);
  63          $this->context = context_module::instance($this->cm->id);
  64          $this->assign = new testable_assign($this->context, $this->cm, $this->course);
  65  
  66          $this->setUser($this->user->id);
  67          $this->submission = $this->assign->get_user_submission($this->user->id, true);
  68          $this->data = new stdClass();
  69          $this->data->onlinetext_editor = array(
  70              'itemid' => file_get_unused_draft_itemid(),
  71              'text' => 'Submission text',
  72              'format' => FORMAT_PLAIN
  73          );
  74      }
  75  
  76      /**
  77       * Test that the assessable_uploaded event is fired when an online text submission is saved.
  78       */
  79      public function test_assessable_uploaded() {
  80          $this->resetAfterTest();
  81  
  82          $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
  83          $sink = $this->redirectEvents();
  84          $plugin->save($this->submission, $this->data);
  85          $events = $sink->get_events();
  86  
  87          $this->assertCount(2, $events);
  88          $event = reset($events);
  89          $this->assertInstanceOf('\assignsubmission_onlinetext\event\assessable_uploaded', $event);
  90          $this->assertEquals($this->context->id, $event->contextid);
  91          $this->assertEquals($this->submission->id, $event->objectid);
  92          $this->assertEquals(array(), $event->other['pathnamehashes']);
  93          $this->assertEquals(FORMAT_PLAIN, $event->other['format']);
  94          $this->assertEquals('Submission text', $event->other['content']);
  95          $expected = new stdClass();
  96          $expected->modulename = 'assign';
  97          $expected->cmid = $this->cm->id;
  98          $expected->itemid = $this->submission->id;
  99          $expected->courseid = $this->course->id;
 100          $expected->userid = $this->user->id;
 101          $expected->content = 'Submission text';
 102          $this->assertEventLegacyData($expected, $event);
 103          $this->assertEventContextNotUsed($event);
 104      }
 105  
 106      /**
 107       * Test that the submission_created event is fired when an onlinetext submission is saved.
 108       */
 109      public function test_submission_created() {
 110          $this->resetAfterTest();
 111  
 112          $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
 113          $sink = $this->redirectEvents();
 114          $plugin->save($this->submission, $this->data);
 115          $events = $sink->get_events();
 116  
 117          $this->assertCount(2, $events);
 118          $event = $events[1];
 119          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_created', $event);
 120          $this->assertEquals($this->context->id, $event->contextid);
 121          $this->assertEquals($this->course->id, $event->courseid);
 122          $this->assertEquals($this->submission->id, $event->other['submissionid']);
 123          $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
 124          $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
 125          $this->assertEquals($this->submission->userid, $event->relateduserid);
 126      }
 127  
 128      /**
 129       * Test that the submission_updated event is fired when an onlinetext
 130       * submission is saved and an existing submission already exists.
 131       */
 132      public function test_submission_updated() {
 133          $this->resetAfterTest();
 134  
 135          $plugin = $this->assign->get_submission_plugin_by_type('onlinetext');
 136          $sink = $this->redirectEvents();
 137          // Create a submission.
 138          $plugin->save($this->submission, $this->data);
 139          // Update a submission.
 140          $plugin->save($this->submission, $this->data);
 141          $events = $sink->get_events();
 142  
 143          $this->assertCount(4, $events);
 144          $event = $events[3];
 145          $this->assertInstanceOf('\assignsubmission_onlinetext\event\submission_updated', $event);
 146          $this->assertEquals($this->context->id, $event->contextid);
 147          $this->assertEquals($this->course->id, $event->courseid);
 148          $this->assertEquals($this->submission->id, $event->other['submissionid']);
 149          $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
 150          $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
 151          $this->assertEquals($this->submission->userid, $event->relateduserid);
 152      }
 153  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1