Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #102: exclude deleted users when using OR operator #103

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions classes/condition_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public static function build_sql_data(array $conditions, string $operator = rule
}
}

// If we have conditions let's wrap them all in one big condition.
if (!empty($where)) {
$where = ' ( ' . $where . ' ) ';
}

if ($userid) {
$userparam = condition_sql::generate_param_alias();
$where .= " AND ( u.id = :{$userparam}) ";
Expand Down
58 changes: 58 additions & 0 deletions tests/condition_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,62 @@ public function test_build_sql_data() {
$this->assertTrue(in_array('user2username', $sql->get_params()));
$this->assertTrue(in_array(777, $sql->get_params()));
}

/**
* Test that generated SQL should exclude deleted users.
*/
public function test_should_exclude_deleted_users() {
global $DB;

$this->resetAfterTest();

$this->getDataGenerator()->create_user(['username' => 'user1username', 'auth' => 'lti']);
$this->getDataGenerator()->create_user(['username' => 'user2username', 'auth' => 'lti']);
$usertodeleted = $this->getDataGenerator()->create_user(['username' => 'user3username', 'auth' => 'lti']);
$this->getDataGenerator()->create_user(['username' => 'test']);

$cohort = $this->getDataGenerator()->create_cohort();

$rule = new rule(0, (object)['name' => 'Test rule 1', 'cohortid' => $cohort->id,
'operator' => rule_manager::CONDITIONS_OPERATOR_OR]);
$rule->save();

$conditions = [];
$condition = user_profile::get_instance(0, (object)['ruleid' => $rule->get('id'), 'sortorder' => 1]);
$condition->set_config_data([
'profilefield' => 'username',
'username_operator' => condition_base::TEXT_CONTAINS,
'username_value' => 'username',
]);
$condition->get_record()->save();
$conditions[] = $condition->get_record();

$condition = user_profile::get_instance(0, (object)['ruleid' => $rule->get('id'), 'sortorder' => 1]);
$condition->set_config_data([
'profilefield' => 'auth',
'auth_operator' => condition_base::TEXT_IS_EQUAL_TO,
'auth_value' => 'lti',
]);
$condition->get_record()->save();
$conditions[] = $condition->get_record();

$sqldataand = condition_manager::build_sql_data($conditions);
$sqldataor = condition_manager::build_sql_data($conditions, rule_manager::CONDITIONS_OPERATOR_OR);

$basesql = "SELECT DISTINCT u.id FROM {user} u ";
$sqland = $basesql . $sqldataand->get_join() . ' WHERE ' . $sqldataand->get_where();
$sqlor = $basesql . $sqldataor->get_join() . ' WHERE ' . $sqldataor->get_where();

$this->assertCount(3, $DB->get_records_sql($sqland, $sqldataand->get_params()));
$this->assertCount(3, $DB->get_records_sql($sqlor, $sqldataor->get_params()));
$this->assertArrayHasKey($usertodeleted->id, $DB->get_records_sql($sqland, $sqldataand->get_params()));
$this->assertArrayHasKey($usertodeleted->id, $DB->get_records_sql($sqlor, $sqldataor->get_params()));

delete_user($usertodeleted);

$this->assertCount(2, $DB->get_records_sql($sqland, $sqldataand->get_params()));
$this->assertCount(2, $DB->get_records_sql($sqlor, $sqldataor->get_params()));
$this->assertArrayNotHasKey($usertodeleted->id, $DB->get_records_sql($sqland, $sqldataand->get_params()));
$this->assertArrayNotHasKey($usertodeleted->id, $DB->get_records_sql($sqlor, $sqldataor->get_params()));
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'tool_dynamic_cohorts';
$plugin->release = 2024051000;
$plugin->version = 2024051000;
$plugin->release = 2024051001;
$plugin->version = 2024051001;
$plugin->requires = 2022112800;
$plugin->supported = [401, 403];
$plugin->maturity = MATURITY_STABLE;
Loading