weekly release 3.8dev+
[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
63 $library = [
64 'machineName' => 'H5P.Accordion',
65 'majorVersion' => 1,
66 'minorVersion' => 0,
67 'patchVersion' => 0,
68 ];
69
70 $sql = 'SELECT count(id)
71 FROM {files}
72 WHERE ' . $DB->sql_like('filepath', ':filepath');
73 $params['filepath'] = "/{$library['machineName']}-%";
74
75 $contentfiles = $DB->count_records_sql($sql, $params);
76
77 $this->assertEquals(0, $contentfiles);
78
79 $this->core->fetch_content_type($library);
80
81 $contentfiles = $DB->count_records_sql($sql, $params);
82 $this->assertGreaterThan(0, $contentfiles);
83 }
84
85 /**
86 * Test that latest version of non installed H5P content type libraries are fetched and installed from the
87 * official H5P repository. To speed up the test, only if checked that one content type is installed.
5315acb4
VDF
88 */
89 public function test_fetch_latest_content_types(): void {
90 global $DB;
91
92 $this->resetAfterTest(true);
93
fc0ded84 94 if (!PHPUNIT_LONGTEST) {
5315acb4
VDF
95 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
96 }
97
98 $contentfiles = $DB->count_records('h5p_libraries');
99
100 // Initially there are no h5p records in database.
101 $this->assertEquals(0, $contentfiles);
102
103 // Fetch generator.
104 $generator = \testing_util::get_data_generator();
105 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
106
107 // Get info of latest content types versions.
108 [$contenttypes, $contenttoinstall] = $h5pgenerator->create_content_types(1);
109 // Number of H5P content types.
110 $numcontenttypes = count($contenttypes) + count($contenttoinstall);
111
112 $contenttoinstall = $contenttoinstall[0];
113
114 // Content type libraries has runnable set to 1.
115 $conditions = ['runnable' => 1];
116 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
117
118 // There is a record for each installed content type, except the one that was hold for later.
119 $this->assertEquals($numcontenttypes - 1, count($contentfiles));
120 $this->assertArrayNotHasKey($contenttoinstall->id, $contentfiles);
121
122 $result = $this->core->fetch_latest_content_types();
123
124 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
125
126 // There is a new record for the new installed content type.
127 $this->assertCount($numcontenttypes, $contentfiles);
128 $this->assertArrayHasKey($contenttoinstall->id, $contentfiles);
129 $this->assertCount(1, $result->typesinstalled);
130 $this->assertStringStartsWith($contenttoinstall->id, $result->typesinstalled[0]['name']);
131
132 // New execution doesn't install any content type.
133 $result = $this->core->fetch_latest_content_types();
134
135 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
136
137 $this->assertEquals($numcontenttypes, count($contentfiles));
138 $this->assertCount(0, $result->typesinstalled);
139 }
140}