Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace mod_forum;
18
 
19
use mod_forum_tests_cron_trait;
20
use mod_forum_tests_generator_trait;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once($CFG->dirroot . '/mod/forum/lib.php');
26
require_once(__DIR__ . '/cron_trait.php');
27
require_once(__DIR__ . '/generator_trait.php');
28
 
29
/**
30
 * The forum module mail generation tests for groups.
31
 *
32
 * @copyright  2013 Andrew Nicols
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class mail_group_test extends \advanced_testcase {
1 efrain 36
    // Make use of the cron tester trait.
37
    use mod_forum_tests_cron_trait;
38
 
39
    // Make use of the test generator trait.
40
    use mod_forum_tests_generator_trait;
41
 
42
    /**
43
     * @var \phpunit_message_sink
44
     */
45
    protected $messagesink;
46
 
47
    /**
48
     * @var \phpunit_mailer_sink
49
     */
50
    protected $mailsink;
51
 
52
    public function setUp(): void {
53
        global $CFG;
1441 ariadna 54
        parent::setUp();
1 efrain 55
 
56
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
57
        // tests using these functions.
58
        \mod_forum\subscriptions::reset_forum_cache();
59
        \mod_forum\subscriptions::reset_discussion_cache();
60
 
61
        // Messaging is not compatible with transactions...
62
        $this->preventResetByRollback();
63
 
64
        // Catch all messages.
65
        $this->messagesink = $this->redirectMessages();
66
        $this->mailsink = $this->redirectEmails();
67
 
68
        // Forcibly reduce the maxeditingtime to a second in the past to
69
        // ensure that messages are sent out.
70
        $CFG->maxeditingtime = -1;
71
    }
72
 
73
    public function tearDown(): void {
74
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
75
        // tests using these functions.
76
        \mod_forum\subscriptions::reset_forum_cache();
77
 
78
        $this->messagesink->clear();
79
        $this->messagesink->close();
80
        unset($this->messagesink);
81
 
82
        $this->mailsink->clear();
83
        $this->mailsink->close();
84
        unset($this->mailsink);
1441 ariadna 85
        parent::tearDown();
1 efrain 86
    }
87
 
88
    /**
89
     * Ensure that posts written in a forum marked for separate groups includes notifications for the members of that
90
     * group, and any user with accessallgroups.
91
     */
11 efrain 92
    public function test_separate_group(): void {
1 efrain 93
        global $CFG, $DB;
94
 
95
        $this->resetAfterTest(true);
96
 
97
        // Create a course, with a forum.
98
        $course = $this->getDataGenerator()->create_course();
99
 
100
        $forum = $this->getDataGenerator()->create_module('forum', [
101
            'course' => $course->id,
102
            'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
103
            'groupmode' => SEPARATEGROUPS,
104
        ]);
105
 
106
        // Create three students:
107
        // - author, enrolled in group A; and
108
        // - recipient, enrolled in group B; and
109
        // - other, enrolled in the course, but no groups.
110
        list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
111
 
112
        // Create one teacher, not in any group and no accessallgroups capability.
113
        list($teacher) = $this->helper_create_users($course, 1, 'teacher');
114
 
115
        // Create one editing teacher, not in any group but with accessallgroups capability.
116
        list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
117
 
118
        $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
119
        $groupb = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
120
        $this->getDataGenerator()->create_group_member([
121
            'groupid' => $groupa->id,
122
            'userid' => $author->id,
123
        ]);
124
        $this->getDataGenerator()->create_group_member([
125
            'groupid' => $groupb->id,
126
            'userid' => $recipient->id,
127
        ]);
128
 
129
        // Post a discussion to the forum.
130
        list($discussion, $post) = $this->helper_post_to_forum($forum, $author, [
131
            'groupid' => $groupa->id,
132
        ]);
133
 
134
        // Only the author should receive.
135
        $expect = [
136
            'author' => (object) [
137
                'userid' => $author->id,
138
                'messages' => 1,
139
            ],
140
            'recipient' => (object) [
141
                'userid' => $recipient->id,
142
                'messages' => 0,
143
            ],
144
            'otheruser' => (object) [
145
                'userid' => $otheruser->id,
146
                'messages' => 0,
147
            ],
148
            'teacher' => (object) [
149
                'userid' => $teacher->id,
150
                'messages' => 0,
151
            ],
152
            'editingteacher' => (object) [
153
                'userid' => $editingteacher->id,
154
                'messages' => 1,
155
            ],
156
        ];
157
        $this->queue_tasks_and_assert($expect);
158
 
159
        // No notifications should be queued.
160
        $this->send_notifications_and_assert($author, [$post]);
161
        $this->send_notifications_and_assert($recipient, []);
162
        $this->send_notifications_and_assert($otheruser, []);
163
        $this->send_notifications_and_assert($teacher, []);
164
        $this->send_notifications_and_assert($editingteacher, [$post]);
165
    }
166
 
167
    /**
168
     * Ensure that posts written in a forum marked for visible groups includes notifications for the members of that
169
     * group, and any user with accessallgroups.
170
     */
11 efrain 171
    public function test_visible_group(): void {
1 efrain 172
        global $CFG, $DB;
173
 
174
        $this->resetAfterTest(true);
175
 
176
        // Create a course, with a forum.
177
        $course = $this->getDataGenerator()->create_course();
178
 
179
        $forum = $this->getDataGenerator()->create_module('forum', [
180
            'course' => $course->id,
181
            'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
182
            'groupmode' => VISIBLEGROUPS,
183
        ]);
184
 
185
        // Create three students:
186
        // - author, enrolled in group A; and
187
        // - recipient, enrolled in group B; and
188
        // - other, enrolled in the course, but no groups.
189
        list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
190
 
191
        // Create one teacher, not in any group and no accessallgroups capability.
192
        list($teacher) = $this->helper_create_users($course, 1, 'teacher');
193
 
194
        // Create one editing teacher, not in any group but with accessallgroups capability.
195
        list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
196
 
197
        $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
198
        $groupb = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
199
        $this->getDataGenerator()->create_group_member([
200
            'groupid' => $groupa->id,
201
            'userid' => $author->id,
202
        ]);
203
        $this->getDataGenerator()->create_group_member([
204
            'groupid' => $groupb->id,
205
            'userid' => $recipient->id,
206
        ]);
207
 
208
        // Post a discussion to the forum.
209
        list($discussion, $post) = $this->helper_post_to_forum($forum, $author, [
210
            'groupid' => $groupa->id,
211
        ]);
212
 
213
        // Only the author should receive.
214
        $expect = [
215
            'author' => (object) [
216
                'userid' => $author->id,
217
                'messages' => 1,
218
            ],
219
            'recipient' => (object) [
220
                'userid' => $recipient->id,
221
                'messages' => 0,
222
            ],
223
            'otheruser' => (object) [
224
                'userid' => $otheruser->id,
225
                'messages' => 0,
226
            ],
227
            'teacher' => (object) [
228
                'userid' => $teacher->id,
229
                'messages' => 0,
230
            ],
231
            'editingteacher' => (object) [
232
                'userid' => $editingteacher->id,
233
                'messages' => 1,
234
            ],
235
        ];
236
        $this->queue_tasks_and_assert($expect);
237
 
238
        // No notifications should be queued.
239
        $this->send_notifications_and_assert($author, [$post]);
240
        $this->send_notifications_and_assert($recipient, []);
241
        $this->send_notifications_and_assert($otheruser, []);
242
        $this->send_notifications_and_assert($teacher, []);
243
        $this->send_notifications_and_assert($editingteacher, [$post]);
244
    }
245
}