forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5p_core_test.php
216 lines (172 loc) · 7.94 KB
/
h5p_core_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Testing the H5P core methods.
*
* @package core_h5p
* @category test
* @copyright 2019 Victor Deniz <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p;
use core_h5p\local\library\autoloader;
defined('MOODLE_INTERNAL') || die();
/**
* Test class covering the H5PFileStorage interface implementation.
*
* @package core_h5p
* @copyright 2019 Victor Deniz <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @runTestsInSeparateProcesses
*/
class h5p_core_test extends \advanced_testcase {
/** @var core */
protected $core;
protected function setUp(): void {
global $CFG;
parent::setUp();
autoloader::register();
require_once($CFG->libdir . '/tests/fixtures/testable_core_h5p.php');
$factory = new h5p_test_factory();
$this->core = $factory->get_core();
$this->core->set_endpoint($this->getExternalTestFileUrl(''));
}
/**
* Check that given an H5P content type machine name, the required library are fetched and installed from the official H5P
* repository.
*/
public function test_fetch_content_type(): void {
global $DB;
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest(true);
// Get info of latest content types versions.
$contenttypes = $this->core->get_latest_content_types()->contentTypes;
// We are installing the first content type with tutorial and example fields (or the first one if none has them).
$librarydata = $contenttypes[0];
foreach ($contenttypes as $contentype) {
if (isset($contenttype->tutorial) && isset($contenttype->example)) {
$librarydata = $contenttype;
break;
}
}
$library = [
'machineName' => $librarydata->id,
'majorVersion' => $librarydata->version->major,
'minorVersion' => $librarydata->version->minor,
'patchVersion' => $librarydata->version->patch,
];
// Add example and tutorial to the library.
if (isset($librarydata->example)) {
$library['example'] = $librarydata->example;
}
if (isset($librarydata->tutorial)) {
$library['tutorial'] = $librarydata->tutorial;
}
// Verify that the content type is not yet installed.
$conditions['machinename'] = $library['machineName'];
$typeinstalled = $DB->count_records('h5p_libraries', $conditions);
$this->assertEquals(0, $typeinstalled);
// Fetch the content type.
$this->core->fetch_content_type($library);
// Check that the content type is now installed.
$typeinstalled = $DB->get_record('h5p_libraries', $conditions);
$this->assertEquals($librarydata->id, $typeinstalled->machinename);
$this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor);
$this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor);
if (isset($librarydata->tutorial)) {
$this->assertEquals($librarydata->tutorial, $typeinstalled->tutorial);
$this->assertEquals($librarydata->example, $typeinstalled->example);
}
}
/**
* Test that latest version of non installed H5P content type libraries are fetched and installed from the
* official H5P repository. To speed up the test, only if checked that one content type is installed.
*/
public function test_fetch_latest_content_types(): void {
global $DB;
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest(true);
$contentfiles = $DB->count_records('h5p_libraries');
// Initially there are no h5p records in database.
$this->assertEquals(0, $contentfiles);
$contenttypespending = ['H5P.Accordion'];
// Fetch generator.
$generator = \testing_util::get_data_generator();
$h5pgenerator = $generator->get_plugin_generator('core_h5p');
// Get info of latest content types versions.
[$installedtypes, $typesnotinstalled] = $h5pgenerator->create_content_types($contenttypespending, $this->core);
// Number of H5P content types.
$numcontenttypes = $installedtypes + $typesnotinstalled;
// Content type libraries has runnable set to 1.
$conditions = ['runnable' => 1];
$contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
// There is a record for each installed content type, except the one that was hold for later.
$this->assertEquals($numcontenttypes - 1, count($contentfiles));
$this->assertArrayNotHasKey($contenttypespending[0], $contentfiles);
$result = $this->core->fetch_latest_content_types();
$contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
// There is a new record for the new installed content type.
$this->assertCount($numcontenttypes, $contentfiles);
$this->assertArrayHasKey($contenttypespending[0], $contentfiles);
$this->assertCount(1, $result->typesinstalled);
$this->assertStringStartsWith($contenttypespending[0], $result->typesinstalled[0]['name']);
// New execution doesn't install any content type.
$result = $this->core->fetch_latest_content_types();
$contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
$this->assertEquals($numcontenttypes, count($contentfiles));
$this->assertCount(0, $result->typesinstalled);
}
/**
* Test that if site_uuid is not set, the site is registered and site_uuid is set.
*
*/
public function test_get_site_uuid(): void {
$this->resetAfterTest(true);
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
// Check that site_uuid does not have a value.
$this->assertFalse(get_config('core_h5p', 'site_uuid'));
$siteuuid = $this->core->get_site_uuid();
$this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));
// Check that after a new request the site_uuid remains the same.
$siteuuid2 = $this->core->get_site_uuid();
$this->assertEquals( $siteuuid, $siteuuid2);
}
/**
* Test if no handler has been defined.
*/
public function test_get_default_handler() {
global $CFG;
$this->resetAfterTest(true);
// Emtpy the h5plibraryhandler setting.
$CFG->h5plibraryhandler = '';
// Get the default habdler library to use in the settings h5p page.
// For instance, h5plib_v124.
$handlerlib = autoloader::get_default_handler_library();
$this->assertNotNull($handlerlib);
$this->assertStringNotContainsString($handlerlib, '\local\library\handler');
// Get the default handler class.
// For instance, \h5plib_v124\local\library\handler.
$handlerclass = autoloader::get_default_handler();
$this->assertStringEndsWith('\local\library\handler', $handlerclass);
}
}