MDL-41258 Forum: Add additional unit tests for forum digest settings
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 14 Aug 2013 15:50:43 +0000 (16:50 +0100)
committerAndrew Nicols <andrew@nicols.co.uk>
Sat, 17 Aug 2013 23:03:06 +0000 (00:03 +0100)
mod/forum/tests/maildigest_test.php

index b6bed86..6dfc293 100644 (file)
@@ -30,38 +30,104 @@ global $CFG;
 
 class mod_forum_maildigest_testcase extends advanced_testcase {
 
-    public function test_mod_forum_set_maildigest() {
-        global $USER, $DB;
+    public function helper_setup_user_in_course() {
+        global $DB;
 
-        $this->resetAfterTest(true);
+        $return = new stdClass();
+        $return->courses = new stdClass();
+        $return->forums = new stdClass();
+        $return->forumids = array();
 
         // Create a user.
         $user = self::getDataGenerator()->create_user();
-
-        // Set to the user.
-        self::setUser($user);
+        $return->user = $user;
 
         // Create courses to add the modules.
-        $course1 = self::getDataGenerator()->create_course();
-
-        $forumids = array();
+        $return->courses->course1 = self::getDataGenerator()->create_course();
 
-        // First forum.
+        // Create forums.
         $record = new stdClass();
         $record->introformat = FORMAT_HTML;
-        $record->course = $course1->id;
-        $forum1 = self::getDataGenerator()->create_module('forum', $record);
-        $forumids[] = $forum1->id;
+        $record->course = $return->courses->course1->id;
+        $record->forcesubscribe = 1;
+
+        $return->forums->forum1 = self::getDataGenerator()->create_module('forum', $record);
+        $return->forumsids[] = $return->forums->forum1->id;
+
+        $return->forums->forum2 = self::getDataGenerator()->create_module('forum', $record);
+        $return->forumsids[] = $return->forums->forum2->id;
 
         // Check the forum was correctly created.
-        list ($test, $params) = $DB->get_in_or_equal($forumids, SQL_PARAMS_NAMED, 'forum');
+        list ($test, $params) = $DB->get_in_or_equal($return->forumsids, SQL_PARAMS_NAMED, 'forum');
 
-        $this->assertEquals(count($forumids),
+        $this->assertEquals(count($return->forumsids),
             $DB->count_records_select('forum', 'id ' . $test, $params));
 
         // Enrol the user in the courses.
         // DataGenerator->enrol_user automatically sets a role for the user
-        $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
+        $this->getDataGenerator()->enrol_user($return->user->id, $return->courses->course1->id, null, 'manual');
+
+        return $return;
+    }
+
+    public function helper_mod_forum_cron() {
+        global $CFG;
+
+        $return = new stdClass();
+
+        // Messaging is not compatible with transactions...
+        $this->preventResetByRollback();
+
+        $return->max_execution_time = ini_get('max_execution_time');
+
+        // Catch all messages
+        $return->messagesink = $this->redirectMessages();
+        $return->mailsink = $this->redirectEmails();
+
+        // Confirm that we have an empty message sink so far.
+        $messages = $return->messagesink->get_messages();
+        $this->assertEquals(0, count($messages));
+
+        $messages = $return->mailsink->get_messages();
+        $this->assertEquals(0, count($messages));
+
+        // Tell Moodle that we've not sent any digest messages out recently.
+        $CFG->digestmailtimelast = 0;
+        // And set the digest sending time to just after midnight to ensure it gets processed.
+        $CFG->digestmailtime = date('H');
+
+        // Forcibly reduce the maxeditingtime to a one second to ensure that
+        // messages are sent out.
+        $CFG->maxeditingtime = 1;
+
+        // Ensure that we don't prevent e-mail as this will cause unit test failures.
+        $CFG->noemailever = false;
+
+        return $return;
+    }
+
+    public function helper_force_digest_mail_times() {
+        global $CFG, $DB;
+        // Fake all of the post editing times because digests aren't sent until
+        // the start of an hour where the modification time on the message is before
+        // the start of that hour
+        $digesttime = usergetmidnight(time(), $CFG->timezone) + ($CFG->digestmailtime * 3600) - (60 * 60);
+        $DB->set_field('forum_posts', 'modified', $digesttime, array('mailed' => 0));
+        $DB->set_field('forum_posts', 'created', $digesttime, array('mailed' => 0));
+    }
+
+    public function test_set_maildigest() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $helper = $this->helper_setup_user_in_course();
+        $user = $helper->user;
+        $course1 = $helper->courses->course1;
+        $forum1 = $helper->forums->forum1;
+
+        // Set to the user.
+        self::setUser($helper->user);
 
         // Confirm that there is no current value.
         $currentsetting = $DB->get_record('forum_digests', array(
@@ -106,16 +172,19 @@ class mod_forum_maildigest_testcase extends advanced_testcase {
         forum_set_user_maildigest($forum1, 42, $user);
     }
 
-    public function test_mod_forum_get_user_digest_options_default() {
+    public function test_get_user_digest_options_default() {
         global $USER, $DB;
 
         $this->resetAfterTest(true);
 
-        // Create a user.
-        $user = self::getDataGenerator()->create_user();
+        // Set up a basic user enrolled in a course.
+        $helper = $this->helper_setup_user_in_course();
+        $user = $helper->user;
+        $course1 = $helper->courses->course1;
+        $forum1 = $helper->forums->forum1;
 
         // Set to the user.
-        self::setUser($user);
+        self::setUser($helper->user);
 
         // We test against these options.
         $digestoptions = array(
@@ -142,16 +211,19 @@ class mod_forum_maildigest_testcase extends advanced_testcase {
         $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[2]));
     }
 
-    public function test_mod_forum_get_user_digest_options_sorting() {
+    public function test_get_user_digest_options_sorting() {
         global $USER, $DB;
 
         $this->resetAfterTest(true);
 
-        // Create a user.
-        $user = self::getDataGenerator()->create_user();
+        // Set up a basic user enrolled in a course.
+        $helper = $this->helper_setup_user_in_course();
+        $user = $helper->user;
+        $course1 = $helper->courses->course1;
+        $forum1 = $helper->forums->forum1;
 
         // Set to the user.
-        self::setUser($user);
+        self::setUser($helper->user);
 
         // Retrieve the list of applicable options.
         $options = forum_get_user_digest_options();
@@ -164,4 +236,325 @@ class mod_forum_maildigest_testcase extends advanced_testcase {
         }
     }
 
+    public function test_cron_no_posts() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $helper = $this->helper_mod_forum_cron();
+
+        $this->helper_force_digest_mail_times();
+
+        // Initially the forum cron should generate no messages as we've made no posts.
+        ob_start();
+        forum_cron();
+        ob_end_clean();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(0, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(0, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
+    /**
+     * Sends several notifications to one user as:
+     * * single messages based on a user profile setting.
+     */
+    public function test_cron_profile_single_mails() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        // Set up the basic cron
+        $helper = $this->helper_mod_forum_cron();
+
+        // Set up a basic user enrolled in a course.
+        $userhelper = $this->helper_setup_user_in_course();
+        $user = $userhelper->user;
+        $course1 = $userhelper->courses->course1;
+        $forum1 = $userhelper->forums->forum1;
+        $forum2 = $userhelper->forums->forum2;
+
+        // Add some discussions to the forum.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user->id;
+        $record->mailnow = 1;
+
+        $record->forum = $forum1->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $record->forum = $forum2->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $this->helper_force_digest_mail_times();
+
+        // Set the tested user's default maildigest setting.
+        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
+
+        // Set the maildigest preference for forum1 to default.
+        forum_set_user_maildigest($forum1, -1, $user);
+
+        // Set the maildigest preference for forum2 to default.
+        forum_set_user_maildigest($forum2, -1, $user);
+
+        $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/');
+        forum_cron();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(10, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(0, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
+    /**
+     * Sends several notifications to one user as:
+     * * daily digests coming from the user profile setting.
+     */
+    public function test_cron_profile_digest_email() {
+        global $DB, $CFG;
+
+        $this->resetAfterTest(true);
+
+        // Set up the basic cron
+        $helper = $this->helper_mod_forum_cron();
+
+        // Set up a basic user enrolled in a course.
+        $userhelper = $this->helper_setup_user_in_course();
+        $user = $userhelper->user;
+        $course1 = $userhelper->courses->course1;
+        $forum1 = $userhelper->forums->forum1;
+        $forum2 = $userhelper->forums->forum2;
+
+        // Add a discussion to the forums.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user->id;
+        $record->mailnow = 1;
+
+        $record->forum = $forum1->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $record->forum = $forum2->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $this->helper_force_digest_mail_times();
+
+        // Set the tested user's default maildigest setting.
+        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
+
+        // Set the maildigest preference for forum1 to default.
+        forum_set_user_maildigest($forum1, -1, $user);
+
+        // Set the maildigest preference for forum2 to default.
+        forum_set_user_maildigest($forum2, -1, $user);
+
+        $this->expectOutputRegex('/Email digests successfully sent to 1 users./');
+        forum_cron();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(0, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(1, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
+    /**
+     * Sends several notifications to one user as:
+     * * daily digests coming from the per-forum setting; and
+     * * single e-mails from the profile setting.
+     */
+    public function test_cron_mixed_email_1() {
+        global $DB, $CFG;
+
+        $this->resetAfterTest(true);
+
+        // Set up the basic cron
+        $helper = $this->helper_mod_forum_cron();
+
+        // Set up a basic user enrolled in a course.
+        $userhelper = $this->helper_setup_user_in_course();
+        $user = $userhelper->user;
+        $course1 = $userhelper->courses->course1;
+        $forum1 = $userhelper->forums->forum1;
+        $forum2 = $userhelper->forums->forum2;
+
+        // Add a discussion to the forums.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user->id;
+        $record->mailnow = 1;
+
+        $record->forum = $forum1->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $record->forum = $forum2->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $this->helper_force_digest_mail_times();
+
+        // Set the tested user's default maildigest setting.
+        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
+
+        // Set the maildigest preference for forum1 to digest.
+        forum_set_user_maildigest($forum1, 1, $user);
+
+        // Set the maildigest preference for forum2 to single.
+        forum_set_user_maildigest($forum2, -1, $user);
+
+        $this->expectOutputRegex('/Email digests successfully sent to 1 users./');
+        forum_cron();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(5, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(1, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
+    /**
+     * Sends several notifications to one user as:
+     * * single e-mails from the per-forum setting; and
+     * * daily digests coming from the per-user setting.
+     */
+    public function test_cron_mixed_email_2() {
+        global $DB, $CFG;
+
+        $this->resetAfterTest(true);
+
+        // Set up the basic cron
+        $helper = $this->helper_mod_forum_cron();
+
+        // Set up a basic user enrolled in a course.
+        $userhelper = $this->helper_setup_user_in_course();
+        $user = $userhelper->user;
+        $course1 = $userhelper->courses->course1;
+        $forum1 = $userhelper->forums->forum1;
+        $forum2 = $userhelper->forums->forum2;
+
+        // Add a discussion to the forums.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user->id;
+        $record->mailnow = 1;
+
+        $record->forum = $forum1->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $record->forum = $forum2->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $this->helper_force_digest_mail_times();
+
+        // Set the tested user's default maildigest setting.
+        $DB->set_field('user', 'maildigest', 1, array('id' => $user->id));
+
+        // Set the maildigest preference for forum1 to digest.
+        forum_set_user_maildigest($forum1, -1, $user);
+
+        // Set the maildigest preference for forum2 to single.
+        forum_set_user_maildigest($forum2, 0, $user);
+
+        $this->expectOutputRegex('/Email digests successfully sent to 1 users./');
+        forum_cron();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(5, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(1, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
+    /**
+     * Sends several notifications to one user as:
+     * * daily digests coming from the per-forum setting.
+     */
+    public function test_cron_forum_digest_email() {
+        global $DB, $CFG;
+
+        $this->resetAfterTest(true);
+
+        // Set up the basic cron
+        $helper = $this->helper_mod_forum_cron();
+
+        // Set up a basic user enrolled in a course.
+        $userhelper = $this->helper_setup_user_in_course();
+        $user = $userhelper->user;
+        $course1 = $userhelper->courses->course1;
+        $forum1 = $userhelper->forums->forum1;
+        $forum2 = $userhelper->forums->forum2;
+
+        // Add a discussion to the forums.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user->id;
+        $record->mailnow = 1;
+
+        $record->forum = $forum1->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $record->forum = $forum2->id;
+        for ($i = 0; $i < 5; $i++) {
+            self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+        }
+
+        $this->helper_force_digest_mail_times();
+
+        // Set the tested user's default maildigest setting.
+        $DB->set_field('user', 'maildigest', 0, array('id' => $user->id));
+
+        // Set the maildigest preference for forum1 to digest.
+        forum_set_user_maildigest($forum1, 1, $user);
+
+        // Set the maildigest preference for forum2 to single.
+        forum_set_user_maildigest($forum2, 2, $user);
+
+        $this->expectOutputRegex('/Email digests successfully sent to 1 users./');
+        forum_cron();
+
+        $messages = $helper->messagesink->get_messages();
+        $this->assertEquals(0, count($messages));
+        $messages = $helper->mailsink->get_messages();
+        $this->assertEquals(1, count($messages));
+
+        $helper->messagesink->clear();
+        $helper->mailsink->clear();
+        @ini_set('max_execution_time', $helper->max_execution_time);
+    }
+
 }