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