MDL-68430 filter_mathjaxloader: update default CDN to 2.7.8
[moodle.git] / lib / tests / event_course_module_viewed.php
blob28e7bd144ad91a69123bde99dd8afdb4569e16b5
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.
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/>.
17 /**
18 * Tests for base course module viewed event.
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Ankit Agarwal
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__.'/fixtures/event_fixtures.php');
29 /**
30 * Class core_event_course_module_viewed_testcase
32 * Tests for event \core\event\course_module_viewed
34 class core_event_course_module_viewed_testcase extends advanced_testcase {
36 /**
37 * Test event properties and methods.
39 public function test_event_attributes() {
41 $this->resetAfterTest();
42 $course = $this->getDataGenerator()->create_course();
43 $record = new stdClass();
44 $record->course = $course->id;
45 $feed = $this->getDataGenerator()->create_module('feedback', $record);
46 $cm = get_coursemodule_from_instance('feedback', $feed->id);
47 $context = context_module::instance($cm->id);
49 // Trigger the page view event.
50 $sink = $this->redirectEvents();
51 $pageevent = \core_tests\event\course_module_viewed::create(array(
52 'context' => $context,
53 'courseid' => $course->id,
54 'objectid' => $feed->id
55 ));
56 $pageevent->trigger();
57 $result = $sink->get_events();
58 $event = reset($result);
59 $sink->close();
61 // Test event data.
62 $legacydata = array($course->id, 'feedback', 'view', 'view.php?id=' . $cm->id, $feed->id, $cm->id);
63 $this->assertEventLegacyLogData($legacydata, $event);
64 $this->assertSame('feedback', $event->objecttable);
65 $url = new moodle_url('/mod/feedback/view.php', array('id' => $cm->id));
66 $this->assertEquals($url, $event->get_url());
67 $this->assertEventContextNotUsed($event);
71 /**
72 * Test custom validations of the event.
74 public function test_event_validations() {
76 // Make sure objecttable and object id is always set.
77 try {
78 \core_tests\event\course_module_viewed_noinit::create(array(
79 'contextid' => 1,
80 'courseid' => 2,
81 'objectid' => 3 ));
82 } catch (coding_exception $e) {
83 $this->assertContains("course_module_viewed event must define objectid and object table.", $e->getMessage());
86 try {
87 \core_tests\event\course_module_viewed::create(array(
88 'contextid' => 1,
89 'courseid' => 2,
90 ));
91 } catch (coding_exception $e) {
92 $this->assertContains("course_module_viewed event must define objectid and object table.", $e->getMessage());