mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-75017 questions: give a clear error if the context type is invalid
This commit is contained in:
parent
2e1fde188e
commit
94a584bf38
2 changed files with 85 additions and 0 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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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