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_comment;
18
 
19
use comment_exception;
20
use core_comment_external;
21
use core_external\external_api;
22
use externallib_advanced_testcase;
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
global $CFG;
27
 
28
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
29
 
30
/**
31
 * External comment functions unit tests
32
 *
33
 * @package    core_comment
34
 * @category   external
35
 * @copyright  2015 Juan Leyva <juan@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @since      Moodle 2.9
38
 */
1441 ariadna 39
final class externallib_test extends externallib_advanced_testcase {
1 efrain 40
 
41
    /**
42
     * Tests set up
43
     */
44
    protected function setUp(): void {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        $this->resetAfterTest();
47
    }
48
 
49
    /**
50
     * Helper used to set up a course, with a module, a teacher and two students.
51
     *
52
     * @return array the array of records corresponding to the course, teacher, and students.
53
     */
54
    protected function setup_course_and_users_basic() {
55
        global $CFG, $DB;
56
 
57
        require_once($CFG->dirroot . '/comment/lib.php');
58
 
59
        $CFG->usecomments = true;
60
 
61
        $student1 = $this->getDataGenerator()->create_user();
62
        $student2 = $this->getDataGenerator()->create_user();
63
        $teacher1 = $this->getDataGenerator()->create_user();
64
        $course1 = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
65
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
66
        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
67
        $this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
68
        $this->getDataGenerator()->enrol_user($student2->id, $course1->id, $studentrole->id);
69
        $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
70
 
71
        // Create a database module instance.
72
        $record = new \stdClass();
73
        $record->course = $course1->id;
74
        $record->name = "Mod data test";
75
        $record->intro = "Some intro of some sort";
76
        $record->comments = 1;
77
 
78
        $module1 = $this->getDataGenerator()->create_module('data', $record);
79
        $field = data_get_field_new('text', $module1);
80
 
81
        $fielddetail = new \stdClass();
82
        $fielddetail->name = 'Name';
83
        $fielddetail->description = 'Some name';
84
 
85
        $field->define_field($fielddetail);
86
        $field->insert_field();
87
        $recordid = data_add_record($module1);
88
 
89
        $datacontent = array();
90
        $datacontent['fieldid'] = $field->field->id;
91
        $datacontent['recordid'] = $recordid;
92
        $datacontent['content'] = 'Asterix';
93
        $DB->insert_record('data_content', $datacontent);
94
 
95
        return [$module1, $recordid, $teacher1, $student1, $student2];
96
    }
97
 
98
    /**
99
     * Test get_comments
100
     */
11 efrain 101
    public function test_get_comments(): void {
1 efrain 102
        global $CFG;
103
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
104
 
105
        // Create some comments as student 1.
106
        $this->setUser($student1);
107
        $inputdata = [
108
            [
109
                'contextlevel' => 'module',
110
                'instanceid' => $module1->cmid,
111
                'component' => 'mod_data',
112
                'content' => 'abc',
113
                'itemid' => $recordid,
114
                'area' => 'database_entry'
115
            ],
116
            [
117
                'contextlevel' => 'module',
118
                'instanceid' => $module1->cmid,
119
                'component' => 'mod_data',
120
                'content' => 'def',
121
                'itemid' => $recordid,
122
                'area' => 'database_entry'
123
            ]
124
        ];
125
        $result = core_comment_external::add_comments($inputdata);
126
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
127
        $ids = array_column($result, 'id');
128
 
129
        // Verify we can get the comments.
130
        $contextlevel = 'module';
131
        $instanceid = $module1->cmid;
132
        $component = 'mod_data';
133
        $itemid = $recordid;
134
        $area = 'database_entry';
135
        $page = 0;
136
        $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page);
137
        $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
138
 
139
        $this->assertCount(0, $result['warnings']);
140
        $this->assertCount(2, $result['comments']);
141
        $this->assertEquals(2, $result['count']);
142
        $this->assertEquals(15, $result['perpage']);
143
        $this->assertTrue($result['canpost']);
144
 
145
        $this->assertEquals($student1->id, $result['comments'][0]['userid']);
146
        $this->assertEquals($student1->id, $result['comments'][1]['userid']);
147
 
148
        $this->assertEquals($ids[1], $result['comments'][0]['id']); // Default ordering newer first.
149
        $this->assertEquals($ids[0], $result['comments'][1]['id']);
150
 
151
        // Test sort direction and pagination.
152
        $CFG->commentsperpage = 1;
153
        $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page, 'ASC');
154
        $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
155
 
156
        $this->assertCount(0, $result['warnings']);
157
        $this->assertCount(1, $result['comments']); // Only one per page.
158
        $this->assertEquals(2, $result['count']);
159
        $this->assertEquals($CFG->commentsperpage, $result['perpage']);
160
        $this->assertEquals($ids[0], $result['comments'][0]['id']); // Comments order older first.
161
 
162
        // Next page.
163
        $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page + 1, 'ASC');
164
        $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
165
 
166
        $this->assertCount(0, $result['warnings']);
167
        $this->assertCount(1, $result['comments']);
168
        $this->assertEquals(2, $result['count']);
169
        $this->assertEquals($CFG->commentsperpage, $result['perpage']);
170
        $this->assertEquals($ids[1], $result['comments'][0]['id']);
171
    }
172
 
173
    /**
174
     * Test add_comments not enabled site level
175
     */
11 efrain 176
    public function test_add_comments_not_enabled_site_level(): void {
1 efrain 177
        global $CFG;
178
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
179
 
180
        // Try to add a comment, as student 1, when comments is disabled at site level.
181
        $this->setUser($student1);
182
        $CFG->usecomments = false;
183
 
184
        $this->expectException(comment_exception::class);
185
        core_comment_external::add_comments([
186
            [
187
                'contextlevel' => 'module',
188
                'instanceid' => $module1->cmid,
189
                'component' => 'mod_data',
190
                'content' => 'abc',
191
                'itemid' => $recordid,
192
                'area' => 'database_entry'
193
            ]
194
        ]);
195
    }
196
 
197
    /**
198
     * Test add_comments not enabled module level
199
     */
11 efrain 200
    public function test_add_comments_not_enabled_module_level(): void {
1 efrain 201
        global $DB;
202
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
203
 
204
        // Disable comments for the module.
205
        $DB->set_field('data', 'comments', 0, array('id' => $module1->id));
206
 
207
        // Verify we can't add a comment.
208
        $this->setUser($student1);
209
        $this->expectException(comment_exception::class);
210
        core_comment_external::add_comments([
211
            [
212
                'contextlevel' => 'module',
213
                'instanceid' => $module1->cmid,
214
                'component' => 'mod_data',
215
                'content' => 'abc',
216
                'itemid' => $recordid,
217
                'area' => 'database_entry'
218
            ]
219
        ]);
220
    }
221
 
222
    /**
223
     * Test add_comments
224
     */
11 efrain 225
    public function test_add_comments_single(): void {
1 efrain 226
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
227
 
228
        // Add a comment as student 1.
229
        $this->setUser($student1);
230
        $result = core_comment_external::add_comments([
231
            [
232
                'contextlevel' => 'module',
233
                'instanceid' => $module1->cmid,
234
                'component' => 'mod_data',
235
                'content' => 'abc',
236
                'itemid' => $recordid,
237
                'area' => 'database_entry'
238
            ]
239
        ]);
240
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
241
 
242
        // Verify the result contains 1 result having the correct structure.
243
        $this->assertCount(1, $result);
244
 
245
        $expectedkeys = [
246
            'id',
247
            'content',
248
            'format',
249
            'timecreated',
250
            'strftimeformat',
251
            'profileurl',
252
            'fullname',
253
            'time',
254
            'avatar',
255
            'userid',
256
            'delete',
257
        ];
258
        foreach ($expectedkeys as $key) {
259
            $this->assertArrayHasKey($key, $result[0]);
260
        }
261
    }
262
 
263
    /**
264
     * Test add_comments when one of the comments contains invalid data and cannot be created.
265
     *
266
     * This simply verifies that the entire operation fails.
267
     */
11 efrain 268
    public function test_add_comments_multiple_contains_invalid(): void {
1 efrain 269
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
270
 
271
        // Try to create some comments as student 1, but provide a bad area for the second comment.
272
        $this->setUser($student1);
273
        $this->expectException(comment_exception::class);
274
        core_comment_external::add_comments([
275
            [
276
                'contextlevel' => 'module',
277
                'instanceid' => $module1->cmid,
278
                'component' => 'mod_data',
279
                'content' => 'abc',
280
                'itemid' => $recordid,
281
                'area' => 'database_entry'
282
            ],
283
            [
284
                'contextlevel' => 'module',
285
                'instanceid' => $module1->cmid,
286
                'component' => 'mod_data',
287
                'content' => 'def',
288
                'itemid' => $recordid,
289
                'area' => 'badarea'
290
            ],
291
        ]);
292
    }
293
 
294
    /**
295
     * Test add_comments when one of the comments contains invalid data and cannot be created.
296
     *
297
     * This simply verifies that the entire operation fails.
298
     */
11 efrain 299
    public function test_add_comments_multiple_all_valid(): void {
1 efrain 300
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
301
 
302
        // Try to create some comments as student 1.
303
        $this->setUser($student1);
304
        $inputdata = [
305
            [
306
                'contextlevel' => 'module',
307
                'instanceid' => $module1->cmid,
308
                'component' => 'mod_data',
309
                'content' => 'abc',
310
                'itemid' => $recordid,
311
                'area' => 'database_entry'
312
            ],
313
            [
314
                'contextlevel' => 'module',
315
                'instanceid' => $module1->cmid,
316
                'component' => 'mod_data',
317
                'content' => 'def',
318
                'itemid' => $recordid,
319
                'area' => 'database_entry'
320
            ]
321
        ];
322
        $result = core_comment_external::add_comments($inputdata);
323
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
324
 
325
        // Two comments should have been created.
326
        $this->assertCount(2, $result);
327
 
328
        // The content for each comment should come back formatted.
329
        foreach ($result as $index => $comment) {
330
            $formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
331
            $expectedcontent = format_text($inputdata[$index]['content'], FORMAT_MOODLE, $formatoptions);
332
            $this->assertEquals($expectedcontent, $comment['content']);
333
        }
334
    }
335
 
336
    /**
337
     * Test add_comments invalid area
338
     */
11 efrain 339
    public function test_add_comments_invalid_area(): void {
1 efrain 340
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
341
 
342
        // Try to create a comment with an invalid area, verifying failure.
343
        $this->setUser($student1);
344
        $comments = [
345
            [
346
                'contextlevel' => 'module',
347
                'instanceid' => $module1->cmid,
348
                'component' => 'mod_data',
349
                'content' => 'abc',
350
                'itemid' => $recordid,
351
                'area' => 'spaghetti'
352
            ]
353
        ];
354
        $this->expectException(comment_exception::class);
355
        core_comment_external::add_comments($comments);
356
    }
357
 
358
    /**
359
     * Test delete_comment invalid comment.
360
     */
11 efrain 361
    public function test_delete_comments_invalid_comment_id(): void {
1 efrain 362
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
363
        $this->setUser($student1);
364
 
365
        $this->expectException(comment_exception::class);
366
        core_comment_external::delete_comments([-1, 0]);
367
    }
368
 
369
    /**
370
     * Test delete_comment own user.
371
     */
11 efrain 372
    public function test_delete_comments_own_user(): void {
1 efrain 373
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
374
 
375
        // Create a few comments as student 1.
376
        $this->setUser($student1);
377
        $result = core_comment_external::add_comments([
378
            [
379
                'contextlevel' => 'module',
380
                'instanceid' => $module1->cmid,
381
                'component' => 'mod_data',
382
                'content' => 'abc',
383
                'itemid' => $recordid,
384
                'area' => 'database_entry'
385
            ],
386
            [
387
                'contextlevel' => 'module',
388
                'instanceid' => $module1->cmid,
389
                'component' => 'mod_data',
390
                'content' => 'def',
391
                'itemid' => $recordid,
392
                'area' => 'database_entry'
393
            ]
394
        ]);
395
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
396
 
397
        // Delete those comments we just created.
398
        $result = core_comment_external::delete_comments([
399
            $result[0]['id'],
400
            $result[1]['id']
401
        ]);
402
        $result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result);
403
        $this->assertEquals([], $result);
404
    }
405
 
406
    /**
407
     * Test delete_comment other student.
408
     */
11 efrain 409
    public function test_delete_comment_other_student(): void {
1 efrain 410
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
411
 
412
        // Create a comment as the student.
413
        $this->setUser($student1);
414
        $result = core_comment_external::add_comments([
415
            [
416
                'contextlevel' => 'module',
417
                'instanceid' => $module1->cmid,
418
                'component' => 'mod_data',
419
                'content' => 'abc',
420
                'itemid' => $recordid,
421
                'area' => 'database_entry'
422
            ]
423
        ]);
424
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
425
 
426
        // Now, as student 2, try to delete the comment made by student 1. Verify we can't.
427
        $this->setUser($student2);
428
        $this->expectException(comment_exception::class);
429
        core_comment_external::delete_comments([$result[0]['id']]);
430
    }
431
 
432
    /**
433
     * Test delete_comment as teacher.
434
     */
11 efrain 435
    public function test_delete_comments_as_teacher(): void {
1 efrain 436
        [$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
437
 
438
        // Create a comment as the student.
439
        $this->setUser($student1);
440
        $result = core_comment_external::add_comments([
441
            [
442
                'contextlevel' => 'module',
443
                'instanceid' => $module1->cmid,
444
                'component' => 'mod_data',
445
                'content' => 'abc',
446
                'itemid' => $recordid,
447
                'area' => 'database_entry'
448
            ]
449
        ]);
450
        $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
451
 
452
        // Verify teachers can delete the comment.
453
        $this->setUser($teacher1);
454
        $result = core_comment_external::delete_comments([$result[0]['id']]);
455
        $result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result);
456
        $this->assertEquals([], $result);
457
    }
458
}