MDL-67058 h5p: Content fetch should use save_h5p 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
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.
53 *
54 * This test require access to an external URL (H5P libraries repository), so can take long time to execute.
55 * PHPUNIT_LONGTEST constant should be set in phpunit.xml or directly in config.php.
56 * define('PHPUNIT_LONGTEST', true);
57 *
58 * return void
59 */
60 public function test_fetch_content_type(): void {
61 global $DB;
62
63 $this->resetAfterTest(true);
64
65 if (!defined('PHPUNIT_LONGTEST')) {
66 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
67 }
68
69 $library = [
70 'machineName' => 'H5P.Accordion',
71 'majorVersion' => 1,
72 'minorVersion' => 0,
73 'patchVersion' => 0,
74 ];
75
76 $sql = 'SELECT count(id)
77 FROM {files}
78 WHERE ' . $DB->sql_like('filepath', ':filepath');
79 $params['filepath'] = "/{$library['machineName']}-%";
80
81 $contentfiles = $DB->count_records_sql($sql, $params);
82
83 $this->assertEquals(0, $contentfiles);
84
85 $this->core->fetch_content_type($library);
86
87 $contentfiles = $DB->count_records_sql($sql, $params);
88 $this->assertGreaterThan(0, $contentfiles);
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.
94 *
95 * This test require access to an external URL (H5P libraries repository), so can take long time to execute.
96 * PHPUNIT_LONGTEST constant should be set in phpunit.xml or directly in config.php.
97 * define('PHPUNIT_LONGTEST', true);
98 *
99 * return void
100 */
101 public function test_fetch_latest_content_types(): void {
102 global $DB;
103
104 $this->resetAfterTest(true);
105
106 if (!defined('PHPUNIT_LONGTEST')) {
107 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
108 }
109
110 $contentfiles = $DB->count_records('h5p_libraries');
111
112 // Initially there are no h5p records in database.
113 $this->assertEquals(0, $contentfiles);
114
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.
120 [$contenttypes, $contenttoinstall] = $h5pgenerator->create_content_types(1);
121 // Number of H5P content types.
122 $numcontenttypes = count($contenttypes) + count($contenttoinstall);
123
124 $contenttoinstall = $contenttoinstall[0];
125
126 // Content type libraries has runnable set to 1.
127 $conditions = ['runnable' => 1];
128 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
129
130 // There is a record for each installed content type, except the one that was hold for later.
131 $this->assertEquals($numcontenttypes - 1, count($contentfiles));
132 $this->assertArrayNotHasKey($contenttoinstall->id, $contentfiles);
133
134 $result = $this->core->fetch_latest_content_types();
135
136 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
137
138 // There is a new record for the new installed content type.
139 $this->assertCount($numcontenttypes, $contentfiles);
140 $this->assertArrayHasKey($contenttoinstall->id, $contentfiles);
141 $this->assertCount(1, $result->typesinstalled);
142 $this->assertStringStartsWith($contenttoinstall->id, $result->typesinstalled[0]['name']);
143
144 // New execution doesn't install any content type.
145 $result = $this->core->fetch_latest_content_types();
146
147 $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
148
149 $this->assertEquals($numcontenttypes, count($contentfiles));
150 $this->assertCount(0, $result->typesinstalled);
151 }
152}