Skip to content

Commit 8609e69

Browse files
authored
Merge pull request #103 from catalyst/issue102-MOODLE_401_STABLE
issue #102: exclude deleted users when using OR operator
2 parents c5f373e + f89a89a commit 8609e69

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

classes/condition_manager.php

+5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ public static function build_sql_data(array $conditions, string $operator = rule
252252
}
253253
}
254254

255+
// If we have conditions let's wrap them all in one big condition.
256+
if (!empty($where)) {
257+
$where = ' ( ' . $where . ' ) ';
258+
}
259+
255260
if ($userid) {
256261
$userparam = condition_sql::generate_param_alias();
257262
$where .= " AND ( u.id = :{$userparam}) ";

tests/condition_manager_test.php

+58
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,62 @@ public function test_build_sql_data() {
275275
$this->assertTrue(in_array('user2username', $sql->get_params()));
276276
$this->assertTrue(in_array(777, $sql->get_params()));
277277
}
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+
}
278336
}

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
28-
$plugin->release = 2024051000;
29-
$plugin->version = 2024051000;
28+
$plugin->release = 2024051001;
29+
$plugin->version = 2024051001;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [401, 403];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)