Merge branch 'MDL-68569-310' of https://github.com/paulholden/moodle into MOODLE_310_...
[moodle.git] / contentbank / contenttype / h5p / tests / contenttype_h5p_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  * Test for H5P content bank plugin.
19  *
20  * @package    contenttype_h5p
21  * @category   test
22  * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 /**
27  * Test for H5P content bank plugin.
28  *
29  * @package    contenttype_h5p
30  * @category   test
31  * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
32  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  * @coversDefaultClass \contenttype_h5p\contenttype
34  */
35 class contenttype_h5p_contenttype_plugin_testcase extends advanced_testcase {
37     /**
38      * Test the behaviour of delete_content().
39      */
40     public function test_delete_content() {
41         global $CFG, $USER, $DB;
43         $this->resetAfterTest();
44         $systemcontext = context_system::instance();
46         // Create users.
47         $roleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
48         $manager = $this->getDataGenerator()->create_user();
49         $this->getDataGenerator()->role_assign($roleid, $manager->id);
50         $this->setUser($manager);
52         // Add an H5P file to the content bank.
53         $filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
54         $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
55         $contents = $generator->generate_contentbank_data('contenttype_h5p', 2, $USER->id, $systemcontext, true, $filepath);
56         $content1 = array_shift($contents);
57         $content2 = array_shift($contents);
59         // Load this H5P file though the player to create the H5P DB entries.
60         $h5pplayer = new \core_h5p\player($content1->get_file_url(), new \stdClass(), true);
61         $h5pplayer->add_assets_to_page();
62         $h5pplayer->output();
63         $h5pplayer = new \core_h5p\player($content2->get_file_url(), new \stdClass(), true);
64         $h5pplayer->add_assets_to_page();
65         $h5pplayer->output();
67         // Check the H5P content has been created.
68         $this->assertEquals(2, $DB->count_records('h5p'));
69         $this->assertEquals(2, $DB->count_records('contentbank_content'));
71         // Check the H5P content is removed after calling this method.
72         $contenttype = new \contenttype_h5p\contenttype($systemcontext);
73         $contenttype->delete_content($content1);
74         $this->assertEquals(1, $DB->count_records('h5p'));
75         $this->assertEquals(1, $DB->count_records('contentbank_content'));
76     }
78     /**
79      * Tests can_upload behavior.
80      *
81      * @covers ::can_upload
82      */
83     public function test_can_upload() {
84         $this->resetAfterTest();
86         $systemcontext = \context_system::instance();
87         $systemtype = new \contenttype_h5p\contenttype($systemcontext);
89         // Admins can upload.
90         $this->setAdminUser();
91         $this->assertTrue($systemtype->can_upload());
93         // Teacher can upload in the course but not at system level.
94         $course = $this->getDataGenerator()->create_course();
95         $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
96         $coursecontext = \context_course::instance($course->id);
97         $coursetype = new \contenttype_h5p\contenttype($coursecontext);
98         $this->setUser($teacher);
99         $this->assertTrue($coursetype->can_upload());
100         $this->assertFalse($systemtype->can_upload());
102         // Users can't upload.
103         $user = $this->getDataGenerator()->create_user();
104         $this->setUser($user);
105         $this->assertFalse($coursetype->can_upload());
106         $this->assertFalse($systemtype->can_upload());
107     }
109     /**
110      * Tests get_icon result.
111      *
112      * @covers ::get_icon
113      */
114     public function test_get_icon() {
115         global $CFG;
117         $this->resetAfterTest();
118         $systemcontext = context_system::instance();
119         $this->setAdminUser();
120         $contenttype = new contenttype_h5p\contenttype($systemcontext);
122         // Add an H5P fill the blanks file to the content bank.
123         $filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
124         $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
125         $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
126         $filltheblanks = array_shift($contents);
128         // Add an H5P find the words file to the content bank.
129         $filepath = $CFG->dirroot . '/h5p/tests/fixtures/find-the-words.h5p';
130         $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
131         $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
132         $findethewords = array_shift($contents);
134         // Check before deploying the icon for both contents is the same: default one.
135         // Because we don't know specific H5P content type yet.
136         $defaulticon = $contenttype->get_icon($filltheblanks);
137         $this->assertEquals($defaulticon, $contenttype->get_icon($findethewords));
138         $this->assertContains('h5p', $defaulticon);
140         // Deploy one of the contents though the player to create the H5P DB entries and know specific content type.
141         $h5pplayer = new \core_h5p\player($findethewords->get_file_url(), new \stdClass(), true);
142         $h5pplayer->add_assets_to_page();
143         $h5pplayer->output();
145         // Once the H5P has been deployed, we know the specific H5P content type, so the icon returned is not default one.
146         $findicon = $contenttype->get_icon($findethewords);
147         $this->assertNotEquals($defaulticon, $findicon);
148         $this->assertContains('find', $findicon, '', true);
149     }