[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/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   * Events tests.
  19   *
  20   * @package    mod_lesson
  21   * @category   test
  22   * @copyright  2013 Mark Nelson <[email protected]>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  31  
  32  class mod_lesson_events_testcase extends advanced_testcase {
  33  
  34      /** @var stdClass the course used for testing */
  35      private $course;
  36  
  37      /** @var lesson the lesson used for testing */
  38      private $lesson;
  39  
  40      /**
  41       * Test set up.
  42       *
  43       * This is executed before running any test in this file.
  44       */
  45      public function setUp() {
  46          $this->resetAfterTest();
  47  
  48          $this->setAdminUser();
  49          $this->course = $this->getDataGenerator()->create_course();
  50          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $this->course->id));
  51  
  52          // Convert to a lesson object.
  53          $this->lesson = new lesson($lesson);
  54      }
  55  
  56      /**
  57       * Test the essay attempt viewed event.
  58       *
  59       * There is no external API for viewing an essay attempt, so the unit test will simply
  60       * create and trigger the event and ensure the legacy log data is returned as expected.
  61       */
  62      public function test_essay_attempt_viewed() {
  63          // Create a essays list viewed event
  64          $event = \mod_lesson\event\essay_attempt_viewed::create(array(
  65              'objectid' => $this->lesson->id,
  66              'relateduserid' => 3,
  67              'context' => context_module::instance($this->lesson->properties()->cmid),
  68              'courseid' => $this->course->id
  69          ));
  70  
  71          // Trigger and capture the event.
  72          $sink = $this->redirectEvents();
  73          $event->trigger();
  74          $events = $sink->get_events();
  75          $event = reset($events);
  76  
  77          // Check that the event data is valid.
  78          $this->assertInstanceOf('\mod_lesson\event\essay_attempt_viewed', $event);
  79          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
  80          $expected = array($this->course->id, 'lesson', 'view grade', 'essay.php?id=' . $this->lesson->properties()->cmid .
  81              '&mode=grade&attemptid='.$this->lesson->id, get_string('manualgrading', 'lesson'), $this->lesson->properties()->cmid);
  82          $this->assertEventLegacyLogData($expected, $event);
  83          $this->assertEventContextNotUsed($event);
  84      }
  85  
  86      /**
  87       * Test the highscore added event.
  88       *
  89       * There is no external API for adding a highscore, so the unit test will simply create
  90       * and trigger the event and ensure the legacy log data is returned as expected.
  91       */
  92      public function test_highscore_added() {
  93          global $DB;
  94  
  95          // Create a highscore.
  96          $newhighscore = new stdClass;
  97          $newhighscore->lessonid = $this->lesson->id;
  98          $newhighscore->userid = 3;
  99          $newhighscore->gradeid = 70;
 100          $newhighscore->nickname = 'noob';
 101  
 102          $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore);
 103  
 104          // Create a highscore added event.
 105          $event = \mod_lesson\event\highscore_added::create(array(
 106              'objectid' => $newhighscore->id,
 107              'context' => context_module::instance($this->lesson->properties()->cmid),
 108              'courseid' => $this->course->id,
 109              'other' => array(
 110                  'lessonid' => $this->lesson->id,
 111                  'nickname' => 'noob'
 112              )
 113          ));
 114  
 115          // Trigger and capture the event.
 116          $sink = $this->redirectEvents();
 117          $event->trigger();
 118          $events = $sink->get_events();
 119          $event = reset($events);
 120  
 121          // Check that the event data is valid.
 122          $this->assertInstanceOf('\mod_lesson\event\highscore_added', $event);
 123          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
 124          $expected = array($this->course->id, 'lesson', 'update highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
 125              'noob', $this->lesson->properties()->cmid);
 126          $this->assertEventLegacyLogData($expected, $event);
 127          $this->assertEventContextNotUsed($event);
 128      }
 129  
 130      /**
 131       * Test the highscores viewed event.
 132       *
 133       * There is no external API for viewing highscores, so the unit test will simply create
 134       * and trigger the event and ensure the legacy log data is returned as expected.
 135       */
 136      public function test_highscores_viewed() {
 137          // Create a highscore viewed event.
 138          $event = \mod_lesson\event\highscores_viewed::create(array(
 139              'objectid' => $this->lesson->id,
 140              'context' => context_module::instance($this->lesson->properties()->cmid),
 141              'courseid' => $this->course->id
 142          ));
 143  
 144          // Trigger and capture the event.
 145          $sink = $this->redirectEvents();
 146          $event->trigger();
 147          $events = $sink->get_events();
 148          $event = reset($events);
 149  
 150          // Check that the event data is valid.
 151          $this->assertInstanceOf('\mod_lesson\event\highscores_viewed', $event);
 152          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
 153          $expected = array($this->course->id, 'lesson', 'view highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
 154              $this->lesson->properties()->name, $this->lesson->properties()->cmid);
 155          $this->assertEventLegacyLogData($expected, $event);
 156          $this->assertEventContextNotUsed($event);
 157      }
 158  
 159      /**
 160       * Test the lesson started event.
 161       */
 162      public function test_lesson_started() {
 163          // Trigger and capture the event.
 164          $sink = $this->redirectEvents();
 165          $this->lesson->start_timer();
 166          $events = $sink->get_events();
 167          $event = reset($events);
 168  
 169          // Check that the event data is valid.
 170          $this->assertInstanceOf('\mod_lesson\event\lesson_started', $event);
 171          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
 172          $expected = array($this->course->id, 'lesson', 'start', 'view.php?id=' . $this->lesson->properties()->cmid,
 173              $this->lesson->properties()->id, $this->lesson->properties()->cmid);
 174          $this->assertEventLegacyLogData($expected, $event);
 175          $this->assertEventContextNotUsed($event);
 176      }
 177  
 178      /**
 179       * Test the lesson ended event.
 180       */
 181      public function test_lesson_ended() {
 182          global $DB, $USER;
 183  
 184          // Add a lesson timer so that stop_timer() does not complain.
 185          $lessontimer = new stdClass();
 186          $lessontimer->lessonid = $this->lesson->properties()->id;
 187          $lessontimer->userid = $USER->id;
 188          $lessontimer->startime = time();
 189          $lessontimer->lessontime = time();
 190          $DB->insert_record('lesson_timer', $lessontimer);
 191  
 192          // Trigger and capture the event.
 193          $sink = $this->redirectEvents();
 194          $this->lesson->stop_timer();
 195          $events = $sink->get_events();
 196          $event = reset($events);
 197  
 198          // Check that the event data is valid.
 199          $this->assertInstanceOf('\mod_lesson\event\lesson_ended', $event);
 200          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
 201          $expected = array($this->course->id, 'lesson', 'end', 'view.php?id=' . $this->lesson->properties()->cmid,
 202              $this->lesson->properties()->id, $this->lesson->properties()->cmid);
 203          $this->assertEventLegacyLogData($expected, $event);
 204          $this->assertEventContextNotUsed($event);
 205      }
 206  
 207      /**
 208       * Test the essay assessed event.
 209       *
 210       * There is no external API for assessing an essay, so the unit test will simply
 211       * create and trigger the event and ensure the legacy log data is returned as expected.
 212       */
 213      public function test_essay_assessed() {
 214          // Create an essay assessed event
 215          $gradeid = 5;
 216          $attemptid = 7;
 217          $event = \mod_lesson\event\essay_assessed::create(array(
 218              'objectid' => $gradeid,
 219              'relateduserid' => 3,
 220              'context' => context_module::instance($this->lesson->properties()->cmid),
 221              'courseid' => $this->course->id,
 222              'other' => array(
 223                  'lessonid' => $this->lesson->id,
 224                  'attemptid' => $attemptid
 225              )
 226          ));
 227  
 228          // Trigger and capture the event.
 229          $sink = $this->redirectEvents();
 230          $event->trigger();
 231          $events = $sink->get_events();
 232          $event = reset($events);
 233  
 234          // Check that the event data is valid.
 235          $this->assertInstanceOf('\mod_lesson\event\essay_assessed', $event);
 236          $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
 237          $expected = array($this->course->id, 'lesson', 'update grade', 'essay.php?id=' . $this->lesson->properties()->cmid,
 238                  $this->lesson->name, $this->lesson->properties()->cmid);
 239          $this->assertEventLegacyLogData($expected, $event);
 240          $this->assertEventContextNotUsed($event);
 241      }
 242  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1