MDL-67296 core_h5p: minor changes
[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
26namespace core_h5p\local\tests;
27
28use core_h5p\factory;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Test class covering the H5PFileStorage interface implementation.
34 *
35 * @package core_h5p
36 * @copyright 2019 Victor Deniz <victor@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 *
39 * @runTestsInSeparateProcesses
40 */
41class h5p_core_test extends \advanced_testcase {
42
43 protected function setup() {
44 parent::setUp();
45
46 $factory = new factory();
47 $this->core = $factory->get_core();
48 }
49
50 /**
51 * Check that given an H5P content type machine name, the required library are fetched and installed from the official H5P
52 * repository.
5315acb4
VDF
53 */
54 public function test_fetch_content_type(): void {
55 global $DB;
56
57 $this->resetAfterTest(true);
58
fc0ded84 59 if (!PHPUNIT_LONGTEST) {
5315acb4
VDF
60 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
61 }
62
b7c307f3
VDF
63 // Get info of latest content types versions.
64 $contenttypes = $this->core->get_latest_content_types()->contentTypes;
65 // We are installing the first content type.
66 $librarydata = $contenttypes[0];
67
5315acb4 68 $library = [
b7c307f3
VDF
69 'machineName' => $librarydata->id,
70 'majorVersion' => $librarydata->version->major,
71 'minorVersion' => $librarydata->version->minor,
72 'patchVersion' => $librarydata->version->patch,
5315acb4
VDF
73 ];
74
b7c307f3
VDF
75 // Verify that the content type is not yet installed.
76 $conditions['machinename'] = $library['machineName'];
77 $typeinstalled = $DB->count_records('h5p_libraries', $conditions);
5315acb4 78
b7c307f3 79 $this->assertEquals(0, $typeinstalled);
5315acb4 80
b7c307f3 81 // Fetch the content type.
5315acb4
VDF
82 $this->core->fetch_content_type($library);
83
b7c307f3
VDF
84 // Check that the content type is now installed.
85 $typeinstalled = $DB->get_record('h5p_libraries', $conditions);
86 $this->assertEquals($librarydata->id, $typeinstalled->machinename);
87 $this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor);
88 $this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor);
5315acb4
VDF
89 }
90
91 /**
92 * Test that latest version of non installed H5P content type libraries are fetched and installed from the
93 * official H5P repository. To speed up the test, only if checked that one content type is installed.
5315acb4
VDF
94 */
95 public function test_fetch_latest_content_types(): void {
96 global $DB;
97
98 $this->resetAfterTest(true);
99
fc0ded84 100 if (!PHPUNIT_LONGTEST) {
5315acb4
VDF
101 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
102 }
103
104 $contentfiles = $DB->count_records('h5p_libraries');
105
106 // Initially there are no h5p records in database.
107 $this->assertEquals(0, $contentfiles);
108
109 // Fetch generator.
110 $generator = \testing_util::get_data_generator();
111 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
112
113 // Get info of latest content types versions.
114 [$contenttypes, $contenttoinstall] = $h5pgenerator->create_content_types(1);
115 // Number of H5P content types.
116 $numcontenttypes = count($contenttypes) + count($contenttoinstall);
117
118 $contenttoinstall = $contenttoinstall[0];
119
120 // Content type libraries has runnable set to 1.
121 $conditions = ['runnable' => 1];
122 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
123
124 // There is a record for each installed content type, except the one that was hold for later.
125 $this->assertEquals($numcontenttypes - 1, count($contentfiles));
126 $this->assertArrayNotHasKey($contenttoinstall->id, $contentfiles);
127
128 $result = $this->core->fetch_latest_content_types();
129
130 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
131
132 // There is a new record for the new installed content type.
133 $this->assertCount($numcontenttypes, $contentfiles);
134 $this->assertArrayHasKey($contenttoinstall->id, $contentfiles);
135 $this->assertCount(1, $result->typesinstalled);
136 $this->assertStringStartsWith($contenttoinstall->id, $result->typesinstalled[0]['name']);
137
138 // New execution doesn't install any content type.
139 $result = $this->core->fetch_latest_content_types();
140
141 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
142
143 $this->assertEquals($numcontenttypes, count($contentfiles));
144 $this->assertCount(0, $result->typesinstalled);
145 }
146}