MDL-67192 core_h5p: get site UUID from H5P using the API
[moodle.git] / h5p / tests / h5p_core_test.php
CommitLineData
5315acb4
VDF
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 * Testing the H5P core methods.
19 *
20 * @package core_h5p
21 * @category test
22 * @copyright 2019 Victor Deniz <victor@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
e942844d 26namespace core_h5p;
5315acb4
VDF
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Test class covering the H5PFileStorage interface implementation.
32 *
33 * @package core_h5p
34 * @copyright 2019 Victor Deniz <victor@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 *
37 * @runTestsInSeparateProcesses
38 */
a23969a5 39class h5p_core_testcase extends \advanced_testcase {
5315acb4
VDF
40
41 protected function setup() {
e942844d 42 global $CFG;
5315acb4
VDF
43 parent::setUp();
44
e942844d
VDF
45 autoloader::register();
46
47 require_once($CFG->libdir . '/tests/fixtures/testable_core_h5p.php');
48
49 $factory = new h5p_test_factory();
5315acb4 50 $this->core = $factory->get_core();
e942844d 51 $this->core->set_endpoint($this->getExternalTestFileUrl(''));
5315acb4
VDF
52 }
53
54 /**
55 * Check that given an H5P content type machine name, the required library are fetched and installed from the official H5P
56 * repository.
5315acb4
VDF
57 */
58 public function test_fetch_content_type(): void {
59 global $DB;
60
fc0ded84 61 if (!PHPUNIT_LONGTEST) {
5315acb4
VDF
62 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
63 }
64
e942844d
VDF
65 $this->resetAfterTest(true);
66
b7c307f3
VDF
67 // Get info of latest content types versions.
68 $contenttypes = $this->core->get_latest_content_types()->contentTypes;
69 // We are installing the first content type.
70 $librarydata = $contenttypes[0];
71
5315acb4 72 $library = [
b7c307f3
VDF
73 'machineName' => $librarydata->id,
74 'majorVersion' => $librarydata->version->major,
75 'minorVersion' => $librarydata->version->minor,
76 'patchVersion' => $librarydata->version->patch,
5315acb4
VDF
77 ];
78
b7c307f3
VDF
79 // Verify that the content type is not yet installed.
80 $conditions['machinename'] = $library['machineName'];
81 $typeinstalled = $DB->count_records('h5p_libraries', $conditions);
5315acb4 82
b7c307f3 83 $this->assertEquals(0, $typeinstalled);
5315acb4 84
b7c307f3 85 // Fetch the content type.
5315acb4
VDF
86 $this->core->fetch_content_type($library);
87
b7c307f3
VDF
88 // Check that the content type is now installed.
89 $typeinstalled = $DB->get_record('h5p_libraries', $conditions);
90 $this->assertEquals($librarydata->id, $typeinstalled->machinename);
91 $this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor);
92 $this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor);
5315acb4
VDF
93 }
94
95 /**
96 * Test that latest version of non installed H5P content type libraries are fetched and installed from the
97 * official H5P repository. To speed up the test, only if checked that one content type is installed.
5315acb4
VDF
98 */
99 public function test_fetch_latest_content_types(): void {
100 global $DB;
101
fc0ded84 102 if (!PHPUNIT_LONGTEST) {
5315acb4
VDF
103 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
104 }
105
e942844d
VDF
106 $this->resetAfterTest(true);
107
5315acb4
VDF
108 $contentfiles = $DB->count_records('h5p_libraries');
109
110 // Initially there are no h5p records in database.
111 $this->assertEquals(0, $contentfiles);
112
e942844d
VDF
113 $contenttypespending = ['H5P.Accordion'];
114
5315acb4
VDF
115 // Fetch generator.
116 $generator = \testing_util::get_data_generator();
117 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
118
119 // Get info of latest content types versions.
e942844d 120 [$installedtypes, $typesnotinstalled] = $h5pgenerator->create_content_types($contenttypespending, $this->core);
5315acb4 121 // Number of H5P content types.
e942844d 122 $numcontenttypes = $installedtypes + $typesnotinstalled;
5315acb4
VDF
123
124 // Content type libraries has runnable set to 1.
125 $conditions = ['runnable' => 1];
126 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
127
128 // There is a record for each installed content type, except the one that was hold for later.
129 $this->assertEquals($numcontenttypes - 1, count($contentfiles));
e942844d 130 $this->assertArrayNotHasKey($contenttypespending[0], $contentfiles);
5315acb4
VDF
131
132 $result = $this->core->fetch_latest_content_types();
133
134 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
135
136 // There is a new record for the new installed content type.
137 $this->assertCount($numcontenttypes, $contentfiles);
e942844d 138 $this->assertArrayHasKey($contenttypespending[0], $contentfiles);
5315acb4 139 $this->assertCount(1, $result->typesinstalled);
e942844d 140 $this->assertStringStartsWith($contenttypespending[0], $result->typesinstalled[0]['name']);
5315acb4
VDF
141
142 // New execution doesn't install any content type.
143 $result = $this->core->fetch_latest_content_types();
144
145 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
146
147 $this->assertEquals($numcontenttypes, count($contentfiles));
148 $this->assertCount(0, $result->typesinstalled);
149 }
a23969a5
VDF
150
151 /**
152 * Test that if site_uuid is not set, the site is registered and site_uuid is set.
153 *
154 */
155 public function test_get_site_uuid(): void {
156 $this->resetAfterTest(true);
157
158 if (!PHPUNIT_LONGTEST) {
159 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
160 }
161
162 // Check that site_uuid does not have a value.
163 $this->assertFalse(get_config('core_h5p', 'site_uuid'));
164
165 $siteuuid = $this->core->get_site_uuid();
166
167 $this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));
168
169 // Check that after a new request the site_uuid remains the same.
170 $siteuuid2 = $this->core->get_site_uuid();
171 $this->assertEquals( $siteuuid, $siteuuid2);
172 }
5315acb4 173}