Skip to content

Commit

Permalink
GH-510 Add a test to check import has no errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszkiba committed Sep 30, 2024
1 parent 05dcce0 commit 33427ea
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 3 deletions.
6 changes: 4 additions & 2 deletions tests/fixtures/simulation_comma.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
componentid,status,qtype,model,difficulty,discrimination,guessing,label,catscalename,parentscalenames,componentname
0,4,Multiple-Choice,raschbirnbaum,"-3,321","0,123","0,1800",SIMA01-02,SimA01,Simulation|SimA,question
componentid,status,qtype,model,difficulty,discrimination,guessing,label,catscalename,parentscalenames,componentname
0,4,Multiple-Choice,raschbirnbaum,"-3,321","0,123","0,1800",SIMA01-02,SimA01,Simulation|SimA,question
0,4,Multiple-Choice,raschbirnbaum,"1.123","0,123","0,1800",SIMA01-03,SimA01,Simulation|SimA,question
0,4,Multiple-Choice,raschbirnbaum,"11","0,123","0,1800",SIMA01-04,SimA01,Simulation|SimA,question
191 changes: 191 additions & 0 deletions tests/importer/testitemimporter_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?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/>.

/**
* Tests the testitemimporter functionality.
*
* @package local_catquiz
* @author David Szkiba <[email protected]>
* @copyright 2023 Georg Maißer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_catquiz;

use advanced_testcase;
use coding_exception;
use context_course;
use context_module;
use core_question\local\bank\question_edit_contexts;
use Exception;
use local_catquiz\importer\testitemimporter;
use local_catquiz\local\model\model_item_param;
use local_catquiz\local\model\model_item_param_list;
use moodle_exception;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
use qformat_xml;
use question_engine;
use question_usage_by_activity;
use stdClass;
use UnexpectedValueException;

defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/format/xml/format.php');
require_once($CFG->dirroot . '/question/format.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/local/catquiz/lib.php');
require_once($CFG->dirroot . '/local/catquiz/tests/lib.php');
require_once($CFG->dirroot . '/local/catquiz/tests/lib.php');

/**
* Tests the testitemimporter functionality.
*
* @package local_catquiz
* @author David Szkiba <[email protected]>
* @copyright 2023 Georg Maißer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \local_catquiz\importer\testitemimporter
*
*/
final class testitemimporter_test extends advanced_testcase {

/**
* @var An instance of an adaptive quiz
*/
private \stdClass $adaptivequiz;

/**
* @var stdClass $course Stores the course we create for this test.
*/
private stdClass $course;

public function setUp(): void {
$this->import('simulation.xml', 'simulation.csv');
}

/**
* Check if importing a comma separated file works.
*
* @return void
*/
public function test_import_overrides(): void {
$importer = new testitemimporter();
$content = file_get_contents(__DIR__ . '/../fixtures/simulation_comma.csv');
$result = $importer->execute_testitems_csv_import(
(object) [
'delimiter_name' => 'comma',
'dateparseformat' => null,
'encoding' => null,
],
$content
);
// Check that there are no errors.
$this->assertEquals(0, count($result['errors']), implode(', ', $result['errors']));
}

/**
* Import both questions and item params from fixture files.
*
* @param string $questionsfile The path to an XML questions file.
* @param string $itemparamsfile The path to a CSV itemparams file.
*
* @return void
*/
private function import(string $questionsfile, string $itemparamsfile): void {
global $DB;
$this->resetAfterTest(true);

// Import questions.
$this->setAdminUser();
$this->course = $this->getDataGenerator()->create_course();

$this->adaptivequiz = $this->getDataGenerator()
->get_plugin_generator('mod_adaptivequiz')
->create_instance([
'highestlevel' => 10,
'lowestlevel' => 1,
'standarderror' => 14,
'course' => $this->course->id,
]);
$qformat = $this->create_qformat($questionsfile, $this->course);
$imported = $qformat->importprocess();
$this->assertTrue($imported);

$qformat = $this->create_qformat('pilotquestions.xml', $this->course);
$imported = $qformat->importprocess();
$this->assertTrue($imported);

$this->import_itemparams($itemparamsfile);
}

/**
* Import the item params from the given CSV file
*
* @param string $filename The name of the itemparams file.
*
* @return array
*/
private function import_itemparams($filename): array {
global $DB;
$questions = $DB->get_records('question');
if (! $questions) {
exit('No questions were imported');
}
$importer = new testitemimporter();
$content = file_get_contents(__DIR__ . '/../fixtures/' . $filename);
$result = $importer->execute_testitems_csv_import(
(object) [
'delimiter_name' => 'semicolon',
'encoding' => null,
'dateparseformat' => null,
],
$content
);
return $result;
}

/**
* Create a new qformat object so that we can import questions.
*
* NOTE: copied from qformat_xml_import_export_test.php
*
* Create object qformat_xml for test.
* @param string $filename with name for testing file.
* @param \stdClass $course
* @return \qformat_xml XML question format object.
*/
private function create_qformat($filename, $course) {
$qformat = new qformat_xml();
$qformat->setContexts((new question_edit_contexts(context_course::instance($course->id)))->all());
$qformat->setCourse($course);
$qformat->setFilename(__DIR__ . '/../fixtures/' . $filename);
$qformat->setRealfilename($filename);
$qformat->setMatchgrades('error');
$qformat->setCatfromfile(1);
$qformat->setContextfromfile(1);
$qformat->setStoponerror(1);
$qformat->setCattofile(1);
$qformat->setContexttofile(1);
$qformat->set_display_progress(false);

return $qformat;
}
}
5 changes: 4 additions & 1 deletion tests/teststrategy/strategy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function test_import_worked() {
public function test_import_overrides(string $filename, array $expectedscales): void {
// 1. Import items.
global $DB;
$this->import_itemparams($filename);
$result = $this->import_itemparams($filename);
[$initems, $initemsparams] = $DB->get_in_or_equal(
array_keys($expectedscales),
SQL_PARAMS_NAMED,
Expand Down Expand Up @@ -149,6 +149,9 @@ public function test_import_overrides(string $filename, array $expectedscales):
);
$this->assertEquals($expected, $scalenames);
}

// Check that there are no errors.
$this->assertEquals(0, count($result['errors']), implode(', ', $result['errors']));
}

/**
Expand Down

0 comments on commit 33427ea

Please sign in to comment.