Revert "MDL-41258 Forum: Add additional unit tests for forum digest settings"
[moodle.git] / mod / forum / tests / maildigest_test.php
CommitLineData
0462b6a1
AN
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * The module forums external functions unit tests
20 *
21 * @package mod_forum
22 * @category external
23 * @copyright 2013 Andrew Nicols
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31class mod_forum_maildigest_testcase extends advanced_testcase {
32
f79f3c00
SH
33 public function test_mod_forum_set_maildigest() {
34 global $USER, $DB;
0462b6a1 35
f79f3c00 36 $this->resetAfterTest(true);
0462b6a1
AN
37
38 // Create a user.
39 $user = self::getDataGenerator()->create_user();
f79f3c00
SH
40
41 // Set to the user.
42 self::setUser($user);
0462b6a1
AN
43
44 // Create courses to add the modules.
f79f3c00
SH
45 $course1 = self::getDataGenerator()->create_course();
46
47 $forumids = array();
0462b6a1 48
f79f3c00 49 // First forum.
0462b6a1
AN
50 $record = new stdClass();
51 $record->introformat = FORMAT_HTML;
f79f3c00
SH
52 $record->course = $course1->id;
53 $forum1 = self::getDataGenerator()->create_module('forum', $record);
54 $forumids[] = $forum1->id;
0462b6a1
AN
55
56 // Check the forum was correctly created.
f79f3c00 57 list ($test, $params) = $DB->get_in_or_equal($forumids, SQL_PARAMS_NAMED, 'forum');
0462b6a1 58
f79f3c00 59 $this->assertEquals(count($forumids),
0462b6a1
AN
60 $DB->count_records_select('forum', 'id ' . $test, $params));
61
62 // Enrol the user in the courses.
63 // DataGenerator->enrol_user automatically sets a role for the user
f79f3c00 64 $this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
0462b6a1
AN
65
66 // Confirm that there is no current value.
67 $currentsetting = $DB->get_record('forum_digests', array(
68 'forum' => $forum1->id,
69 'userid' => $user->id,
70 ));
71 $this->assertFalse($currentsetting);
72
73 // Test with each of the valid values:
74 // 0, 1, and 2 are valid values.
75 forum_set_user_maildigest($forum1, 0, $user);
76 $currentsetting = $DB->get_record('forum_digests', array(
77 'forum' => $forum1->id,
78 'userid' => $user->id,
79 ));
80 $this->assertEquals($currentsetting->maildigest, 0);
81
82 forum_set_user_maildigest($forum1, 1, $user);
83 $currentsetting = $DB->get_record('forum_digests', array(
84 'forum' => $forum1->id,
85 'userid' => $user->id,
86 ));
87 $this->assertEquals($currentsetting->maildigest, 1);
88
89 forum_set_user_maildigest($forum1, 2, $user);
90 $currentsetting = $DB->get_record('forum_digests', array(
91 'forum' => $forum1->id,
92 'userid' => $user->id,
93 ));
94 $this->assertEquals($currentsetting->maildigest, 2);
95
96 // And the default value - this should delete the record again
97 forum_set_user_maildigest($forum1, -1, $user);
98 $currentsetting = $DB->get_record('forum_digests', array(
99 'forum' => $forum1->id,
100 'userid' => $user->id,
101 ));
102 $this->assertFalse($currentsetting);
103
104 // Try with an invalid value.
105 $this->setExpectedException('moodle_exception');
106 forum_set_user_maildigest($forum1, 42, $user);
107 }
108
f79f3c00 109 public function test_mod_forum_get_user_digest_options_default() {
0462b6a1
AN
110 global $USER, $DB;
111
112 $this->resetAfterTest(true);
113
f79f3c00
SH
114 // Create a user.
115 $user = self::getDataGenerator()->create_user();
0462b6a1
AN
116
117 // Set to the user.
f79f3c00 118 self::setUser($user);
0462b6a1
AN
119
120 // We test against these options.
121 $digestoptions = array(
122 '0' => get_string('emaildigestoffshort', 'mod_forum'),
123 '1' => get_string('emaildigestcompleteshort', 'mod_forum'),
124 '2' => get_string('emaildigestsubjectsshort', 'mod_forum'),
125 );
126
127 // The default settings is 0.
128 $this->assertEquals(0, $user->maildigest);
129 $options = forum_get_user_digest_options();
130 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[0]));
131
132 // Update the setting to 1.
133 $USER->maildigest = 1;
134 $this->assertEquals(1, $USER->maildigest);
135 $options = forum_get_user_digest_options();
136 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[1]));
137
138 // Update the setting to 2.
139 $USER->maildigest = 2;
140 $this->assertEquals(2, $USER->maildigest);
141 $options = forum_get_user_digest_options();
142 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[2]));
143 }
144
f79f3c00 145 public function test_mod_forum_get_user_digest_options_sorting() {
0462b6a1
AN
146 global $USER, $DB;
147
148 $this->resetAfterTest(true);
149
f79f3c00
SH
150 // Create a user.
151 $user = self::getDataGenerator()->create_user();
0462b6a1
AN
152
153 // Set to the user.
f79f3c00 154 self::setUser($user);
0462b6a1
AN
155
156 // Retrieve the list of applicable options.
157 $options = forum_get_user_digest_options();
158
159 // The default option must always be at the top of the list.
160 $lastoption = -2;
161 foreach ($options as $value => $description) {
162 $this->assertGreaterThan($lastoption, $value);
163 $lastoption = $value;
164 }
165 }
166
167}