Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / event_content_viewed_test.php
blob859173e98d761b61c8ee60819fb6154fb6741eff
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 content 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_page_viewed_testcase
32 * Tests for event \core\event\page_viewed
34 class core_event_content_viewed_testcase extends advanced_testcase {
36 /**
37 * Set basic page properties.
39 public function setUp() {
40 global $PAGE;
41 // Set page details.
42 $PAGE->set_url('/someurl.php');
43 $PAGE->set_pagelayout('somelayout');
46 /**
47 * Test event properties and methods.
49 public function test_event_attributes() {
50 global $PAGE;
52 $this->resetAfterTest();
54 // Trigger the page view event.
55 $sink = $this->redirectEvents();
56 $pageevent = \core_tests\event\content_viewed::create(array('other' => array('content' => 'tests')));
57 $pageevent->set_page_detail(); // Set page details.
58 $legacydata = array(SITEID, 'site', 'view', 'view.php?id=' . SITEID, SITEID);
59 $pageevent->set_legacy_logdata($legacydata); // Set legacy data.
60 $pageevent->trigger();
61 $result = $sink->get_events();
62 $event = reset($result);
64 // Test page details.
65 $data = array( 'url' => $PAGE->url->out_as_local_url(false),
66 'heading' => $PAGE->heading,
67 'title' => $PAGE->title,
68 'content' => 'tests');
69 $this->assertEquals($data, $event->other);
71 // Test legacy stuff.
72 $this->assertEventLegacyLogData($legacydata, $event);
73 $pageevent = \core_tests\event\content_viewed::create(array('other' => array('content' => 'tests')));
74 $pageevent->trigger();
75 $result = $sink->get_events();
76 $event = $result[1];
77 $this->assertEventLegacyLogData(null, $event);
80 /**
81 * Test custom validations.
83 public function test_event_context_exception() {
85 $this->resetAfterTest();
87 // Make sure content identifier is always set.
88 $this->setExpectedException('coding_exception');
89 $pageevent = \core_tests\event\content_viewed::create();
90 $pageevent->set_page_detail();
91 $pageevent->trigger();