mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
Merge branch 'MDL-75017' of https://github.com/timhunt/moodle
This commit is contained in:
commit
813800d218
3 changed files with 121 additions and 33 deletions
|
@ -68,6 +68,7 @@ class context_to_string_translator {
|
||||||
protected function generate_context_to_string_array($contexts) {
|
protected function generate_context_to_string_array($contexts) {
|
||||||
if (!$this->contexttostringarray) {
|
if (!$this->contexttostringarray) {
|
||||||
$catno = 1;
|
$catno = 1;
|
||||||
|
/** @var \context $context */
|
||||||
foreach ($contexts as $context) {
|
foreach ($contexts as $context) {
|
||||||
switch ($context->contextlevel) {
|
switch ($context->contextlevel) {
|
||||||
case CONTEXT_MODULE :
|
case CONTEXT_MODULE :
|
||||||
|
@ -83,6 +84,11 @@ class context_to_string_translator {
|
||||||
case CONTEXT_SYSTEM :
|
case CONTEXT_SYSTEM :
|
||||||
$contextstring = 'system';
|
$contextstring = 'system';
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
throw new \coding_exception('Unexpected context level ' .
|
||||||
|
\context_helper::get_level_name($context->contextlevel) . ' for context ' .
|
||||||
|
$context->id . ' in generate_context_to_string_array. ' .
|
||||||
|
'Questions can never exist in this type of context.');
|
||||||
}
|
}
|
||||||
$this->contexttostringarray[$context->id] = $contextstring;
|
$this->contexttostringarray[$context->id] = $contextstring;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@
|
||||||
* @copyright 2014 Nikita Nikitsky, Volgograd State Technical University
|
* @copyright 2014 Nikita Nikitsky, Volgograd State Technical University
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use core_question\local\bank\question_edit_contexts;
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
global $CFG;
|
global $CFG;
|
||||||
require_once($CFG->libdir . '/questionlib.php');
|
require_once($CFG->libdir . '/questionlib.php');
|
||||||
|
@ -42,15 +45,11 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* @return qformat_xml XML question format object.
|
* @return qformat_xml XML question format object.
|
||||||
*/
|
*/
|
||||||
public function create_qformat($filename, $course) {
|
public function create_qformat($filename, $course) {
|
||||||
global $DB;
|
|
||||||
$qformat = new qformat_xml();
|
$qformat = new qformat_xml();
|
||||||
$contexts = $DB->get_records('context');
|
$qformat->setContexts((new question_edit_contexts(context_course::instance($course->id)))->all());
|
||||||
$importfile = __DIR__ . '/fixtures/' .$filename;
|
|
||||||
$realfilename = $filename;
|
|
||||||
$qformat->setContexts($contexts);
|
|
||||||
$qformat->setCourse($course);
|
$qformat->setCourse($course);
|
||||||
$qformat->setFilename($importfile);
|
$qformat->setFilename(__DIR__ . '/fixtures/' . $filename);
|
||||||
$qformat->setRealfilename($realfilename);
|
$qformat->setRealfilename($filename);
|
||||||
$qformat->setMatchgrades('error');
|
$qformat->setMatchgrades('error');
|
||||||
$qformat->setCatfromfile(1);
|
$qformat->setCatfromfile(1);
|
||||||
$qformat->setContextfromfile(1);
|
$qformat->setContextfromfile(1);
|
||||||
|
@ -147,7 +146,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Simple check for importing a category with a description.
|
* Simple check for importing a category with a description.
|
||||||
*/
|
*/
|
||||||
public function test_import_category() {
|
public function test_import_category() {
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
$course = $this->getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$qformat = $this->create_qformat('category_with_description.xml', $course);
|
$qformat = $this->create_qformat('category_with_description.xml', $course);
|
||||||
|
@ -162,7 +161,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check importing nested categories.
|
* Check importing nested categories.
|
||||||
*/
|
*/
|
||||||
public function test_import_nested_categories() {
|
public function test_import_nested_categories() {
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
$course = $this->getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$qformat = $this->create_qformat('nested_categories.xml', $course);
|
$qformat = $this->create_qformat('nested_categories.xml', $course);
|
||||||
|
@ -180,7 +179,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check importing nested categories contain the right questions.
|
* Check importing nested categories contain the right questions.
|
||||||
*/
|
*/
|
||||||
public function test_import_nested_categories_with_questions() {
|
public function test_import_nested_categories_with_questions() {
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
$course = $this->getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$qformat = $this->create_qformat('nested_categories_with_questions.xml', $course);
|
$qformat = $this->create_qformat('nested_categories_with_questions.xml', $course);
|
||||||
|
@ -204,7 +203,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check import of an old file (without format), for backward compatability.
|
* Check import of an old file (without format), for backward compatability.
|
||||||
*/
|
*/
|
||||||
public function test_import_old_format() {
|
public function test_import_old_format() {
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
$course = $this->getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$qformat = $this->create_qformat('old_format_file.xml', $course);
|
$qformat = $this->create_qformat('old_format_file.xml', $course);
|
||||||
|
@ -222,7 +221,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check the import of an xml file where the child category exists before the parent category.
|
* Check the import of an xml file where the child category exists before the parent category.
|
||||||
*/
|
*/
|
||||||
public function test_import_categories_in_reverse_order() {
|
public function test_import_categories_in_reverse_order() {
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
$course = $this->getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$qformat = $this->create_qformat('categories_reverse_order.xml', $course);
|
$qformat = $this->create_qformat('categories_reverse_order.xml', $course);
|
||||||
|
@ -240,18 +239,19 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Simple check for exporting a category.
|
* Simple check for exporting a category.
|
||||||
*/
|
*/
|
||||||
public function test_export_category() {
|
public function test_export_category() {
|
||||||
|
global $SITE;
|
||||||
|
|
||||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
// Note while this loads $qformat with all the 'right' data from the xml file,
|
// Note while this loads $qformat with all the 'right' data from the xml file,
|
||||||
// the call to setCategory, followed by exportprocess will actually only export data
|
// the call to setCategory, followed by exportprocess will actually only export data
|
||||||
// from the database (created by the generator).
|
// from the database (created by the generator).
|
||||||
$qformat = $this->create_qformat('export_category.xml', $course);
|
$qformat = $this->create_qformat('export_category.xml', $SITE);
|
||||||
|
|
||||||
$category = $generator->create_question_category([
|
$category = $generator->create_question_category([
|
||||||
'name' => 'Alpha',
|
'name' => 'Alpha',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Alpha category for test',
|
'info' => 'This is Alpha category for test',
|
||||||
'infoformat' => '0',
|
'infoformat' => '0',
|
||||||
'idnumber' => 'alpha-idnumber',
|
'idnumber' => 'alpha-idnumber',
|
||||||
|
@ -277,15 +277,16 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check exporting nested categories.
|
* Check exporting nested categories.
|
||||||
*/
|
*/
|
||||||
public function test_export_nested_categories() {
|
public function test_export_nested_categories() {
|
||||||
$this->resetAfterTest(true);
|
global $SITE;
|
||||||
$course = $this->getDataGenerator()->create_course();
|
|
||||||
|
$this->resetAfterTest();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||||
$qformat = $this->create_qformat('nested_categories.zml', $course);
|
$qformat = $this->create_qformat('nested_categories.zml', $SITE);
|
||||||
|
|
||||||
$categorydelta = $generator->create_question_category([
|
$categorydelta = $generator->create_question_category([
|
||||||
'name' => 'Delta',
|
'name' => 'Delta',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Delta category for test',
|
'info' => 'This is Delta category for test',
|
||||||
'infoformat' => '2',
|
'infoformat' => '2',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -293,7 +294,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
'sortorder' => '999']);
|
'sortorder' => '999']);
|
||||||
$categoryepsilon = $generator->create_question_category([
|
$categoryepsilon = $generator->create_question_category([
|
||||||
'name' => 'Epsilon',
|
'name' => 'Epsilon',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Epsilon category for test',
|
'info' => 'This is Epsilon category for test',
|
||||||
'infoformat' => '4',
|
'infoformat' => '4',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -301,7 +302,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
'sortorder' => '999']);
|
'sortorder' => '999']);
|
||||||
$categoryzeta = $generator->create_question_category([
|
$categoryzeta = $generator->create_question_category([
|
||||||
'name' => 'Zeta',
|
'name' => 'Zeta',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Zeta category for test',
|
'info' => 'This is Zeta category for test',
|
||||||
'infoformat' => '0',
|
'infoformat' => '0',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -330,15 +331,16 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Check exporting nested categories contain the right questions.
|
* Check exporting nested categories contain the right questions.
|
||||||
*/
|
*/
|
||||||
public function test_export_nested_categories_with_questions() {
|
public function test_export_nested_categories_with_questions() {
|
||||||
$this->resetAfterTest(true);
|
global $SITE;
|
||||||
$course = $this->getDataGenerator()->create_course();
|
|
||||||
|
$this->resetAfterTest();
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||||
$qformat = $this->create_qformat('nested_categories_with_questions.xml', $course);
|
$qformat = $this->create_qformat('nested_categories_with_questions.xml', $SITE);
|
||||||
|
|
||||||
$categoryiota = $generator->create_question_category([
|
$categoryiota = $generator->create_question_category([
|
||||||
'name' => 'Iota',
|
'name' => 'Iota',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Iota category for test',
|
'info' => 'This is Iota category for test',
|
||||||
'infoformat' => '2',
|
'infoformat' => '2',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -357,7 +359,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
'penalty' => '1']);
|
'penalty' => '1']);
|
||||||
$categorykappa = $generator->create_question_category([
|
$categorykappa = $generator->create_question_category([
|
||||||
'name' => 'Kappa',
|
'name' => 'Kappa',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Kappa category for test',
|
'info' => 'This is Kappa category for test',
|
||||||
'infoformat' => '4',
|
'infoformat' => '4',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -390,7 +392,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
'idnumber' => '']);
|
'idnumber' => '']);
|
||||||
$categorylambda = $generator->create_question_category([
|
$categorylambda = $generator->create_question_category([
|
||||||
'name' => 'Lambda',
|
'name' => 'Lambda',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Lambda category for test',
|
'info' => 'This is Lambda category for test',
|
||||||
'infoformat' => '0',
|
'infoformat' => '0',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -409,7 +411,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
'penalty' => '1']);
|
'penalty' => '1']);
|
||||||
$categorymu = $generator->create_question_category([
|
$categorymu = $generator->create_question_category([
|
||||||
'name' => 'Mu',
|
'name' => 'Mu',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Mu category for test',
|
'info' => 'This is Mu category for test',
|
||||||
'infoformat' => '0',
|
'infoformat' => '0',
|
||||||
'stamp' => make_unique_id_code(),
|
'stamp' => make_unique_id_code(),
|
||||||
|
@ -436,18 +438,19 @@ class qformat_xml_import_export_test extends advanced_testcase {
|
||||||
* Simple check for exporting a category.
|
* Simple check for exporting a category.
|
||||||
*/
|
*/
|
||||||
public function test_export_category_with_special_chars() {
|
public function test_export_category_with_special_chars() {
|
||||||
|
global $SITE;
|
||||||
|
|
||||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest();
|
||||||
$course = $this->getDataGenerator()->create_course();
|
|
||||||
$this->setAdminUser();
|
$this->setAdminUser();
|
||||||
// Note while this loads $qformat with all the 'right' data from the xml file,
|
// Note while this loads $qformat with all the 'right' data from the xml file,
|
||||||
// the call to setCategory, followed by exportprocess will actually only export data
|
// the call to setCategory, followed by exportprocess will actually only export data
|
||||||
// from the database (created by the generator).
|
// from the database (created by the generator).
|
||||||
$qformat = $this->create_qformat('export_category.xml', $course);
|
$qformat = $this->create_qformat('export_category.xml', $SITE);
|
||||||
|
|
||||||
$category = $generator->create_question_category([
|
$category = $generator->create_question_category([
|
||||||
'name' => 'Alpha',
|
'name' => 'Alpha',
|
||||||
'contextid' => '2',
|
'contextid' => context_course::instance($SITE->id)->id,
|
||||||
'info' => 'This is Alpha category for test',
|
'info' => 'This is Alpha category for test',
|
||||||
'infoformat' => '0',
|
'infoformat' => '0',
|
||||||
'idnumber' => 'The inequalities < & >',
|
'idnumber' => 'The inequalities < & >',
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
<?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 core_question\local\bank;
|
||||||
|
|
||||||
|
use context_course;
|
||||||
|
use context_coursecat;
|
||||||
|
use context_module;
|
||||||
|
use context_system;
|
||||||
|
use context_user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for the context_to_string_translator class.
|
||||||
|
*
|
||||||
|
* @package core_question
|
||||||
|
* @category test
|
||||||
|
* @copyright 2023 the Open University
|
||||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
|
* @covers \core_question\local\bank\context_to_string_translator
|
||||||
|
*/
|
||||||
|
class context_to_string_translator_test extends \advanced_testcase {
|
||||||
|
|
||||||
|
public function test_context_to_string_translator_test_good_case() {
|
||||||
|
$this->resetAfterTest();
|
||||||
|
$generator = $this->getDataGenerator();
|
||||||
|
|
||||||
|
// Generate a quiz in a course in a category.
|
||||||
|
$systemcontext = context_system::instance();
|
||||||
|
|
||||||
|
$category = $generator->create_category();
|
||||||
|
$categorycontext = context_coursecat::instance($category->id);
|
||||||
|
|
||||||
|
$course = $generator->create_course(['category' => $category->id]);
|
||||||
|
$coursecontext = context_course::instance($course->id);
|
||||||
|
|
||||||
|
$quiz = $generator->create_module('quiz', ['course' => $course->id]);
|
||||||
|
$quizcontext = context_module::instance($quiz->cmid);
|
||||||
|
|
||||||
|
// Create the context_to_string_translator.
|
||||||
|
$translator = new context_to_string_translator((new question_edit_contexts($quizcontext))->all());
|
||||||
|
|
||||||
|
// Verify its behaviour.
|
||||||
|
$this->assertEquals('module', $translator->context_to_string($quizcontext->id));
|
||||||
|
$this->assertEquals($quizcontext->id, $translator->string_to_context('module'));
|
||||||
|
|
||||||
|
$this->assertEquals('course', $translator->context_to_string($coursecontext->id));
|
||||||
|
$this->assertEquals($coursecontext->id, $translator->string_to_context('course'));
|
||||||
|
|
||||||
|
$this->assertEquals('cat1', $translator->context_to_string($categorycontext->id));
|
||||||
|
$this->assertEquals($categorycontext->id, $translator->string_to_context('cat1'));
|
||||||
|
|
||||||
|
$this->assertEquals('system', $translator->context_to_string($systemcontext->id));
|
||||||
|
$this->assertEquals($systemcontext->id, $translator->string_to_context('system'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_context_to_string_translator_throws_exception_with_bad_context() {
|
||||||
|
global $USER;
|
||||||
|
$this->resetAfterTest();
|
||||||
|
$this->setAdminUser();
|
||||||
|
$context = context_user::instance($USER->id);
|
||||||
|
$this->expectExceptionMessage('Unexpected context level User for context ' .
|
||||||
|
$context->id . ' in generate_context_to_string_array. ' .
|
||||||
|
'Questions can never exist in this type of context.');
|
||||||
|
new context_to_string_translator((new question_edit_contexts($context))->all());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue