From 0ec724711733fd88c6e317b2a5ce18571f05e346 Mon Sep 17 00:00:00 2001
From: mkassaei
Date: Tue, 2 Jul 2024 16:34:31 +0100
Subject: [PATCH] phpunit and behat for backup and restore #520123
---
.../backup_qtype_drawlines_plugin.class.php | 4 +-
tests/backup_and_restore_test.php | 99 +++++++++++++++++++
tests/behat/backup_and_restore.feature | 65 ++++++++++++
tests/behat/import.feature | 2 +-
.../testquestion_drawlines_mkmap_twolines.xml | 2 +-
tests/helper.php | 6 +-
6 files changed, 171 insertions(+), 7 deletions(-)
create mode 100644 tests/backup_and_restore_test.php
create mode 100644 tests/behat/backup_and_restore.feature
diff --git a/backup/moodle2/backup_qtype_drawlines_plugin.class.php b/backup/moodle2/backup_qtype_drawlines_plugin.class.php
index f00b0f2..7494185 100644
--- a/backup/moodle2/backup_qtype_drawlines_plugin.class.php
+++ b/backup/moodle2/backup_qtype_drawlines_plugin.class.php
@@ -53,11 +53,11 @@ protected function define_question_plugin_structure() {
$lines = new backup_nested_element('lines');
$line = new backup_nested_element('line', ['id'], ['number', 'type',
'labelstart', 'labelmiddle', 'labelend', 'zonestart', 'zoneend']);
- $pluginwrapper->add_child($lines);
$lines->add_child($line);
+ $pluginwrapper->add_child($lines);
$options->set_source_table('qtype_drawlines_options', ['questionid' => backup::VAR_PARENTID]);
- $lines->set_source_table('qtype_drawlines_lines', ['questionid' => backup::VAR_PARENTID]);
+ $line->set_source_table('qtype_drawlines_lines', ['questionid' => backup::VAR_PARENTID]);
return $plugin;
}
diff --git a/tests/backup_and_restore_test.php b/tests/backup_and_restore_test.php
new file mode 100644
index 0000000..651841d
--- /dev/null
+++ b/tests/backup_and_restore_test.php
@@ -0,0 +1,99 @@
+.
+
+namespace qtype_drawlines;
+
+use question_bank;
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
+require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
+require_once($CFG->dirroot . '/course/externallib.php');
+require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
+
+/**
+ * Tests for drawlines question type backup and restore.
+ *
+ * @package qtype_drawlines
+ * @copyright 2024 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+class backup_and_restore_test extends \restore_date_testcase {
+
+ /**
+ * Backup and restore the course containing an drawlines question for testing drawlines backup and restore.
+ * @covers \restore_qtype_drawlines_plugin
+ */
+ public function test_restore_create_qtype_drawlines_mkmap_twolines(): void {
+ global $DB;
+
+ // Create a course with one drawlines question in its question bank.
+ $generator = $this->getDataGenerator();
+ $course = $generator->create_course();
+ $contexts = new \core_question\local\bank\question_edit_contexts(\context_course::instance($course->id));
+ $category = question_make_default_categories($contexts->all());
+ $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
+ $drawlines = $questiongenerator->create_question('drawlines', 'mkmap_twolines', ['category' => $category->id]);
+
+ // Do backup and restore the course.
+ $newcourseid = $this->backup_and_restore($course);
+
+ // Verify that the restored question has the extra data such as options, lines.
+ $contexts = new \core_question\local\bank\question_edit_contexts(\context_course::instance($newcourseid));
+ $newcategory = question_make_default_categories($contexts->all());
+ $newdrawlines = $DB->get_record_sql('SELECT q.*
+ FROM {question} q
+ JOIN {question_versions} qv ON qv.questionid = q.id
+ JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
+ WHERE qbe.questioncategoryid = ?
+ AND q.qtype = ?', [$newcategory->id, 'drawlines']);
+
+ $newdrawlines->options = $DB->get_record('qtype_drawlines_options', ['questionid' => $newdrawlines->id]);
+ $newdrawlines->lines = $DB->get_records('qtype_drawlines_lines', ['questionid' => $newdrawlines->id]);
+
+ $this->assertSame($newcourseid, $course->id + 1);
+ $this->assertSame((int)$newdrawlines->id, $drawlines->id + 1);
+
+ $this->assertTrue($DB->record_exists('question', ['id' => $newdrawlines->id]));
+ $this->assertTrue($DB->record_exists('qtype_drawlines_options', ['questionid' => $newdrawlines->id]));
+ $this->assertTrue($DB->record_exists('qtype_drawlines_lines', ['questionid' => $newdrawlines->id]));
+
+ $questionata = question_bank::load_question_data($newdrawlines->id);
+
+ $this->assertSame('partial', $questionata->options->grademethod);
+
+ $questionlines = array_values((array) $questionata->lines);
+ $this->assertSame($newdrawlines->id, $questionlines[0]->questionid);
+ $this->assertSame('1', $questionlines[0]->number);
+ $this->assertSame(line::TYPE_LINE_SEGMENT, $questionlines[0]->type);
+ $this->assertSame('Start 1', $questionlines[0]->labelstart);
+ $this->assertSame('Mid 1', $questionlines[0]->labelmiddle);
+ $this->assertSame('', $questionlines[0]->labelend);
+ $this->assertSame('10,10;12', $questionlines[0]->zonestart);
+ $this->assertSame('300,10;12', $questionlines[0]->zoneend);
+
+ $this->assertSame($newdrawlines->id, $questionlines[1]->questionid);
+ $this->assertSame('2', $questionlines[1]->number);
+ $this->assertSame(line::TYPE_LINE_SEGMENT, $questionlines[1]->type);
+ $this->assertSame('Start 2', $questionlines[1]->labelstart);
+ $this->assertSame('Mid 2', $questionlines[1]->labelmiddle);
+ $this->assertSame('End 2', $questionlines[1]->labelend);
+ $this->assertSame('10,100;12', $questionlines[1]->zonestart);
+ $this->assertSame('300,100;12', $questionlines[1]->zoneend);
+ }
+}
diff --git a/tests/behat/backup_and_restore.feature b/tests/behat/backup_and_restore.feature
new file mode 100644
index 0000000..8f0b638
--- /dev/null
+++ b/tests/behat/backup_and_restore.feature
@@ -0,0 +1,65 @@
+@qtype @qtype_drawlines
+Feature: Test duplicating a course containing a DrawLines question
+ As a teacher
+ In order re-use my courses containing DrawLines questions
+ I need to be able to backup and restore them
+
+ Background:
+ And the following "courses" exist:
+ | fullname | shortname | category |
+ | Course 1 | C1 | 0 |
+ And the following "question categories" exist:
+ | contextlevel | reference | name |
+ | Course | C1 | Test questions |
+ And the following "questions" exist:
+ | questioncategory | qtype | name | template |
+ | Test questions | drawlines | Draw lines 01 | mkmap_twolines |
+ And the following "activities" exist:
+ | activity | name | course | idnumber |
+ | quiz | Test quiz | C1 | quiz1 |
+ And quiz "Test quiz" contains the following questions:
+ | Draw lines 01 | 1 |
+ And I log in as "admin"
+ And I am on "Course 1" course homepage
+
+ @javascript
+ Scenario: Backup and restore a course containing a DrawLines question
+ Given I backup "Course 1" course using this options:
+ | Confirmation | Filename | test_backup.mbz |
+ When I restore "test_backup.mbz" backup into a new course using this options:
+ | Schema | Course name | Course 2 |
+ | Schema | Course short name | C2 |
+ And I am on the "Course 2" "core_question > course question bank" page
+ And I choose "Edit question" action for "Draw lines 01" in the question bank
+ Then the following fields match these values:
+ | id_name | Draw lines 01 |
+ | id_questiontext | Draw 2 lines on the map. A line segment from A (line starting point) to B (line Ending point), and another one from C to D. A is ..., B is ..., C is ... and D is ...
|
+ | id_status | Ready |
+ | id_defaultmark | 1 |
+ | id_generalfeedback | We draw lines from a starting to an end point.
|
+ | id_grademethod | partial |
+ And I click on "Line 1" "link"
+ And the following fields match these values:
+ | id_type_0 | linesegment |
+ | id_labelstart_0 | Start 1 |
+ | id_labelmiddle_0 | Mid 1 |
+ | id_labelend_0 | |
+ | id_zonestart_0 | 10,10;12 |
+ | id_zoneend_0 | 300,10;12 |
+ And I click on "Line 2" "link"
+ And the following fields match these values:
+ | id_type_1 | linesegment |
+ | id_labelstart_1 | Start 2 |
+ | id_labelmiddle_1 | Mid 2 |
+ | id_labelend_1 | End 2 |
+ | id_zonestart_1 | 10,100;12 |
+ | id_zoneend_1 | 300,100;12 |
+ And I click on "Combined feedback" "link"
+ And the following fields match these values:
+ | For any correct response | Well done! |
+ | For any partially correct response | Parts, but only parts, of your response are correct. |
+ | For any incorrect response | That is not right at all. |
+ And I click on "Multiple tries" "link"
+ And the following fields match these values:
+ | Hint 1 | You are trying to draw 2 lines by placing the start and end markers for each line on the map. |
+ | Hint 2 | You have to find the positins for start and end of each line as described in the question text. |
diff --git a/tests/behat/import.feature b/tests/behat/import.feature
index f62efd9..ef3b23d 100644
--- a/tests/behat/import.feature
+++ b/tests/behat/import.feature
@@ -24,6 +24,6 @@ Feature: Test importing DrawLines questions
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. Draw 2 lines on the map. A line segent from A (line starting point) to B (line Ending point), and another one from C to D."
+ And I should see "1. Draw 2 lines on the map. A line segment from A (line starting point) to B (line Ending point), and another one from C to D."
And I press "Continue"
And I should see "Drawline edited"
diff --git a/tests/fixtures/testquestion_drawlines_mkmap_twolines.xml b/tests/fixtures/testquestion_drawlines_mkmap_twolines.xml
index ec5f678..63bf42c 100644
--- a/tests/fixtures/testquestion_drawlines_mkmap_twolines.xml
+++ b/tests/fixtures/testquestion_drawlines_mkmap_twolines.xml
@@ -6,7 +6,7 @@
Drawline edited
- Draw 2 lines on the map. A line segent from A (line starting point) to B (line Ending point), and another one from C to D. A is ..., B is ..., C is ... and D is ...
]]>
+ Draw 2 lines on the map. A line segment from A (line starting point) to B (line Ending point), and another one from C to D. A is ..., B is ..., C is ... and D is ...]]>
We draw lines from a starting to an end point.]]>
diff --git a/tests/helper.php b/tests/helper.php
index 449493c..b12e1d2 100644
--- a/tests/helper.php
+++ b/tests/helper.php
@@ -59,7 +59,7 @@ public function get_drawlines_question_data_mkmap_twolines(): stdClass {
$qdata->modifiedby = $USER->id;
$qdata->qtype = 'drawlines';
$qdata->name = 'drawlines_mkmap01';
- $qdata->questiontext = 'Draw 2 lines on the map. A line segent from A (line starting point) to B (line Ending point),' .
+ $qdata->questiontext = 'Draw 2 lines on the map. A line segment from A (line starting point) to B (line Ending point),' .
' and another one from C to D. A is ..., B is ..., C is ... and D is ...';
$qdata->questiontextformat = FORMAT_HTML;
$qdata->generalfeedback = 'We draw lines from a starting to an end point.';
@@ -149,7 +149,7 @@ public function get_drawlines_question_form_data_mkmap_twolines(): stdClass {
$fromform->id = 123;
$fromform->name = 'MK landmarks';
$fromform->questiontext = [
- 'text' => 'Draw 2 lines on the map. A line segent from A (line starting point) to B (line Ending point),' .
+ 'text' => 'Draw 2 lines on the map. A line segment from A (line starting point) to B (line Ending point),' .
' and another one from C to D. A is ..., B is ..., C is ... and D is ...',
'format' => FORMAT_HTML,
];
@@ -220,7 +220,7 @@ public function make_drawlines_question_mkmap_twolines(): qtype_drawlines_questi
$question->modifiedby = $USER->id;
$question->qtype = 'qtype_drawlines_question';
$question->name = 'drawlines_mkmap_twolines';
- $question->questiontext = 'Draw 2 lines on the map. A line segent from A (line starting point) to B (line Ending point),' .
+ $question->questiontext = 'Draw 2 lines on the map. A line segennt from A (line starting point) to B (line Ending point),' .
' and another one from C to D. A is ..., B is ..., C is ... and D is ...';
$question->questiontextformat = FORMAT_HTML;
$question->generalfeedback = 'We draw lines from a starting to an end point.';