22
22
* @copyright 2024 Catalyst IT {@link http://www.catalyst-eu.net/}
23
23
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
24
*/
25
- namespace tool_disable_delete_students ;
25
+
26
+ use tool_disable_delete_students \util ;
26
27
27
28
/**
28
29
* Unit tests for the tool_disable_delete_students plugin.
@@ -77,7 +78,7 @@ protected function setUp(): void {
77
78
*/
78
79
private function create_course_with_dates (int $ endoffset ): \stdClass {
79
80
$ 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.
81
82
82
83
return $ this ->getDataGenerator ()->create_course ([
83
84
'startdate ' => $ startdate ,
@@ -127,11 +128,20 @@ public function test_disable_after_course_end(): void {
127
128
// Enrol student in course.
128
129
$ this ->getDataGenerator ()->enrol_user ($ student ->id , $ course ->id , $ this ->studentrole ->id );
129
130
131
+ // Start output buffering.
132
+ ob_start ();
133
+
130
134
// Run cleanup.
131
135
util::process_student_accounts ();
132
136
133
137
// Check if student account was disabled.
134
138
$ 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 );
135
145
$ this ->assertEquals (1 , $ updateduser ->suspended );
136
146
}
137
147
@@ -146,17 +156,26 @@ public function test_disable_after_creation(): void {
146
156
147
157
// Create student with old creation date.
148
158
$ 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.
150
160
151
161
// Assign student role.
152
162
$ systemcontext = \context_system::instance ();
153
163
role_assign ($ this ->studentrole ->id , $ student ->id , $ systemcontext ->id );
154
164
165
+ // Start output buffering.
166
+ ob_start ();
167
+
155
168
// Run cleanup.
156
169
util::process_student_accounts ();
157
170
158
171
// Check if student account was disabled.
159
172
$ 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 );
160
179
$ this ->assertEquals (1 , $ updateduser ->suspended );
161
180
}
162
181
@@ -171,18 +190,30 @@ public function test_delete_after_months(): void {
171
190
172
191
// Create test data.
173
192
$ 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 ]);
174
195
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
177
198
178
- // Enrol student in course.
199
+ // Enrol student in the course.
179
200
$ this ->getDataGenerator ()->enrol_user ($ student ->id , $ course ->id , $ this ->studentrole ->id );
180
201
202
+ // Start output buffering.
203
+ ob_start ();
204
+
181
205
// Run cleanup.
182
206
util::process_student_accounts ();
183
207
184
208
// Check if student account was deleted.
185
209
$ 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 );
186
217
$ this ->assertFalse ($ userexists );
187
218
}
188
219
0 commit comments