From ebfa74c7990d039f1f615f57c5bbb568f277eea5 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 26 Mar 2015 18:16:27 +0100 Subject: [PATCH] MDL-49502 forum: Unit tests for forum_view --- mod/forum/tests/lib_test.php | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 53c1716d92f..27d23627dcf 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -1256,6 +1256,45 @@ class mod_forum_lib_testcase extends advanced_testcase { } } + public function test_forum_view() { + global $CFG; + + $CFG->enablecompletion = 1; + $this->resetAfterTest(); + + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), + array('completion' => 2, 'completionview' => 1)); + $context = context_module::instance($forum->cmid); + $cm = get_coursemodule_from_instance('forum', $forum->id); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $this->setAdminUser(); + forum_view($forum, $course, $cm, $context); + + $events = $sink->get_events(); + // 2 additional events thanks to completion. + $this->assertCount(3, $events); + $event = array_pop($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id)); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + // Check completion status. + $completion = new completion_info($course); + $completiondata = $completion->get_data($cm); + $this->assertEquals(1, $completiondata->completionstate); + + } + /** * Create a new course, forum, and user with a number of discussions and replies. *