@@ -275,4 +275,62 @@ public function test_build_sql_data() {
275
275
$ this ->assertTrue (in_array ('user2username ' , $ sql ->get_params ()));
276
276
$ this ->assertTrue (in_array (777 , $ sql ->get_params ()));
277
277
}
278
+
279
+ /**
280
+ * Test that generated SQL should exclude deleted users.
281
+ */
282
+ public function test_should_exclude_deleted_users () {
283
+ global $ DB ;
284
+
285
+ $ this ->resetAfterTest ();
286
+
287
+ $ this ->getDataGenerator ()->create_user (['username ' => 'user1username ' , 'auth ' => 'lti ' ]);
288
+ $ this ->getDataGenerator ()->create_user (['username ' => 'user2username ' , 'auth ' => 'lti ' ]);
289
+ $ usertodeleted = $ this ->getDataGenerator ()->create_user (['username ' => 'user3username ' , 'auth ' => 'lti ' ]);
290
+ $ this ->getDataGenerator ()->create_user (['username ' => 'test ' ]);
291
+
292
+ $ cohort = $ this ->getDataGenerator ()->create_cohort ();
293
+
294
+ $ rule = new rule (0 , (object )['name ' => 'Test rule 1 ' , 'cohortid ' => $ cohort ->id ,
295
+ 'operator ' => rule_manager::CONDITIONS_OPERATOR_OR ]);
296
+ $ rule ->save ();
297
+
298
+ $ conditions = [];
299
+ $ condition = user_profile::get_instance (0 , (object )['ruleid ' => $ rule ->get ('id ' ), 'sortorder ' => 1 ]);
300
+ $ condition ->set_config_data ([
301
+ 'profilefield ' => 'username ' ,
302
+ 'username_operator ' => condition_base::TEXT_CONTAINS ,
303
+ 'username_value ' => 'username ' ,
304
+ ]);
305
+ $ condition ->get_record ()->save ();
306
+ $ conditions [] = $ condition ->get_record ();
307
+
308
+ $ condition = user_profile::get_instance (0 , (object )['ruleid ' => $ rule ->get ('id ' ), 'sortorder ' => 1 ]);
309
+ $ condition ->set_config_data ([
310
+ 'profilefield ' => 'auth ' ,
311
+ 'auth_operator ' => condition_base::TEXT_IS_EQUAL_TO ,
312
+ 'auth_value ' => 'lti ' ,
313
+ ]);
314
+ $ condition ->get_record ()->save ();
315
+ $ conditions [] = $ condition ->get_record ();
316
+
317
+ $ sqldataand = condition_manager::build_sql_data ($ conditions );
318
+ $ sqldataor = condition_manager::build_sql_data ($ conditions , rule_manager::CONDITIONS_OPERATOR_OR );
319
+
320
+ $ basesql = "SELECT DISTINCT u.id FROM {user} u " ;
321
+ $ sqland = $ basesql . $ sqldataand ->get_join () . ' WHERE ' . $ sqldataand ->get_where ();
322
+ $ sqlor = $ basesql . $ sqldataor ->get_join () . ' WHERE ' . $ sqldataor ->get_where ();
323
+
324
+ $ this ->assertCount (3 , $ DB ->get_records_sql ($ sqland , $ sqldataand ->get_params ()));
325
+ $ this ->assertCount (3 , $ DB ->get_records_sql ($ sqlor , $ sqldataor ->get_params ()));
326
+ $ this ->assertArrayHasKey ($ usertodeleted ->id , $ DB ->get_records_sql ($ sqland , $ sqldataand ->get_params ()));
327
+ $ this ->assertArrayHasKey ($ usertodeleted ->id , $ DB ->get_records_sql ($ sqlor , $ sqldataor ->get_params ()));
328
+
329
+ delete_user ($ usertodeleted );
330
+
331
+ $ this ->assertCount (2 , $ DB ->get_records_sql ($ sqland , $ sqldataand ->get_params ()));
332
+ $ this ->assertCount (2 , $ DB ->get_records_sql ($ sqlor , $ sqldataor ->get_params ()));
333
+ $ this ->assertArrayNotHasKey ($ usertodeleted ->id , $ DB ->get_records_sql ($ sqland , $ sqldataand ->get_params ()));
334
+ $ this ->assertArrayNotHasKey ($ usertodeleted ->id , $ DB ->get_records_sql ($ sqlor , $ sqldataor ->get_params ()));
335
+ }
278
336
}
0 commit comments