MDL-68963 qtype_calculatedmulti: show question content inline
[moodle.git] / mod / h5pactivity / tests / external / get_h5pactivities_by_courses_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_h5pactivities_by_courses.
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;
33 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
35 use external_api;
36 use externallib_advanced_testcase;
37 use stdClass;
38 use context_module;
40 /**
41  * External function test for get_h5pactivities_by_courses.
42  *
43  * @package    mod_h5pactivity
44  * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
45  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46  */
47 class get_h5pactivities_by_courses_testcase extends externallib_advanced_testcase {
49     /**
50      * Test test_get_h5pactivities_by_courses user student.
51      */
52     public function test_get_h5pactivities_by_courses() {
53         global $CFG, $DB;
55         $this->resetAfterTest();
56         $this->setAdminUser();
58         // Create 2 courses.
59         // Course 1 -> 2 activities with H5P files package without deploy.
60         // Course 2 -> 1 activity with H5P file package deployed.
61         $course1 = $this->getDataGenerator()->create_course();
62         $params = [
63             'course' => $course1->id,
64             'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/filltheblanks.h5p',
65             'introformat' => 1
66         ];
67         $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
68         // Add filename to make easier the asserts.
69         $activities[0]->filename = 'filltheblanks.h5p';
70         $params = [
71             'course' => $course1->id,
72             'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/greeting-card-887.h5p',
73             'introformat' => 1
74         ];
75         $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
76         // Add filename to make easier the asserts.
77         $activities[1]->filename = 'greeting-card-887.h5p';
79         $course2 = $this->getDataGenerator()->create_course();
80         $params = [
81             'course' => $course2->id,
82             'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/guess-the-answer.h5p',
83             'introformat' => 1
84         ];
85         $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
86         $activities[2]->filename = 'guess-the-answer.h5p';
88         $context = context_module::instance($activities[2]->cmid);
89         // Create a fake deploy H5P file.
90         $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
91         $deployedfile = $generator->create_export_file($activities[2]->filename, $context->id, 'mod_h5pactivity', 'package');
93         // Create a user and enrol as student in both courses.
94         $user = $this->getDataGenerator()->create_user();
95         $studentrole = $DB->get_record('role', ['shortname' => 'student']);
96         $maninstance1 = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'manual'], '*', MUST_EXIST);
97         $maninstance2 = $DB->get_record('enrol', ['courseid' => $course2->id, 'enrol' => 'manual'], '*', MUST_EXIST);
98         $manual = enrol_get_plugin('manual');
99         $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
100         $manual->enrol_user($maninstance2, $user->id, $studentrole->id);
102         // Check the activities returned by the first course.
103         $this->setUser($user);
104         $courseids = [$course1->id];
105         $result = get_h5pactivities_by_courses::execute($courseids);
106         $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
107         $this->assertCount(0, $result['warnings']);
108         $this->assertCount(2, $result['h5pactivities']);
109         $this->assert_activities($activities, $result);
110         $this->assertNotContains('deployedfile', $result['h5pactivities'][0]);
111         $this->assertNotContains('deployedfile', $result['h5pactivities'][1]);
113         // Call the external function without passing course id.
114         // Expected result, all the courses, course1 and course2.
115         $result = get_h5pactivities_by_courses::execute([]);
116         $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
117         $this->assertCount(0, $result['warnings']);
118         $this->assertCount(3, $result['h5pactivities']);
119         // We need to sort the $result by id.
120         // Because we are not sure how it is ordered with more than one course.
121         array_multisort(array_map(function($element) {
122             return $element['id'];
123         }, $result['h5pactivities']), SORT_ASC, $result['h5pactivities']);
124         $this->assert_activities($activities, $result);
125         $this->assertNotContains('deployedfile', $result['h5pactivities'][0]);
126         $this->assertNotContains('deployedfile', $result['h5pactivities'][1]);
127         // Only the activity from the second course has been deployed.
128         $this->assertEquals($deployedfile['filename'], $result['h5pactivities'][2]['deployedfile']['filename']);
129         $this->assertEquals($deployedfile['filepath'], $result['h5pactivities'][2]['deployedfile']['filepath']);
130         $this->assertEquals($deployedfile['filesize'], $result['h5pactivities'][2]['deployedfile']['filesize']);
131         $this->assertEquals($deployedfile['timemodified'], $result['h5pactivities'][2]['deployedfile']['timemodified']);
132         $this->assertEquals($deployedfile['mimetype'], $result['h5pactivities'][2]['deployedfile']['mimetype']);
133         $this->assertEquals($deployedfile['fileurl'], $result['h5pactivities'][2]['deployedfile']['fileurl']);
135         // Unenrol user from second course.
136         $manual->unenrol_user($maninstance2, $user->id);
137         // Remove the last activity from the array.
138         array_pop($activities);
140         // Call the external function without passing course id.
141         $result = get_h5pactivities_by_courses::execute([]);
142         $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
143         $this->assertCount(0, $result['warnings']);
144         $this->assertCount(2, $result['h5pactivities']);
145         $this->assert_activities($activities, $result);
147         // Call for the second course we unenrolled the user from, expected warning.
148         $result = get_h5pactivities_by_courses::execute([$course2->id]);
149         $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
150         $this->assertCount(1, $result['warnings']);
151         $this->assertEquals('1', $result['warnings'][0]['warningcode']);
152         $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
153     }
155     /**
156      * Create a scenario to use into the tests.
157      *
158      * @param  array $activities list of H5P activities.
159      * @param  array $result list of H5P activities by WS.
160      * @return void
161      */
162     protected function assert_activities(array $activities, array $result): void {
164         $total = count($result);
165         for ($i = 0; $i < $total; $i++) {
166             $this->assertEquals($activities[$i]->id, $result['h5pactivities'][$i]['id']);
167             $this->assertEquals($activities[$i]->course, $result['h5pactivities'][$i]['course']);
168             $this->assertEquals($activities[$i]->name, $result['h5pactivities'][$i]['name']);
169             $this->assertEquals($activities[$i]->timecreated, $result['h5pactivities'][$i]['timecreated']);
170             $this->assertEquals($activities[$i]->timemodified, $result['h5pactivities'][$i]['timemodified']);
171             $this->assertEquals($activities[$i]->intro, $result['h5pactivities'][$i]['intro']);
172             $this->assertEquals($activities[$i]->introformat, $result['h5pactivities'][$i]['introformat']);
173             $this->assertEquals([], $result['h5pactivities'][$i]['introfiles']);
174             $this->assertEquals($activities[$i]->grade, $result['h5pactivities'][$i]['grade']);
175             $this->assertEquals($activities[$i]->displayoptions, $result['h5pactivities'][$i]['displayoptions']);
176             $this->assertEquals($activities[$i]->enabletracking, $result['h5pactivities'][$i]['enabletracking']);
177             $this->assertEquals($activities[$i]->grademethod, $result['h5pactivities'][$i]['grademethod']);
178             $this->assertEquals($activities[$i]->cmid, $result['h5pactivities'][$i]['coursemodule']);
179             $this->assertEquals($activities[$i]->filename, $result['h5pactivities'][$i]['package'][0]['filename']);
180         }
181     }