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;
|
|
|
18 |
|
|
|
19 |
use core\output\stored_progress_bar;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Unit tests for \core\output\stored_progress_bar
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
26 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core\output\stored_progress_bar
|
|
|
29 |
*/
|
|
|
30 |
final class stored_progress_bar_test extends \advanced_testcase {
|
|
|
31 |
/**
|
|
|
32 |
* Test the progress bar initialisation.
|
|
|
33 |
*
|
|
|
34 |
* Creating a new stored progress bar object should set the idnumber,
|
|
|
35 |
* and not generate any output.
|
|
|
36 |
*
|
|
|
37 |
* @return void
|
|
|
38 |
*/
|
|
|
39 |
public function test_init(): void {
|
|
|
40 |
$idnumber = random_string();
|
|
|
41 |
$progress = new stored_progress_bar($idnumber);
|
|
|
42 |
$this->assertEquals($idnumber, $progress->get_id());
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Calling get_by_idnumber() fetches the correct record.
|
|
|
47 |
*
|
|
|
48 |
* @return void
|
|
|
49 |
* @throws \dml_exception
|
|
|
50 |
*/
|
|
|
51 |
public function test_get_by_idnumber(): void {
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
$generator = $this->getDataGenerator();
|
|
|
54 |
$progress1 = $generator->create_stored_progress(message: 'progress1');
|
|
|
55 |
$progress2 = $generator->create_stored_progress(message: 'progress2');
|
|
|
56 |
$progress3 = $generator->create_stored_progress(message: 'progress3');
|
|
|
57 |
|
|
|
58 |
$progressbar = stored_progress_bar::get_by_idnumber($progress2->idnumber);
|
|
|
59 |
$this->assertEquals('progress2', $progressbar->get_message());
|
|
|
60 |
$progressbar = stored_progress_bar::get_by_idnumber($progress1->idnumber);
|
|
|
61 |
$this->assertEquals('progress1', $progressbar->get_message());
|
|
|
62 |
$progressbar = stored_progress_bar::get_by_idnumber($progress3->idnumber);
|
|
|
63 |
$this->assertEquals('progress3', $progressbar->get_message());
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Calling get_by_id() fetches the correct record.
|
|
|
68 |
*
|
|
|
69 |
* @return void
|
|
|
70 |
*/
|
|
|
71 |
public function test_get_by_id(): void {
|
|
|
72 |
$this->resetAfterTest();
|
|
|
73 |
$generator = $this->getDataGenerator();
|
|
|
74 |
$progress1 = $generator->create_stored_progress();
|
|
|
75 |
$progress2 = $generator->create_stored_progress();
|
|
|
76 |
$progress3 = $generator->create_stored_progress();
|
|
|
77 |
|
|
|
78 |
$progressbar = stored_progress_bar::get_by_id($progress2->id);
|
|
|
79 |
$this->assertEquals($progress2->idnumber, $progressbar->get_id());
|
|
|
80 |
$progressbar = stored_progress_bar::get_by_id($progress1->id);
|
|
|
81 |
$this->assertEquals($progress1->idnumber, $progressbar->get_id());
|
|
|
82 |
$progressbar = stored_progress_bar::get_by_id($progress3->id);
|
|
|
83 |
$this->assertEquals($progress3->idnumber, $progressbar->get_id());
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Calling error() method updates the record with the new message and haserrored = true.
|
|
|
88 |
*
|
|
|
89 |
* @return void
|
|
|
90 |
*/
|
|
|
91 |
public function test_error(): void {
|
|
|
92 |
$this->resetAfterTest();
|
|
|
93 |
$generator = $this->getDataGenerator();
|
|
|
94 |
$progress = $generator->create_stored_progress();
|
|
|
95 |
|
|
|
96 |
$originalprogressbar = stored_progress_bar::get_by_id($progress->id);
|
|
|
97 |
$originalprogressbar->auto_update(false);
|
|
|
98 |
$this->assertEmpty($originalprogressbar->get_message());
|
|
|
99 |
$this->assertFalse($originalprogressbar->get_haserrored());
|
|
|
100 |
|
|
|
101 |
$message = 'There was an error';
|
|
|
102 |
$originalprogressbar->error($message);
|
|
|
103 |
|
|
|
104 |
$updatedprogressbar = stored_progress_bar::get_by_id($progress->id);
|
|
|
105 |
$this->assertEquals($message, $updatedprogressbar->get_message());
|
|
|
106 |
$this->assertTrue($updatedprogressbar->get_haserrored());
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Calling start() replaces the existing record with a new one for the same idnumber.
|
|
|
111 |
*/
|
|
|
112 |
public function test_start(): void {
|
|
|
113 |
$this->resetAfterTest();
|
|
|
114 |
$generator = $this->getDataGenerator();
|
|
|
115 |
$originalprogress = $generator->create_stored_progress();
|
|
|
116 |
|
|
|
117 |
$progressbar = stored_progress_bar::get_by_id($originalprogress->id);
|
|
|
118 |
$this->assertNotNull($progressbar);
|
|
|
119 |
$this->assertEquals($originalprogress->idnumber, $progressbar->get_id());
|
|
|
120 |
|
|
|
121 |
$newid = $progressbar->start();
|
|
|
122 |
|
|
|
123 |
$oldprogressbar = stored_progress_bar::get_by_id($originalprogress->id);
|
|
|
124 |
$this->assertNull($oldprogressbar);
|
|
|
125 |
|
|
|
126 |
$newprogressbar = stored_progress_bar::get_by_id($newid);
|
|
|
127 |
$this->assertNotNull($newprogressbar);
|
|
|
128 |
$this->assertEquals($originalprogress->idnumber, $newprogressbar->get_id());
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Calling convert_to_idnumber() returns a valid idnumber.
|
|
|
133 |
*
|
|
|
134 |
* Leading backslashes are stripped from the class name, and any disallowed characters
|
|
|
135 |
* (any except lower-case letters, numbers and underscores) are replaced with underscores.
|
|
|
136 |
* The result is then concatenated with an underscore and the id argument.
|
|
|
137 |
*
|
|
|
138 |
* @return void
|
|
|
139 |
*/
|
|
|
140 |
public function test_convert_to_idnumber(): void {
|
|
|
141 |
$classname = '\\foo\\bar\\class-1_Name';
|
|
|
142 |
$id = rand(1, 10);
|
|
|
143 |
|
|
|
144 |
$idnumber = stored_progress_bar::convert_to_idnumber($classname, $id);
|
|
|
145 |
$this->assertEquals('foo_bar_class_1__ame_' . $id, $idnumber);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Calling get_timeout() returns the global progresspollinterval setting, or 5 by default.
|
|
|
150 |
*
|
|
|
151 |
* @return void
|
|
|
152 |
*/
|
|
|
153 |
public function test_get_timeout(): void {
|
|
|
154 |
global $CFG;
|
|
|
155 |
$this->resetAfterTest();
|
|
|
156 |
|
|
|
157 |
$this->assertEquals(5, stored_progress_bar::get_timeout());
|
|
|
158 |
$progresspollinterval = rand(10, 20);
|
|
|
159 |
$CFG->progresspollinterval = $progresspollinterval;
|
|
|
160 |
$this->assertEquals($progresspollinterval, stored_progress_bar::get_timeout());
|
|
|
161 |
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Calling export_for_template() returns the current values for rendering the progress bar.
|
|
|
166 |
*/
|
|
|
167 |
public function test_export_for_template(): void {
|
|
|
168 |
global $PAGE;
|
|
|
169 |
$this->resetAfterTest();
|
|
|
170 |
$generator = $this->getDataGenerator();
|
|
|
171 |
$timenow = time();
|
|
|
172 |
$progress = $generator->create_stored_progress(
|
|
|
173 |
'foo_bar_123',
|
|
|
174 |
$timenow - 10,
|
|
|
175 |
$timenow - 1,
|
|
|
176 |
50.00,
|
|
|
177 |
'error',
|
|
|
178 |
true
|
|
|
179 |
);
|
|
|
180 |
|
|
|
181 |
$progressbar = stored_progress_bar::get_by_id($progress->id);
|
|
|
182 |
|
|
|
183 |
$templatecontext = $progressbar->export_for_template($PAGE->get_renderer('core'));
|
|
|
184 |
|
|
|
185 |
$this->assertEquals([
|
|
|
186 |
'id' => $progress->id,
|
|
|
187 |
'idnumber' => $progress->idnumber,
|
|
|
188 |
'width' => 0,
|
|
|
189 |
'class' => 'stored-progress-bar',
|
|
|
190 |
'value' => $progress->percentcompleted,
|
|
|
191 |
'message' => $progress->message,
|
|
|
192 |
'error' => $progress->haserrored,
|
|
|
193 |
], $templatecontext);
|
|
|
194 |
}
|
|
|
195 |
}
|