From a16fb194f84c2ef0c14b241a9cf3951fce02f3ba Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 23 Feb 2024 15:06:00 +0000 Subject: [PATCH] MDL-80550 cohort: get IDs for customfields correctly in external search. --- cohort/externallib.php | 4 ++-- cohort/tests/externallib_test.php | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cohort/externallib.php b/cohort/externallib.php index 6a31ed39e29..a1241f20911 100644 --- a/cohort/externallib.php +++ b/cohort/externallib.php @@ -402,7 +402,7 @@ class core_cohort_external extends external_api { $results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query); $results = $results['cohorts']; if (!$context instanceof context_system) { - $results = array_merge($results, cohort_get_available_cohorts($context, COHORT_ALL, $limitfrom, $limitnum, $query)); + $results = $results + cohort_get_available_cohorts($context, COHORT_ALL, $limitfrom, $limitnum, $query); } } else if ($includes == 'all') { $results = cohort_get_all_cohorts($limitfrom, $limitnum, $query); @@ -414,7 +414,7 @@ class core_cohort_external extends external_api { $cohorts = array(); if (!empty($results)) { - $cohortids = array_keys($results); + $cohortids = array_column($results, 'id'); $customfieldsdata = self::get_custom_fields_data($cohortids); } diff --git a/cohort/tests/externallib_test.php b/cohort/tests/externallib_test.php index 0e1bc5f0b23..0d25c58ff9f 100644 --- a/cohort/tests/externallib_test.php +++ b/cohort/tests/externallib_test.php @@ -716,11 +716,16 @@ class externallib_test extends externallib_advanced_testcase { $this->assertCount(1, $result['cohorts']); $this->assertEquals('Cohortsearch 4', $result['cohorts'][$cohort4->id]->name); - $result = core_cohort_external::search_cohorts("Cohortsearch 4", $syscontext, 'parents'); + // A user with permissions in the system, searching category context. + $result = core_cohort_external::search_cohorts("Cohortsearch 4", $catcontext, 'parents'); $this->assertCount(1, $result['cohorts']); $this->assertEquals('Cohortsearch 4', $result['cohorts'][$cohort4->id]->name); - $this->assertIsArray($result['cohorts'][$cohort4->id]->customfields); - $this->assertCount(2, $result['cohorts'][$cohort4->id]->customfields); + + $this->assertEqualsCanonicalizing([ + 'Test value 1', + 'Test value 2', + ], array_column($result['cohorts'][$cohort4->id]->customfields, 'value')); + $actual = []; foreach ($result['cohorts'][$cohort4->id]->customfields as $customfield) { $this->assertArrayHasKey('name', $customfield); @@ -730,8 +735,6 @@ class externallib_test extends externallib_advanced_testcase { $this->assertArrayHasKey('value', $customfield); $actual[$customfield['shortname']] = $customfield; } - $this->assertEquals('Test value 1', $actual['testfield1']['value']); - $this->assertEquals('Test value 2', $actual['testfield2']['value']); // A user with permissions in the category. $this->setUser($catcreator);