MDL-60815 theme: fix loading editor styles
authorRyan Wyllie <ryan@moodle.com>
Thu, 30 Nov 2017 05:49:45 +0000 (05:49 +0000)
committerRyan Wyllie <ryan@moodle.com>
Tue, 16 Jan 2018 03:39:09 +0000 (03:39 +0000)
lib/outputlib.php
lib/tests/theme_config_test.php
theme/styles.php

index 7a8beff..e5a8514 100644 (file)
@@ -897,6 +897,14 @@ class theme_config {
         global $CFG;
         $rev = theme_get_revision();
         if ($rev > -1) {
+            $themesubrevision = theme_get_sub_revision_for_theme($this->name);
+
+            // Provide the sub revision to allow us to invalidate cached theme CSS
+            // on a per theme basis, rather than globally.
+            if ($themesubrevision && $themesubrevision > 0) {
+                $rev .= "_{$themesubrevision}";
+            }
+
             $url = new moodle_url("/theme/styles.php");
             if (!empty($CFG->slasharguments)) {
                 $url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true);
index 99ab6a9..85945c4 100644 (file)
@@ -153,4 +153,26 @@ class core_theme_config_testcase extends advanced_testcase {
         $this->assertTrue(core_useragent::set_user_device_type('tablet'));
         $this->assertTrue(core_useragent::set_user_device_type('featurephone'));
     }
+
+    /**
+     * Confirm that the editor_css_url contains the theme revision and the
+     * theme subrevision if not in theme designer mode.
+     */
+    public function test_editor_css_url_has_revision_and_subrevision() {
+        global $CFG;
+
+        $this->resetAfterTest();
+        $theme = theme_config::load(theme_config::DEFAULT_THEME);
+        $themename = $theme->name;
+        $themerevision = 1234;
+        $themesubrevision = 5678;
+
+        $CFG->themedesignermode = false;
+        $CFG->themerev = $themerevision;
+
+        theme_set_sub_revision_for_theme($themename, $themesubrevision);
+        $url = $theme->editor_css_url();
+
+        $this->assertRegExp("/{$themerevision}_{$themesubrevision}/", $url->out(false));
+    }
 }
index 4977095..6a4aa9b 100644 (file)
@@ -69,11 +69,7 @@ $values = explode('_', $rev);
 $rev = min_clean_param(array_shift($values), 'INT');
 $themesubrev = array_shift($values);
 
-if (is_null($themesubrev)) {
-    // Default to the current theme subrevision if one isn't
-    // provided in the URL.
-    $themesubrev = theme_get_sub_revision_for_theme($themename);
-} else {
+if (!is_null($themesubrev)) {
     $themesubrev = min_clean_param($themesubrev, 'INT');
 }
 
@@ -144,12 +140,12 @@ make_localcache_directory('theme', false);
 
 if ($type === 'editor') {
     $csscontent = $theme->get_css_content_editor();
-    css_store_css($theme, $candidatesheet, $csscontent, false);
 
     if ($cache) {
+        css_store_css($theme, $candidatesheet, $csscontent, false);
         css_send_cached_css($candidatesheet, $etag);
     } else {
-        css_send_uncached_css(file_get_contents($candidatesheet));
+        css_send_uncached_css($csscontent);
     }
 
 }