MDL-64588 comment: New WebService core_comment_add_comment
[moodle.git] / comment / tests / externallib_test.php
blobb43347967b8fb33f4bbb4f8eaddb49137b747f62
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.
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/>.
17 /**
18 * External comment functions unit tests
20 * @package core_comment
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 2.9
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33 /**
34 * External comment functions unit tests
36 * @package core_comment
37 * @category external
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 2.9
42 class core_comment_externallib_testcase extends externallib_advanced_testcase {
44 /**
45 * Tests set up
47 protected function setUp() {
48 global $CFG, $DB;
50 require_once($CFG->dirroot . '/comment/lib.php');
52 $CFG->usecomments = true;
54 $this->student = $this->getDataGenerator()->create_user();
55 $this->course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
56 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
57 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $studentrole->id);
59 $record = new stdClass();
60 $record->course = $this->course->id;
61 $record->name = "Mod data test";
62 $record->intro = "Some intro of some sort";
63 $record->comments = 1;
65 $this->module = $this->getDataGenerator()->create_module('data', $record);
66 $field = data_get_field_new('text', $this->module);
68 $fielddetail = new stdClass();
69 $fielddetail->name = 'Name';
70 $fielddetail->description = 'Some name';
72 $field->define_field($fielddetail);
73 $field->insert_field();
74 $this->recordid = data_add_record($this->module);
76 $datacontent = array();
77 $datacontent['fieldid'] = $field->field->id;
78 $datacontent['recordid'] = $this->recordid;
79 $datacontent['content'] = 'Asterix';
81 $contentid = $DB->insert_record('data_content', $datacontent);
82 $this->cm = get_coursemodule_from_instance('data', $this->module->id, $this->course->id);
84 $this->context = context_module::instance($this->module->cmid);
87 /**
88 * Test get_comments
90 public function test_get_comments() {
91 global $DB;
93 $this->resetAfterTest(true);
95 $this->setUser($this->student);
97 // We need to add the comments manually, the comment API uses the global OUTPUT and this is going to make the WS to fail.
98 $newcmt = new stdClass;
99 $newcmt->contextid = $this->context->id;
100 $newcmt->commentarea = 'database_entry';
101 $newcmt->itemid = $this->recordid;
102 $newcmt->content = 'New comment';
103 $newcmt->format = 0;
104 $newcmt->userid = $this->student->id;
105 $newcmt->timecreated = time();
106 $cmtid1 = $DB->insert_record('comments', $newcmt);
108 $newcmt->content = 'New comment 2';
109 $newcmt->timecreated = time() + 1;
110 $cmtid2 = $DB->insert_record('comments', $newcmt);
112 $contextlevel = 'module';
113 $instanceid = $this->cm->id;
114 $component = 'mod_data';
115 $itemid = $this->recordid;
116 $area = 'database_entry';
117 $page = 0;
119 $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page);
120 // We need to execute the return values cleaning process to simulate the web service server.
121 $result = external_api::clean_returnvalue(
122 core_comment_external::get_comments_returns(), $result);
124 $this->assertCount(0, $result['warnings']);
125 $this->assertCount(2, $result['comments']);
126 $this->assertEquals(2, $result['count']);
127 $this->assertEquals(15, $result['perpage']);
128 $this->assertTrue($result['canpost']);
130 $this->assertEquals($this->student->id, $result['comments'][0]['userid']);
131 $this->assertEquals($this->student->id, $result['comments'][1]['userid']);
133 $this->assertEquals($cmtid2, $result['comments'][0]['id']); // Default ordering newer first.
134 $this->assertEquals($cmtid1, $result['comments'][1]['id']);
136 // Test sort direction and pagination.
137 $CFG->commentsperpage = 1;
138 $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page, 'ASC');
139 $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
141 $this->assertCount(0, $result['warnings']);
142 $this->assertCount(1, $result['comments']); // Only one per page.
143 $this->assertEquals(2, $result['count']);
144 $this->assertEquals($CFG->commentsperpage, $result['perpage']);
145 $this->assertEquals($cmtid1, $result['comments'][0]['id']); // Comments order older first.
147 // Next page.
148 $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page + 1, 'ASC');
149 $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
151 $this->assertCount(0, $result['warnings']);
152 $this->assertCount(1, $result['comments']);
153 $this->assertEquals(2, $result['count']);
154 $this->assertEquals($CFG->commentsperpage, $result['perpage']);
155 $this->assertEquals($cmtid2, $result['comments'][0]['id']);
159 * Test add_comments not enabled site level
161 public function test_add_comments_not_enabled_site_level() {
162 global $CFG;
163 $this->resetAfterTest(true);
165 $CFG->usecomments = false;
166 $this->setUser($this->student);
167 $this->expectException(comment_exception::class);
168 core_comment_external::add_comments([
170 'contextlevel' => 'module',
171 'instanceid' => $this->cm->id,
172 'component' => 'mod_data',
173 'content' => 'abc',
174 'itemid' => $this->recordid,
175 'area' => 'database_entry'
181 * Test add_comments not enabled module level
183 public function test_add_comments_not_enabled_module_level() {
184 global $DB;
185 $this->resetAfterTest(true);
187 $DB->set_field('data', 'comments', 0, array('id' => $this->module->id));
188 $this->setUser($this->student);
189 $this->expectException(comment_exception::class);
190 core_comment_external::add_comments([
192 'contextlevel' => 'module',
193 'instanceid' => $this->cm->id,
194 'component' => 'mod_data',
195 'content' => 'abc',
196 'itemid' => $this->recordid,
197 'area' => 'database_entry'
203 * Test add_comments
205 public function test_add_comments_single() {
206 $this->resetAfterTest(true);
207 $this->setUser($this->student);
209 $result = core_comment_external::add_comments([
211 'contextlevel' => 'module',
212 'instanceid' => $this->cm->id,
213 'component' => 'mod_data',
214 'content' => 'abc',
215 'itemid' => $this->recordid,
216 'area' => 'database_entry'
219 $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
221 $expectedkeys = [
222 'id',
223 'content',
224 'format',
225 'timecreated',
226 'strftimeformat',
227 'profileurl',
228 'fullname',
229 'time',
230 'avatar',
231 'userid',
232 'delete',
235 // Verify the result contains 1 result having the correct structure.
236 $this->assertCount(1, $result);
237 foreach ($expectedkeys as $key) {
238 $this->assertArrayHasKey($key, $result[0]);
243 * Test add_comments when one of the comments contains invalid data and cannot be created.
245 * This simply verifies that the entire operation fails.
247 public function test_add_comments_multiple_contains_invalid() {
248 $this->resetAfterTest(true);
249 $this->setUser($this->student);
251 $this->expectException(comment_exception::class);
252 core_comment_external::add_comments([
254 'contextlevel' => 'module',
255 'instanceid' => $this->cm->id,
256 'component' => 'mod_data',
257 'content' => 'abc',
258 'itemid' => $this->recordid,
259 'area' => 'database_entry'
262 'contextlevel' => 'module',
263 'instanceid' => $this->cm->id,
264 'component' => 'mod_data',
265 'content' => 'abc',
266 'itemid' => $this->recordid,
267 'area' => 'areanotfound'
273 * Test add_comments when one of the comments contains invalid data and cannot be created.
275 * This simply verifies that the entire operation fails.
277 public function test_add_comments_multiple_all_valid() {
278 $this->resetAfterTest(true);
279 $this->setUser($this->student);
281 $inputdata = [
283 'contextlevel' => 'module',
284 'instanceid' => $this->cm->id,
285 'component' => 'mod_data',
286 'content' => 'cat',
287 'itemid' => $this->recordid,
288 'area' => 'database_entry'
291 'contextlevel' => 'module',
292 'instanceid' => $this->cm->id,
293 'component' => 'mod_data',
294 'content' => 'dog',
295 'itemid' => $this->recordid,
296 'area' => 'database_entry'
299 $result = core_comment_external::add_comments($inputdata);
300 $result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
302 // Two comments should have been created.
303 $this->assertCount(2, $result);
305 // The content for each comment should come back formatted.
306 foreach ($result as $index => $comment) {
307 $formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
308 $expectedcontent = format_text($inputdata[$index]['content'], FORMAT_MOODLE, $formatoptions);
309 $this->assertEquals($expectedcontent, $comment['content']);
314 * Test add_comments invalid area
316 public function test_add_comments_invalid_area() {
317 $this->resetAfterTest(true);
318 $this->setUser($this->student);
320 $comments = [
322 'contextlevel' => 'module',
323 'instanceid' => $this->cm->id,
324 'component' => 'mod_data',
325 'content' => 'abc',
326 'itemid' => $this->recordid,
327 'area' => 'rhomboid'
330 $this->expectException(comment_exception::class);
331 core_comment_external::add_comments($comments);