Skip to content

Commit

Permalink
Add more Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnupamaSarjoshi committed Oct 17, 2023
1 parent 15f5c0a commit 0427e0a
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 115 deletions.
6 changes: 3 additions & 3 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class row {
/** @var string The row name. */
public $name;

/** @var array The list of correct answers, A json-encoded list of correct answerids for a given row. */
public $correctanswers = [];
/** @var string The string of json encoded correct answers. */
public $correctanswers;

/** @var string The row specific feedback. */
public $feedback;
Expand All @@ -63,7 +63,7 @@ class row {
* @param int $numberofrows
* @param int $numberofcolumns
*/
public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '', array $correctanswers = [],
public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '', string $correctanswers = '',
string $feedback = '', int $feedbackformat = 0) {
$this->questionid = $questionid;
$this->number = $number;
Expand Down
43 changes: 22 additions & 21 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ protected function initialise_question_rows(question_definition $question, $ques
foreach ($questiondata->rows as $index => $row) {
$newrow = $this->make_row($row);
$correctanswers = [];
$todecode = implode(",", $newrow->correctanswers);
$decodedanswers = json_decode($todecode, true);
foreach ($questiondata->columns as $key => $column) {
$decodedanswers = json_decode($newrow->correctanswers, true);
foreach ($questiondata->columns as $column) {
if ($decodedanswers != null && array_key_exists($column->id, $decodedanswers)) {
if ($questiondata->options->inputtype == 'single') {
$anslabel = 'a' . ($column->number + 1);
Expand Down Expand Up @@ -325,12 +324,11 @@ protected function make_column($columndata) {
}

public function make_row($rowdata) {
// Need to explode correctanswers as it is in the string format.
return new row($rowdata->id, $rowdata->questionid, $rowdata->number, $rowdata->name,
explode(',', $rowdata->correctanswers), $rowdata->feedback, $rowdata->feedbackformat);
$rowdata->correctanswers, $rowdata->feedback, $rowdata->feedbackformat);
}

public function import_from_xml($data, $question, qformat_xml $format, $extra = null): object {
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
if (!isset($data['@']['type']) || $data['@']['type'] != 'oumatrix') {
return false;
}
Expand Down Expand Up @@ -443,33 +441,36 @@ public function export_to_xml($question, qformat_xml $format, $extra = null) {
$question->options->shuffleanswers) . "</shuffleanswers>\n";

// Export columns data.
$output .= "<columns>\n";
$output .= " <columns>\n";
ksort($question->columns);
foreach ($question->columns as $columnkey => $column) {
$output .= " <column key=\"{$columnkey}\">\n";
$output .= " <column key=\"{$columnkey}\">\n";
$output .= $format->writetext($column->name, 4);
$output .= " </column>\n";
$output .= " </column>\n";
}
$output .= "</columns>\n";
$output .= " </columns>\n";

// Export rows data.
$fs = get_file_storage();
$output .= " <rows>\n";
ksort($question->rows);
$indent = 5;
foreach ($question->rows as $rowkey => $row) {
$output .= " <row key=\"{$rowkey}\">\n";
$output .= " <name>\n";
$output .= $format->writetext($row->name, 3);
$output .= " </name>\n";
$output .= " <correctanswers>\n";
$output .= $format->writetext($row->correctanswers, 3);
$output .= " </correctanswers>\n";
$output .= " <row key=\"{$rowkey}\">\n";
$output .= " <name>\n";
$output .= $format->writetext($row->name, $indent);
$output .= " </name>\n";
$output .= " <correctanswers>\n";
$output .= $format->writetext($row->correctanswers, $indent);
$output .= " </correctanswers>\n";
if (($row->feedback ?? '') != '') {
$output .= ' <feedback ' . $format->format($row->feedbackformat) . ">\n";
$output .= $format->writetext($row->feedback, 4);
$output .= ' <feedback ' . $format->format($row->feedbackformat) . ">\n";
$output .= $format->writetext($row->feedback, $indent);
$files = $fs->get_area_files($question->contextid, 'qtype_oumatrix', 'feedback', $row->id);
$output .= $format->write_files($files);
$output .= " </feedback>\n";
$output .= " </feedback>\n";
}
$output .= " </row>\n";
$output .= " </row>\n";
}
$output .= " </rows>\n";
$output .= $format->write_combined_feedback($question->options,
Expand Down
50 changes: 48 additions & 2 deletions tests/behat/edit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,65 @@ Feature: Test editing an ouMatrix question
@javascript
Scenario: Edit a Matrix question with single response (radio buttons)
When I am on the "Single matrix for editing" "core_question > edit" page logged in as teacher

Then "input[id=id_rowanswers_0_a1][checked]" "css_element" should exist
And "input[id=id_rowanswers_0_a2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_0_a3]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_0_a4]:not([checked])" "css_element" should exist

And "input[id=id_rowanswers_1_a1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_1_a2][checked]" "css_element" should exist
And "input[id=id_rowanswers_1_a3]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_1_a4]:not([checked])" "css_element" should exist

And "input[id=id_rowanswers_2_a1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_2_a2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_2_a3][checked]" "css_element" should exist
And "input[id=id_rowanswers_2_a4]:not([checked])" "css_element" should exist

And "input[id=id_rowanswers_3_a1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_3_a2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_3_a3]:not([checked])" "css_element" should exist
And "input[id=id_rowanswers_3_a4][checked]" "css_element" should exist

And I set the following fields to these values:
| Question name | Edited Single matrix name |
And I press "id_submitbutton"
Then I should see "Edited Single matrix name"
And I should see "Edited Single matrix name"

@javascript
Scenario: Edit a Matrix question with multiple response (checkboxes)
When I am on the "Multiple matrix for editing" "core_question > edit" page logged in as teacher

Then "input[id=id_rowanswersa1_0][checked]" "css_element" should exist
And "input[id=id_rowanswersa2_0]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa3_0][checked]" "css_element" should exist
And "input[id=id_rowanswersa4_0]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa5_0]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa6_0][checked]" "css_element" should exist
And "input[id=id_rowanswersa7_0]:not([checked])" "css_element" should exist

And "input[id=id_rowanswersa1_1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa2_1][checked]" "css_element" should exist
And "input[id=id_rowanswersa3_1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa4_1][checked]" "css_element" should exist
And "input[id=id_rowanswersa5_1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa6_1]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa7_1][checked]" "css_element" should exist

And "input[id=id_rowanswersa1_2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa2_2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa3_2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa4_2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa5_2][checked]" "css_element" should exist
And "input[id=id_rowanswersa6_2]:not([checked])" "css_element" should exist
And "input[id=id_rowanswersa7_2]:not([checked])" "css_element" should exist

And I set the following fields to these values:
| Question name | |
And I press "id_submitbutton"
And I should see "You must supply a value here."
And I set the following fields to these values:
| Question name | Edited Multiple matrix name |
And I press "id_submitbutton"
Then I should see "Edited Multiple matrix name"
And I should see "Edited Multiple matrix name"
2 changes: 1 addition & 1 deletion tests/behat/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Feature: Test exporting Numerical questions
When I am on the "Course 1" "core_question > course question export" page logged in as teacher
And I set the field "id_format_xml" to "1"
And I press "Export questions to file"
Then following "click here" should download between "5200" and "5500" bytes
Then following "click here" should download between "5500" and "5800" bytes
# If the download step is the last in the scenario then we can sometimes run
# into the situation where the download page causes a http redirect but behat
# has already conducted its reset (generating an error). By putting a logout
Expand Down
28 changes: 20 additions & 8 deletions tests/behat/import.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@qtype @qtype_oumatrix
Feature: Test importing Numerical questions
Feature: Test importing OUMatrix questions
As a teacher
In order to reuse Numerical questions
In order to reuse OUMatrix questions
I need to import them

Background:
Expand All @@ -16,13 +16,25 @@ Feature: Test importing Numerical questions
| teacher | C1 | editingteacher |

@javascript @_file_upload
Scenario: Import OUMatrix question.
When I am on the "Course 1" "core_question > course question import" page logged in as teacher
And I set the field "id_format_xml" to "1"
And I upload "question/type/oumatrix/tests/fixtures/testquestion_multiple.moodle.xml" file to "Import" filemanager
Scenario: Import OUMatrix single choice question.
Given I am on the "Course 1" "core_question > course question import" page logged in as teacher
When I set the field "id_format_xml" to "1"
And I upload "question/type/oumatrix/tests/fixtures/testquestion_singlechoice.moodle.xml" file to "Import" filemanager
And I press "id_submitbutton"
Then I should see "Parsing questions from import file."
And I should see "Importing 1 questions from file"
And I should see "1. Test this multiple choice question"
And I should see "1. Select the correct option for each of the animals and the family they belong to."
And I press "Continue"
And I should see "Test converting a question to multiple"
And I should see "OUMatrix single choice"

@javascript @_file_upload
Scenario: Import OUMatrix multiple response question.
Given I am on the "Course 1" "core_question > course question import" page logged in as teacher
When I set the field "id_format_xml" to "1"
And I upload "question/type/oumatrix/tests/fixtures/testquestion_multipleresponse.moodle.xml" file to "Import" filemanager
And I press "id_submitbutton"
Then I should see "Parsing questions from import file."
And I should see "Importing 1 questions from file"
And I should see "1. Select the true statements for each of the materials by ticking the boxes in the table."
And I press "Continue"
And I should see "OUMatrix multiple choice"
80 changes: 0 additions & 80 deletions tests/fixtures/testquestion_multiple.moodle.xml

This file was deleted.

152 changes: 152 additions & 0 deletions tests/fixtures/testquestion_multipleresponse.moodle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 460 -->
<question type="oumatrix">
<name>
<text>OUMatrix multiple choice</text>
</name>
<questiontext format="html">
<text><![CDATA[<p>Select the true statements for each of the materials by ticking the boxes in the table.</p>]]></text>
</questiontext>
<generalfeedback format="html">
<text>This is general feedback</text>
</generalfeedback>
<defaultgrade>10</defaultgrade>
<penalty>0.3333333</penalty>
<hidden>0</hidden>
<idnumber>002</idnumber>
<inputtype>multiple</inputtype>
<grademethod>allnone</grademethod>
<shuffleanswers>false</shuffleanswers>
<columns>
<column key="1124">
<text>is a good conductor of electricity</text>
</column>
<column key="1125">
<text>is an insulator</text>
</column>
<column key="1126">
<text>can be magnetised</text>
</column>
</columns>
<rows>
<row key="990">
<name>
<text>copper</text>
</name>
<correctanswers>
<text><![CDATA[{"1124":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Copper is a good conductor of electricity.</p>]]></text>
</feedback>
</row>
<row key="991">
<name>
<text>glass</text>
</name>
<correctanswers>
<text><![CDATA[{"1125":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Glass is an insulator.</p>]]></text>
</feedback>
</row>
<row key="992">
<name>
<text>iron</text>
</name>
<correctanswers>
<text><![CDATA[{"1124":"1","1126":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Iron is a good conductor of electricity and can be magnetised.</p>]]></text>
</feedback>
</row>
<row key="993">
<name>
<text>gold</text>
</name>
<correctanswers>
<text><![CDATA[{"1124":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Gold is a good conductor of electricity.</p>]]></text>
</feedback>
</row>
<row key="994">
<name>
<text>carbon</text>
</name>
<correctanswers>
<text><![CDATA[{"1124":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Carbon is a good conductor of electricity.</p>]]></text>
</feedback>
</row>
<row key="995">
<name>
<text>rubber</text>
</name>
<correctanswers>
<text><![CDATA[{"1125":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Rubber is an insulator.</p>]]></text>
</feedback>
</row>
<row key="996">
<name>
<text>silver</text>
</name>
<correctanswers>
<text><![CDATA[{"1124":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Silver is a good conductor of electricity.</p>]]></text>
</feedback>
</row>
<row key="997">
<name>
<text>nitrogen</text>
</name>
<correctanswers>
<text><![CDATA[{"1125":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Nitrogen is an insulator.</p>]]></text>
</feedback>
</row>
<row key="998">
<name>
<text>wood</text>
</name>
<correctanswers>
<text><![CDATA[{"1125":"1"}]]></text>
</correctanswers>
<feedback format="html">
<text><![CDATA[<p>Wood is an insulator.</p>]]></text>
</feedback>
</row>
</rows>
<correctfeedback format="html">
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
</correctfeedback>
<partiallycorrectfeedback format="html">
<text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
</partiallycorrectfeedback>
<incorrectfeedback format="html">
<text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
</incorrectfeedback>
<shownumcorrect/>
<hint format="html">
<text><![CDATA[<p>Hint 1</p>]]></text>
<shownumcorrect/>
</hint>
<hint format="html">
<text><![CDATA[<p>Hint 2</p>]]></text>
</hint>
</question>

</quiz>
Loading

0 comments on commit 0427e0a

Please sign in to comment.