Skip to content

Commit

Permalink
add behat tests for index page
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Apr 29, 2024
1 parent 35f7284 commit e7d006c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 34 deletions.
77 changes: 43 additions & 34 deletions tests/behat/behat_mod_adleradaptivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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<pattern>"(?:[^"]|\\")*")$/
*/
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>[^"]*)" element$/
*/
public function assertElementOnPage($element) {
Expand All @@ -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>[^"]*)" element$/
*/
public function assertElementNotOnPage($element) {
Expand All @@ -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:
Expand Down Expand Up @@ -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',
]);
}
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/behat/list_all_adaptivity_elements_in_course.feature
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e7d006c

Please sign in to comment.