MDL-37247 mod_forum: adding webservice function to allow for the retrieval of forum...
[moodle.git] / mod / forum / tests / externallib_test.php
CommitLineData
2b9fe87d
MN
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * The module forums external functions unit tests
20 *
21 * @package mod_forum
22 * @category external
23 * @copyright 2012 Mark Nelson <markn@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33class mod_forum_external_testcase extends externallib_advanced_testcase {
34
35 /**
36 * Tests set up
37 */
38 protected function setUp() {
39 global $CFG;
40
41 require_once($CFG->dirroot . '/mod/forum/externallib.php');
42 }
43
44 /**
45 * Test get forums
46 */
47 public function test_mod_forum_get_forums_by_courses() {
48 global $USER, $CFG, $DB;
49
50 $this->resetAfterTest(true);
51
52 // Create a user.
53 $user = self::getDataGenerator()->create_user();
54
55 // Set to the user.
56 self::setUser($user);
57
58 // Create courses to add the modules.
59 $course1 = self::getDataGenerator()->create_course();
60 $course2 = self::getDataGenerator()->create_course();
61
62 // First forum.
63 $record = new stdClass();
64 $record->introformat = FORMAT_HTML;
65 $record->course = $course1->id;
66 $forum1 = self::getDataGenerator()->create_module('forum', $record);
67
68 // Second forum.
69 $record = new stdClass();
70 $record->introformat = FORMAT_HTML;
71 $record->course = $course2->id;
72 $forum2 = self::getDataGenerator()->create_module('forum', $record);
73
74 // Check the forum was correctly created.
75 $this->assertEquals(2, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2',
76 array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
77
78 // Enrol the user in two courses.
79 // Enrol them in the first course.
80 $enrol = enrol_get_plugin('manual');
81 $enrolinstances = enrol_get_instances($course1->id, true);
82 foreach ($enrolinstances as $courseenrolinstance) {
83 if ($courseenrolinstance->enrol == "manual") {
84 $instance1 = $courseenrolinstance;
85 break;
86 }
87 }
88 $enrol->enrol_user($instance1, $user->id);
89 // Now enrol into the second course.
90 $enrolinstances = enrol_get_instances($course2->id, true);
91 foreach ($enrolinstances as $courseenrolinstance) {
92 if ($courseenrolinstance->enrol == "manual") {
93 $instance2 = $courseenrolinstance;
94 break;
95 }
96 }
97 $enrol->enrol_user($instance2, $user->id);
98
99 // Assign capabilities to view forums for forum 1.
100 $cm1 = get_coursemodule_from_id('forum', $forum1->id, 0, false, MUST_EXIST);
101 $context1 = context_module::instance($cm1->id);
102 $roleid1 = $this->assignUserCapability('mod/forum:viewdiscussion', $context1->id);
103
104 // Assign capabilities to view forums for forum 2.
105 $cm2 = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
106 $context2 = context_module::instance($cm2->id);
107 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
108 $roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
109
110 // Create what we expect to be returned when querying the two courses.
111 $expectedforums = array();
112 $expectedforums[$forum1->id] = (array) $forum1;
113 $expectedforums[$forum2->id] = (array) $forum2;
114
115 // Call the external function passing course ids.
116 $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
117 $this->assertEquals($forums, $expectedforums);
118 external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
119
120 // Call the external function without passing course id.
121 $forums = mod_forum_external::get_forums_by_courses();
122 $this->assertEquals($forums, $expectedforums);
123 external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
124
125 // Unenrol user from second course and alter expected forums.
126 $enrol->unenrol_user($instance2, $user->id);
127 unset($expectedforums[$forum2->id]);
128
129 // Call the external function without passing course id.
130 $forums = mod_forum_external::get_forums_by_courses();
131 $this->assertEquals($forums, $expectedforums);
132 external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
133
134 // Call for the second course we unenrolled the user from, make sure exception thrown.
135 $this->setExpectedException('require_login_exception');
136 mod_forum_external::get_forums_by_courses(array($course2->id));
137
138 // Call without required capability.
139 $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $roleid1);
140 $this->setExpectedException('required_capability_exception');
141 mod_forum_external::get_forums_by_courses(array($course1->id));
142 }
143}