diff --git a/lib/behat/classes/util.php b/lib/behat/classes/util.php index fda4f1c1321..b24ea485065 100644 --- a/lib/behat/classes/util.php +++ b/lib/behat/classes/util.php @@ -427,6 +427,37 @@ class behat_util extends testing_util { initialise_cfg(); } + /** + * Restore theme CSS stored during behat setup. + */ + public static function restore_saved_themes(): void { + global $CFG; + + $themerev = theme_get_revision(); + + $framework = self::get_framework(); + $storageroot = self::get_dataroot() . "/{$framework}/themedata"; + $themenames = array_keys(\core_component::get_plugin_list('theme')); + $directions = ['ltr', 'rtl']; + + $themeconfigs = array_map(function($themename) { + return \theme_config::load($themename); + }, $themenames); + + foreach ($themeconfigs as $themeconfig) { + $themename = $themeconfig->name; + $themesubrev = theme_get_sub_revision_for_theme($themename); + + $dirname = "{$storageroot}/{$themename}"; + foreach ($directions as $direction) { + $cssfile = "{$dirname}/{$direction}.css"; + if (file_exists($cssfile)) { + $themeconfig->set_css_content_cache(file_get_contents($cssfile)); + } + } + } + } + /** * Pause execution immediately. * diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index c7b53df6b46..115765a97f0 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -367,6 +367,16 @@ class behat_hooks extends behat_base { behat_util::reset_all_data(); error_reporting($errorlevel); + if ($this->running_javascript()) { + // Fetch the user agent. + // This isused to choose between the SVG/Non-SVG versions of themes. + $useragent = $this->getSession()->evaluateScript('return navigator.userAgent;'); + \core_useragent::instance(true, $useragent); + + // Restore the saved themes. + behat_util::restore_saved_themes(); + } + // Assign valid data to admin user (some generator-related code needs a valid user). $user = $DB->get_record('user', array('username' => 'admin')); \core\session\manager::set_user($user);