weekly release 3.9dev
[moodle.git] / contentbank / tests / contentbank_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 extensions manager.
19 *
20 * @package core_contentbank
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
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
30
31/**
32 * Test for extensions manager.
33 *
34 * @package core_contentbank
35 * @category test
36 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @coversDefaultClass \core_contentbank\contentbank
39 */
40class core_contentbank_testcase extends advanced_testcase {
41 /**
42 * Data provider for test_get_extension_supporter.
43 *
44 * @return array
45 */
46 public function get_extension_provider() {
47 return [
48 'H5P file' => ['something.h5p', '.h5p'],
49 'PDF file' => ['something.pdf', '.pdf']
50 ];
51 }
52
53 /**
54 * Tests for get_extension() function.
55 *
56 * @dataProvider get_extension_provider
57 * @param string $filename The filename given
58 * @param string $expected The extension of the file
59 *
60 * @covers ::get_extension
61 */
62 public function test_get_extension(string $filename, string $expected) {
63 $this->resetAfterTest();
64
65 $cb = new \core_contentbank\contentbank();
66
67 $extension = $cb->get_extension($filename);
68 $this->assertEquals($expected, $extension);
69 }
70
71 /**
72 * Data provider for test_load_context_supported_extensions.
73 *
74 * @return array
75 */
76 public function get_extension_supporters_provider() {
77 return [
78 'H5P first' => [['.h5p' => ['h5p', 'testable']], '.h5p', 'h5p'],
79 'Testable first (but upload not implemented)' => [['.h5p' => ['testable', 'h5p']], '.h5p', 'h5p'],
80 ];
81 }
82
83 /**
84 * Tests for get_extension_supporter() function with admin permissions.
85 *
86 * @dataProvider get_extension_supporters_provider
87 * @param array $supporters The content type plugin supporters for each extension
88 * @param string $extension The extension of the file given
89 * @param string $expected The supporter contenttype of the file
90 *
91 * @covers ::load_context_supported_extensions
92 */
93 public function test_get_extension_supporter_for_admins(array $supporters, string $extension, string $expected) {
94 $this->resetAfterTest();
95
96 $cb = new \core_contentbank\contentbank();
97 $expectedsupporters = [$extension => $expected];
98
99 $systemcontext = context_system::instance();
100
101 // All contexts allowed for admins.
102 $this->setAdminUser();
103 $contextsupporters = $cb->load_context_supported_extensions($systemcontext);
104 $this->assertEquals($expectedsupporters, $contextsupporters);
105 }
106
107 /**
108 * Tests for get_extension_supporter() function with user default permissions.
109 *
110 * @dataProvider get_extension_supporters_provider
111 * @param array $supporters The content type plugin supporters for each extension
112 * @param string $extension The extension of the file given
113 * @param string $expected The supporter contenttype of the file
114 *
115 * @covers ::load_context_supported_extensions
116 */
117 public function test_get_extension_supporter_for_users(array $supporters, string $extension, string $expected) {
118 $this->resetAfterTest();
119
120 $cb = new \core_contentbank\contentbank();
121 $systemcontext = context_system::instance();
122
123 // Set a user with no permissions.
124 $user = $this->getDataGenerator()->create_user();
125 $this->setUser($user);
126
127 // Users with no capabilities can't upload content.
128 $contextsupporters = $cb->load_context_supported_extensions($systemcontext);
129 $this->assertEquals([], $contextsupporters);
130 }
131
132 /**
133 * Tests for get_extension_supporter() function with teacher defaul permissions.
134 *
135 * @dataProvider get_extension_supporters_provider
136 * @param array $supporters The content type plugin supporters for each extension
137 * @param string $extension The extension of the file given
138 * @param string $expected The supporter contenttype of the file
139 *
140 * @covers ::load_context_supported_extensions
141 */
142 public function test_get_extension_supporter_for_teachers(array $supporters, string $extension, string $expected) {
143 $this->resetAfterTest();
144
145 $cb = new \core_contentbank\contentbank();
146 $expectedsupporters = [$extension => $expected];
147
148 $course = $this->getDataGenerator()->create_course();
149 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
150 $this->setUser($teacher);
151 $coursecontext = context_course::instance($course->id);
152
153 // Teachers has permission in their context to upload supported by H5P content type.
154 $contextsupporters = $cb->load_context_supported_extensions($coursecontext);
155 $this->assertEquals($expectedsupporters, $contextsupporters);
156 }
157
158 /**
159 * Tests for get_extension_supporter() function.
160 *
161 * @dataProvider get_extension_supporters_provider
162 * @param array $supporters The content type plugin supporters for each extension
163 * @param string $extension The extension of the file given
164 * @param string $expected The supporter contenttype of the file
165 *
166 * @covers ::get_extension_supporter
167 */
168 public function test_get_extension_supporter(array $supporters, string $extension, string $expected) {
169 $this->resetAfterTest();
170
171 $cb = new \core_contentbank\contentbank();
172 $systemcontext = context_system::instance();
173 $this->setAdminUser();
174
175 $supporter = $cb->get_extension_supporter($extension, $systemcontext);
176 $this->assertEquals($expected, $supporter);
177 }
178}