MDL-49791 page: New Web Service mod_page_view_page
[moodle.git] / mod / page / tests / externallib_test.php
CommitLineData
09188110
JL
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 mod_page functions unit tests
19 *
20 * @package mod_page
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 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * External mod_page functions unit tests
35 *
36 * @package mod_page
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 */
42class mod_page_external_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test view_page
46 */
47 public function test_view_page() {
48 global $DB;
49
50 $this->resetAfterTest(true);
51
52 // Setup test data.
53 $course = $this->getDataGenerator()->create_course();
54 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
55 $context = context_module::instance($page->cmid);
56 $cm = get_coursemodule_from_instance('page', $page->id);
57
58 // Test invalid instance id.
59 try {
60 mod_page_external::view_page(0);
61 $this->fail('Exception expected due to invalid mod_page instance id.');
62 } catch (moodle_exception $e) {
63 $this->assertEquals('invalidrecord', $e->errorcode);
64 }
65
66 // Test not-enrolled user.
67 $user = self::getDataGenerator()->create_user();
68 $this->setUser($user);
69 try {
70 mod_page_external::view_page($page->id);
71 $this->fail('Exception expected due to not enrolled user.');
72 } catch (moodle_exception $e) {
73 $this->assertEquals('requireloginerror', $e->errorcode);
74 }
75
76 // Test user with full capabilities.
77 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
78 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
79
80 // Trigger and capture the event.
81 $sink = $this->redirectEvents();
82
83 $result = mod_page_external::view_page($page->id);
84 $result = external_api::clean_returnvalue(mod_page_external::view_page_returns(), $result);
85
86 $events = $sink->get_events();
87 $this->assertCount(1, $events);
88 $event = array_shift($events);
89
90 // Checking that the event contains the expected values.
91 $this->assertInstanceOf('\mod_page\event\course_module_viewed', $event);
92 $this->assertEquals($context, $event->get_context());
93 $moodlepage = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
94 $this->assertEquals($moodlepage, $event->get_url());
95 $this->assertEventContextNotUsed($event);
96 $this->assertNotEmpty($event->get_name());
97
98 // Test user with no capabilities.
99 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
100 assign_capability('mod/page:view', CAP_PROHIBIT, $studentrole->id, $context->id);
101 accesslib_clear_all_caches_for_unit_testing();
102
103 try {
104 mod_page_external::view_page($page->id);
105 $this->fail('Exception expected due to missing capability.');
106 } catch (moodle_exception $e) {
107 $this->assertEquals('nopermissions', $e->errorcode);
108 }
109
110 }
111}