1441 |
ariadna |
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 |
namespace core\route\api;
|
|
|
18 |
|
|
|
19 |
use core\tests\router\route_testcase;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for Templates API.
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core\route\api\templates
|
|
|
29 |
*/
|
|
|
30 |
final class templates_test extends route_testcase {
|
|
|
31 |
/**
|
|
|
32 |
* Test fetching templates.
|
|
|
33 |
*
|
|
|
34 |
* Note: This is a risky test because it relies on data in other parts of Moodle.
|
|
|
35 |
*
|
|
|
36 |
* @dataProvider fetch_templates_provider
|
|
|
37 |
* @param string $path
|
|
|
38 |
* @param array $requiredtemplates
|
|
|
39 |
* @param array $requiredstrings
|
|
|
40 |
*/
|
|
|
41 |
public function test_fetch_known_templates(
|
|
|
42 |
string $path,
|
|
|
43 |
array $requiredtemplates,
|
|
|
44 |
array $requiredstrings,
|
|
|
45 |
): void {
|
|
|
46 |
$this->add_class_routes_to_route_loader(\core\route\api\templates::class);
|
|
|
47 |
$response = $this->process_api_request('GET', "/templates/{$path}");
|
|
|
48 |
|
|
|
49 |
$this->assert_valid_response($response);
|
|
|
50 |
$payload = $this->decode_response($response, true);
|
|
|
51 |
|
|
|
52 |
$this->assert_payload_contains($payload, $requiredtemplates, $requiredstrings);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Data propvider for template tests.
|
|
|
57 |
*
|
|
|
58 |
* @return array
|
|
|
59 |
*/
|
|
|
60 |
public static function fetch_templates_provider(): array {
|
|
|
61 |
return [
|
|
|
62 |
'fetch single template' => [
|
|
|
63 |
'boost/core/modal',
|
|
|
64 |
['core/modal'],
|
|
|
65 |
[],
|
|
|
66 |
],
|
|
|
67 |
'fetch nested template' => [
|
|
|
68 |
'boost/core/local/modal/alert',
|
|
|
69 |
[
|
|
|
70 |
'core/local/modal/alert',
|
|
|
71 |
'core/modal',
|
|
|
72 |
],
|
|
|
73 |
[
|
|
|
74 |
'moodle/ok',
|
|
|
75 |
],
|
|
|
76 |
],
|
|
|
77 |
'multiple templates' => [
|
|
|
78 |
'boost/core/notification',
|
|
|
79 |
[
|
|
|
80 |
'core/notification',
|
|
|
81 |
'core/notification_success',
|
|
|
82 |
'core/notification_warning',
|
|
|
83 |
'core/notification_error',
|
|
|
84 |
'core/notification_info',
|
|
|
85 |
],
|
|
|
86 |
[
|
|
|
87 |
'core/dismissnotification',
|
|
|
88 |
],
|
|
|
89 |
],
|
|
|
90 |
];
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Assertthat the payload contains the required templates and strings.
|
|
|
95 |
*
|
|
|
96 |
* @param array $payload
|
|
|
97 |
* @param array $requiredtemplates
|
|
|
98 |
* @param array $requiredstrings
|
|
|
99 |
*/
|
|
|
100 |
protected function assert_payload_contains(
|
|
|
101 |
array $payload,
|
|
|
102 |
array $requiredtemplates = [],
|
|
|
103 |
array $requiredstrings = [],
|
|
|
104 |
): void {
|
|
|
105 |
$this->assertArrayHasKey('templates', $payload);
|
|
|
106 |
$this->assertArrayHasKey('strings', $payload);
|
|
|
107 |
|
|
|
108 |
foreach ($requiredtemplates as $template) {
|
|
|
109 |
$this->assertArrayHasKey($template, $payload['templates']);
|
|
|
110 |
}
|
|
|
111 |
foreach ($requiredstrings as $string) {
|
|
|
112 |
$this->assertArrayHasKey($string, $payload['strings']);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public function test_template_missing(): void {
|
|
|
117 |
$this->add_class_routes_to_route_loader(\core\route\api\templates::class);
|
|
|
118 |
$response = $this->process_api_request('GET', '/templates/boost/core/missing');
|
|
|
119 |
|
|
|
120 |
$this->assert_not_found_response($response);
|
|
|
121 |
}
|
|
|
122 |
}
|