Merge branch 'MDL-68963-master' of git://github.com/bmbrands/moodle
[moodle.git] / mod / h5pactivity / tests / external / get_h5pactivity_access_information_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 function test for get_h5pactivity_access_information.
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  */
27 namespace mod_h5pactivity\external;
29 defined('MOODLE_INTERNAL') || die();
31 global $CFG;
32 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
34 use dml_missing_record_exception;
35 use external_api;
36 use externallib_advanced_testcase;
38 /**
39  * External function test for get_h5pactivity_access_information.
40  *
41  * @package    mod_h5pactivity
42  * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
43  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class get_h5pactivity_access_information_testcase extends externallib_advanced_testcase {
47     /**
48      * Test the behaviour of get_h5pactivity_access_information().
49      *
50      * @dataProvider get_h5pactivity_access_information_data
51      * @param string $role user role in course
52      * @param int $enabletracking if tracking is enabled
53      * @param array $enabledcaps capabilities enabled
54      */
55     public function test_get_h5pactivity_access_information(string $role, int $enabletracking, array $enabledcaps) {
56         $this->resetAfterTest();
57         $this->setAdminUser();
59         $course = $this->getDataGenerator()->create_course();
60         $activity = $this->getDataGenerator()->create_module('h5pactivity',
61             [
62                 'course' => $course,
63                 'enabletracking' => $enabletracking
64             ]
65         );
67         if ($role) {
68             $user = $this->getDataGenerator()->create_and_enrol($course, $role);
69             $this->setUser($user);
70         }
72         // Check the access information.
73         $result = get_h5pactivity_access_information::execute($activity->id);
74         $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
75         $this->assertCount(0, $result['warnings']);
76         unset($result['warnings']);
78         // Check the values for capabilities.
79         foreach ($result as $capname => $capvalue) {
80             if (in_array($capname, $enabledcaps)) {
81                 $this->assertTrue($capvalue);
82             } else {
83                 $this->assertFalse($capvalue);
84             }
85         }
86     }
88     /**
89      * Data provider for get_h5pactivity_access_information.
90      *
91      * @return array
92      */
93     public function get_h5pactivity_access_information_data(): array {
94         return [
95             'Admin, tracking enabled' => [
96                 '', 1, ['canview', 'canreviewattempts', 'canaddinstance']
97             ],
98             'Admin, tracking disabled' => [
99                 '', 0, ['canview', 'canreviewattempts', 'canaddinstance']
100             ],
101             'Student, tracking enabled' => [
102                 'student', 1, ['canview', 'cansubmit']
103             ],
104             'Student, tracking disabled' => [
105                 'student', 0, ['canview']
106             ],
107             'Teacher, tracking enabled' => [
108                 'editingteacher', 1, [
109                     'canview',
110                     'canreviewattempts',
111                     'canaddinstance'
112                 ]
113             ],
114             'Teacher, tracking disabled' => [
115                 'editingteacher', 0, [
116                     'canview',
117                     'canreviewattempts',
118                     'canaddinstance'
119                 ]
120             ],
121         ];
122     }
124     /**
125      * Test dml_missing_record_exception in get_h5pactivity_access_information.
126      */
127     public function test_dml_missing_record_exception() {
128         $this->resetAfterTest();
129         $this->setAdminUser();
131         $course = $this->getDataGenerator()->create_course();
133         // Call the WS using an unexisting h5pactivityid.
134         $this->expectException(dml_missing_record_exception::class);
135         $result = get_h5pactivity_access_information::execute(1);
136     }