MDL-45127 mod_lesson: Fixed reviewing lesson answers changing the lesson grade.
[moodle.git] / notes / tests / externallib_test.php
blob645b7ddcf1787838a4a85beaab3d31888e8fd316
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 notes functions unit tests
20 * @package core_notes
21 * @category external
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot . '/notes/externallib.php');
33 class core_notes_externallib_testcase extends externallib_advanced_testcase {
35 /**
36 * Test create_notes
38 public function test_create_notes() {
40 global $DB, $USER;
42 $this->resetAfterTest(true);
44 $course = self::getDataGenerator()->create_course();
46 // Set the required capabilities by the external function.
47 $contextid = context_course::instance($course->id)->id;
48 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
49 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
51 // Create test note data.
52 $note1 = array();
53 $note1['userid'] = $USER->id;
54 $note1['publishstate'] = 'personal';
55 $note1['courseid'] = $course->id;
56 $note1['text'] = 'the text';
57 $note1['clientnoteid'] = 4;
58 $notes = array($note1);
60 $creatednotes = core_notes_external::create_notes($notes);
61 // We need to execute the return values cleaning process to simulate the web service server.
62 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
64 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
66 // Confirm that base note data was inserted correctly.
67 $this->assertEquals($thenote->userid, $note1['userid']);
68 $this->assertEquals($thenote->courseid, $note1['courseid']);
69 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
70 $this->assertEquals($thenote->content, $note1['text']);
71 $this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);
73 // Call without required capability.
74 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
75 $this->setExpectedException('required_capability_exception');
76 $creatednotes = core_notes_external::create_notes($notes);
79 public function test_delete_notes() {
81 global $DB, $USER;
83 $this->resetAfterTest(true);
85 $course = self::getDataGenerator()->create_course();
87 // Set the required capabilities by the external function.
88 $contextid = context_course::instance($course->id)->id;
89 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
90 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
92 // Create test note data.
93 $cnote = array();
94 $cnote['userid'] = $USER->id;
95 $cnote['publishstate'] = 'personal';
96 $cnote['courseid'] = $course->id;
97 $cnote['text'] = 'the text';
98 $cnote['clientnoteid'] = 4;
99 $cnotes = array($cnote);
100 $creatednotes = core_notes_external::create_notes($cnotes);
101 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
103 $dnotes1 = array("notes"=>array($creatednotes[0]['noteid']));
104 $deletednotes1 = core_notes_external::delete_notes($dnotes1);
105 $deletednotes1 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1);
107 // Confirm that base note data was deleted correctly.
108 $notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']);
109 $this->assertEquals(0, $notdeletedcount);
111 $dnotes2 = array("notes"=>array(33)); // This note does not exist.
112 $deletednotes2 = core_notes_external::delete_notes($dnotes2);
113 $deletednotes2 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2);
115 $this->assertEquals("note", $deletednotes2[0]["item"]);
116 $this->assertEquals(33, $deletednotes2[0]["itemid"]);
117 $this->assertEquals("badid", $deletednotes2[0]["warningcode"]);
118 $this->assertEquals("Note does not exist", $deletednotes2[0]["message"]);
120 // Call without required capability.
121 $creatednotes = core_notes_external::create_notes($cnotes);
122 $dnotes3 = array("notes"=>array($creatednotes[0]['noteid']));
124 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
125 $this->setExpectedException('required_capability_exception');
126 $deletednotes = core_notes_external::delete_notes($dnotes3);
129 public function test_get_notes() {
131 global $DB, $USER;
133 $this->resetAfterTest(true);
135 $course = self::getDataGenerator()->create_course();
137 // Set the required capabilities by the external function.
138 $contextid = context_course::instance($course->id)->id;
139 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
140 $this->assignUserCapability('moodle/notes:view', $contextid, $roleid);
141 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
143 // Create test note data.
144 $cnote = array();
145 $cnote['userid'] = $USER->id;
146 $cnote['publishstate'] = 'personal';
147 $cnote['courseid'] = $course->id;
148 $cnote['text'] = 'the text';
149 $cnotes = array($cnote);
151 $creatednotes1 = core_notes_external::create_notes($cnotes);
152 $creatednotes2 = core_notes_external::create_notes($cnotes);
153 $creatednotes3 = core_notes_external::create_notes($cnotes);
155 $creatednotes1 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1);
156 $creatednotes2 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2);
157 $creatednotes3 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3);
159 // Note 33 does not exist.
160 $gnotes = array("notes"=>array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33));
161 $getnotes = core_notes_external::get_notes($gnotes);
162 $getnotes = external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes);
164 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
165 // Confirm that base note data was retrieved correctly.
166 $this->assertEquals($cnote['userid'], $getnotes["notes"][0]["userid"]);
167 $this->assertEquals($cnote['text'], $getnotes["notes"][0]["text"]);
168 $this->assertEquals($cnote['userid'], $getnotes["notes"][1]["userid"]);
169 $this->assertEquals($cnote['text'], $getnotes["notes"][1]["text"]);
170 $this->assertEquals($cnote['userid'], $getnotes["notes"][2]["userid"]);
171 $this->assertEquals($cnote['text'], $getnotes["notes"][2]["text"]);
172 $this->assertEquals("note", $getnotes["warnings"][0]["item"]);
173 $this->assertEquals(33, $getnotes["warnings"][0]["itemid"]);
174 $this->assertEquals("badid", $getnotes["warnings"][0]["warningcode"]);
175 $this->assertEquals("Note does not exist", $getnotes["warnings"][0]["message"]);
177 // Call without required capability.
178 $this->unassignUserCapability('moodle/notes:view', $contextid, $roleid);
179 $this->setExpectedException('required_capability_exception');
180 $creatednotes = core_notes_external::get_notes($gnotes);
183 public function test_update_notes() {
185 global $DB, $USER;
187 $this->resetAfterTest(true);
189 $course = self::getDataGenerator()->create_course();
191 // Set the required capabilities by the external function.
192 $contextid = context_course::instance($course->id)->id;
193 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
194 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
196 // Create test note data.
197 $note1 = array();
198 $note1['userid'] = $USER->id;
199 $note1['publishstate'] = 'personal';
200 $note1['courseid'] = $course->id;
201 $note1['text'] = 'the text';
202 $note2['userid'] = $USER->id;
203 $note2['publishstate'] = 'course';
204 $note2['courseid'] = $course->id;
205 $note2['text'] = 'the text';
206 $note3['userid'] = $USER->id;
207 $note3['publishstate'] = 'site';
208 $note3['courseid'] = $course->id;
209 $note3['text'] = 'the text';
210 $notes1 = array($note1, $note2, $note3);
212 $creatednotes = core_notes_external::create_notes($notes1);
213 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
215 $note2 = array();
216 $note2["id"] = $creatednotes[0]['noteid'];
217 $note2['publishstate'] = 'personal';
218 $note2['text'] = 'the new text';
219 $note2['format'] = FORMAT_HTML;
220 $notes2 = array($note2);
222 $updatednotes = core_notes_external::update_notes($notes2);
224 $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes);
225 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
227 // Confirm that base note data was updated correctly.
228 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
229 $this->assertEquals($note2['text'], $thenote->content);
231 // Call without required capability.
232 $creatednotes = core_notes_external::create_notes($notes1);
233 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
234 $this->setExpectedException('required_capability_exception');
235 $note2 = array();
236 $note2["id"] = $creatednotes[0]['noteid'];
237 $note2['publishstate'] = 'personal';
238 $note2['text'] = 'the new text';
239 $note2['format'] = FORMAT_HTML;
240 $notes2 = array($note2);
241 $updatednotes = core_notes_external::update_notes($notes2);