From 46017edb0e16fd82ea41083815637c4db4bc28bf 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 034a5904d4c..6873eb048eb 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -123,4 +123,118 @@ class mod_forum_lib_testcase extends advanced_testcase { $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname)); } } + + /** + * 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)); + } + } }