See Release Notes
Long Term Support Release
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_question\local\bank; 18 19 use context_course; 20 use context_coursecat; 21 use context_module; 22 use context_system; 23 use context_user; 24 25 /** 26 * Unit tests for the context_to_string_translator class. 27 * 28 * @package core_question 29 * @category test 30 * @copyright 2023 the Open University 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 * @covers \core_question\local\bank\context_to_string_translator 33 */ 34 class context_to_string_translator_test extends \advanced_testcase { 35 36 public function test_context_to_string_translator_test_good_case() { 37 $this->resetAfterTest(); 38 $generator = $this->getDataGenerator(); 39 40 // Generate a quiz in a course in a category. 41 $systemcontext = context_system::instance(); 42 43 $category = $generator->create_category(); 44 $categorycontext = context_coursecat::instance($category->id); 45 46 $course = $generator->create_course(['category' => $category->id]); 47 $coursecontext = context_course::instance($course->id); 48 49 $quiz = $generator->create_module('quiz', ['course' => $course->id]); 50 $quizcontext = context_module::instance($quiz->cmid); 51 52 // Create the context_to_string_translator. 53 $translator = new context_to_string_translator((new question_edit_contexts($quizcontext))->all()); 54 55 // Verify its behaviour. 56 $this->assertEquals('module', $translator->context_to_string($quizcontext->id)); 57 $this->assertEquals($quizcontext->id, $translator->string_to_context('module')); 58 59 $this->assertEquals('course', $translator->context_to_string($coursecontext->id)); 60 $this->assertEquals($coursecontext->id, $translator->string_to_context('course')); 61 62 $this->assertEquals('cat1', $translator->context_to_string($categorycontext->id)); 63 $this->assertEquals($categorycontext->id, $translator->string_to_context('cat1')); 64 65 $this->assertEquals('system', $translator->context_to_string($systemcontext->id)); 66 $this->assertEquals($systemcontext->id, $translator->string_to_context('system')); 67 } 68 69 public function test_context_to_string_translator_throws_exception_with_bad_context() { 70 global $USER; 71 $this->resetAfterTest(); 72 $this->setAdminUser(); 73 $context = context_user::instance($USER->id); 74 $this->expectExceptionMessage('Unexpected context level User for context ' . 75 $context->id . ' in generate_context_to_string_array. ' . 76 'Questions can never exist in this type of context.'); 77 new context_to_string_translator((new question_edit_contexts($context))->all()); 78 } 79 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body