2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package block_comments
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();
31 * @package block_comments
33 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_comment_commentlib_testcase extends advanced_testcase {
42 public function test_comments_get_comments() {
44 $commenttext = 'New comment';
45 //$this->resetAfterTest();
46 $this->setAdminUser();
48 $this->course = $this->getDataGenerator()->create_course();
49 $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
50 require_once($CFG->dirroot . '/comment/lib.php');
52 // Comment on course page.
53 $context = context_course::instance($this->course->id);
55 $args->context = $context;
56 $args->course = $this->course;
57 $args->area = 'page_comments';
59 $args->component = 'block_comments';
60 $args->linktext = get_string('showcomments');
61 $args->notoggle = true;
62 $args->autostart = true;
63 $args->displaycancel = false;
64 $comment = new comment($args);
66 // Triggering and capturing the event.
67 $sink = $this->redirectEvents();
68 $comment->add($commenttext);
69 $events = $sink->get_events();
70 $this->assertCount(1, $events);
71 $event = reset($events);
73 // Checking that the event contains the expected values.
74 $this->assertInstanceOf('\block_comments\event\comment_created', $event);
75 $this->assertEquals($context, $event->get_context());
76 $url = new moodle_url('/course/view.php', array('id' => $this->course->id));
77 $this->assertEquals($url, $event->get_url());
78 $a = $comment->get_comments(0);
83 // Comments when block is on module (wiki) page.
84 $context = context_module::instance($this->wiki->cmid);
86 $args->context = $context;
87 $args->course = $this->course;
88 $args->area = 'page_comments';
90 $args->component = 'block_comments';
91 $args->linktext = get_string('showcomments');
92 $args->notoggle = true;
93 $args->autostart = true;
94 $args->displaycancel = false;
95 $comment = new comment($args);
97 // Triggering and capturing the event.
98 $sink = $this->redirectEvents();
99 $comment->add('New comment 1');
100 $events = $sink->get_events();
101 $this->assertCount(1, $events);
102 $event = reset($events);
104 // Checking that the event contains the expected values.
105 $this->assertInstanceOf('\block_comments\event\comment_created', $event);
106 $this->assertEquals($context, $event->get_context());
107 $url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
108 $this->assertEquals($url, $event->get_url());
109 $this->assertEventContextNotUsed($event);