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 |
/**
|
|
|
18 |
* External function test for view_h5pactivity.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_h5pactivity
|
|
|
21 |
* @category external
|
|
|
22 |
* @since Moodle 3.9
|
|
|
23 |
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace mod_h5pactivity\external;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
|
|
|
33 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
34 |
|
|
|
35 |
use core_external\external_api;
|
|
|
36 |
use externallib_advanced_testcase;
|
|
|
37 |
use stdClass;
|
|
|
38 |
use context_module;
|
|
|
39 |
use course_modinfo;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* External function test for view_h5pactivity.
|
|
|
43 |
*
|
|
|
44 |
* @package mod_h5pactivity
|
|
|
45 |
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
|
|
|
46 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
47 |
*/
|
|
|
48 |
class view_h5pactivity_test extends externallib_advanced_testcase {
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Test test_view_h5pactivity invalid id.
|
|
|
52 |
*/
|
|
|
53 |
public function test_view_h5pactivity_invalid_id() {
|
|
|
54 |
$this->resetAfterTest();
|
|
|
55 |
$this->setAdminUser();
|
|
|
56 |
|
|
|
57 |
$this->expectException('moodle_exception');
|
|
|
58 |
$result = view_h5pactivity::execute(0);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Test test_view_h5pactivity user not enrolled.
|
|
|
63 |
*/
|
|
|
64 |
public function test_view_h5pactivity_user_not_enrolled() {
|
|
|
65 |
$this->resetAfterTest();
|
|
|
66 |
$this->setAdminUser();
|
|
|
67 |
|
|
|
68 |
// Setup scenario.
|
|
|
69 |
$scenario = $this->setup_scenario();
|
|
|
70 |
|
|
|
71 |
// Test not-enrolled user.
|
|
|
72 |
$usernotenrolled = self::getDataGenerator()->create_user();
|
|
|
73 |
$this->setUser($usernotenrolled);
|
|
|
74 |
$this->expectException('moodle_exception');
|
|
|
75 |
$result = view_h5pactivity::execute($scenario->h5pactivity->id);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Test test_view_h5pactivity user student.
|
|
|
80 |
*/
|
|
|
81 |
public function test_view_h5pactivity_user_student() {
|
|
|
82 |
$this->resetAfterTest();
|
|
|
83 |
$this->setAdminUser();
|
|
|
84 |
|
|
|
85 |
// Setup scenario.
|
|
|
86 |
$scenario = $this->setup_scenario();
|
|
|
87 |
|
|
|
88 |
$cm = get_coursemodule_from_instance('h5pactivity', $scenario->h5pactivity->id);
|
|
|
89 |
$this->setUser($scenario->student);
|
|
|
90 |
|
|
|
91 |
// Trigger and capture the event.
|
|
|
92 |
$sink = $this->redirectEvents();
|
|
|
93 |
|
|
|
94 |
$result = view_h5pactivity::execute($scenario->h5pactivity->id);
|
|
|
95 |
$result = external_api::clean_returnvalue(view_h5pactivity::execute_returns(), $result);
|
|
|
96 |
$this->assertTrue($result['status']);
|
|
|
97 |
|
|
|
98 |
$events = $sink->get_events();
|
|
|
99 |
$this->assertCount(1, $events);
|
|
|
100 |
$event = array_shift($events);
|
|
|
101 |
|
|
|
102 |
// Checking that the event contains the expected values.
|
|
|
103 |
$this->assertInstanceOf('\mod_h5pactivity\event\course_module_viewed', $event);
|
|
|
104 |
$this->assertEquals($scenario->contextmodule, $event->get_context());
|
|
|
105 |
$h5pactivity = new \moodle_url('/mod/h5pactivity/view.php', array('id' => $cm->id));
|
|
|
106 |
$this->assertEquals($h5pactivity, $event->get_url());
|
|
|
107 |
$this->assertEventContextNotUsed($event);
|
|
|
108 |
$this->assertNotEmpty($event->get_name());
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Test test_view_h5pactivity user missing capabilities.
|
|
|
113 |
*/
|
|
|
114 |
public function test_view_h5pactivity_user_missing_capabilities() {
|
|
|
115 |
global $DB;
|
|
|
116 |
|
|
|
117 |
$this->resetAfterTest();
|
|
|
118 |
$this->setAdminUser();
|
|
|
119 |
|
|
|
120 |
// Setup scenario.
|
|
|
121 |
$scenario = $this->setup_scenario();
|
|
|
122 |
|
|
|
123 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
124 |
// Test user with no capabilities.
|
|
|
125 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
126 |
assign_capability('mod/h5pactivity:view', CAP_PROHIBIT, $studentrole->id, $scenario->contextmodule->id);
|
|
|
127 |
// Empty all the caches that may be affected by this change.
|
|
|
128 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
129 |
course_modinfo::clear_instance_cache();
|
|
|
130 |
|
|
|
131 |
$this->setUser($scenario->student);
|
|
|
132 |
$this->expectException('moodle_exception');
|
|
|
133 |
$result = view_h5pactivity::execute($scenario->h5pactivity->id);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Create a scenario to use into the tests.
|
|
|
138 |
*
|
|
|
139 |
* @return stdClass $scenario
|
|
|
140 |
*/
|
|
|
141 |
protected function setup_scenario() {
|
|
|
142 |
|
|
|
143 |
$course = $this->getDataGenerator()->create_course();
|
|
|
144 |
$h5pactivity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
|
|
|
145 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
146 |
$contextmodule = context_module::instance($h5pactivity->cmid);
|
|
|
147 |
|
|
|
148 |
$scenario = new stdClass();
|
|
|
149 |
$scenario->contextmodule = $contextmodule;
|
|
|
150 |
$scenario->student = $student;
|
|
|
151 |
$scenario->h5pactivity = $h5pactivity;
|
|
|
152 |
|
|
|
153 |
return $scenario;
|
|
|
154 |
}
|
|
|
155 |
}
|