From 693231b04874e6054b8a6990681fd4541a486944 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 11 Jun 2024 21:58:20 +0100 Subject: [PATCH] MDL-82170 user: replace increment operator on string type variable. --- lib/datalib.php | 11 ++++------- lib/tests/datalib_test.php | 35 +++++++++++++++++------------------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index 82edd764c7cbf..769c9a94550b3 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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)); @@ -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) . diff --git a/lib/tests/datalib_test.php b/lib/tests/datalib_test.php index 713680bebbb98..0f4a49ac1a5cf 100644 --- a/lib/tests/datalib_test.php +++ b/lib/tests/datalib_test.php @@ -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 { @@ -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 { @@ -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 {