MDL-62838 filters: dont use statics inside functions. Use MUC
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 29 Jun 2018 15:37:28 +0000 (16:37 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 29 Jun 2018 16:24:41 +0000 (17:24 +0100)
lib/filterlib.php
lib/tests/filterlib_test.php

index 0a9753b..be862e7 100644 (file)
@@ -692,16 +692,21 @@ function filter_is_enabled($filtername) {
  * @return array where the keys and values are both the filter name, like 'tex'.
  */
 function filter_get_globally_enabled() {
-    static $enabledfilters = null;
-    if (is_null($enabledfilters)) {
-        $filters = filter_get_global_states();
-        $enabledfilters = array();
-        foreach ($filters as $filter => $filerinfo) {
-            if ($filerinfo->active != TEXTFILTER_DISABLED) {
-                $enabledfilters[$filter] = $filter;
-            }
+    $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'core_filter', 'global_filters');
+    $enabledfilters = $cache->get('enabled');
+    if ($enabledfilters !== false) {
+        return $enabledfilters;
+    }
+
+    $filters = filter_get_global_states();
+    $enabledfilters = array();
+    foreach ($filters as $filter => $filerinfo) {
+        if ($filerinfo->active != TEXTFILTER_DISABLED) {
+            $enabledfilters[$filter] = $filter;
         }
     }
+
+    $cache->set('enabled', $enabledfilters);
     return $enabledfilters;
 }
 
index b8af6ff..c3aab08 100644 (file)
@@ -676,6 +676,17 @@ class core_filterlib_testcase extends advanced_testcase {
         $this->assertInstanceOf('performance_measuring_filter_manager', $filterman);
     }
 
+    public function test_filter_get_globally_enabled_default() {
+        $enabledfilters = filter_get_globally_enabled();
+        $this->assertArrayNotHasKey('glossary', $enabledfilters);
+    }
+
+    public function test_filter_get_globally_enabled_after_change() {
+        filter_set_global_state('glossary', TEXTFILTER_ON);
+        $enabledfilters = filter_get_globally_enabled();
+        $this->assertArrayHasKey('glossary', $enabledfilters);
+    }
+
     public function test_filter_get_globally_enabled_filters_with_config() {
         $this->setup_available_in_context_tests();