[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/submission/file/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_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  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  29  
  30  class assignsubmission_file_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 $files Files that are being submitted for the assignment. */
  48      protected $files;
  49  
  50      /** @var stdClass $submission Submission information. */
  51      protected $submission;
  52  
  53      /** @var stdClass $fi File information - First file*/
  54      protected $fi;
  55  
  56      /** @var stdClass $fi2 File information - Second file*/
  57      protected $fi2;
  58  
  59      /**
  60       * Setup all the various parts of an assignment activity including creating a file submission.
  61       */
  62      protected function setUp() {
  63          $this->user = $this->getDataGenerator()->create_user();
  64          $this->course = $this->getDataGenerator()->create_course();
  65          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  66          $params['course'] = $this->course->id;
  67          $instance = $generator->create_instance($params);
  68          $this->cm = get_coursemodule_from_instance('assign', $instance->id);
  69          $this->context = context_module::instance($this->cm->id);
  70          $this->assign = new testable_assign($this->context, $this->cm, $this->course);
  71  
  72          $this->setUser($this->user->id);
  73          $this->submission = $this->assign->get_user_submission($this->user->id, true);
  74  
  75          $fs = get_file_storage();
  76          $dummy = (object) array(
  77              'contextid' => $this->context->id,
  78              'component' => 'assignsubmission_file',
  79              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  80              'itemid' => $this->submission->id,
  81              'filepath' => '/',
  82              'filename' => 'myassignmnent.pdf'
  83          );
  84          $this->fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  85          $dummy = (object) array(
  86              'contextid' => $this->context->id,
  87              'component' => 'assignsubmission_file',
  88              'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  89              'itemid' => $this->submission->id,
  90              'filepath' => '/',
  91              'filename' => 'myassignmnent.png'
  92          );
  93          $this->fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  94          $this->files = $fs->get_area_files($this->context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
  95              $this->submission->id, 'id', false);
  96  
  97      }
  98  
  99      /**
 100       * Test that the assessable_uploaded event is fired when a file submission has been made.
 101       */
 102      public function test_assessable_uploaded() {
 103          $this->resetAfterTest();
 104  
 105          $data = new stdClass();
 106          $plugin = $this->assign->get_submission_plugin_by_type('file');
 107          $sink = $this->redirectEvents();
 108          $plugin->save($this->submission, $data);
 109          $events = $sink->get_events();
 110  
 111          $this->assertCount(2, $events);
 112          $event = reset($events);
 113          $this->assertInstanceOf('\assignsubmission_file\event\assessable_uploaded', $event);
 114          $this->assertEquals($this->context->id, $event->contextid);
 115          $this->assertEquals($this->submission->id, $event->objectid);
 116          $this->assertCount(2, $event->other['pathnamehashes']);
 117          $this->assertEquals($this->fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
 118          $this->assertEquals($this->fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
 119          $expected = new stdClass();
 120          $expected->modulename = 'assign';
 121          $expected->cmid = $this->cm->id;
 122          $expected->itemid = $this->submission->id;
 123          $expected->courseid = $this->course->id;
 124          $expected->userid = $this->user->id;
 125          $expected->file = $this->files;
 126          $expected->files = $this->files;
 127          $expected->pathnamehashes = array($this->fi->get_pathnamehash(), $this->fi2->get_pathnamehash());
 128          $this->assertEventLegacyData($expected, $event);
 129          $this->assertEventContextNotUsed($event);
 130      }
 131  
 132      /**
 133       * Test that the submission_created event is fired when a file submission is saved.
 134       */
 135      public function test_submission_created() {
 136          $this->resetAfterTest();
 137  
 138          $data = new stdClass();
 139          $plugin = $this->assign->get_submission_plugin_by_type('file');
 140          $sink = $this->redirectEvents();
 141          $plugin->save($this->submission, $data);
 142          $events = $sink->get_events();
 143  
 144          $this->assertCount(2, $events);
 145          // We want to test the last event fired.
 146          $event = $events[1];
 147          $this->assertInstanceOf('\assignsubmission_file\event\submission_created', $event);
 148          $this->assertEquals($this->context->id, $event->contextid);
 149          $this->assertEquals($this->course->id, $event->courseid);
 150          $this->assertEquals($this->submission->id, $event->other['submissionid']);
 151          $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
 152          $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
 153          $this->assertEquals($this->submission->userid, $event->relateduserid);
 154      }
 155  
 156      /**
 157       * Test that the submission_updated event is fired when a file submission is saved when an existing submission already exists.
 158       */
 159      public function test_submission_updated() {
 160          $this->resetAfterTest();
 161  
 162          $data = new stdClass();
 163          $plugin = $this->assign->get_submission_plugin_by_type('file');
 164          $sink = $this->redirectEvents();
 165          // Create a submission.
 166          $plugin->save($this->submission, $data);
 167          // Update a submission.
 168          $plugin->save($this->submission, $data);
 169          $events = $sink->get_events();
 170  
 171          $this->assertCount(4, $events);
 172          // We want to test the last event fired.
 173          $event = $events[3];
 174          $this->assertInstanceOf('\assignsubmission_file\event\submission_updated', $event);
 175          $this->assertEquals($this->context->id, $event->contextid);
 176          $this->assertEquals($this->course->id, $event->courseid);
 177          $this->assertEquals($this->submission->id, $event->other['submissionid']);
 178          $this->assertEquals($this->submission->attemptnumber, $event->other['submissionattempt']);
 179          $this->assertEquals($this->submission->status, $event->other['submissionstatus']);
 180          $this->assertEquals($this->submission->userid, $event->relateduserid);
 181      }
 182  
 183  }


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