11 |
efrain |
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 |
namespace core\event;
|
|
|
18 |
|
|
|
19 |
use advanced_testcase;
|
|
|
20 |
use coding_exception;
|
|
|
21 |
use context_module;
|
|
|
22 |
use stdClass;
|
|
|
23 |
use moodle_url;
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
require_once(__DIR__.'/../fixtures/event_fixtures.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Tests for base course module viewed event.
|
|
|
30 |
*
|
|
|
31 |
* @package core
|
|
|
32 |
* @category phpunit
|
|
|
33 |
* @covers \core\event\course_module_viewed
|
|
|
34 |
* @copyright 2013 Ankit Agarwal
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
final class course_module_viewed_test extends advanced_testcase {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Test event properties and methods.
|
|
|
41 |
*/
|
|
|
42 |
public function test_event_attributes() {
|
|
|
43 |
|
|
|
44 |
$this->resetAfterTest();
|
|
|
45 |
$course = $this->getDataGenerator()->create_course();
|
|
|
46 |
$record = new stdClass();
|
|
|
47 |
$record->course = $course->id;
|
|
|
48 |
$feed = $this->getDataGenerator()->create_module('feedback', $record);
|
|
|
49 |
$cm = get_coursemodule_from_instance('feedback', $feed->id);
|
|
|
50 |
$context = context_module::instance($cm->id);
|
|
|
51 |
|
|
|
52 |
// Trigger the page view event.
|
|
|
53 |
$sink = $this->redirectEvents();
|
|
|
54 |
$pageevent = \core_tests\event\course_module_viewed::create(array(
|
|
|
55 |
'context' => $context,
|
|
|
56 |
'courseid' => $course->id,
|
|
|
57 |
'objectid' => $feed->id
|
|
|
58 |
));
|
|
|
59 |
$pageevent->trigger();
|
|
|
60 |
$result = $sink->get_events();
|
|
|
61 |
$event = reset($result);
|
|
|
62 |
$sink->close();
|
|
|
63 |
|
|
|
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);
|
|
|
68 |
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test custom validations of the event.
|
|
|
73 |
*/
|
|
|
74 |
public function test_event_validations() {
|
|
|
75 |
|
|
|
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->assertStringContainsString("course_module_viewed event must define objectid and object table.", $e->getMessage());
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->assertDebuggingCalled('Inconsistent courseid - context combination detected.');
|
|
|
87 |
$this->resetDebugging();
|
|
|
88 |
|
|
|
89 |
try {
|
|
|
90 |
\core_tests\event\course_module_viewed::create(array(
|
|
|
91 |
'contextid' => 1,
|
|
|
92 |
'courseid' => 2,
|
|
|
93 |
));
|
|
|
94 |
} catch (coding_exception $e) {
|
|
|
95 |
$this->assertStringContainsString("course_module_viewed event must define objectid and object table.", $e->getMessage());
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$this->assertDebuggingCalled('Inconsistent courseid - context combination detected.');
|
|
|
99 |
}
|
|
|
100 |
}
|