MDL-68963 qtype_calculatedmulti: show question content inline
[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     public function test_get_h5pactivity_access_information() {
51         $this->resetAfterTest();
52         $this->setAdminUser();
54         $course = $this->getDataGenerator()->create_course();
55         $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
56         $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
57         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
59         // Check the access information for a student.
60         $this->setUser($student);
61         $result = get_h5pactivity_access_information::execute($activity->id);
62         $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
63         $this->assertCount(0, $result['warnings']);
64         unset($result['warnings']);
66         // Check default values for capabilities for student.
67         $enabledcaps = ['canview', 'cansubmit'];
68         foreach ($result as $capname => $capvalue) {
69             if (in_array($capname, $enabledcaps)) {
70                 $this->assertTrue($capvalue);
71             } else {
72                 $this->assertFalse($capvalue);
73             }
74         }
76         // Check the access information for a teacher.
77         $this->setUser($teacher);
78         $result = get_h5pactivity_access_information::execute($activity->id);
79         $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
80         $this->assertCount(0, $result['warnings']);
81         unset($result['warnings']);
83         // Check default values for capabilities for teacher.
84         $enabledcaps = ['canview', 'canaddinstance', 'canreviewattempts'];
85         foreach ($result as $capname => $capvalue) {
86             if (in_array($capname, $enabledcaps)) {
87                 $this->assertTrue($capvalue);
88             } else {
89                 $this->assertFalse($capvalue);
90             }
91         }
93         // Call the WS using an unexisting h5pactivityid.
94         $this->expectException(dml_missing_record_exception::class);
95         $result = get_h5pactivity_access_information::execute($activity->id + 1);
96     }
97 }