MDL-50013 mod_book: New WS mod_book_view_book
[moodle.git] / mod / book / tests / externallib_test.php
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/>.
17 /**
18  * External mod_book functions unit tests
19  *
20  * @package    mod_book
21  * @category   external
22  * @copyright  2015 Juan Leyva <juan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since      Moodle 3.0
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33 /**
34  * External mod_book functions unit tests
35  *
36  * @package    mod_book
37  * @category   external
38  * @copyright  2015 Juan Leyva <juan@moodle.com>
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  * @since      Moodle 3.0
41  */
42 class mod_book_external_testcase extends externallib_advanced_testcase {
44     /**
45      * Test view_book
46      */
47     public function test_view_book() {
48         global $DB;
50         $this->resetAfterTest(true);
52         $this->setAdminUser();
53         // Setup test data.
54         $course = $this->getDataGenerator()->create_course();
55         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
56         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
57         $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
58         $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1));
60         $context = context_module::instance($book->cmid);
61         $cm = get_coursemodule_from_instance('book', $book->id);
63         // Test invalid instance id.
64         try {
65             mod_book_external::view_book(0);
66             $this->fail('Exception expected due to invalid mod_book instance id.');
67         } catch (moodle_exception $e) {
68             $this->assertEquals('invalidrecord', $e->errorcode);
69         }
71         // Test not-enrolled user.
72         $user = self::getDataGenerator()->create_user();
73         $this->setUser($user);
74         try {
75             mod_book_external::view_book($book->id, 0);
76             $this->fail('Exception expected due to not enrolled user.');
77         } catch (moodle_exception $e) {
78             $this->assertEquals('requireloginerror', $e->errorcode);
79         }
81         // Test user with full capabilities.
82         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
83         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
85         // Trigger and capture the event.
86         $sink = $this->redirectEvents();
88         $result = mod_book_external::view_book($book->id, 0);
89         $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
91         $events = $sink->get_events();
92         $this->assertCount(2, $events);
93         $event = array_shift($events);
95         // Checking that the event contains the expected values.
96         $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
97         $this->assertEquals($context, $event->get_context());
98         $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
99         $this->assertEquals($moodleurl, $event->get_url());
100         $this->assertEventContextNotUsed($event);
101         $this->assertNotEmpty($event->get_name());
103         $event = array_shift($events);
104         $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
105         $this->assertEquals($chapter->id, $event->objectid);
107         $result = mod_book_external::view_book($book->id, $chapter->id);
108         $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
110         $events = $sink->get_events();
111         // We expect a total of 3 events.
112         $this->assertCount(3, $events);
114         // Try to view a hidden chapter.
115         try {
116             mod_book_external::view_book($book->id, $chapterhidden->id);
117             $this->fail('Exception expected due to missing capability.');
118         } catch (moodle_exception $e) {
119             $this->assertEquals('errorchapter', $e->errorcode);
120         }
122         // Test user with no capabilities.
123         // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
124         assign_capability('mod/book:read', CAP_PROHIBIT, $studentrole->id, $context->id);
125         accesslib_clear_all_caches_for_unit_testing();
127         try {
128             mod_book_external::view_book($book->id, 0);
129             $this->fail('Exception expected due to missing capability.');
130         } catch (moodle_exception $e) {
131             $this->assertEquals('nopermissions', $e->errorcode);
132         }
134     }