From 1f2aeda91e07ca2db252148b408f19e24f788e2b Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Wed, 4 Dec 2013 16:48:37 +0800 Subject: [PATCH] MDL-43044 events: Add unit tests --- ...ent_course_module_instance_list_viewed.php | 75 +++++++++++++++++++ lib/tests/event_deprecated_test.php | 45 +++++++++++ lib/tests/fixtures/event_mod_badfixtures.php | 33 ++++++++ lib/tests/fixtures/event_mod_fixtures.php | 33 ++++++++ mod/book/tests/events_test.php | 6 +- mod/chat/tests/events_test.php | 6 +- mod/choice/tests/events_test.php | 6 +- 7 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 lib/tests/event_course_module_instance_list_viewed.php create mode 100644 lib/tests/event_deprecated_test.php create mode 100644 lib/tests/fixtures/event_mod_badfixtures.php create mode 100644 lib/tests/fixtures/event_mod_fixtures.php diff --git a/lib/tests/event_course_module_instance_list_viewed.php b/lib/tests/event_course_module_instance_list_viewed.php new file mode 100644 index 00000000000..b129c7814d7 --- /dev/null +++ b/lib/tests/event_course_module_instance_list_viewed.php @@ -0,0 +1,75 @@ +. + +/** + * 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); + } + } +} diff --git a/lib/tests/event_deprecated_test.php b/lib/tests/event_deprecated_test.php new file mode 100644 index 00000000000..aa20e91d118 --- /dev/null +++ b/lib/tests/event_deprecated_test.php @@ -0,0 +1,45 @@ +. + +/** + * 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); + + } +} diff --git a/lib/tests/fixtures/event_mod_badfixtures.php b/lib/tests/fixtures/event_mod_badfixtures.php new file mode 100644 index 00000000000..4b08455b195 --- /dev/null +++ b/lib/tests/fixtures/event_mod_badfixtures.php @@ -0,0 +1,33 @@ +. + +/** + * 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 { +} diff --git a/lib/tests/fixtures/event_mod_fixtures.php b/lib/tests/fixtures/event_mod_fixtures.php new file mode 100644 index 00000000000..5af9fa7c68d --- /dev/null +++ b/lib/tests/fixtures/event_mod_fixtures.php @@ -0,0 +1,33 @@ +. + +/** + * 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 { +} diff --git a/mod/book/tests/events_test.php b/mod/book/tests/events_test.php index d53db31529d..81d5089410f 100644 --- a/mod/book/tests/events_test.php +++ b/mod/book/tests/events_test.php @@ -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); diff --git a/mod/chat/tests/events_test.php b/mod/chat/tests/events_test.php index 1ba312f19af..fe115e07b53 100644 --- a/mod/chat/tests/events_test.php +++ b/mod/chat/tests/events_test.php @@ -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", ''); diff --git a/mod/choice/tests/events_test.php b/mod/choice/tests/events_test.php index ef38abb2327..b747e93b0be 100644 --- a/mod/choice/tests/events_test.php +++ b/mod/choice/tests/events_test.php @@ -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, '');