Skip to content

Commit

Permalink
behaviour test: multiple references to the same question
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed May 2, 2024
1 parent 7eb19f3 commit 9bd0799
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
15 changes: 0 additions & 15 deletions classes/local/db/adleradaptivity_question_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ public function get_adleradaptivity_question_by_question_bank_entries_id(int $qu
AND qr.usingcontextid = :usingcontextid
";

// TODO: this fails if there are multiple references to the same question (e.g. cloning a module)
// Multiple records found, only one record expected.
// Error code: multiplerecordsfound

// questions are loaded from question_usage. This questions are then tried to be matched to adleradaptivity_questions.
// This expects that there is only one question_reference per question.
//
// -> 1) verify question is only referenced once in the current module instance
// -> 2) additional condition in sql query to only get the question_reference for the current module instance
// -> 2b) load all questions via the modules database (instance -> tasks -> questions -> moodle questions) and verify it is in the question_usage (cross check both directions)

// Update: done 2
// TODO: test that tests this new behavior (for testing: remove `AND qr.usingcontextid = ?` and `, 'usingcontextid' => $module_context->id`
// TODO: 1

return $this->db->get_record_sql($sql, ['questionbankentryid' => $question_bank_entries_id, 'usingcontextid' => $module_context->id], MUST_EXIST);
}
}
43 changes: 35 additions & 8 deletions tests/behat/behat_mod_adleradaptivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public function adleradaptivity_contains_the_following_tasks(string $adleradapti

/**
* Create adler questions for the specified adleradaptivity task.
* If a question with the same name already exists it will be reused (resulting in multiple references to the same moodle question)
*
* The first row has to be column names:
* | task_title | question_category | question_name | difficulty | singlechoice | questiontext |
Expand Down Expand Up @@ -283,14 +284,18 @@ public function the_following_adleradaptivity_questions_are_added(TableNode $dat
'difficulty' => $questiondata['difficulty'] ?? 100,
]);

// create moodle question
$question = $adleradaptivity_generator->create_moodle_question(
$qcat->id,
$questiondata['singlechoice'] ?? false,
$questiondata['question_name'],
$questiondata['uuid'] ?? $questiondata['question_name'],
$questiondata['question_name'] ?? null
);
// load or create question by name
$question = $this->get_question_by_name($questiondata['question_name']);
if($question === null) {
// create moodle question
$question = $adleradaptivity_generator->create_moodle_question(
$qcat->id,
$questiondata['singlechoice'] ?? false,
$questiondata['question_name'],
$questiondata['uuid'] ?? $questiondata['question_name'],
$questiondata['question_name'] ?? null
);
}

// create question reference
$adleradaptivity_generator->create_question_reference(
Expand All @@ -302,6 +307,28 @@ public function the_following_adleradaptivity_questions_are_added(TableNode $dat
echo "Created question: ";
}

private function get_question_by_name(string $question_name) {
global $DB;

$question_id = $DB->get_field('question', 'id', ['name' => $question_name]);

// If the question id is not found, return null.
if ($question_id === false) {
return null;
}

// Load the question data.
$question_data = question_bank::load_question_data((int)$question_id);

// If the question data is empty, return null.
if (empty($question_data)) {
return null;
}

// Return the first (and only) question object.
return $question_data;
}

// ATM not required by this plugin
// /**
// * Return a list of the exact named selectors for the component.
Expand Down
17 changes: 15 additions & 2 deletions tests/behat/view_module.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Feature: View an adleradaptivity
Scenario: Display module without any attempts
When I am on the "Adler Activity 1" "mod_adleradaptivity > View" page logged in as "student"
Then I should see a ".behat_module-failure" element
# todo: implement the class stuff for tasks (get_task_status_message_translation_key)
And I should see "1" ".behat_task-not-attempted" element
And I should see "1" ".behat_task-optional-not-attempted" element
And I should not see ".behat_question-status-success"
Expand All @@ -51,7 +50,6 @@ Feature: View an adleradaptivity
And I should see "1" ".behat_task-incorrect" element
And I should see "1" ".behat_task-optional-incorrect" element

@javascript
Scenario: Display attempt sufficient to complete the module
Given user "student" has attempted "Adler Activity 1" with results:
| question_name | answer |
Expand All @@ -63,3 +61,18 @@ Feature: View an adleradaptivity
And I should not see a ".behat_task-optional-incorrect" element
And I should not see a ".behat_task-optional-not-attempted" element
And I should see a ".behat_task-correct" element

Scenario: Display module with a question that has multiple references to it (in another module)
Given the following "activities" exist:
| activity | name | intro | course | completion |
| adleradaptivity | Adler Activity 2 | Adler Activity 2 Intro | C1 | 2 |
And adleradaptivity "Adler Activity 2" contains the following tasks:
| title | required_difficulty |
| Task2_1 | 100 |
And the following adleradaptivity questions are added:
| task_title | question_category | question_name | difficulty |
| Task2_1 | Test questions | Q1 | 0 |
When I am on the "Adler Activity 2" "mod_adleradaptivity > View" page logged in as "student"
Then I should see a ".behat_module-failure" element
And I should see "1" ".behat_task-not-attempted" element
And I should not see ".behat_question-status-success"

0 comments on commit 9bd0799

Please sign in to comment.