1 |
efrain |
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 |
namespace mod_book;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use externallib_advanced_testcase;
|
|
|
21 |
use mod_book_external;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* External mod_book functions unit tests
|
|
|
31 |
*
|
|
|
32 |
* @package mod_book
|
|
|
33 |
* @category external
|
|
|
34 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @since Moodle 3.0
|
|
|
37 |
*/
|
|
|
38 |
class externallib_test extends externallib_advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Test view_book
|
|
|
42 |
*/
|
11 |
efrain |
43 |
public function test_view_book(): void {
|
1 |
efrain |
44 |
global $DB;
|
|
|
45 |
|
|
|
46 |
$this->resetAfterTest(true);
|
|
|
47 |
|
|
|
48 |
$this->setAdminUser();
|
|
|
49 |
// Setup test data.
|
|
|
50 |
$course = $this->getDataGenerator()->create_course();
|
|
|
51 |
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
|
|
|
52 |
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
|
|
|
53 |
$chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
|
|
|
54 |
$chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1));
|
|
|
55 |
|
|
|
56 |
$context = \context_module::instance($book->cmid);
|
|
|
57 |
$cm = get_coursemodule_from_instance('book', $book->id);
|
|
|
58 |
|
|
|
59 |
// Test invalid instance id.
|
|
|
60 |
try {
|
|
|
61 |
mod_book_external::view_book(0);
|
|
|
62 |
$this->fail('Exception expected due to invalid mod_book instance id.');
|
|
|
63 |
} catch (\moodle_exception $e) {
|
|
|
64 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// Test not-enrolled user.
|
|
|
68 |
$user = self::getDataGenerator()->create_user();
|
|
|
69 |
$this->setUser($user);
|
|
|
70 |
try {
|
|
|
71 |
mod_book_external::view_book($book->id, 0);
|
|
|
72 |
$this->fail('Exception expected due to not enrolled user.');
|
|
|
73 |
} catch (\moodle_exception $e) {
|
|
|
74 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Test user with full capabilities.
|
|
|
78 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
79 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
80 |
|
|
|
81 |
// Trigger and capture the event.
|
|
|
82 |
$sink = $this->redirectEvents();
|
|
|
83 |
|
|
|
84 |
$result = mod_book_external::view_book($book->id, 0);
|
|
|
85 |
$result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
|
|
|
86 |
|
|
|
87 |
$events = $sink->get_events();
|
|
|
88 |
$this->assertCount(2, $events);
|
|
|
89 |
$event = array_shift($events);
|
|
|
90 |
|
|
|
91 |
// Checking that the event contains the expected values.
|
|
|
92 |
$this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
|
|
|
93 |
$this->assertEquals($context, $event->get_context());
|
|
|
94 |
$moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
|
|
|
95 |
$this->assertEquals($moodleurl, $event->get_url());
|
|
|
96 |
$this->assertEventContextNotUsed($event);
|
|
|
97 |
$this->assertNotEmpty($event->get_name());
|
|
|
98 |
|
|
|
99 |
$event = array_shift($events);
|
|
|
100 |
$this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
|
|
|
101 |
$this->assertEquals($chapter->id, $event->objectid);
|
|
|
102 |
|
|
|
103 |
$result = mod_book_external::view_book($book->id, $chapter->id);
|
|
|
104 |
$result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
|
|
|
105 |
|
|
|
106 |
$events = $sink->get_events();
|
|
|
107 |
// We expect a total of 3 events.
|
|
|
108 |
$this->assertCount(3, $events);
|
|
|
109 |
|
|
|
110 |
// Try to view a hidden chapter.
|
|
|
111 |
try {
|
|
|
112 |
mod_book_external::view_book($book->id, $chapterhidden->id);
|
|
|
113 |
$this->fail('Exception expected due to missing capability.');
|
|
|
114 |
} catch (\moodle_exception $e) {
|
|
|
115 |
$this->assertEquals('errorchapter', $e->errorcode);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// Test user with no capabilities.
|
|
|
119 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
120 |
assign_capability('mod/book:read', CAP_PROHIBIT, $studentrole->id, $context->id);
|
|
|
121 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
122 |
|
|
|
123 |
try {
|
|
|
124 |
mod_book_external::view_book($book->id, 0);
|
|
|
125 |
$this->fail('Exception expected due to missing capability.');
|
|
|
126 |
} catch (\moodle_exception $e) {
|
|
|
127 |
$this->assertEquals('nopermissions', $e->errorcode);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Test get_books_by_courses
|
|
|
134 |
*/
|
11 |
efrain |
135 |
public function test_get_books_by_courses(): void {
|
1 |
efrain |
136 |
global $DB, $USER;
|
|
|
137 |
$this->resetAfterTest(true);
|
|
|
138 |
$this->setAdminUser();
|
|
|
139 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
140 |
$bookoptions1 = array(
|
|
|
141 |
'course' => $course1->id,
|
|
|
142 |
'name' => 'First Book'
|
|
|
143 |
);
|
|
|
144 |
$book1 = self::getDataGenerator()->create_module('book', $bookoptions1);
|
|
|
145 |
$course2 = self::getDataGenerator()->create_course();
|
|
|
146 |
$bookoptions2 = array(
|
|
|
147 |
'course' => $course2->id,
|
|
|
148 |
'name' => 'Second Book'
|
|
|
149 |
);
|
|
|
150 |
$book2 = self::getDataGenerator()->create_module('book', $bookoptions2);
|
|
|
151 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
152 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
153 |
|
|
|
154 |
// Enroll Student1 in Course1.
|
|
|
155 |
self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
|
|
|
156 |
$this->setUser($student1);
|
|
|
157 |
|
|
|
158 |
$books = mod_book_external::get_books_by_courses();
|
|
|
159 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
160 |
$books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
|
|
|
161 |
$this->assertCount(1, $books['books']);
|
|
|
162 |
$this->assertEquals('First Book', $books['books'][0]['name']);
|
|
|
163 |
// We see 10 fields.
|
|
|
164 |
$this->assertCount(11, $books['books'][0]);
|
|
|
165 |
|
|
|
166 |
// As Student you cannot see some book properties like 'section'.
|
|
|
167 |
$this->assertFalse(isset($books['books'][0]['section']));
|
|
|
168 |
|
|
|
169 |
// Student1 is not enrolled in course2. The webservice will return a warning!
|
|
|
170 |
$books = mod_book_external::get_books_by_courses(array($course2->id));
|
|
|
171 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
172 |
$books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
|
|
|
173 |
$this->assertCount(0, $books['books']);
|
|
|
174 |
$this->assertEquals(1, $books['warnings'][0]['warningcode']);
|
|
|
175 |
|
|
|
176 |
// Now as admin.
|
|
|
177 |
$this->setAdminUser();
|
|
|
178 |
// As Admin we can see this book.
|
|
|
179 |
$books = mod_book_external::get_books_by_courses(array($course2->id));
|
|
|
180 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
181 |
$books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
|
|
|
182 |
|
|
|
183 |
$this->assertCount(1, $books['books']);
|
|
|
184 |
$this->assertEquals('Second Book', $books['books'][0]['name']);
|
|
|
185 |
// We see 17 fields.
|
|
|
186 |
$this->assertCount(18, $books['books'][0]);
|
|
|
187 |
// As an Admin you can see some book properties like 'section'.
|
|
|
188 |
$this->assertEquals(0, $books['books'][0]['section']);
|
|
|
189 |
|
|
|
190 |
// Enrol student in the second course.
|
|
|
191 |
self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
|
|
|
192 |
$this->setUser($student1);
|
|
|
193 |
$books = mod_book_external::get_books_by_courses();
|
|
|
194 |
$books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
|
|
|
195 |
$this->assertCount(2, $books['books']);
|
|
|
196 |
|
|
|
197 |
}
|
|
|
198 |
}
|