Merge branch 'MDL-67062-master' of git://github.com/sarjona/moodle
[moodle.git] / h5p / tests / api_test.php
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/>.
17 /**
18  * Testing the H5P API.
19  *
20  * @package    core_h5p
21  * @category   test
22  * @copyright  2020 Sara Arjona <sara@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 declare(strict_types = 1);
28 namespace core_h5p;
30 defined('MOODLE_INTERNAL') || die();
32 /**
33  * Test class covering the H5P API.
34  *
35  * @package    core_h5p
36  * @copyright  2020 Sara Arjona <sara@moodle.com>
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class api_testcase extends \advanced_testcase {
41     /**
42      * Test the behaviour of delete_library().
43      *
44      * @dataProvider  delete_library_provider
45      * @param  string $libraryname          Machine name of the library to delete.
46      * @param  int    $expectedh5p          Total of H5P contents expected after deleting the library.
47      * @param  int    $expectedlibraries    Total of H5P libraries expected after deleting the library.
48      * @param  int    $expectedcontents     Total of H5P content_libraries expected after deleting the library.
49      * @param  int    $expecteddependencies Total of H5P library dependencies expected after deleting the library.
50      */
51     public function test_delete_library(string $libraryname, int $expectedh5p, int $expectedlibraries,
52             int $expectedcontents, int $expecteddependencies): void {
53         global $DB;
55         $this->setRunTestInSeparateProcess(true);
56         $this->resetAfterTest();
58         // Generate h5p related data.
59         $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
60         $generator->generate_h5p_data();
61         $generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
63         // Check the current content in H5P tables is the expected.
64         $counth5p = $DB->count_records('h5p');
65         $counth5plibraries = $DB->count_records('h5p_libraries');
66         $counth5pcontents = $DB->count_records('h5p_contents_libraries');
67         $counth5pdependencies = $DB->count_records('h5p_library_dependencies');
69         $this->assertSame(1, $counth5p);
70         $this->assertSame(7, $counth5plibraries);
71         $this->assertSame(5, $counth5pcontents);
72         $this->assertSame(7, $counth5pdependencies);
74         // Delete this library.
75         $factory = new factory();
76         $library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname]);
77         if ($library) {
78             api::delete_library($factory, $library);
79         }
81         // Check the expected libraries and content have been removed.
82         $counth5p = $DB->count_records('h5p');
83         $counth5plibraries = $DB->count_records('h5p_libraries');
84         $counth5pcontents = $DB->count_records('h5p_contents_libraries');
85         $counth5pdependencies = $DB->count_records('h5p_library_dependencies');
87         $this->assertSame($expectedh5p, $counth5p);
88         $this->assertSame($expectedlibraries, $counth5plibraries);
89         $this->assertSame($expectedcontents, $counth5pcontents);
90         $this->assertSame($expecteddependencies, $counth5pdependencies);
91     }
93     /**
94      * Data provider for test_delete_library().
95      *
96      * @return array
97      */
98     public function delete_library_provider(): array {
99         return [
100             'Delete MainLibrary' => [
101                 'MainLibrary',
102                 0,
103                 6,
104                 0,
105                 4,
106             ],
107             'Delete Library1' => [
108                 'Library1',
109                 0,
110                 5,
111                 0,
112                 1,
113             ],
114             'Delete Library2' => [
115                 'Library2',
116                 0,
117                 4,
118                 0,
119                 1,
120             ],
121             'Delete Library3' => [
122                 'Library3',
123                 0,
124                 4,
125                 0,
126                 0,
127             ],
128             'Delete Library4' => [
129                 'Library4',
130                 0,
131                 4,
132                 0,
133                 1,
134             ],
135             'Delete Library5' => [
136                 'Library5',
137                 0,
138                 3,
139                 0,
140                 0,
141             ],
142             'Delete a library without dependencies' => [
143                 'H5P.TestingLibrary',
144                 1,
145                 6,
146                 5,
147                 7,
148             ],
149             'Delete unexisting library' => [
150                 'LibraryX',
151                 1,
152                 7,
153                 5,
154                 7,
155             ],
156         ];
157     }
159     /**
160      * Test the behaviour of get_dependent_libraries().
161      *
162      * @dataProvider  get_dependent_libraries_provider
163      * @param  string $libraryname     Machine name of the library to delete.
164      * @param  int    $expectedvalue   Total of H5P required libraries expected.
165      */
166     public function test_get_dependent_libraries(string $libraryname, int $expectedvalue): void {
167         global $DB;
169         $this->resetAfterTest();
171         // Generate h5p related data.
172         $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
173         $generator->generate_h5p_data();
174         $generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
176         // Get required libraries.
177         $library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname], 'id');
178         if ($library) {
179             $libraries = api::get_dependent_libraries((int)$library->id);
180         } else {
181             $libraries = [];
182         }
184         $this->assertCount($expectedvalue, $libraries);
185     }
187     /**
188      * Data provider for test_get_dependent_libraries().
189      *
190      * @return array
191      */
192     public function get_dependent_libraries_provider(): array {
193         return [
194             'Main library of a content' => [
195                 'MainLibrary',
196                 0,
197             ],
198             'Library1' => [
199                 'Library1',
200                 1,
201             ],
202             'Library2' => [
203                 'Library2',
204                 2,
205             ],
206             'Library without dependencies' => [
207                 'H5P.TestingLibrary',
208                 0,
209             ],
210             'Unexisting library' => [
211                 'LibraryX',
212                 0,
213             ],
214         ];
215     }
217     /**
218      * Test the behaviour of get_library().
219      *
220      * @dataProvider  get_library_provider
221      * @param  string $libraryname     Machine name of the library to delete.
222      * @param  bool   $emptyexpected   Wether the expected result is empty or not.
223      */
224     public function test_get_library(string $libraryname, bool $emptyexpected): void {
225         global $DB;
227         $this->resetAfterTest();
229         // Generate h5p related data.
230         $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
231         $generator->generate_h5p_data();
232         $generator->create_library_record('H5P.TestingLibrary', 'TestingLibrary', 1, 0);
234         // Get the library identifier.
235         $library = $DB->get_record('h5p_libraries', ['machinename' => $libraryname], 'id');
236         if ($library) {
237             $result = api::get_library((int)$library->id);
238         } else {
239             $result = null;
240         }
242         if ($emptyexpected) {
243             $this->assertEmpty($result);
244         } else {
245             $this->assertEquals($library->id, $result->id);
246             $this->assertEquals($libraryname, $result->machinename);
247         }
249     }
251     /**
252      * Data provider for test_get_library().
253      *
254      * @return array
255      */
256     public function get_library_provider(): array {
257         return [
258             'Main library of a content' => [
259                 'MainLibrary',
260                 false,
261             ],
262             'Library1' => [
263                 'Library1',
264                 false,
265             ],
266             'Library without dependencies' => [
267                 'H5P.TestingLibrary',
268                 false,
269             ],
270             'Unexisting library' => [
271                 'LibraryX',
272                 true,
273             ],
274         ];
275     }