From 999bb6578b7f3ad7e2873a57c49116ce5279f4a3 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Tue, 26 Nov 2019 16:24:59 +0800 Subject: [PATCH 1/2] MDL-67364 output: Allow child themes to use parent editor_scss config This still allows child themes to override editor_scss, but will fall back to what the parent is using if they do not specify it. This negates the need for every child them to have the config duplicated in order to utilise it. --- lib/outputlib.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index bb36d5db81b3c..d3648dddc40bc 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -961,12 +961,31 @@ public function editor_css_files() { */ public function editor_scss_to_css() { $css = ''; + $dir = $this->dir; + $filenames = []; + // Use editor_scss file(s) provided by this theme if set. if (!empty($this->editor_scss)) { + $filenames = $this->editor_scss; + } else { + // If no editor_scss set, move up theme hierarchy until one is found (if at all). + // This is so child themes only need to set editor_scss if an override is required. + foreach (array_reverse($this->parent_configs) as $parentconfig) { + if (!empty($parentconfig->editor_scss)) { + $dir = $parentconfig->dir; + $filenames = $parentconfig->editor_scss; + + // Config found, stop looking. + break; + } + } + } + + if (!empty($filenames)) { $compiler = new core_scss(); - foreach ($this->editor_scss as $filename) { - $compiler->set_file("{$this->dir}/scss/{$filename}.scss"); + foreach ($filenames as $filename) { + $compiler->set_file("{$dir}/scss/{$filename}.scss"); try { $css .= $compiler->to_css(); From 2359ec14f75df072b0561c5839c90a8ef16fd153 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Thu, 13 Feb 2020 17:20:32 +0800 Subject: [PATCH 2/2] MDL-67364 output: Add unit testing for editor_scss_to_css --- lib/tests/theme_config_test.php | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/tests/theme_config_test.php b/lib/tests/theme_config_test.php index 85945c41cd014..35f3df73c63ad 100644 --- a/lib/tests/theme_config_test.php +++ b/lib/tests/theme_config_test.php @@ -175,4 +175,42 @@ public function test_editor_css_url_has_revision_and_subrevision() { $this->assertRegExp("/{$themerevision}_{$themesubrevision}/", $url->out(false)); } + + /** + * Confirm that editor_scss_to_css is correctly compiling for themes with no parent. + */ + public function test_editor_scss_to_css_root_theme() { + global $CFG; + + $this->resetAfterTest(); + $theme = theme_config::load('boost'); + $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss'; + + $this->assertTrue(file_exists($editorscss)); + $compiler = new core_scss(); + $compiler->set_file($editorscss); + $cssexpected = $compiler->to_css(); + $cssactual = $theme->editor_scss_to_css(); + + $this->assertEquals($cssexpected, $cssactual); + } + + /** + * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS. + */ + public function test_editor_scss_to_css_child_theme() { + global $CFG; + + $this->resetAfterTest(); + $theme = theme_config::load('classic'); + $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss'; + + $this->assertTrue(file_exists($editorscss)); + $compiler = new core_scss(); + $compiler->set_file($editorscss); + $cssexpected = $compiler->to_css(); + $cssactual = $theme->editor_scss_to_css(); + + $this->assertEquals($cssexpected, $cssactual); + } }