Skip to content

Commit

Permalink
fixup! MDL-70854 core: Add stored progress bars.
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed May 10, 2024
1 parent 4da2ed8 commit b7ec423
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 6 deletions.
20 changes: 15 additions & 5 deletions lib/classes/task/delete_unconfirmed_users_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Simple task to delete user accounts for users who have not confirmed in time.
*/
class delete_unconfirmed_users_task extends scheduled_task {
use stored_progress_task_trait;

/**
* Get a descriptive name for this task (shown to admins).
Expand All @@ -48,14 +49,23 @@ public function execute() {

// Delete users who haven't confirmed within required period.
if (!empty($CFG->deleteunconfirmed)) {
$this->start_stored_progress();
$cuttime = $timenow - ($CFG->deleteunconfirmed * 3600);
$rs = $DB->get_recordset_sql ("SELECT *
FROM {user}
WHERE confirmed = 0 AND timecreated > 0
AND timecreated < ? AND deleted = 0", array($cuttime));
$selectcount = "SELECT COUNT(*)";
$select = "SELECT *";
$sql = "
FROM {user}
WHERE confirmed = 0 AND timecreated > 0
AND timecreated < ? AND deleted = 0";
$params = [$cuttime];
$count = $DB->count_records_sql($selectcount . $sql, $params);
$rs = $DB->get_recordset_sql($select . $sql, $params);
$processed = 0;
foreach ($rs as $user) {
delete_user($user);
mtrace(" Deleted unconfirmed user ".fullname($user, true)." ($user->id)");
$message = " Deleted unconfirmed user ".fullname($user, true)." ($user->id)";
$processed++;
$this->progress->update($processed, $count, $message);
}
$rs->close();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/task/stored_progress_task_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
trait stored_progress_task_trait {

/** @var stored_progress_bar|null $progress */
/** @var \core\stored_progress_bar|null $progress */
protected $progress = null;

/**
Expand Down
35 changes: 35 additions & 0 deletions lib/testing/generator/data_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,41 @@ public function create_user_course_lastaccess(\stdClass $user, \stdClass $course
return $DB->get_record('user_lastaccess', ['id' => $recordid], '*', MUST_EXIST);
}

/**
* Generate a stored_progress record and return the ID.
*
* All fields are optional, required fields will be generated if not supplied.
*
* @param ?string $idnumber The unique ID Number for this stored progress.
* @param ?int $timestart The time progress was started, defaults to now.
* @param ?int $lastupdate The time the progress was last updated.
* @param ?float $percent The percentage progress so far.
* @param ?string $message An error message.
* @param ?bool $haserrored Whether the process has encountered an error.
* @return stdClass The record including the inserted id.
* @throws dml_exception
*/
public function create_stored_progress(
?string $idnumber = null,
?int $timestart = null,
?int $lastupdate = null,
float $percent = 0.00,
?string $message = null,
?bool $haserrored = false,
): stdClass {
global $DB;
$record = (object)[
'idnumber' => $idnumber ?? random_string(),
'timestart' => $timestart ?? time(),
'lastupdate' => $lastupdate,
'percent' => $percent,
'message' => $message,
'haserrored' => $haserrored,
];
$record->id = $DB->insert_record('stored_progress', $record);
return $record;
}

/**
* Gets a default generator for a given component.
*
Expand Down
192 changes: 192 additions & 0 deletions lib/tests/stored_progress_bar_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;

/**
* Unit tests for \core\stored_progress_bar
*
* @package core
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\stored_progress_bar
*/
class stored_progress_bar_test extends \advanced_testcase {
/**
* Test the progress bar initialisation.
*
* Creating a new stored progress bar object should set the idnumber,
* and not generate any output.
*
* @return void
*/
public function test_init(): void {
$idnumber = random_string();
$progress = new stored_progress_bar($idnumber);
$this->assertEquals($idnumber, $progress->get_id());
}

/**
* Calling get_by_idnumber() fetches the correct record.
*
* @return void
* @throws \dml_exception
*/
public function test_get_by_idnumber(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$progress1 = $generator->create_stored_progress(message: 'progress1');
$progress2 = $generator->create_stored_progress(message: 'progress2');
$progress3 = $generator->create_stored_progress(message: 'progress3');

$progressbar = stored_progress_bar::get_by_idnumber($progress2->idnumber);
$this->assertEquals('progress2', $progressbar->get_message());
$progressbar = stored_progress_bar::get_by_idnumber($progress1->idnumber);
$this->assertEquals('progress1', $progressbar->get_message());
$progressbar = stored_progress_bar::get_by_idnumber($progress3->idnumber);
$this->assertEquals('progress3', $progressbar->get_message());
}

/**
* Calling get_by_id() fetches the correct record.
*
* @return void
*/
public function test_get_by_id(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$progress1 = $generator->create_stored_progress();
$progress2 = $generator->create_stored_progress();
$progress3 = $generator->create_stored_progress();

$progressbar = stored_progress_bar::get_by_id($progress2->id);
$this->assertEquals($progress2->idnumber, $progressbar->get_id());
$progressbar = stored_progress_bar::get_by_id($progress1->id);
$this->assertEquals($progress1->idnumber, $progressbar->get_id());
$progressbar = stored_progress_bar::get_by_id($progress3->id);
$this->assertEquals($progress3->idnumber, $progressbar->get_id());
}

/**
* Calling error() method updates the record with the new message and haserrored = true.
*
* @return void
*/
public function test_error(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$progress = $generator->create_stored_progress();

$originalprogressbar = stored_progress_bar::get_by_id($progress->id);
$originalprogressbar->auto_update(false);
$this->assertEmpty($originalprogressbar->get_message());
$this->assertFalse($originalprogressbar->get_haserrored());

$message = 'There was an error';
$originalprogressbar->error($message);

$updatedprogressbar = stored_progress_bar::get_by_id($progress->id);
$this->assertEquals($message, $updatedprogressbar->get_message());
$this->assertTrue($updatedprogressbar->get_haserrored());
}

/**
* Calling start() replaces the existing record with a new one for the same idnumber.
*/
public function test_start(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$originalprogress = $generator->create_stored_progress();

$progressbar = stored_progress_bar::get_by_id($originalprogress->id);
$this->assertNotNull($progressbar);
$this->assertEquals($originalprogress->idnumber, $progressbar->get_id());

$newid = $progressbar->start();

$oldprogressbar = stored_progress_bar::get_by_id($originalprogress->id);
$this->assertNull($oldprogressbar);

$newprogressbar = stored_progress_bar::get_by_id($newid);
$this->assertNotNull($newprogressbar);
$this->assertEquals($originalprogress->idnumber, $newprogressbar->get_id());
}

/**
* Calling convert_to_idnumber() returns a valid idnumber.
*
* Leading backslashes are stripped from the class name, and any disallowed characters
* (any except lower-case letters, numbers and underscores) are replaced with underscores.
* The result is then concatenated with an underscore and the id argument.
*
* @return void
*/
public function test_convert_to_idnumber(): void {
$classname = '\\foo\\bar\\class-1_Name';
$id = rand(1, 10);

$idnumber = stored_progress_bar::convert_to_idnumber($classname, $id);
$this->assertEquals('foo_bar_class_1__ame_' . $id, $idnumber);
}

/**
* Calling get_timeout() returns the global progresspollinterval setting, or 5 by default.
*
* @return void
*/
public function test_get_timeout(): void {
global $CFG;
$this->resetAfterTest();

$this->assertEquals(5, stored_progress_bar::get_timeout());
$progresspollinterval = rand(10, 20);
$CFG->progresspollinterval = $progresspollinterval;
$this->assertEquals($progresspollinterval, stored_progress_bar::get_timeout());

}

/**
* Calling export_for_template() returns the current values for rendering the progress bar.
*/
public function test_export_for_template(): void {
global $PAGE;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$timenow = time();
$progress = $generator->create_stored_progress(
'foo_bar_123',
$timenow - 10,
$timenow - 1,
50.00,
'error',
true
);

$progressbar = stored_progress_bar::get_by_id($progress->id);

$templatecontext = $progressbar->export_for_template($PAGE->get_renderer('core'));

$this->assertEquals([
'id' => $progress->id,
'idnumber' => $progress->idnumber,
'width' => 0,
'class' => 'stored-progress-bar',
'value' => $progress->percent,
'message' => $progress->message,
'error' => $progress->haserrored,
], $templatecontext);
}
}
50 changes: 50 additions & 0 deletions lib/tests/task/stored_progress_bar_cleanup_task_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\task;

use core\stored_progress_bar;

/**
* Unit tests for stored_progress_bar_cleanup
*
* @package core
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers core\task\stored_progress_bar_cleanup
*/
class stored_progress_bar_cleanup_task_test extends \advanced_testcase {
/**
* Clean up stored_progress records that were last updated over 24 hours ago.
*/
public function test_execute(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$neverupdated = $generator->create_stored_progress();
$updatednow = $generator->create_stored_progress(lastupdate: time());
$updated23hours = $generator->create_stored_progress(lastupdate: time() - (HOURSECS * 23));
$updated24hours = $generator->create_stored_progress(lastupdate: time() - DAYSECS - 1);

$task = new stored_progress_bar_cleanup_task();
$this->expectOutputString('Deleted old stored_progress records' . PHP_EOL);
$task->execute();

$this->assertNotNull(stored_progress_bar::get_by_id($neverupdated->id));
$this->assertNotNull(stored_progress_bar::get_by_id($updatednow->id));
$this->assertNotNull(stored_progress_bar::get_by_id($updated23hours->id));
$this->assertNull(stored_progress_bar::get_by_id($updated24hours->id));
}
}

0 comments on commit b7ec423

Please sign in to comment.