Skip to content

Commit

Permalink
Optimise Smart menu behat features for dynamic courses and rules (moo…
Browse files Browse the repository at this point in the history
…dle-an-hochschulen#765).

This performs several optimisations to make these tests a lot faster.

Creation of smart menus and menu item has been changed to use
generators, rather than filling the forms each time.

Manual navigation has been replaced with `I am on the 'identifier' 'type'
page` as much as possible.

The assertions being made have been optimised. Previously, every
assertion of what was in a menu was opening each menu location and
checking for each item. Given that there's no option to display
different items when a menu is in different location, a lot of this is
redunant, and actually opening each menu takes time.

I have optimised this so that one scenario does a check that the menu
opens and the items are actually visible. Then each scenario tests than
an expected item exists in each menu (without opening the menu) and
checks for subsequent items just check existance in a single menu. This
balances coverage of the different functionality with speed and
conciseness of tests.
  • Loading branch information
marxjohnson committed Nov 22, 2024
1 parent cbead11 commit 4d719ac
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 517 deletions.
8 changes: 2 additions & 6 deletions classes/form/smartmenu_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@ public function definition() {
}

// Add restriction as select element.
$rolecontext = [
smartmenu::BYADMIN_ALL => get_string('smartmenusbyadmin_all', 'theme_boost_union'),
smartmenu::BYADMIN_ADMINS => get_string('smartmenusbyadmin_admins', 'theme_boost_union'),
smartmenu::BYADMIN_NONADMINS => get_string('smartmenusbyadmin_nonadmins', 'theme_boost_union'),
];
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $rolecontext);
$byadminoptions = smartmenu::get_byadmin_options();
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $byadminoptions);
$mform->setDefault('byadmin', smartmenu::BYADMIN_ALL);
$mform->setType('byadmin', PARAM_INT);
$mform->addHelpButton('byadmin', 'smartmenusbyadmin', 'theme_boost_union');
Expand Down
8 changes: 2 additions & 6 deletions classes/form/smartmenu_item_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,8 @@ public function definition() {
}

// Add restriction as select element.
$rolecontext = [
smartmenu::BYADMIN_ALL => get_string('smartmenusbyadmin_all', 'theme_boost_union'),
smartmenu::BYADMIN_ADMINS => get_string('smartmenusbyadmin_admins', 'theme_boost_union'),
smartmenu::BYADMIN_NONADMINS => get_string('smartmenusbyadmin_nonadmins', 'theme_boost_union'),
];
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $rolecontext);
$byadminoptions = smartmenu::get_byadmin_options();
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $byadminoptions);
$mform->setDefault('byadmin', smartmenu::BYADMIN_ALL);
$mform->setType('byadmin', PARAM_INT);
$mform->addHelpButton('byadmin', 'smartmenusbyadmin', 'theme_boost_union');
Expand Down
13 changes: 13 additions & 0 deletions classes/smartmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,19 @@ public static function get_rolecontext_options(): array {
];
}

/**
* Return options for the byadmin setting.
*
* @return array
*/
public static function get_byadmin_options(): array {
return [
smartmenu::BYADMIN_ALL => get_string('smartmenusbyadmin_all', 'theme_boost_union'),
smartmenu::BYADMIN_ADMINS => get_string('smartmenusbyadmin_admins', 'theme_boost_union'),
smartmenu::BYADMIN_NONADMINS => get_string('smartmenusbyadmin_nonadmins', 'theme_boost_union'),
];
}

/**
* Return options for the operator setting.
*
Expand Down

Large diffs are not rendered by default.

431 changes: 192 additions & 239 deletions tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/generator/behat_theme_boost_union_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function get_creatable_entities(): array {
'cohorts' => 'cohorts',
'operator' => 'operator',
'languages' => 'languages',
'byadmin' => 'byadmin',
],
],

Expand All @@ -92,6 +93,7 @@ protected function get_creatable_entities(): array {
'cohorts' => 'cohorts',
'operator' => 'operator',
'languages' => 'languages',
'byadmin' => 'byadmin',
],
],
];
Expand Down Expand Up @@ -277,6 +279,17 @@ protected function get_operator_id(string $operator): int {
return $this->option_id('operator', $operator, smartmenu::get_operator_options());
}

/**
* Return the ID for the given byadmin setting.
*
* @param string $byadmin
* @return int
* @throws Exception
*/
protected function get_byadmin_id(string $byadmin): int {
return $this->option_id('byadmin', $byadmin, smartmenu::get_byadmin_options());
}

/**
* Given a comma-separated list of language short codes, return an array of each trimmed value.
*
Expand Down
98 changes: 24 additions & 74 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,26 @@ public function create_smartmenu(array $data): \stdClass {
if (empty($location)) {
throw new Exception('A Smart menu location must be specified.');
}
$validlocations = [
smartmenu::LOCATION_MAIN,
smartmenu::LOCATION_MENU,
smartmenu::LOCATION_BOTTOM,
smartmenu::LOCATION_USER,
];
$validlocations = array_keys(smartmenu::get_locations());
if (!empty(array_diff($validlocations, $location))) {
throw new Exception('Invalid Smart menu location.');
}
$validdescriptions = [
smartmenu::DESC_NEVER,
smartmenu::DESC_ABOVE,
smartmenu::DESC_BELOW,
smartmenu::DESC_HELP,
];
$validdescriptions = array_keys(smartmenu::get_showdescription_options());
$showdescription = $data['showdescription'] ?? smartmenu::DESC_NEVER;
if (!in_array($showdescription, $validdescriptions)) {
throw new Exception('Invalid showdescription.');
}
$validtypes = [
smartmenu::TYPE_LIST,
smartmenu::TYPE_CARD,
];
$validtypes = array_keys(smartmenu::get_types());
$type = $data['type'] ?? smartmenu::TYPE_LIST;
if (!in_array($type, $validtypes)) {
throw new Exception('Invalid showdescription.');
}
$validmodes = [
smartmenu::MODE_SUBMENU,
smartmenu::MODE_INLINE,
];
$validmodes = array_keys(smartmenu::get_mode_options());
$mode = $data['mode'] ?? smartmenu::MODE_SUBMENU;
if (!in_array($mode, $validmodes)) {
throw new Exception('Invalid mode.');
}
$validbehaviours = [
smartmenu::MOREMENU_DONOTCHANGE,
smartmenu::MOREMENU_INTO,
smartmenu::MOREMENU_OUTSIDE,
];
$validbehaviours = array_keys(smartmenu::get_moremenu_options());
$moremenubehaviour = $data['moremenubehaviour'] ?? smartmenu::MOREMENU_DONOTCHANGE;
if (!in_array($moremenubehaviour, $validbehaviours)) {
throw new Exception('Invalid moremenubehaviour.');
Expand All @@ -90,30 +70,17 @@ public function create_smartmenu(array $data): \stdClass {
$cardform = null;
$cardoverflowbehaviour = null;
if ($type === smartmenu::TYPE_CARD) {
$validcardsizes = [
smartmenu::CARDSIZE_TINY,
smartmenu::CARDSIZE_SMALL,
smartmenu::CARDSIZE_MEDIUM,
smartmenu::CARDSIZE_LARGE,
];
$validcardsizes = array_keys(smartmenu::get_cardsize_options());
$cardsize = $data['cardsize'] ?? smartmenu::CARDSIZE_SMALL;
if (!in_array($cardsize, $validcardsizes)) {
throw new Exception('Invalid cardsize.');
}
$validcardforms = [
smartmenu::CARDFORM_SQUARE,
smartmenu::CARDFORM_PORTRAIT,
smartmenu::CARDFORM_LANDSCAPE,
smartmenu::CARDFORM_FULLWIDTH,
];
$validcardforms = array_keys(smartmenu::get_cardform_options());
$cardform = $data['cardform'] ?? smartmenu::CARDFORM_SQUARE;
if (!in_array($cardform, $validcardforms)) {
throw new Exception('Invalid cardform.');
}
$validbehaviours = [
smartmenu::CARDOVERFLOWBEHAVIOUR_NOWRAP,
smartmenu::CARDOVERFLOWBEHAVIOUR_WRAP,
];
$validbehaviours = array_keys(smartmenu::get_cardoverflowbehaviour_options());
$cardoverflowbehaviour = strtolower($data['cardoverflowbehaviour']) ?? smartmenu::CARDOVERFLOWBEHAVIOUR_NOWRAP;
if (!in_array($cardoverflowbehaviour, $validbehaviours)) {
throw new Exception('Invalid cardoverflowbehaviour.');
Expand All @@ -124,6 +91,7 @@ public function create_smartmenu(array $data): \stdClass {
$rolecontext,
$cohorts,
$operator,
$byadmin,
$languages,
$startdate,
$enddate,
Expand All @@ -148,6 +116,7 @@ public function create_smartmenu(array $data): \stdClass {
'rolecontext' => $rolecontext,
'cohorts' => $cohorts,
'operator' => $operator,
'byadmin' => $byadmin,
'languages' => $languages,
'start_date' => $startdate,
'end_date' => $enddate,
Expand All @@ -174,11 +143,7 @@ public function create_smartmenu_item(array $data): \stdClass {

$sortorder = $data['sortorder'] ?? $DB->count_records('theme_boost_union_menus') + 1;

$validtypes = [
smartmenu_item::TYPESTATIC,
smartmenu_item::TYPEHEADING,
smartmenu_item::TYPEDYNAMIC,
];
$validtypes = array_keys(smartmenu_item::get_types());
$type = $data['type'] ?? smartmenu_item::TYPESTATIC;
if (!in_array($type, $validtypes)) {
throw new Exception('Invalid type.');
Expand All @@ -202,24 +167,12 @@ public function create_smartmenu_item(array $data): \stdClass {
$displayfield = null;
$textcount = null;
if ($type == smartmenu_item::TYPEDYNAMIC) {
$validsorts = [
smartmenu_item::LISTSORT_FULLNAME_ASC,
smartmenu_item::LISTSORT_FULLNAME_DESC,
smartmenu_item::LISTSORT_SHORTNAME_ASC,
smartmenu_item::LISTSORT_SHORTNAME_DESC,
smartmenu_item::LISTSORT_COURSEID_ASC,
smartmenu_item::LISTSORT_COURSEID_DESC,
smartmenu_item::LISTSORT_COURSEIDNUMBER_ASC,
smartmenu_item::LISTSORT_COURSEIDNUMBER_DESC,
];
$validsorts = array_keys(smartmenu_item::get_listsort_options());
$listsort = $data['listsort'] ?? smartmenu_item::LISTSORT_FULLNAME_ASC;
if (!in_array($listsort, $validsorts)) {
throw new Exception('Invalid listsort.');
}
$validdisplayfields = [
smartmenu_item::FIELD_FULLNAME,
smartmenu_item::FIELD_SHORTNAME,
];
$validdisplayfields = array_keys(smartmenu_item::get_displayfield_options());
$displayfield = $data['displayfield'] ?? smartmenu_item::FIELD_FULLNAME;
if (!in_array($displayfield, $validdisplayfields)) {
throw new Exception('Invalid displayfield.');
Expand All @@ -236,30 +189,19 @@ public function create_smartmenu_item(array $data): \stdClass {
throw new Exception('Invalid mode.');
}

$validdisplays = [
smartmenu_item::DISPLAY_SHOWTITLEICON,
smartmenu_item::DISPLAY_HIDETITLE,
smartmenu_item::DISPLAY_HIDETITLEMOBILE,
];
$validdisplays = array_keys(smartmenu_item::get_display_options());
$display = $data['display'] ?? smartmenu_item::DISPLAY_SHOWTITLEICON;
if (!in_array($display, $validdisplays)) {
throw new Exception('Invalid display.');
}

$validtargets = [
smartmenu_item::TARGET_SAME,
smartmenu_item::TARGET_NEW,
];
$validtargets = array_keys(smartmenu_item::get_target_options());
$target = $data['target'] ?? smartmenu_item::TARGET_SAME;
if (!in_array($target, $validtargets)) {
throw new Exception('Invalid target.');
}

$validtextpositions = [
smartmenu_item::POSITION_BELOW,
smartmenu_item::POSITION_OVERLAYTOP,
smartmenu_item::POSITION_OVERLAYBOTTOM,
];
$validtextpositions = array_keys(smartmenu_item::get_textposition_options());
$textposition = $data['textposition'] ?? smartmenu_item::POSITION_BELOW;
if (!in_array($textposition, $validtextpositions)) {
throw new Exception('Invalid text position.');
Expand All @@ -270,6 +212,7 @@ public function create_smartmenu_item(array $data): \stdClass {
$rolecontext,
$cohorts,
$operator,
$byadmin,
$languages,
$startdate,
$enddate,
Expand Down Expand Up @@ -304,6 +247,7 @@ public function create_smartmenu_item(array $data): \stdClass {
'mobile' => $data['mobile'] ?? 0,
'roles' => $roles,
'rolecontext' => $rolecontext,
'byadmin' => $byadmin,
'cohorts' => $cohorts,
'operator' => $operator,
'languages' => $languages,
Expand Down Expand Up @@ -341,6 +285,11 @@ protected function parse_restrictions(array $data): array {
throw new Exception('Invalid operator.');
}
}
$validbyadmins = array_keys(smartmenu::get_byadmin_options());
$byadmin = $data['byadmin'] ?? smartmenu::BYADMIN_ALL;
if (!in_array($byadmin, $validbyadmins)) {
throw new Exception('Invalid byadmin.');
}
$languages = $data['languages'] ?? [];
$startdate = $data['start_date'] ?? 0;
$enddate = $data['end_date'] ?? 0;
Expand All @@ -349,6 +298,7 @@ protected function parse_restrictions(array $data): array {
$rolecontext,
json_encode($cohorts),
$operator,
$byadmin,
json_encode($languages),
$startdate,
$enddate,
Expand Down

0 comments on commit 4d719ac

Please sign in to comment.