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 |
*/
|
|
|
28 |
class activity_header_test extends \advanced_testcase {
|
|
|
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 |
*/
|
|
|
63 |
public function set_title_provider(): array {
|
|
|
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 |
*/
|
|
|
132 |
public function get_heading_level_provider(): array {
|
|
|
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 |
}
|
|
|
159 |
}
|