Skip to content

Commit 84e6557

Browse files
committed
Fixed test cases for the plugin
1 parent 48a15c7 commit 84e6557

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

classes/util.php

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
namespace tool_disable_delete_students;
3232

33-
defined('MOODLE_INTERNAL') || die();
3433

3534
/**
3635
* Utility class containing static methods for student account management.

tests/cleanup_test.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* @copyright 2024 Catalyst IT {@link http://www.catalyst-eu.net/}
2323
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2424
*/
25-
namespace tool_disable_delete_students;
25+
26+
use tool_disable_delete_students\util;
2627

2728
/**
2829
* Unit tests for the tool_disable_delete_students plugin.
@@ -77,7 +78,7 @@ protected function setUp(): void {
7778
*/
7879
private function create_course_with_dates(int $endoffset): \stdClass {
7980
$startdate = time() - (360 * DAYSECS); // Start date 360 days ago.
80-
$enddate = $startdate + $endoffset; // End date based on offset.
81+
$enddate = time() + $endoffset; // End date based on offset.
8182

8283
return $this->getDataGenerator()->create_course([
8384
'startdate' => $startdate,
@@ -127,11 +128,20 @@ public function test_disable_after_course_end(): void {
127128
// Enrol student in course.
128129
$this->getDataGenerator()->enrol_user($student->id, $course->id, $this->studentrole->id);
129130

131+
// Start output buffering.
132+
ob_start();
133+
130134
// Run cleanup.
131135
util::process_student_accounts();
132136

133137
// Check if student account was disabled.
134138
$updateduser = $DB->get_record('user', ['id' => $student->id]);
139+
140+
// Capture the output.
141+
$output = ob_get_clean();
142+
143+
// Assert the expected output.
144+
$this->assertStringContainsString("Disabled user: {$student->username}", $output);
135145
$this->assertEquals(1, $updateduser->suspended);
136146
}
137147

@@ -146,17 +156,26 @@ public function test_disable_after_creation(): void {
146156

147157
// Create student with old creation date.
148158
$student = $this->getDataGenerator()->create_user();
149-
$DB->set_field('user', 'timecreated', time() - (46 * DAYSECS), ['id' => $student->id]);
159+
$DB->set_field('user', 'timecreated', time() - (46 * DAYSECS), ['id' => $student->id]); // Set creation date to 46 days ago.
150160

151161
// Assign student role.
152162
$systemcontext = \context_system::instance();
153163
role_assign($this->studentrole->id, $student->id, $systemcontext->id);
154164

165+
// Start output buffering.
166+
ob_start();
167+
155168
// Run cleanup.
156169
util::process_student_accounts();
157170

158171
// Check if student account was disabled.
159172
$updateduser = $DB->get_record('user', ['id' => $student->id]);
173+
174+
// Capture the output.
175+
$output = ob_get_clean();
176+
177+
// Assert the expected output.
178+
$this->assertStringContainsString("Disabled user: {$student->username}", $output);
160179
$this->assertEquals(1, $updateduser->suspended);
161180
}
162181

@@ -171,18 +190,30 @@ public function test_delete_after_months(): void {
171190

172191
// Create test data.
173192
$student = $this->getDataGenerator()->create_user();
193+
// Set the creation date to now (this is not relevant for deletion in this test).
194+
$DB->set_field('user', 'timecreated', time(), ['id' => $student->id]);
174195

175-
// Create course that ended 6 months ago.
176-
$course = $this->create_course_with_dates(-180 * DAYSECS);
196+
// Create a course that ended 7 months ago (more than the delete threshold).
197+
$course = $this->create_course_with_dates(-210 * DAYSECS); // 210 days ago
177198

178-
// Enrol student in course.
199+
// Enrol student in the course.
179200
$this->getDataGenerator()->enrol_user($student->id, $course->id, $this->studentrole->id);
180201

202+
// Start output buffering.
203+
ob_start();
204+
181205
// Run cleanup.
182206
util::process_student_accounts();
183207

184208
// Check if student account was deleted.
185209
$userexists = $DB->record_exists('user', ['id' => $student->id, 'deleted' => 0]);
210+
211+
212+
// Capture the output.
213+
$output = ob_get_clean();
214+
215+
// Assert the expected output.
216+
$this->assertStringContainsString("Deleted user: {$student->username}", $output);
186217
$this->assertFalse($userexists);
187218
}
188219

0 commit comments

Comments
 (0)