MDL-42913 lib/grouplib.php: Avoid cache sometimes for groups_get_all_groups

The new groups cache returns unexpected results when you pass
in a field parameter than would normally change the result array index
This commit is contained in:
Aaron Barnes 2013-11-15 13:34:06 +13:00
parent b58bc15af5
commit 2eaa440c34
2 changed files with 24 additions and 6 deletions

View File

@ -203,12 +203,18 @@ function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*
// aliased. If its something else we need to avoid the cache and run the query as who knows whats going on.
$knownfields = true;
if ($fields !== 'g.*') {
$fieldbits = explode(',', $fields);
foreach ($fieldbits as $bit) {
$bit = trim($bit);
if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
$knownfields = false;
break;
// Quickly check if the first field is no longer g.id as using the
// cache will return an array indexed differently than when expect
if (strpos($fields, 'g.*') !== 0 && strpos($fields, 'g.id') !== 0) {
$knownfields = false;
} else {
$fieldbits = explode(',', $fields);
foreach ($fieldbits as $bit) {
$bit = trim($bit);
if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
$knownfields = false;
break;
}
}
}
}

View File

@ -26,6 +26,10 @@
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for lib/grouplib.php
* @group core_group
*/
class core_grouplib_testcase extends advanced_testcase {
public function test_groups_get_group_by_idnumber() {
@ -404,6 +408,14 @@ class core_grouplib_testcase extends advanced_testcase {
$this->assertNotContains($group2->id, $groupkeys);
$this->assertContains($group3->id, $groupkeys);
$this->assertContains($group4->id, $groupkeys);
// Test this function using an alternate column for the result index
$groups = groups_get_all_groups($course->id, null, $grouping2->id, 'g.name, g.id');
$groupkeys = array_keys($groups);
$this->assertCount(2, $groups);
$this->assertNotContains($group3->id, $groupkeys);
$this->assertContains($group3->name, $groupkeys);
$this->assertEquals($group3->id, $groups[$group3->name]->id);
}
/**