From 1bfb41f45ad7b3f0769a7241060e88875c49aa02 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= <commits@skodak.org>
Date: Mon, 14 Apr 2014 15:16:28 +0800
Subject: [PATCH] MDL-44366 fix use of cm->uservisible in glossary concept
 cache

---
 mod/glossary/classes/local/concept_cache.php |  6 ++++--
 mod/glossary/tests/concept_cache_test.php    | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/mod/glossary/classes/local/concept_cache.php b/mod/glossary/classes/local/concept_cache.php
index 6f0fbeffad4..a7f42b9be5a 100644
--- a/mod/glossary/classes/local/concept_cache.php
+++ b/mod/glossary/classes/local/concept_cache.php
@@ -206,11 +206,13 @@ class concept_cache {
         foreach ($concepts as $modid => $unused) {
             if (!isset($cminfos[$modid])) {
                 // This should not happen.
-                unset($concepts[$modinfo]);
+                unset($concepts[$modid]);
+                unset($glossaries[$modid]);
                 continue;
             }
             if (!$cminfos[$modid]->uservisible) {
-                unset($concepts[$modinfo]);
+                unset($concepts[$modid]);
+                unset($glossaries[$modid]);
                 continue;
             }
         }
diff --git a/mod/glossary/tests/concept_cache_test.php b/mod/glossary/tests/concept_cache_test.php
index 3d740feec7f..5539e004ee1 100644
--- a/mod/glossary/tests/concept_cache_test.php
+++ b/mod/glossary/tests/concept_cache_test.php
@@ -158,5 +158,24 @@ class mod_glossary_concept_cache_testcase extends advanced_testcase {
 
         $concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2->id);
         $this->assertEquals($concepts3, $concepts2);
+
+        // Test uservisible flag.
+        set_config('enablegroupmembersonly', 1);
+        $glossary1d = $this->getDataGenerator()->create_module('glossary',
+            array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1, 'groupmembersonly' => 1));
+        $entry1d1 = $generator->create_content($glossary1d, array('concept' => 'membersonly', 'usedynalink' => 1));
+        $user = $this->getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
+        \mod_glossary\local\concept_cache::reset_caches();
+        $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
+        $this->assertCount(4, $concepts1[0]);
+        $this->assertCount(4, $concepts1[1]);
+        $this->setUser($user);
+        course_modinfo::clear_instance_cache();
+        \mod_glossary\local\concept_cache::reset_caches();
+        $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
+        $this->assertCount(3, $concepts1[0]);
+        $this->assertCount(3, $concepts1[1]);
     }
 }