Skip to content
Browse files

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
  • Loading branch information...
1 parent 2b10fa0 commit b642702aa234510dd94a1180fd5c2f47df6a6c59 @srynot4sale srynot4sale committed Nov 15, 2013
Showing with 24 additions and 6 deletions.
  1. +12 −6 lib/grouplib.php
  2. +12 −0 lib/tests/grouplib_test.php
View
18 lib/grouplib.php
@@ -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
12 lib/tests/grouplib_test.php
@@ -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 @@ public function test_groups_get_course_data() {
$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);
}
/**

0 comments on commit b642702

Please sign in to comment.
Something went wrong with that request. Please try again.