weekly release 2.5dev
[moodle.git] / notes / tests / externallib_test.php
CommitLineData
4647b4d7
JM
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/**
18 * External notes functions unit tests
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31require_once($CFG->dirroot . '/notes/externallib.php');
32
33class core_notes_external_testcase extends externallib_advanced_testcase {
34
35 /**
36 * Test create_notes
37 */
38 public function test_create_notes() {
39
40 global $DB, $USER, $DB;
41
42 $this->resetAfterTest(true);
43
44 $course = self::getDataGenerator()->create_course();
45
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);
50
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);
59
60 $creatednotes = core_notes_external::create_notes($notes);
61
fb695f6e
JM
62 // We need to execute the return values cleaning process to simulate the web service server.
63 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
64
4647b4d7
JM
65 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
66
67 // Confirm that base note data was inserted correctly.
68 $this->assertEquals($thenote->userid, $note1['userid']);
69 $this->assertEquals($thenote->courseid, $note1['courseid']);
70 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
71 $this->assertEquals($thenote->content, $note1['text']);
72 $this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);
73
74 // Call without required capability
75 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
76 $this->setExpectedException('required_capability_exception');
77 $creatednotes = core_notes_external::create_notes($notes);
78
79 }
80}