Skip to content

Commit

Permalink
Matrix: backup and restore automated test #732453
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Nov 17, 2023
1 parent c32eede commit ac93671
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 13 deletions.
200 changes: 200 additions & 0 deletions tests/backup_and_restore_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?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_oumatrix;

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 oumatrix question type backup and restore.
*
* @package qtype_oumatrix
* @copyright 2023 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 {

/**
* Duplicate quiz with an oumatrix question for testing backup and restore.
* @return void
*/
public function test_duplicate_oumatrix_question_single(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();

$coregenerator = $this->getDataGenerator();
$questiongenerator = $coregenerator->get_plugin_generator('core_question');

// Create a course, a quiz and a question of type oumatrix single.
$course = $coregenerator->create_course();
$quiz = $coregenerator->create_module('quiz', ['course' => $course->id]);
$quizcontext = \context_module::instance($quiz->cmid);

$cat = $questiongenerator->create_question_category(['contextid' => $quizcontext->id]);
$questiongenerator->create_question('oumatrix', 'animals_single', ['category' => $cat->id]);

// Store some counts.
$numquizzes = count(get_fast_modinfo($course)->instances['quiz']);
$numoumatrixquestions = $DB->count_records('question', ['qtype' => 'oumatrix']);

// Duplicate the page.
duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid));

// Verify the copied quiz exists.
$this->assertCount($numquizzes + 1, get_fast_modinfo($course)->instances['quiz']);

// Verify the copied question.
$this->assertEquals($numoumatrixquestions + 1, $DB->count_records('question', ['qtype' => 'oumatrix']));

$newoumatrixid = $DB->get_field_sql("
SELECT MAX(id)
FROM {question}
WHERE qtype = ?
", ['oumatrix']);
$questionata = question_bank::load_question_data($newoumatrixid);
$this->assertSame('single', $questionata->options->inputtype);
$this->assertSame('0', $questionata->options->shuffleanswers);

$questioncolumns = array_values((array) $questionata->columns);
$this->assertSame('1', $questioncolumns[0]->number);
$this->assertSame('Insects', $questioncolumns[0]->name);

$this->assertSame('2', $questioncolumns[1]->number);
$this->assertSame('Fish', $questioncolumns[1]->name);

$this->assertSame('3', $questioncolumns[2]->number);
$this->assertSame('Birds', $questioncolumns[2]->name);

$this->assertSame('4', $questioncolumns[3]->number);
$this->assertSame('Mammals', $questioncolumns[3]->name);

$questionrows = array_values((array) $questionata->rows);
$this->assertSame('1', $questionrows[0]->number);
$this->assertSame('Bee', $questionrows[0]->name);
$this->assertSame('1', $questionrows[0]->correctanswers);
$this->assertSame('Flies and Bees are insects.', $questionrows[0]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[0]->feedbackformat);

$this->assertSame('2', $questionrows[1]->number);
$this->assertSame('Salmon', $questionrows[1]->name);
$this->assertSame('2', $questionrows[1]->correctanswers);
$this->assertSame('Cod, Salmon and Trout are fish.', $questionrows[1]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[1]->feedbackformat);

$this->assertSame('3', $questionrows[2]->number);
$this->assertSame('Seagull', $questionrows[2]->name);
$this->assertSame('3', $questionrows[2]->correctanswers);
$this->assertSame('Gulls and Owls are birds.', $questionrows[2]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[2]->feedbackformat);

$this->assertSame('4', $questionrows[3]->number);
$this->assertSame('Dog', $questionrows[3]->name);
$this->assertSame('4', $questionrows[3]->correctanswers);
$this->assertSame('Cows, Dogs and Horses are mammals.', $questionrows[3]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[3]->feedbackformat);
}

/**
* Backup and restore the course containing an oumatrix question for testing oumatrix backup and restore.
*/
public function test_restore_create_qtype_oumatrix_multiple(): void {
global $DB;

// Create a course with one oumatrix 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');
$oumatrix = $questiongenerator->create_question('oumatrix', 'food_multiple', ['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, columns, rows.
$contexts = new \core_question\local\bank\question_edit_contexts(\context_course::instance($newcourseid));
$newcategory = question_make_default_categories($contexts->all());
$newoumatrix = $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, 'oumatrix']);

$this->assertSame($newcourseid, $course->id + 1);
$this->assertSame((int)$newoumatrix->id, $oumatrix->id + 1);

$this->assertTrue($DB->record_exists('question', ['id' => $newoumatrix->id]));
$this->assertTrue($DB->record_exists('qtype_oumatrix_options', ['questionid' => $newoumatrix->id]));
$this->assertTrue($DB->record_exists('qtype_oumatrix_columns', ['questionid' => $newoumatrix->id]));
$this->assertTrue($DB->record_exists('qtype_oumatrix_rows', ['questionid' => $newoumatrix->id]));

$questionata = question_bank::load_question_data($newoumatrix->id);

$this->assertSame('multiple', $questionata->options->inputtype);
$this->assertSame('0', $questionata->options->shuffleanswers);

$questioncolumns = array_values((array) $questionata->columns);
$this->assertSame('1', $questioncolumns[0]->number);
$this->assertSame('Chicken breast', $questioncolumns[0]->name);

$this->assertSame('2', $questioncolumns[1]->number);
$this->assertSame('Carrot', $questioncolumns[1]->name);

$this->assertSame('3', $questioncolumns[2]->number);
$this->assertSame('Salmon fillet', $questioncolumns[2]->name);

$this->assertSame('4', $questioncolumns[3]->number);
$this->assertSame('Asparagus', $questioncolumns[3]->name);

$this->assertSame('5', $questioncolumns[4]->number);
$this->assertSame('Olive oil', $questioncolumns[4]->name);

$this->assertSame('6', $questioncolumns[5]->number);
$this->assertSame('Steak', $questioncolumns[5]->name);

$this->assertSame('7', $questioncolumns[6]->number);
$this->assertSame('Potato', $questioncolumns[6]->name);

$questionrows = array_values((array) $questionata->rows);
$this->assertSame('1', $questionrows[0]->number);
$this->assertSame('Proteins', $questionrows[0]->name);
$this->assertSame('1,3,6', $questionrows[0]->correctanswers);
$this->assertSame('Chicken, fish and red meat containing proteins.', $questionrows[0]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[0]->feedbackformat);

$this->assertSame('2', $questionrows[1]->number);
$this->assertSame('Vegetables', $questionrows[1]->name);
$this->assertSame('2,4,7', $questionrows[1]->correctanswers);
$this->assertSame('Carrot, Asparagus, Potato are vegetables.', $questionrows[1]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[1]->feedbackformat);

$this->assertSame('3', $questionrows[2]->number);
$this->assertSame('Fats', $questionrows[2]->name);
$this->assertSame('5', $questionrows[2]->correctanswers);
$this->assertSame('Olive oil contains fat.', $questionrows[2]->feedback);
$this->assertSame(FORMAT_HTML, $questionrows[2]->feedbackformat);
}
}
6 changes: 3 additions & 3 deletions tests/behat/backup_and_restore.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Feature: Test duplicating a quiz containing a Numerical question
| id_columnname_2 | Birds |
| id_columnname_3 | Mammals |
| id_rowname_0 | Bee |
| id_feedback_0 | Fly, Bee and Spider are insects. |
| id_feedback_0 | Flies and Bees are insects. |
| id_rowname_1 | Salmon |
| id_feedback_1 | Cod, Salmon and Trout are fish. |
| id_rowname_2 | Seagull |
| id_feedback_2 | Gull and Owl are birds. |
| id_feedback_2 | Gulls and Owls are birds. |
| id_rowname_3 | Dog |
| id_feedback_3 | Cow, Dog and Horse are mammals. |
| id_feedback_3 | Cows, Dogs and Horses are mammals. |
| id_hint_0 | Hint 1. |
| id_hint_1 | Hint 2. |
6 changes: 3 additions & 3 deletions tests/behat/preview.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ Feature: Preview a Numerical question

# Sub-question(row) specific feedback.
Then I should see "Feedback"
And I should see "Fly, Bee and Spider are insects."
And I should see "Flies and Bees are insects."
And I should see "Cod, Salmon and Trout are fish."
And I should see "Gull and Owl are birds."
And I should see "Cow, Dog and Horse are mammals."
And I should see "Gulls and Owls are birds."
And I should see "Cows, Dogs and Horses are mammals."

# General feedback.
And I should see "Well done!"
Expand Down
14 changes: 7 additions & 7 deletions tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function get_oumatrix_question_data_animals_single(): stdClass {
'number' => 1,
'name' => 'Bee',
'correctanswers' => ['1' => 'a1'],
'feedback' => 'Fly, Bee and spider are insects.',
'feedback' => 'Flies and Bees are insects.',
'feedbackformat' => FORMAT_HTML,
],
12 => (object) [
Expand All @@ -123,7 +123,7 @@ public function get_oumatrix_question_data_animals_single(): stdClass {
'number' => 4,
'name' => 'Dog',
'correctanswers' => ['4' => 'a4'],
'feedback' => 'Cow, Dog and Horse are mammals.',
'feedback' => 'Cows, Dogs and Horses are mammals.',
'feedbackformat' => FORMAT_HTML,
],
];
Expand Down Expand Up @@ -204,19 +204,19 @@ public static function get_oumatrix_question_form_data_animals_single(): stdClas
];
$qfdata->feedback = [
0 => [
'text' => 'Fly, Bee and Spider are insects.',
'text' => 'Flies and Bees are insects.',
'format' => FORMAT_HTML,
],
1 => [
'text' => 'Cod, Salmon and Trout are fish.',
'format' => FORMAT_HTML,
],
2 => [
'text' => 'Gull and Owl are birds.',
'text' => 'Gulls and Owls are birds.',
'format' => FORMAT_HTML,
],
3 => [
'text' => 'Cow, Dog and Horse are mammals.',
'text' => 'Cows, Dogs and Horses are mammals.',
'format' => FORMAT_HTML,
],
];
Expand Down Expand Up @@ -489,13 +489,13 @@ public function make_oumatrix_question_animals_single(): qtype_oumatrix_single {

$question->rows = [
11 => new row(11, $question->id, 1, 'Bee', [1 => '1'],
'Fly, Bee and spider are insects.', FORMAT_HTML),
'Flies and Bees are insects.', FORMAT_HTML),
12 => new row(12, $question->id, 2, 'Salmon', [2 => '1'],
'Cod, Salmon and Trout are fish.', FORMAT_HTML),
13 => new row(13, $question->id, 3, 'Seagull', [3 => '1'],
'Gulls and Owls are birds.', FORMAT_HTML),
14 => new row(14, $question->id, 4, 'Dog', [4 => '1'],
'Cow, Dog and Horse are mammals.', FORMAT_HTML),
'Cows, Dogs and Horses are mammals.', FORMAT_HTML),
];

return $question;
Expand Down

0 comments on commit ac93671

Please sign in to comment.