2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * External comment functions unit tests
20 * @package core_comment
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
34 * External comment functions unit tests
36 * @package core_comment
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_comment_externallib_testcase
extends externallib_advanced_testcase
{
47 protected function setUp() {
50 require_once($CFG->dirroot
. '/comment/lib.php');
56 public function test_get_comments() {
59 $this->resetAfterTest(true);
61 $CFG->usecomments
= true;
63 $user = $this->getDataGenerator()->create_user();
64 $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
65 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
66 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $studentrole->id
);
68 $record = new stdClass();
69 $record->course
= $course->id
;
70 $record->name
= "Mod data test";
71 $record->intro
= "Some intro of some sort";
72 $record->comments
= 1;
74 $module = $this->getDataGenerator()->create_module('data', $record);
75 $field = data_get_field_new('text', $module);
77 $fielddetail = new stdClass();
78 $fielddetail->name
= 'Name';
79 $fielddetail->description
= 'Some name';
81 $field->define_field($fielddetail);
82 $field->insert_field();
83 $recordid = data_add_record($module);
85 $datacontent = array();
86 $datacontent['fieldid'] = $field->field
->id
;
87 $datacontent['recordid'] = $recordid;
88 $datacontent['content'] = 'Asterix';
90 $contentid = $DB->insert_record('data_content', $datacontent);
91 $cm = get_coursemodule_from_instance('data', $module->id
, $course->id
);
93 $context = context_module
::instance($module->cmid
);
95 $this->setUser($user);
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
= $context->id
;
100 $newcmt->commentarea
= 'database_entry';
101 $newcmt->itemid
= $recordid;
102 $newcmt->content
= 'New comment';
104 $newcmt->userid
= $user->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 = $cm->id
;
114 $component = 'mod_data';
116 $area = 'database_entry';
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']);
127 $this->assertEquals($user->id
, $result['comments'][0]['userid']);
128 $this->assertEquals($user->id
, $result['comments'][1]['userid']);
130 $this->assertEquals($cmtid2, $result['comments'][0]['id']);
131 $this->assertEquals($cmtid1, $result['comments'][1]['id']);