Skip to content

Commit

Permalink
MDL-82212 behat: Display error when deprecated icons are found
Browse files Browse the repository at this point in the history
A check for deprecated icons has been integrated into Behat tests.
This check can be disabled by adding the --icon-deprecations flag to
the Behat initialization command.
  • Loading branch information
sarjona committed Aug 12, 2024
1 parent 39cf705 commit a8055d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
6 changes: 5 additions & 1 deletion admin/tool/behat/cli/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'composer-upgrade' => true,
'composer-self-update' => true,
'scss-deprecations' => false,
'icon-deprecations' => true,
),
array(
'j' => 'parallel',
Expand All @@ -70,7 +71,8 @@
Usage:
php init.php [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]]
[--no-axe] [--scss-deprecations] [-o | --optimize-runs] [-a | --add-core-features-to-theme]
[--no-axe] [--scss-deprecations] [--icon-deprecations] [-o | --optimize-runs]
[-a | --add-core-features-to-theme]
[--no-composer-self-update] [--no-composer-upgrade]
[--disable-composer]
[--help]
Expand All @@ -82,6 +84,7 @@
--torun Execute run till (Used for parallel runs on different vms)
--no-axe Disable axe accessibility tests.
--scss-deprecations Enable SCSS deprecation checks.
--icon-deprecations Enable icon deprecation checks.
-o, --optimize-runs
Split features with specified tags in all parallel runs.
Expand Down Expand Up @@ -134,6 +137,7 @@
'add-core-features-to-theme',
'axe',
'scss-deprecations',
'icon-deprecations',
];

foreach ($cmdoptionsforsinglerun as $option) {
Expand Down
4 changes: 3 additions & 1 deletion admin/tool/behat/cli/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'add-core-features-to-theme' => false,
'axe' => true,
'scss-deprecations' => false,
'icon-deprecations' => true,
),
array(
'h' => 'help',
Expand All @@ -74,7 +75,7 @@
Behat utilities to manage the test environment
Usage:
php util.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--no-axe|--scss-deprecations|--help]
php util.php [--install|--drop|--enable|--disable|--diag|--updatesteps|--no-axe|--scss-deprecations|--icon-deprecations|--help]
[--parallel=value [--maxruns=value]]
Options:
Expand All @@ -86,6 +87,7 @@
--updatesteps Update feature step file.
--no-axe Disable axe accessibility tests.
--scss-deprecations Enable SCSS deprecation checks.
--icon-deprecations Enable icon deprecation checks.
-j, --parallel Number of parallel behat run operation
-m, --maxruns Max parallel processes to be executed at one time.
Expand Down
5 changes: 5 additions & 0 deletions admin/tool/behat/cli/util_single_run.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'add-core-features-to-theme' => false,
'axe' => true,
'scss-deprecations' => false,
'icon-deprecations' => true,
),
array(
'h' => 'help',
Expand Down Expand Up @@ -82,6 +83,7 @@
--updatesteps Update feature step file.
--no-axe Disable axe accessibility tests.
--scss-deprecations Enable SCSS deprecation checks.
--icon-deprecations Enable icon deprecation checks.
-o, --optimize-runs Split features with specified tags in all parallel runs.
-a, --add-core-features-to-theme Add all core features to specified theme's
Expand Down Expand Up @@ -194,6 +196,9 @@
// Define whether to run Behat with SCSS deprecation checks.
behat_config_manager::set_behat_run_config_value('scss-deprecations', $options['scss-deprecations']);

// Define whether to run Behat with icon deprecation checks.
behat_config_manager::set_behat_run_config_value('icon-deprecations', $options['icon-deprecations']);

// Enable test mode.
$timestart = microtime(true);
mtrace('Creating Behat configuration ...', '');
Expand Down
36 changes: 36 additions & 0 deletions lib/behat/classes/behat_session_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,39 @@ public function look_for_deprecated_styles() {
}
}


/**
* Internal step definition to find deprecated icons.
*
* Part of behat_hooks class as is part of the testing framework, is auto-executed
* after each step so no features will splicitly use it.
*
* @throws Exception Unknown type, depending on what we caught in the hook or basic \Exception.
* @see Moodle\BehatExtension\Tester\MoodleStepTester
*/
public function look_for_deprecated_icons() {
if (!behat_config_manager::get_behat_run_config_value('icon-deprecations')) {
return;
}

if (!$this->running_javascript()) {
return;
}

// Look for any DOM element with deprecated icon.
$js = <<<EOF
[...document.querySelectorAll('.icon.deprecated')].some(
deprecatedicon => true
);
EOF;
if ($this->evaluate_script($js)) {
throw new \Exception(html_entity_decode(
"Deprecated icon in use. Enable \$CFG->debugdisplay for detailed debugging information in the console",
ENT_COMPAT,
));
}
}

/**
* Converts HTML tags to line breaks to display the info in CLI
*
Expand Down Expand Up @@ -1075,6 +1108,9 @@ protected function execute($contextapi, $params = array()) {

// Look for deprecated styles.
$this->look_for_deprecated_styles();

// Look for deprecated icons.
$this->look_for_deprecated_icons();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/behat/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,13 @@ public static function get_site_info() {

$accessibility = empty(behat_config_manager::get_behat_run_config_value('axe')) ? 'No' : 'Yes';
$scssdeprecations = empty(behat_config_manager::get_behat_run_config_value('scss-deprecations')) ? 'No' : 'Yes';
$icondeprecations = empty(behat_config_manager::get_behat_run_config_value('icon-deprecations')) ? 'No' : 'Yes';

$siteinfo .= <<<EOF
Run optional tests:
- Accessibility: {$accessibility}
- SCSS deprecations: {$scssdeprecations}
- Icon deprecations: {$icondeprecations}
EOF;

Expand Down

0 comments on commit a8055d7

Please sign in to comment.