MDL-43044 events: Add unit tests

This commit is contained in:
Ankit Agarwal
2013-12-06 16:08:03 +08:00
parent 45faf15dfd
commit 1f2aeda91e
7 changed files with 195 additions and 9 deletions
@@ -0,0 +1,75 @@
<?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/>.
/**
* Tests for base course module instance list viewed event.
*
* @package core
* @category phpunit
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/fixtures/event_mod_fixtures.php');
/**
* Class core_event_course_module_instance_list_viewed_testcase
*
* Tests for event \core\event\course_module_instance_list_viewed_testcase
*/
class core_event_course_module_instance_list_viewed_testcase extends advanced_testcase {
/**
* Test event properties and methods.
*/
public function test_event_attributes() {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
// Trigger the page view event.
$sink = $this->redirectEvents();
$event = \mod_unittests\event\course_module_instance_list_viewed::create(array(
'context' => $context,
));
$event->trigger();
$result = $sink->get_events();
$event = reset($result);
$sink->close();
// Test event data.
$legacydata = array($course->id, 'unittests', 'view all', 'index.php?id=' . $course->id, '');
$this->assertEventLegacyLogData($legacydata, $event);
$url = new moodle_url('/mod/unittests/index.php', array('id' => $course->id));
$this->assertEquals($url, $event->get_url());
}
/**
* Test custom validations of the event.
*/
public function test_event_validations() {
try {
\mod_unittests\event\course_module_instance_list_viewed::create(array('context' => context_system::instance()));
$this->fail('Event validation should not allow course_module_instance_list_viewed event to be triggered without outside
course context');
} catch (Exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}
}
+45
View File
@@ -0,0 +1,45 @@
<?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/>.
/**
* Tests for deprecated events. Please add tests for deprecated events in this file.
*
* @package core
* @category phpunit
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Class core_event_instances_list_viewed_testcase
*
* Tests for deprecated events.
*/
class core_event_deprecated_testcase extends advanced_testcase {
/**
* Test event properties and methods.
*/
public function test_deprecated_course_module_instances_list_viewed_events() {
// Make sure the abstract class course_module_instances_list_viewed generates a debugging notice.
require_once(__DIR__.'/fixtures/event_mod_badfixtures.php');
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}
}
+33
View File
@@ -0,0 +1,33 @@
<?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/>.
/**
* Fixtures for new event testing under namespace mod_tests.
*
* @package core
* @category phpunit
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_unittests\event;
defined('MOODLE_INTERNAL') || die();
/**
* Class course_module_instances_list_viewed
*/
class course_module_instances_list_viewed extends \core\event\course_module_instances_list_viewed {
}
+33
View File
@@ -0,0 +1,33 @@
<?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/>.
/**
* Fixtures for new event testing under namespace mod_tests.
*
* @package core
* @category phpunit
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_unittests\event;
defined('MOODLE_INTERNAL') || die();
/**
* Class course_module_instance_list_viewed
*/
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
}
+3 -3
View File
@@ -137,7 +137,7 @@ class mod_book_events_testcase extends advanced_testcase {
$this->assertEventLegacyLogData(array('1', 2, false), $event);
}
public function test_instances_list_viewed() {
public function test_course_module_instance_list_viewed() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
@@ -145,7 +145,7 @@ class mod_book_events_testcase extends advanced_testcase {
$params = array(
'context' => context_course::instance($course->id)
);
$event = \mod_book\event\instances_list_viewed::create($params);
$event = \mod_book\event\course_module_instance_list_viewed::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
@@ -155,7 +155,7 @@ class mod_book_events_testcase extends advanced_testcase {
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_book\event\instances_list_viewed', $event);
$this->assertInstanceOf('\mod_book\event\course_module_instance_list_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'book', 'view all', 'index.php?id='.$course->id, '');
$this->assertEventLegacyLogData($expected, $event);
+3 -3
View File
@@ -134,7 +134,7 @@ class mod_chat_events_testcase extends advanced_testcase {
$this->assertEventLegacyLogData($expected, $event);
}
public function test_instances_list_viewed() {
public function test_course_module_instance_list_viewed() {
global $USER;
$this->resetAfterTest();
@@ -146,12 +146,12 @@ class mod_chat_events_testcase extends advanced_testcase {
$params = array(
'context' => context_course::instance($course->id)
);
$event = \mod_chat\event\instances_list_viewed::create($params);
$event = \mod_chat\event\course_module_instance_list_viewed::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\mod_chat\event\instances_list_viewed', $event);
$this->assertInstanceOf('\mod_chat\event\course_module_instance_list_viewed', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'chat', 'view all', "index.php?id=$course->id", '');
+3 -3
View File
@@ -236,7 +236,7 @@ class mod_choice_events_testcase extends advanced_testcase {
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_instances_list_viewed() {
public function test_course_module_instance_list_viewed_viewed() {
global $USER;
// Not much can be tested here as the event is only triggered on a page load,
@@ -244,12 +244,12 @@ class mod_choice_events_testcase extends advanced_testcase {
$this->setAdminUser();
$params = array('context' => context_course::instance($this->course->id));
$event = \mod_choice\event\instances_list_viewed::create($params);
$event = \mod_choice\event\course_module_instance_list_viewed::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\mod_choice\event\instances_list_viewed', $event);
$this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
$expected = array($this->course->id, 'choice', 'view all', 'index.php?id=' . $this->course->id, '');