| 1 |
efrain |
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\output;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Unit tests for activity header
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @coversDefaultClass \core\output\activity_header
|
|
|
25 |
* @copyright 2021 Peter
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
| 1441 |
ariadna |
28 |
final class activity_header_test extends \advanced_testcase {
|
| 1 |
efrain |
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Test the title setter
|
|
|
32 |
*
|
|
|
33 |
* @dataProvider set_title_provider
|
|
|
34 |
* @param string $value
|
|
|
35 |
* @param string $expected
|
|
|
36 |
* @covers ::set_title
|
|
|
37 |
*/
|
|
|
38 |
public function test_set_title(string $value, string $expected): void {
|
|
|
39 |
global $PAGE, $DB;
|
|
|
40 |
$this->resetAfterTest();
|
|
|
41 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
|
|
|
42 |
$assign = $this->getDataGenerator()->create_module('assign', [
|
|
|
43 |
'course' => $course->id,
|
|
|
44 |
'completion' => COMPLETION_TRACKING_AUTOMATIC,
|
|
|
45 |
'completionview' => 1
|
|
|
46 |
]);
|
|
|
47 |
$this->setAdminUser();
|
|
|
48 |
|
|
|
49 |
$cm = $DB->get_record('course_modules', ['id' => $assign->cmid]);
|
|
|
50 |
$PAGE->set_cm($cm);
|
|
|
51 |
$PAGE->set_activity_record($assign);
|
|
|
52 |
|
|
|
53 |
$header = $PAGE->activityheader;
|
|
|
54 |
$header->set_title($value);
|
|
|
55 |
$data = $header->export_for_template($PAGE->get_renderer('core'));
|
|
|
56 |
$this->assertEquals($expected, $data['title']);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Provider for the test_set_title unit test.
|
|
|
61 |
* @return array
|
|
|
62 |
*/
|
| 1441 |
ariadna |
63 |
public static function set_title_provider(): array {
|
| 1 |
efrain |
64 |
return [
|
|
|
65 |
"Set the title with a plain text" => [
|
|
|
66 |
"Activity title", "Activity title"
|
|
|
67 |
],
|
|
|
68 |
"Set the title with a string with standard header tags" => [
|
|
|
69 |
"<h2>Activity title</h2>", "Activity title"
|
|
|
70 |
],
|
|
|
71 |
"Set the title with a string with multiple header content" => [
|
|
|
72 |
"<h2 id='heading'>Activity title</h2><h2>Header 2</h2>", "Activity title</h2><h2>Header 2"
|
|
|
73 |
],
|
|
|
74 |
];
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Test setting multiple attributes
|
|
|
79 |
*
|
|
|
80 |
* @covers ::set_attrs
|
|
|
81 |
*/
|
|
|
82 |
public function test_set_attrs(): void {
|
|
|
83 |
global $DB, $PAGE;
|
|
|
84 |
|
|
|
85 |
$this->resetAfterTest();
|
|
|
86 |
|
|
|
87 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
|
|
|
88 |
$assign = $this->getDataGenerator()->create_module('assign', [
|
|
|
89 |
'course' => $course->id,
|
|
|
90 |
'completion' => COMPLETION_TRACKING_AUTOMATIC,
|
|
|
91 |
'completionview' => 1
|
|
|
92 |
]);
|
|
|
93 |
|
|
|
94 |
$cm = $DB->get_record('course_modules', ['id' => $assign->cmid]);
|
|
|
95 |
$PAGE->set_cm($cm);
|
|
|
96 |
$PAGE->set_activity_record($assign);
|
|
|
97 |
|
|
|
98 |
$PAGE->activityheader->set_attrs([
|
|
|
99 |
'hidecompletion' => true,
|
|
|
100 |
'additionalnavitems' => new \url_select([]),
|
|
|
101 |
'hideoverflow' => true,
|
|
|
102 |
'title' => 'My title',
|
|
|
103 |
'description' => 'My description',
|
|
|
104 |
]);
|
|
|
105 |
|
|
|
106 |
$renderer = $PAGE->get_renderer('core');
|
|
|
107 |
$export = $PAGE->activityheader->export_for_template($renderer);
|
|
|
108 |
|
|
|
109 |
$this->assertEquals('My title', $export['title']);
|
|
|
110 |
$this->assertEquals('My description', $export['description']);
|
|
|
111 |
$this->assertEmpty($export['completion']); // Because hidecompletion = true.
|
|
|
112 |
$this->assertEmpty($export['additional_items']); // Because hideoverflow = true.
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Test calling set_attrs with an invalid variable name
|
|
|
117 |
*
|
|
|
118 |
* @covers ::set_attrs
|
|
|
119 |
*/
|
|
|
120 |
public function test_set_attrs_invalid_variable(): void {
|
|
|
121 |
global $PAGE;
|
|
|
122 |
|
|
|
123 |
$PAGE->activityheader->set_attrs(['unknown' => true]);
|
|
|
124 |
$this->assertDebuggingCalledCount(1, ['Invalid class member variable: unknown']);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Data provider for {@see test_get_heading_level()}.
|
|
|
129 |
*
|
|
|
130 |
* @return array[]
|
|
|
131 |
*/
|
| 1441 |
ariadna |
132 |
public static function get_heading_level_provider(): array {
|
| 1 |
efrain |
133 |
return [
|
|
|
134 |
'Title not allowed' => [false, '', 2],
|
|
|
135 |
'Title allowed, no title' => [true, '', 2],
|
|
|
136 |
'Title allowed, empty string title' => [true, ' ', 2],
|
|
|
137 |
'Title allowed, non-empty string title' => [true, 'Cool', 3],
|
|
|
138 |
];
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Test the heading level getter
|
|
|
143 |
*
|
|
|
144 |
* @dataProvider get_heading_level_provider
|
|
|
145 |
* @covers ::get_heading_level
|
|
|
146 |
* @param bool $allowtitle Whether the title is allowed.
|
|
|
147 |
* @param string $title The activity heading.
|
|
|
148 |
* @param int $expectedheadinglevel The expected heading level.
|
|
|
149 |
*/
|
|
|
150 |
public function test_get_heading_level(bool $allowtitle, string $title, int $expectedheadinglevel): void {
|
|
|
151 |
$activityheaderstub = $this->getMockBuilder(activity_header::class)
|
|
|
152 |
->disableOriginalConstructor()
|
|
|
153 |
->onlyMethods(['is_title_allowed'])
|
|
|
154 |
->getMock();
|
|
|
155 |
$activityheaderstub->method('is_title_allowed')->willReturn($allowtitle);
|
|
|
156 |
$activityheaderstub->set_title($title);
|
|
|
157 |
$this->assertEquals($expectedheadinglevel, $activityheaderstub->get_heading_level());
|
|
|
158 |
}
|
| 1441 |
ariadna |
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Tests that is_title_allowed returns correctly based on the theme default and current layout options.
|
|
|
162 |
*
|
|
|
163 |
* The current layout has precedence, if the notitle option is set, otherwise the theme default is used if set.
|
|
|
164 |
*
|
|
|
165 |
* @param array $themeoptions The activityheader options array set in the theme.
|
|
|
166 |
* @param array $layoutoptions The activitityheader options array set in the layout.
|
|
|
167 |
* @param bool $allowed The expected return value of is_title_allowed.
|
|
|
168 |
* @covers ::is_title_allowed
|
|
|
169 |
* @dataProvider get_title_options
|
|
|
170 |
* @return void
|
|
|
171 |
*/
|
|
|
172 |
public function test_is_title_allowed(array $themeoptions, array $layoutoptions, bool $allowed): void {
|
|
|
173 |
$themeconfig = $this->getMockBuilder(\theme_config::class)
|
|
|
174 |
->disableOriginalConstructor()
|
|
|
175 |
->getMock();
|
|
|
176 |
$themeconfig->activityheaderconfig = $themeoptions;
|
|
|
177 |
$page = $this->getMockBuilder(\moodle_page::class)
|
|
|
178 |
->getMock();
|
|
|
179 |
// Mocking the magic_get_layout_options() and magic_get_theme() methods directly doesn't work,
|
|
|
180 |
// so mock the whole magic __get() method and just return the test values for those properties.
|
|
|
181 |
$page->expects($this->any())->method('__get')->willReturnCallback(fn($name) => match($name) {
|
|
|
182 |
'layout_options' => ['activityheader' => $layoutoptions],
|
|
|
183 |
'theme' => $themeconfig,
|
|
|
184 |
default => null
|
|
|
185 |
});
|
|
|
186 |
$user = new \stdClass();
|
|
|
187 |
|
|
|
188 |
$activityheader = new activity_header($page, $user);
|
|
|
189 |
$this->assertEquals($allowed, $activityheader->is_title_allowed());
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Return scenarios for test_is_title_allowed.
|
|
|
194 |
*
|
|
|
195 |
* Test each combination of the 'notitle' option being unset, true and false in each of the theme and layout options.
|
|
|
196 |
*
|
|
|
197 |
* @return array[]
|
|
|
198 |
*/
|
|
|
199 |
public static function get_title_options(): array {
|
|
|
200 |
return [
|
|
|
201 |
'Undefined in theme, undefined in layout' => [[], [], true],
|
|
|
202 |
'Undefined in theme, disallowed in layout' => [[], ['notitle' => true], false],
|
|
|
203 |
'Undefined in theme, allowed in layout' => [[], ['notitle' => false], true],
|
|
|
204 |
'Disallowed in theme, undefined in layout' => [['notitle' => true], [], false],
|
|
|
205 |
'Disallowed in theme, disallowed in layout' => [['notitle' => true], ['notitle' => true], false],
|
|
|
206 |
'Disallowed in theme, allowed in layout' => [['notitle' => true], ['notitle' => false], true],
|
|
|
207 |
'Allowed in theme, undefined in layout' => [['notitle' => false], [], true],
|
|
|
208 |
'Allowed in theme, disallowed in layout' => [['notitle' => false], ['notitle' => true], false],
|
|
|
209 |
'Allowed in theme, allowed in layout' => [['notitle' => false], ['notitle' => false], true],
|
|
|
210 |
];
|
|
|
211 |
}
|
| 1 |
efrain |
212 |
}
|