Merge branch 'MDL-55609-master' of git://github.com/andrewnicols/moodle
[moodle.git] / blocks / comments / tests / events_test.php
blob1e7aae4b73193ce39859208853d52389cd9692c7
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 * Events tests.
20 * @package block_comments
21 * @category test
22 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Events tests class.
31 * @package block_comments
32 * @category test
33 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class block_comments_events_testcase extends advanced_testcase {
37 /** @var stdClass Keeps course object */
38 private $course;
40 /** @var stdClass Keeps wiki object */
41 private $wiki;
43 /**
44 * Setup test data.
46 public function setUp() {
47 $this->resetAfterTest();
48 $this->setAdminUser();
50 // Create course and wiki.
51 $this->course = $this->getDataGenerator()->create_course();
52 $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
55 /**
56 * Test comment_created event.
58 public function test_comment_created() {
59 global $CFG;
61 require_once($CFG->dirroot . '/comment/lib.php');
63 // Comment on course page.
64 $context = context_course::instance($this->course->id);
65 $args = new stdClass;
66 $args->context = $context;
67 $args->course = $this->course;
68 $args->area = 'page_comments';
69 $args->itemid = 0;
70 $args->component = 'block_comments';
71 $args->linktext = get_string('showcomments');
72 $args->notoggle = true;
73 $args->autostart = true;
74 $args->displaycancel = false;
75 $comment = new comment($args);
77 // Triggering and capturing the event.
78 $sink = $this->redirectEvents();
79 $comment->add('New comment');
80 $events = $sink->get_events();
81 $this->assertCount(1, $events);
82 $event = reset($events);
84 // Checking that the event contains the expected values.
85 $this->assertInstanceOf('\block_comments\event\comment_created', $event);
86 $this->assertEquals($context, $event->get_context());
87 $url = new moodle_url('/course/view.php', array('id' => $this->course->id));
88 $this->assertEquals($url, $event->get_url());
90 // Comments when block is on module (wiki) page.
91 $context = context_module::instance($this->wiki->cmid);
92 $args = new stdClass;
93 $args->context = $context;
94 $args->course = $this->course;
95 $args->area = 'page_comments';
96 $args->itemid = 0;
97 $args->component = 'block_comments';
98 $args->linktext = get_string('showcomments');
99 $args->notoggle = true;
100 $args->autostart = true;
101 $args->displaycancel = false;
102 $comment = new comment($args);
104 // Triggering and capturing the event.
105 $sink = $this->redirectEvents();
106 $comment->add('New comment 1');
107 $events = $sink->get_events();
108 $this->assertCount(1, $events);
109 $event = reset($events);
111 // Checking that the event contains the expected values.
112 $this->assertInstanceOf('\block_comments\event\comment_created', $event);
113 $this->assertEquals($context, $event->get_context());
114 $url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
115 $this->assertEquals($url, $event->get_url());
116 $this->assertEventContextNotUsed($event);
120 * Test comment_deleted event.
122 public function test_comment_deleted() {
123 global $CFG;
125 require_once($CFG->dirroot . '/comment/lib.php');
127 // Comment on course page.
128 $context = context_course::instance($this->course->id);
129 $args = new stdClass;
130 $args->context = $context;
131 $args->course = $this->course;
132 $args->area = 'page_comments';
133 $args->itemid = 0;
134 $args->component = 'block_comments';
135 $args->linktext = get_string('showcomments');
136 $args->notoggle = true;
137 $args->autostart = true;
138 $args->displaycancel = false;
139 $comment = new comment($args);
140 $newcomment = $comment->add('New comment');
142 // Triggering and capturing the event.
143 $sink = $this->redirectEvents();
144 $comment->delete($newcomment->id);
145 $events = $sink->get_events();
146 $this->assertCount(1, $events);
147 $event = reset($events);
149 // Checking that the event contains the expected values.
150 $this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
151 $this->assertEquals($context, $event->get_context());
152 $url = new moodle_url('/course/view.php', array('id' => $this->course->id));
153 $this->assertEquals($url, $event->get_url());
155 // Comments when block is on module (wiki) page.
156 $context = context_module::instance($this->wiki->cmid);
157 $args = new stdClass;
158 $args->context = $context;
159 $args->course = $this->course;
160 $args->area = 'page_comments';
161 $args->itemid = 0;
162 $args->component = 'block_comments';
163 $args->linktext = get_string('showcomments');
164 $args->notoggle = true;
165 $args->autostart = true;
166 $args->displaycancel = false;
167 $comment = new comment($args);
168 $newcomment = $comment->add('New comment 1');
170 // Triggering and capturing the event.
171 $sink = $this->redirectEvents();
172 $comment->delete($newcomment->id);
173 $events = $sink->get_events();
174 $this->assertCount(1, $events);
175 $event = reset($events);
177 // Checking that the event contains the expected values.
178 $this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
179 $this->assertEquals($context, $event->get_context());
180 $url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
181 $this->assertEquals($url, $event->get_url());
182 $this->assertEventContextNotUsed($event);