From 7bb7de44cfedcc097344e393be130194315d95aa Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 23 Apr 2014 14:45:54 +0800 Subject: [PATCH] MDL-45075 mod_forum: Add unit tests for forum subscription on forum creation --- mod/forum/tests/lib_test.php | 114 +++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index d50a7a72ed9..78b4d037b84 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -575,4 +575,118 @@ class mod_forum_lib_testcase extends advanced_testcase { $this->assertEquals(true, isset($result[$forumoptional->id])); $this->assertEquals(true, isset($result[$forumforce->id])); } + + /** + * Test subscription using automatic subscription on create. + */ + public function test_forum_auto_subscribe_on_create() { + global $CFG; + + $this->resetAfterTest(); + + $usercount = 5; + $course = $this->getDataGenerator()->create_course(); + $users = array(); + + for ($i = 0; $i < $usercount; $i++) { + $user = $this->getDataGenerator()->create_user(); + $users[] = $user; + $this->getDataGenerator()->enrol_user($user->id, $course->id); + } + + $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); // Automatic Subscription. + $forum = $this->getDataGenerator()->create_module('forum', $options); + + $result = forum_subscribed_users($course, $forum); + $this->assertEquals($usercount, count($result)); + foreach ($users as $user) { + $this->assertTrue(forum_is_subscribed($user->id, $forum)); + } + } + + /** + * Test subscription using forced subscription on create. + */ + public function test_forum_forced_subscribe_on_create() { + global $CFG; + + $this->resetAfterTest(); + + $usercount = 5; + $course = $this->getDataGenerator()->create_course(); + $users = array(); + + for ($i = 0; $i < $usercount; $i++) { + $user = $this->getDataGenerator()->create_user(); + $users[] = $user; + $this->getDataGenerator()->enrol_user($user->id, $course->id); + } + + $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE); // Forced subscription. + $forum = $this->getDataGenerator()->create_module('forum', $options); + + $result = forum_subscribed_users($course, $forum); + $this->assertEquals($usercount, count($result)); + foreach ($users as $user) { + $this->assertTrue(forum_is_subscribed($user->id, $forum)); + } + } + + /** + * Test subscription using optional subscription on create. + */ + public function test_forum_optional_subscribe_on_create() { + global $CFG; + + $this->resetAfterTest(); + + $usercount = 5; + $course = $this->getDataGenerator()->create_course(); + $users = array(); + + for ($i = 0; $i < $usercount; $i++) { + $user = $this->getDataGenerator()->create_user(); + $users[] = $user; + $this->getDataGenerator()->enrol_user($user->id, $course->id); + } + + $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); // Subscription optional. + $forum = $this->getDataGenerator()->create_module('forum', $options); + + $result = forum_subscribed_users($course, $forum); + // No subscriptions by default. + $this->assertEquals(0, count($result)); + foreach ($users as $user) { + $this->assertFalse(forum_is_subscribed($user->id, $forum)); + } + } + + /** + * Test subscription using disallow subscription on create. + */ + public function test_forum_disallow_subscribe_on_create() { + global $CFG; + + $this->resetAfterTest(); + + $usercount = 5; + $course = $this->getDataGenerator()->create_course(); + $users = array(); + + for ($i = 0; $i < $usercount; $i++) { + $user = $this->getDataGenerator()->create_user(); + $users[] = $user; + $this->getDataGenerator()->enrol_user($user->id, $course->id); + } + + $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE); // Subscription prevented. + $forum = $this->getDataGenerator()->create_module('forum', $options); + + $result = forum_subscribed_users($course, $forum); + // No subscriptions by default. + $this->assertEquals(0, count($result)); + foreach ($users as $user) { + $this->assertFalse(forum_is_subscribed($user->id, $forum)); + } + } }