Skip to content

Commit

Permalink
MDL-66568 behat: Reset theme CSS between scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jun 16, 2020
1 parent 42197e9 commit c11fbc5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/behat/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,37 @@ public static function reset_all_data() {
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.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ public function before_scenario(BeforeScenarioScope $scope) {
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);
Expand Down

0 comments on commit c11fbc5

Please sign in to comment.