Skip to content

Commit

Permalink
phpunit and behat for backup and restore #520123
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Jul 2, 2024
1 parent 81601d2 commit 0ec7247
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 7 deletions.
4 changes: 2 additions & 2 deletions backup/moodle2/backup_qtype_drawlines_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
99 changes: 99 additions & 0 deletions tests/backup_and_restore_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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);
}
}
65 changes: 65 additions & 0 deletions tests/behat/backup_and_restore.feature
Original file line number Diff line number Diff line change
@@ -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 | <P>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 ...</P> |
| id_status | Ready |
| id_defaultmark | 1 |
| id_generalfeedback | <p>We draw lines from a starting to an end point.</p> |
| 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. |
2 changes: 1 addition & 1 deletion tests/behat/import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion tests/fixtures/testquestion_drawlines_mkmap_twolines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<text>Drawline edited</text>
</name>
<questiontext format="html">
<text><![CDATA[<p>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 ...</p>]]></text>
<text><![CDATA[<p>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 ...</p>]]></text>
</questiontext>
<generalfeedback format="html">
<text><![CDATA[<p>We draw lines from a starting to an end point.</p>]]></text>
Expand Down
6 changes: 3 additions & 3 deletions tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -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,
];
Expand Down Expand Up @@ -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.';
Expand Down

0 comments on commit 0ec7247

Please sign in to comment.