forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-43293 Events: Added unit test for comment events
- Loading branch information
Rajesh Taneja
committed
Dec 23, 2013
1 parent
a55eaf0
commit cdfd607
Showing
5 changed files
with
620 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.