From bfedbc37c53af77dfdf35709d5ced77512f0e533 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 26 Mar 2021 17:49:34 +0800 Subject: [PATCH] MDL-70821 completion: New Behat steps for activity completion * activity_completion_condition_displayed_as() - Shows the status of an automatic completion condition for a given activity * activity_should_have_the_completion_condition() - Checks that the activity displays the given completion condition * manual_completion_button_displayed_as() - Shows the status of the manual completion button for a given activity * the_manual_completion_button_for_activity_should_be_disabled() - Checks that the manual completion button for the given activity is disabled. * there_should_be_no_completion_for_activity() - Checks that the activity does not show completion information * toggle_the_manual_completion_state() - Toggles the manual completion button's state for a given activity * overridden_manual_completion_button_displayed_as() - Checks the manual completion state of an activity. * overridden_activity_completion_condition_displayed_as() - Checks if the activity with specified name is maked as complete. --- completion/tests/behat/behat_completion.php | 156 ++++++++++++++++++++ completion/upgrade.txt | 19 +++ 2 files changed, 175 insertions(+) diff --git a/completion/tests/behat/behat_completion.php b/completion/tests/behat/behat_completion.php index 893b79fdc78..7ebe22a9a37 100644 --- a/completion/tests/behat/behat_completion.php +++ b/completion/tests/behat/behat_completion.php @@ -153,6 +153,162 @@ class behat_completion extends behat_base { ); } + /** + * Checks if the activity with specified name is maked as complete. + * + * @Given /^the "(?P(?:[^"]|\\")*)" completion condition of "(?P(?:[^"]|\\")*)" is displayed as "(?P(?:[^"]|\\")*)"$/ + * @param string $conditionname The completion condition text. + * @param string $activityname The activity name. + * @param string $completionstatus The completion status. Must be either of the following: 'todo', 'done', 'failed'. + */ + public function activity_completion_condition_displayed_as(string $conditionname, string $activityname, + string $completionstatus): void { + + if (!in_array($completionstatus, ['todo', 'done', 'failed'])) { + throw new coding_exception('Invalid completion status. It must be of type "todo", "done", or "failed".'); + } + + $text = get_string("completion_automatic:$completionstatus", 'core_course') . ' ' . $conditionname; + + $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname); + $selector = "div[aria-label='$conditionslistlabel']"; + + $this->execute("behat_general::assert_element_contains_text", [$text, $selector, "css_element"]); + } + + /** + * Checks if the activity with specified name is maked as complete. + * + * @Given /^the "(?P(?:[^"]|\\")*)" completion condition of "(?P(?:[^"]|\\")*)" overridden by "(?P(?:[^"]|\\")*)" is displayed as "(?P(?:[^"]|\\")*)"$/ + * @param string $conditionname The completion condition text. + * @param string $activityname The activity name. + * @param string $username The full name of the user overriding the student's activity completion. + * @param string $completionstatus The override completion status. Must be either of the following: 'todo', 'done'. + */ + public function overridden_activity_completion_condition_displayed_as(string $conditionname, string $activityname, + string $username, string $completionstatus): void { + if (!in_array($completionstatus, ['todo', 'done'])) { + throw new coding_exception('Invalid override completion status. It must be of type "todo" or "done".'); + } + + $conditionlabel = get_string('completion_setby:auto:' . $completionstatus, 'core_course', (object)[ + 'condition' => $conditionname, + 'setby' => $username, + ]); + $conditionbadge = "span[aria-label='$conditionlabel']"; + + $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname); + $completionconditions = "div[aria-label='$conditionslistlabel']"; + + $params = [$conditionbadge, 'css_element', $completionconditions, 'css_element']; + $this->execute("behat_general::should_exist_in_the", $params); + } + + /** + * Checks the manual completion state of an activity. + * + * @Given /^the manual completion button of "(?P(?:[^"]|\\")*)" is displayed as "(?P(?:[^"]|\\")*)"$/ + * @param string $activityname The activity name. + * @param string $completionstatus The completion status shown on the manual completion button. + * Must be either 'Mark as done' or 'Done'. + */ + public function manual_completion_button_displayed_as(string $activityname, string $completionstatus): void { + if (!in_array($completionstatus, ['Mark as done', 'Done'])) { + throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".'); + } + + $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone'; + $conditionslistlabel = get_string('completion_manual:aria:' . $langstringkey, 'core_course', $activityname); + $selector = "button[aria-label='$conditionslistlabel']"; + + $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]); + } + + /** + * Checks the manual completion state of an activity. + * + * @Given /^the manual completion button of "(?P(?:[^"]|\\")*)" overridden by "(?P(?:[^"]|\\")*)" is displayed as "(?P(?:[^"]|\\")*)"$/ + * @param string $activityname The activity name. + * @param string $username The full name of the user overriding the student's activity completion. + * @param string $completionstatus The completion status shown on the manual completion button. + * Must be either 'Mark as done' or 'Done'. + */ + public function overridden_manual_completion_button_displayed_as(string $activityname, string $username, + string $completionstatus): void { + if (!in_array($completionstatus, ['Mark as done', 'Done'])) { + throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".'); + } + + $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone'; + $conditionslistlabel = get_string('completion_setby:manual:' . $langstringkey, 'core_course', (object)[ + 'activityname' => $activityname, + 'setby' => $username, + ]); + $selector = "button[aria-label='$conditionslistlabel']"; + + $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]); + } + + /** + * Toggles the manual completion button for a given activity. + * + * @Given /^I toggle the manual completion state of "(?P(?:[^"]|\\")*)"$/ + * @param string $activityname The activity name. + */ + public function toggle_the_manual_completion_state(string $activityname): void { + $selector = "button[data-action=toggle-manual-completion][data-activityname='{$activityname}']"; + + $this->execute("behat_general::i_click_on", [$selector, "css_element"]); + } + + /** + * Check that the activity does show completion information. + * + * @Given /^there should be no completion information shown for "(?P(?:[^"]|\\")*)"$/ + * @param string $activityname The activity name. + */ + public function there_should_be_no_completion_for_activity(string $activityname): void { + $containerselector = "div[data-region=activity-information][data-activityname='$activityname']"; + try { + $this->find('css_element', $containerselector); + } catch (ElementNotFoundException $e) { + // If activity information container does not exist (activity dates not shown, completion info not shown), all good. + return; + } + + // Otherwise, ensure that the completion information does not exist. + $elementselector = "div[data-region=completion-info]"; + $params = [$elementselector, "css_element", $containerselector, "css_element"]; + $this->execute("behat_general::should_not_exist_in_the", $params); + } + + /** + * Check that the manual completion button for the activity is disabled. + * + * @Given /^the manual completion button for "(?P(?:[^"]|\\")*)" should be disabled$/ + * @param string $activityname The activity name. + */ + public function the_manual_completion_button_for_activity_should_be_disabled(string $activityname): void { + $selector = "div[data-activityname='$activityname'] button"; + + $params = [$selector, "css_element"]; + $this->execute("behat_general::the_element_should_be_disabled", $params); + } + + /** + * Check that the activity has the given automatic completion condition. + * + * @Given /^"(?P(?:[^"]|\\")*)" should have the "(?P(?:[^"]|\\")*)" completion condition$/ + * @param string $activityname The activity name. + * @param string $conditionname The automatic condition name. + */ + public function activity_should_have_the_completion_condition(string $activityname, string $conditionname): void { + $containerselector = "div[data-region=activity-information][data-activityname='$activityname']"; + + $params = [$conditionname, $containerselector, 'css_element']; + $this->execute("behat_general::assert_element_contains_text", $params); + } + /** * Checks if the activity with specified name shows a information completion checkbox (i.e. showing the completion tracking * configuration). diff --git a/completion/upgrade.txt b/completion/upgrade.txt index dc0dbed677f..f9657dd77fa 100644 --- a/completion/upgrade.txt +++ b/completion/upgrade.txt @@ -1,6 +1,25 @@ This files describes API changes in /completion/* - completion, information provided here is intended especially for developers. +=== 3.11 === +* New Behat steps for activity completion in the behat_completion class: + - activity_completion_condition_displayed_as() + - Given the "" completion condition of "" is displayed as "" + - activity_should_have_the_completion_condition() + - Given "" should have the "" completion condition + - manual_completion_button_displayed_as() + - Given the manual completion button of "" is displayed as "" + - the_manual_completion_button_for_activity_should_be_disabled() + - Given the manual completion button for "" should be disabled + - there_should_be_no_completion_for_activity() + - Given there should be no completion information shown for "" + - toggle_the_manual_completion_state() + - Given I toggle the manual completion state of "" + - overridden_manual_completion_button_displayed_as + - Given the manual completion button of "" overridden by "" is displayed as "" + - overridden_activity_completion_condition_displayed_as + - Given the "" completion condition of "" overridden by "" is displayed as "" + === 3.7 === * External function core_completion_external::get_activities_completion_status new returns the following additional field: - valueused (indicates whether the completion state affects the availability of other content)