From e7d006c77212e4d02d265d1a7f6cad7c3c6b7ee5 Mon Sep 17 00:00:00 2001 From: Markus Heck Date: Mon, 29 Apr 2024 23:17:05 +0200 Subject: [PATCH] add behat tests for index page --- tests/behat/behat_mod_adleradaptivity.php | 77 +++++++++++-------- ..._all_adaptivity_elements_in_course.feature | 54 +++++++++++++ 2 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 tests/behat/list_all_adaptivity_elements_in_course.feature diff --git a/tests/behat/behat_mod_adleradaptivity.php b/tests/behat/behat_mod_adleradaptivity.php index 1ac6972..028b323 100644 --- a/tests/behat/behat_mod_adleradaptivity.php +++ b/tests/behat/behat_mod_adleradaptivity.php @@ -50,11 +50,20 @@ protected function resolve_page_instance_url(string $type, string $identifier): '/mod/adleradaptivity/view.php', ['id' => $this->get_cm_by_adleradaptivity_name($identifier)->id] ); + case 'index': + return new moodle_url('/mod/adleradaptivity/index.php', + ['id' => $this->get_course_by_course_name($identifier)->id] + ); default: throw new Exception('Unrecognised quiz page type "' . $type . '."'); } } + protected function get_course_by_course_name(string $course_name) { + global $DB; + return $DB->get_record('course', ['fullname' => $course_name], '*', MUST_EXIST); + } + /** * Get an adleradaptivity instance by name. * @@ -67,7 +76,7 @@ protected function get_adleradaptivity_by_name(string $name): stdClass { } /** - * Get a quiz cm from the quiz name. + * Get an adleradaptivity cm from the adleradaptivity name. * * @param string $name quiz name. * @return stdClass cm from get_coursemodule_from_instance. @@ -77,11 +86,42 @@ protected function get_cm_by_adleradaptivity_name(string $name): stdClass { return get_coursemodule_from_instance('adleradaptivity', $quiz->id, $quiz->course); } + /** + * Checks, that current page PATH matches regular expression + * Example: Then the url should match "superman is dead" + * Example: Then the uri should match "log in" + * Example: And the url should match "log in" + * + * Taken from MinkExtension + * + * @Then /^the (?i)url(?-i) should match (?P"(?:[^"]|\\")*")$/ + */ + public function assertUrlRegExp($pattern) + { + $this->assertSession()->addressMatches($this->fixStepArgument($pattern)); + } + + /** + * Returns fixed step argument (with \\" replaced back to ") + * + * Taken from MinkExtension + * + * @param string $argument + * + * @return string + */ + protected function fixStepArgument($argument) + { + return str_replace('\\"', '"', $argument); + } + /** * Checks, that element with specified CSS exists on page * Example: Then I should see a "body" element * Example: And I should see a "body" element * + * Taken from MinkExtension + * * @Then /^(?:|I )should see an? "(?P[^"]*)" element$/ */ public function assertElementOnPage($element) { @@ -92,6 +132,8 @@ public function assertElementOnPage($element) { * Checks, that element with specified CSS does not exist on page * Example: Then I should not see a "body" element * + * Taken from MinkExtension + * * @Then /^(?:|I )should not see an? "(?P[^"]*)" element$/ */ public function assertElementNotOnPage($element) { @@ -108,19 +150,6 @@ public function assertNumElementsOnPage($num, $element) { $this->assertSession()->elementsCount('css', $element, $num); } - /** - * Submits a given question with a specified outcome (correctness). - * - * @param string $questionname the name of the question to submit. - * @param string $outcome the outcome of the question. Allowed values are 'correct' and 'incorrect'. - * - * @When /^I submit question "([^"]*)" with an? "([^"]*)" answer$/ - */ - public function i_submit_question_with_an_answer(string $questionname, string $outcome) { -// TODO implement - - } - /** * Create question usages (attempts) for the specified adleradaptivity and user. * The first row has to be column names: @@ -177,26 +206,6 @@ public function user_has_attempted_with_results(string $username, string $adlera question_engine::save_questions_usage_by_activity($quba); // Update completion state answer_questions::update_module_completion($module); - - - return; -// ---------------- - - - $adleradaptivity_cm = $this->get_cm_by_adleradaptivity_name($adleradaptivityname); - $user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST); - - $adleradaptivity_generator = behat_util::get_data_generator()->get_plugin_generator('mod_adleradaptivity'); - - foreach ($data->getHash() as $questiondata) { - $question = $DB->get_record('question', ['name' => $questiondata['question_name']], '*', MUST_EXIST); - $adleradaptivity_question = $DB->get_record('adleradaptivity_questions', ['questionid' => $question->id], '*', MUST_EXIST); - - $adleradaptivity_generator->create_mod_adleradaptivity_attempt($adleradaptivity_cm->instance, $user->id, [ - 'questionid' => $adleradaptivity_question->id, - 'correct' => $questiondata['correct'] === 'yes', - ]); - } } /** diff --git a/tests/behat/list_all_adaptivity_elements_in_course.feature b/tests/behat/list_all_adaptivity_elements_in_course.feature new file mode 100644 index 0000000..add62fd --- /dev/null +++ b/tests/behat/list_all_adaptivity_elements_in_course.feature @@ -0,0 +1,54 @@ +@mod @mod_adleradaptivity +Feature: View adleradaptivity index page + As a student + In order to see all adleradaptivity activities + I need to be able to view the index page + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student | Student | One | student@example.com | + And the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And the following "course enrolments" exist: + | user | course | role | + | student | C1 | student | + + Scenario: View the index page with two adleradaptivity activities + Given the following "activities" exist: + | activity | name | intro | course | completion | + | adleradaptivity | Adler Activity 1 | Adler Activity 1 Intro | C1 | 2 | + | adleradaptivity | Adler Activity 2 | Adler Activity 2 Intro | C1 | 2 | + When I am on the "Course 1" "mod_adleradaptivity > Index" page logged in as "student" + Then I should see "Adler Activity 1" + And I should see "Adler Activity 2" + And I should see "Course 1" + And I should see "2" ".generaltable tbody tr" elements + + Scenario: Check if the links for each activity correctly redirect to the respective activity page; test 1 + Given the following "activities" exist: + | activity | name | intro | course | completion | + | adleradaptivity | Adler Activity 1 | Adler Activity 1 Intro | C1 | 2 | + | adleradaptivity | Adler Activity 2 | Adler Activity 2 Intro | C1 | 2 | + When I am on the "Course 1" "mod_adleradaptivity > Index" page logged in as "student" + And I click on "Adler Activity 1" "link" in the ".generaltable" "css_element" + Then the url should match "mod/adleradaptivity/view.php" + And I should see "Adler Activity 1" + And I should see "0" ".errormessage" elements + + Scenario: Check if the links for each activity correctly redirect to the respective activity page; test 2 + Given the following "activities" exist: + | activity | name | intro | course | completion | + | adleradaptivity | Adler Activity 1 | Adler Activity 1 Intro | C1 | 2 | + | adleradaptivity | Adler Activity 2 | Adler Activity 2 Intro | C1 | 2 | + When I am on the "Course 1" "mod_adleradaptivity > Index" page logged in as "student" + And I click on "Adler Activity 2" "link" in the ".generaltable" "css_element" + Then the url should match "mod/adleradaptivity/view.php" + And I should see "Adler Activity 2" + And I should see "0" ".errormessage" elements + + Scenario: Scenario: Check if the page displays correctly when there are no mod_adleradaptivity activities in the course + When I am on the "Course 1" "mod_adleradaptivity > Index" page logged in as "student" + Then I should not see ".generaltable" + And the url should match "mod/adleradaptivity/index.php"