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 core_message;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
 
23
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
24
 
25
/**
26
 * Tests for the message helper class.
27
 *
28
 * @package core_message
29
 * @category test
30
 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers \core_message\helper
33
 */
1441 ariadna 34
final class helper_test extends \advanced_testcase {
1 efrain 35
 
36
    public function setUp(): void {
1441 ariadna 37
        parent::setUp();
1 efrain 38
        $this->resetAfterTest(true);
39
    }
40
 
11 efrain 41
    public function test_get_member_info_ordering(): void {
1 efrain 42
        // Create a conversation with several users.
43
        $user1 = self::getDataGenerator()->create_user();
44
        $user2 = self::getDataGenerator()->create_user();
45
        $user3 = self::getDataGenerator()->create_user();
46
        $user4 = self::getDataGenerator()->create_user();
47
 
48
        \core_message\api::create_conversation(
49
            \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
50
            [
51
                $user1->id,
52
                $user2->id,
53
                $user3->id,
54
                $user4->id,
55
            ],
56
            'Group conversation'
57
        );
58
 
59
        // Verify that the member information comes back in the same order that we specified in the input array.
60
        $memberinfo = \core_message\helper::get_member_info($user1->id, [$user3->id, $user4->id, $user2->id]);
61
        $this->assertEquals($user3->id, array_shift($memberinfo)->id);
62
        $this->assertEquals($user4->id, array_shift($memberinfo)->id);
63
        $this->assertEquals($user2->id, array_shift($memberinfo)->id);
64
    }
65
 
66
    /**
67
     * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is disabled.
68
     */
11 efrain 69
    public function test_search_get_user_details_sitewide_disabled(): void {
1 efrain 70
        global $DB;
71
        set_config('messagingallusers', false);
72
 
73
        // Two students sharing course 1, visible profile within course (no groups).
74
        $user1 = $this->getDataGenerator()->create_user();
75
        $user2 = $this->getDataGenerator()->create_user();
76
        $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
77
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
78
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
79
 
80
        // A teacher in course 1.
81
        $user3 = $this->getDataGenerator()->create_user();
82
        $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
83
 
84
        // Two students sharing course 2, separate groups (profiles not visible to one another).
85
        // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
86
        $user4 = $this->getDataGenerator()->create_user();
87
        $user5 = $this->getDataGenerator()->create_user();
88
        $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
89
        $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
90
        $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
91
 
92
        // A teacher in course 2.
93
        $user6 = $this->getDataGenerator()->create_user();
94
        $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
95
 
96
        // Teacher and course contact in course 3.
97
        $user7 = $this->getDataGenerator()->create_user();
98
        $course3 = $this->getDataGenerator()->create_course();
99
        $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
100
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
101
 
102
        // Make teachers course contacts.
103
        set_config('coursecontact', $teacherrole->id);
104
 
105
        // User 1 should be able to see users within their course, but not course contacts or students in other courses.
106
        $this->setUser($user1);
107
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
108
        $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
109
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
110
        $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
111
 
112
        // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
113
        // or students in other courses.
114
        $this->setUser($user4);
115
        $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
116
        $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
117
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
118
        $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
119
    }
120
 
121
    /**
122
     * Test search_get_user_details returns the correct profile data we limit the data we wish to be returned.
123
     */
11 efrain 124
    public function test_search_get_user_details_limited_data(): void {
1 efrain 125
        set_config('messagingallusers', false);
126
 
127
        // Two students sharing course 1, visible profile within course (no groups).
128
        $user1 = $this->getDataGenerator()->create_user();
129
        $user2 = $this->getDataGenerator()->create_user();
130
        $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
131
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
132
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
133
 
134
        // Calculate the minimum fields that can be returned.
135
        $namefields = \core_user\fields::for_name()->get_required_fields();
136
        $fields = array_intersect($namefields, user_get_default_fields());
137
 
138
        $minimaluser = (object) [
139
            'id' => $user2->id,
140
            'deleted' => $user2->deleted,
141
        ];
142
 
143
        foreach ($namefields as $field) {
144
            $minimaluser->$field = $user2->$field;
145
        }
146
 
147
        // Test that less data is returned using the filter.
148
        $this->setUser($user1);
149
        $fulldetails = helper::search_get_user_details($user2);
150
        $limiteddetails = helper::search_get_user_details($minimaluser, $fields);
151
        $fullcount = count($fulldetails);
152
        $limitedcount = count($limiteddetails);
153
        $this->assertLessThan($fullcount, $limitedcount);
154
        $this->assertNotEquals($fulldetails, $limiteddetails);
155
    }
156
 
157
    /**
158
     * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is enabled.
159
     */
11 efrain 160
    public function test_search_get_user_details_sitewide_enabled(): void {
1 efrain 161
        global $DB;
162
        set_config('messagingallusers', true);
163
 
164
        // Two students sharing course 1, visible profile within course (no groups).
165
        $user1 = $this->getDataGenerator()->create_user();
166
        $user2 = $this->getDataGenerator()->create_user();
167
        $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
168
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
169
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
170
 
171
        // A teacher in course 1.
172
        $user3 = $this->getDataGenerator()->create_user();
173
        $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
174
 
175
        // Two students sharing course 2, separate groups (profiles not visible to one another).
176
        // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
177
        $user4 = $this->getDataGenerator()->create_user();
178
        $user5 = $this->getDataGenerator()->create_user();
179
        $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
180
        $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
181
        $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
182
 
183
        // A teacher in course 2.
184
        $user6 = $this->getDataGenerator()->create_user();
185
        $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
186
 
187
        // Teacher and course contact in course 3.
188
        $user7 = $this->getDataGenerator()->create_user();
189
        $course3 = $this->getDataGenerator()->create_course();
190
        $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
191
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
192
 
193
        // Make teachers course contacts.
194
        set_config('coursecontact', $teacherrole->id);
195
 
196
        // User 1 should be able to see users within their course and course contacts, but not students in other courses.
197
        $this->setUser($user1);
198
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
199
        $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
200
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
201
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
202
 
203
        // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
204
        // or students in other courses.
205
        $this->setUser($user4);
206
        $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
207
        $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
208
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
209
        $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
210
    }
211
 
212
    /**
213
     * Test prevent_unclosed_html_tags returns the correct html.
214
     *
215
     * @dataProvider prevent_unclosed_html_tags_data
216
     * @param string $text text to preview unclosed html tags.
217
     * @param string $goodhtml html good structured.
218
     * @param bool $removebody true if we want to remove tag body.
219
     */
11 efrain 220
    public function test_prevent_unclosed_html_tags(string $message, string $goodhtml, bool $removebody): void {
1 efrain 221
        $this->setAdminUser();
222
 
223
        $html = \core_message\helper::prevent_unclosed_html_tags($message, $removebody);
224
        $this->assertSame($goodhtml, $html);
225
    }
226
 
227
    /**
228
     * Data provider for the test_prevent_unclosed_html_tags_data tests.
229
     *
230
     * @return  array
231
     */
1441 ariadna 232
    public static function prevent_unclosed_html_tags_data(): array {
1 efrain 233
        return [
234
            'Prevent unclosed html elements' => [
235
                '<h1>Title</h1><p>Paragraph</p><b>Bold', '<h1>Title</h1><p>Paragraph</p><b>Bold</b>', true
236
            ],
237
            'Prevent unclosed html elements including comments' => [
238
                '<h1>Title</h1><p>Paragraph</p><!-- Comments //--><b>Bold', '<h1>Title</h1><p>Paragraph</p><!-- Comments //--><b>Bold</b>', true
239
            ],
240
            'Prevent unclosed comments' => ['<h1>Title</h1><p>Paragraph</p><!-- Comments', '<h1>Title</h1><p>Paragraph</p>', true
241
            ],
242
            'Prevent unclosed html elements without removing tag body' => [
243
                '<body><h2>Title 2</h2><p>Paragraph</p><b>Bold</body>', '<body><h2>Title 2</h2><p>Paragraph</p><b>Bold</b></body>', false
244
            ],
245
            'Empty html' => [
246
                '', '', false
247
            ],
248
            'Check encoding UTF-8 is working' => [
249
                '<body><h1>Title</h1><p>السلام عليكم</p></body>', '<body><h1>Title</h1><p>السلام عليكم</p></body>', false
250
            ],
251
        ];
252
    }
253
}