Skip to content

Commit

Permalink
MDL-82170 user: replace increment operator on string type variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Feb 5, 2025
1 parent 89e6f73 commit 693231b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
11 changes: 4 additions & 7 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,10 @@ function users_order_by_sql(string $usertablealias = '', ?string $search = null,
}

$exactconditions = array();
$paramkey = 'usersortexact1';

$exactconditions[] = $DB->sql_fullname($tableprefix . 'firstname', $tableprefix . 'lastname') .
' = :' . $paramkey;
$params[$paramkey] = $search;
$paramkey++;
' = :usersortexact';
$params['usersortexact'] = $search;

if ($customfieldmappings) {
$fieldstocheck = array_merge([$tableprefix . 'firstname', $tableprefix . 'lastname'], array_values($customfieldmappings));
Expand All @@ -399,9 +397,8 @@ function users_order_by_sql(string $usertablealias = '', ?string $search = null,
}

foreach ($fieldstocheck as $key => $field) {
$exactconditions[] = 'LOWER(' . $field . ') = LOWER(:' . $paramkey . ')';
$params[$paramkey] = $search;
$paramkey++;
$exactconditions[] = 'LOWER(' . $field . ') = LOWER(:usersortfield' . $key . ')';
$params['usersortfield' . $key] = $search;
}

$sort = 'CASE WHEN ' . implode(' OR ', $exactconditions) .
Expand Down
35 changes: 17 additions & 18 deletions lib/tests/datalib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ public function test_users_order_by_sql_search_no_extra_fields(): void {

list($sort, $params) = users_order_by_sql('', 'search', \context_system::instance());
$this->assert_same_sql('CASE WHEN
' . $DB->sql_fullname() . ' = :usersortexact1 OR
LOWER(firstname) = LOWER(:usersortexact2) OR
LOWER(lastname) = LOWER(:usersortexact3)
' . $DB->sql_fullname() . ' = :usersortexact OR
LOWER(firstname) = LOWER(:usersortfield0) OR
LOWER(lastname) = LOWER(:usersortfield1)
THEN 0 ELSE 1 END, lastname, firstname, id', $sort);
$this->assertEquals(array('usersortexact1' => 'search', 'usersortexact2' => 'search',
'usersortexact3' => 'search'), $params);
$this->assertEquals(['usersortexact' => 'search', 'usersortfield0' => 'search', 'usersortfield1' => 'search'], $params);
}

public function test_users_order_by_sql_search_with_extra_fields_and_prefix(): void {
Expand All @@ -191,14 +190,14 @@ public function test_users_order_by_sql_search_with_extra_fields_and_prefix(): v

list($sort, $params) = users_order_by_sql('u', 'search', \context_system::instance());
$this->assert_same_sql('CASE WHEN
' . $DB->sql_fullname('u.firstname', 'u.lastname') . ' = :usersortexact1 OR
LOWER(u.firstname) = LOWER(:usersortexact2) OR
LOWER(u.lastname) = LOWER(:usersortexact3) OR
LOWER(u.email) = LOWER(:usersortexact4) OR
LOWER(u.idnumber) = LOWER(:usersortexact5)
' . $DB->sql_fullname('u.firstname', 'u.lastname') . ' = :usersortexact OR
LOWER(u.firstname) = LOWER(:usersortfield0) OR
LOWER(u.lastname) = LOWER(:usersortfield1) OR
LOWER(u.email) = LOWER(:usersortfield2) OR
LOWER(u.idnumber) = LOWER(:usersortfield3)
THEN 0 ELSE 1 END, u.lastname, u.firstname, u.id', $sort);
$this->assertEquals(array('usersortexact1' => 'search', 'usersortexact2' => 'search',
'usersortexact3' => 'search', 'usersortexact4' => 'search', 'usersortexact5' => 'search'), $params);
$this->assertEquals(['usersortexact' => 'search', 'usersortfield0' => 'search',
'usersortfield1' => 'search', 'usersortfield2' => 'search', 'usersortfield3' => 'search'], $params);
}

public function test_users_order_by_sql_search_with_custom_fields(): void {
Expand All @@ -211,13 +210,13 @@ public function test_users_order_by_sql_search_with_custom_fields(): void {
list($sort, $params) =
users_order_by_sql('u', 'search', \context_system::instance(), ['profile_field_customfield' => 'x.customfield']);
$this->assert_same_sql('CASE WHEN
' . $DB->sql_fullname('u.firstname', 'u.lastname') . ' = :usersortexact1 OR
LOWER(u.firstname) = LOWER(:usersortexact2) OR
LOWER(u.lastname) = LOWER(:usersortexact3) OR
LOWER(x.customfield) = LOWER(:usersortexact4)
' . $DB->sql_fullname('u.firstname', 'u.lastname') . ' = :usersortexact OR
LOWER(u.firstname) = LOWER(:usersortfield0) OR
LOWER(u.lastname) = LOWER(:usersortfield1) OR
LOWER(x.customfield) = LOWER(:usersortfield2)
THEN 0 ELSE 1 END, u.lastname, u.firstname, u.id', $sort);
$this->assertEquals(array('usersortexact1' => 'search', 'usersortexact2' => 'search',
'usersortexact3' => 'search', 'usersortexact4' => 'search'), $params);
$this->assertEquals(['usersortexact' => 'search', 'usersortfield0' => 'search',
'usersortfield1' => 'search', 'usersortfield2' => 'search'], $params);
}

public function test_get_admin(): void {
Expand Down

0 comments on commit 693231b

Please sign in to comment.