MDL-67795 contentbank: delete content API
[moodle.git] / contentbank / contenttype / h5p / tests / contenttype_h5p_test.php
CommitLineData
bd4e0a76
AA
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 * 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 */
25
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 */
35class contenttype_h5p_contenttype_plugin_testcase extends advanced_testcase {
36
c0d615e8
SA
37 /**
38 * Test the behaviour of delete_content().
39 */
40 public function test_delete_content() {
41 global $CFG, $USER, $DB;
42
43 $this->resetAfterTest();
44 $systemcontext = context_system::instance();
45
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);
51
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);
58
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();
66
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'));
70
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 }
77
bd4e0a76
AA
78 /**
79 * Tests can_upload behavior.
80 *
81 * @covers ::can_upload
82 */
83 public function test_can_upload() {
84 $this->resetAfterTest();
85
86 $systemcontext = \context_system::instance();
87 $systemtype = new \contenttype_h5p\contenttype($systemcontext);
88
89 // Admins can upload.
90 $this->setAdminUser();
91 $this->assertTrue($systemtype->can_upload());
92
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());
101
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 }
108}