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_generator_trait;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/mod/forum/lib.php');
25
require_once($CFG->dirroot . '/mod/forum/locallib.php');
26
require_once(__DIR__ . '/generator_trait.php');
27
 
28
/**
29
 * Tests for private reply functionality.
30
 *
31
 * @package    mod_forum
32
 * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class private_replies_test extends \advanced_testcase {
1 efrain 36
 
37
    use mod_forum_tests_generator_trait;
38
 
39
    /**
40
     * Setup before tests.
41
     */
42
    public function setUp(): void {
1441 ariadna 43
        parent::setUp();
1 efrain 44
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
45
        // tests using these functions.
46
        \mod_forum\subscriptions::reset_forum_cache();
47
    }
48
 
49
    /**
50
     * Tear down after tests.
51
     */
52
    public function tearDown(): void {
53
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
54
        // tests using these functions.
55
        \mod_forum\subscriptions::reset_forum_cache();
1441 ariadna 56
        parent::tearDown();
1 efrain 57
    }
58
 
59
    /**
60
     * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another
61
     * user wrote the post, and it is not private.
62
     */
11 efrain 63
    public function test_forum_post_is_visible_privately_not_private(): void {
1 efrain 64
        $this->resetAfterTest();
65
 
66
        $course = $this->getDataGenerator()->create_course();
67
        $forum = $this->getDataGenerator()->create_module('forum', [
68
            'course' => $course->id,
69
        ]);
70
 
71
        [$student] = $this->helper_create_users($course, 1, 'student');
72
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
73
        [$discussion] = $this->helper_post_to_forum($forum, $teacher);
74
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher);
75
 
76
        $this->setUser($student);
77
        $cm = get_coursemodule_from_instance('forum', $forum->id);
78
        $this->assertTrue(forum_post_is_visible_privately($post, $cm));
79
    }
80
 
81
    /**
82
     * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another
83
     * user wrote the post, and the user under test is the intended recipient.
84
     */
11 efrain 85
    public function test_forum_post_is_visible_privately_private_to_user(): void {
1 efrain 86
        $this->resetAfterTest();
87
 
88
        $course = $this->getDataGenerator()->create_course();
89
        $forum = $this->getDataGenerator()->create_module('forum', [
90
            'course' => $course->id,
91
        ]);
92
 
93
        [$student] = $this->helper_create_users($course, 1, 'student');
94
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
95
        [$discussion] = $this->helper_post_to_forum($forum, $teacher);
96
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [
97
                'privatereplyto' => $student->id,
98
            ]);
99
 
100
        $this->setUser($student);
101
        $cm = get_coursemodule_from_instance('forum', $forum->id);
102
        $this->assertTrue(forum_post_is_visible_privately($post, $cm));
103
    }
104
 
105
    /**
106
     * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user when another
107
     * user wrote the post, and the user under test is a role with the view capability.
108
     */
11 efrain 109
    public function test_forum_post_is_visible_privately_private_to_user_view_as_teacher(): void {
1 efrain 110
        $this->resetAfterTest();
111
 
112
        $course = $this->getDataGenerator()->create_course();
113
        $forum = $this->getDataGenerator()->create_module('forum', [
114
            'course' => $course->id,
115
        ]);
116
 
117
        [$student] = $this->helper_create_users($course, 1, 'student');
118
        [$teacher, $otherteacher] = $this->helper_create_users($course, 2, 'teacher');
119
        [$discussion] = $this->helper_post_to_forum($forum, $teacher);
120
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [
121
                'privatereplyto' => $student->id,
122
            ]);
123
 
124
        $this->setUser($otherteacher);
125
        $cm = get_coursemodule_from_instance('forum', $forum->id);
126
        $this->assertTrue(forum_post_is_visible_privately($post, $cm));
127
    }
128
 
129
    /**
130
     * Ensure that the forum_post_is_visible_privately function reports that a post is not visible to a user when
131
     * another user wrote the post, and the user under test is a role without the view capability.
132
     */
11 efrain 133
    public function test_forum_post_is_visible_privately_private_to_user_view_as_other_student(): void {
1 efrain 134
        $this->resetAfterTest();
135
 
136
        $course = $this->getDataGenerator()->create_course();
137
        $forum = $this->getDataGenerator()->create_module('forum', [
138
            'course' => $course->id,
139
        ]);
140
 
141
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
142
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
143
        [$discussion] = $this->helper_post_to_forum($forum, $teacher);
144
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [
145
                'privatereplyto' => $student->id,
146
            ]);
147
 
148
        $this->setUser($otherstudent);
149
        $cm = get_coursemodule_from_instance('forum', $forum->id);
150
        $this->assertFalse(forum_post_is_visible_privately($post, $cm));
151
    }
152
 
153
    /**
154
     * Ensure that the forum_post_is_visible_privately function reports that a post is visible to a user who wrote a
155
     * private reply, but not longer holds the view capability.
156
     */
11 efrain 157
    public function test_forum_post_is_visible_privately_private_to_user_view_as_author(): void {
1 efrain 158
        $this->resetAfterTest();
159
 
160
        $course = $this->getDataGenerator()->create_course();
161
        $forum = $this->getDataGenerator()->create_module('forum', [
162
            'course' => $course->id,
163
        ]);
164
 
165
        [$student] = $this->helper_create_users($course, 1, 'student');
166
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
167
        [$discussion] = $this->helper_post_to_forum($forum, $teacher);
168
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, [
169
                'privatereplyto' => $student->id,
170
            ]);
171
 
172
        unassign_capability('mod/forum:readprivatereplies', $this->get_role_id('teacher'));
173
 
174
        $this->setUser($teacher);
175
        $cm = get_coursemodule_from_instance('forum', $forum->id);
176
        $this->assertTrue(forum_post_is_visible_privately($post, $cm));
177
    }
178
 
179
    /**
180
     * Ensure that the forum_user_can_reply_privately returns true for a teacher replying to a forum post.
181
     */
11 efrain 182
    public function test_forum_user_can_reply_privately_as_teacher(): void {
1 efrain 183
        $this->resetAfterTest();
184
 
185
        $course = $this->getDataGenerator()->create_course();
186
        $forum = $this->getDataGenerator()->create_module('forum', [
187
            'course' => $course->id,
188
        ]);
189
 
190
        [$student] = $this->helper_create_users($course, 1, 'student');
191
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
192
        [, $post] = $this->helper_post_to_forum($forum, $student);
193
 
194
        $this->setUser($teacher);
195
        $cm = get_coursemodule_from_instance('forum', $forum->id);
196
        $context = \context_module::instance($cm->id);
197
        $this->assertTrue(forum_user_can_reply_privately($context, $post));
198
    }
199
 
200
    /**
201
     * Ensure that the forum_user_can_reply_privately returns true for a teacher replying to a forum post.
202
     */
11 efrain 203
    public function test_forum_user_can_reply_privately_as_student(): void {
1 efrain 204
        $this->resetAfterTest();
205
 
206
        $course = $this->getDataGenerator()->create_course();
207
        $forum = $this->getDataGenerator()->create_module('forum', [
208
            'course' => $course->id,
209
        ]);
210
 
211
        [$student, $otherstudent] = $this->helper_create_users($course, 2, 'student');
212
        [, $post] = $this->helper_post_to_forum($forum, $student);
213
 
214
        $this->setUser($otherstudent);
215
        $cm = get_coursemodule_from_instance('forum', $forum->id);
216
        $context = \context_module::instance($cm->id);
217
        $this->assertFalse(forum_user_can_reply_privately($context, $post));
218
    }
219
 
220
    /**
221
     * Ensure that the forum_user_can_reply_privately returns false where the parent post is already a private reply.
222
     */
11 efrain 223
    public function test_forum_user_can_reply_privately_parent_is_already_private(): void {
1 efrain 224
        $this->resetAfterTest();
225
 
226
        $course = $this->getDataGenerator()->create_course();
227
        $forum = $this->getDataGenerator()->create_module('forum', [
228
            'course' => $course->id,
229
        ]);
230
 
231
        [$student] = $this->helper_create_users($course, 1, 'student');
232
        [$teacher] = $this->helper_create_users($course, 1, 'teacher');
233
        [$discussion] = $this->helper_post_to_forum($forum, $student);
234
        $post = $this->helper_post_to_discussion($forum, $discussion, $teacher, ['privatereplyto' => $student->id]);
235
 
236
        $this->setUser($teacher);
237
        $cm = get_coursemodule_from_instance('forum', $forum->id);
238
        $context = \context_module::instance($cm->id);
239
        $this->assertFalse(forum_user_can_reply_privately($context, $post));
240
    }
241
}