Skip to content

Commit

Permalink
MDL-43293 Events: Added unit test for comment events
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Dec 23, 2013
1 parent a55eaf0 commit cdfd607
Show file tree
Hide file tree
Showing 5 changed files with 620 additions and 2 deletions.
182 changes: 182 additions & 0 deletions blocks/comments/tests/events_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Events tests.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Events tests class.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_comments_events_testcase extends advanced_testcase {
/** @var stdClass Keeps course object */
private $course;

/** @var stdClass Keeps wiki object */
private $wiki;

/**
* Setup test data.
*/
public function setUp() {
$this->resetAfterTest();
$this->setAdminUser();

// Create course and wiki.
$this->course = $this->getDataGenerator()->create_course();
$this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
}

/**
* Test comment_created event.
*/
public function test_comment_created() {
global $CFG;

require_once($CFG->dirroot . '/comment/lib.php');

// Comment on course page.
$context = context_course::instance($this->course->id);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/course/view.php', array('id' => $this->course->id));
$this->assertEquals($url, $event->get_url());

// Comments when block is on module (wiki) page.
$context = context_module::instance($this->wiki->id);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment 1');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$this->assertEquals($url, $event->get_url());
}

/**
* Test comment_deleted event.
*/
public function test_comment_deleted() {
global $CFG;

require_once($CFG->dirroot . '/comment/lib.php');

// Comment on course page.
$context = context_course::instance($this->course->id);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);
$newcomment = $comment->add('New comment');

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/course/view.php', array('id' => $this->course->id));
$this->assertEquals($url, $event->get_url());

// Comments when block is on module (wiki) page.
$context = context_module::instance($this->wiki->id);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);
$newcomment = $comment->add('New comment 1');

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$this->assertEquals($url, $event->get_url());
}
}
73 changes: 72 additions & 1 deletion blog/tests/bloglib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function test_blog_association_created_event_validations() {
public function test_blog_entries_viewed_event() {

$this->setAdminUser();
$this->resetAfterTest();

$other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);

Expand All @@ -382,5 +382,76 @@ public function test_blog_entries_viewed_event() {
$arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
$this->assertEventLegacyLogData($arr, $event);
}

/**
* Test comment_created event.
*/
public function test_blog_comment_created_event() {
global $USER, $CFG;

$this->setAdminUser();

require_once($CFG->dirroot . '/comment/lib.php');
$context = context_user::instance($USER->id);

$cmt = new stdClass();
$cmt->context = $context;
$cmt->courseid = $this->courseid;
$cmt->area = 'format_blog';
$cmt->itemid = $this->postid;
$cmt->showcount = 1;
$cmt->component = 'blog';
$manager = new comment($cmt);

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$manager->add("New comment");
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\core\event\blog_comment_created', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
}

/**
* Test comment_deleted event.
*/
public function test_blog_comment_deleted_event() {
global $USER, $CFG;

$this->setAdminUser();

require_once($CFG->dirroot . '/comment/lib.php');
$context = context_user::instance($USER->id);

$cmt = new stdClass();
$cmt->context = $context;
$cmt->courseid = $this->courseid;
$cmt->area = 'format_blog';
$cmt->itemid = $this->postid;
$cmt->showcount = 1;
$cmt->component = 'blog';
$manager = new comment($cmt);
$newcomment = $manager->add("New comment");

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$manager->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
}
}

116 changes: 116 additions & 0 deletions mod/assign/submission/comments/tests/events_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Events tests.
*
* @package assignsubmission_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/mod/assign/lib.php');
require_once($CFG->dirroot . '/mod/assign/locallib.php');
require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');

/**
* Events tests class.
*
* @package assignsubmission_comments
* @category test
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class assignsubmission_comments_events_testcase extends mod_assign_base_testcase {

/**
* Test comment_created event.
*/
public function test_comment_created() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');

$this->setUser($this->editingteachers[0]);
$assign = $this->create_instance();
$submission = $assign->get_user_submission($this->students[0]->id, true);

$context = $assign->get_context();
$options = new stdClass();
$options->area = 'submission_comments';
$options->course = $assign->get_course();
$options->context = $context;
$options->itemid = $submission->id;
$options->component = 'assignsubmission_comments';
$options->showcount = true;
$options->displaycancel = true;

$comment = new comment($options);

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\assignsubmission_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$this->assertEquals($url, $event->get_url());
}

/**
* Test comment_deleted event.
*/
public function test_comment_deleted() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');

$this->setUser($this->editingteachers[0]);
$assign = $this->create_instance();
$submission = $assign->get_user_submission($this->students[0]->id, true);

$context = $assign->get_context();
$options = new stdClass();
$options->area = 'submission_comments';
$options->course = $assign->get_course();
$options->context = $context;
$options->itemid = $submission->id;
$options->component = 'assignsubmission_comments';
$options->showcount = true;
$options->displaycancel = true;
$comment = new comment($options);
$newcomment = $comment->add('New comment 1');

// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\assignsubmission_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$this->assertEquals($url, $event->get_url());
}
}
Loading

0 comments on commit cdfd607

Please sign in to comment.