From f0d92f492cc75fef29fa72ad7fae4d70fa1b4737 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 8 Mar 2024 17:11:59 +0000 Subject: [PATCH] MDL-81114 question: fix coding style in random_question_loader_test --- .../tests/random_question_loader_test.php | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/question/tests/random_question_loader_test.php b/question/tests/random_question_loader_test.php index 13cc9dec3aa..1931b5a3892 100644 --- a/question/tests/random_question_loader_test.php +++ b/question/tests/random_question_loader_test.php @@ -16,6 +16,11 @@ namespace core_question; +use core_question\local\bank\condition; +use core_question\local\bank\question_version_status; +use core_question\local\bank\random_question_loader; +use core_question_generator; +use mod_quiz\quiz_settings; use qubaid_list; use question_bank; use question_engine; @@ -31,19 +36,20 @@ require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.ph * Tests for the {@see \core_question\local\bank\random_question_loader} class. * * @package core_question - * @copyright 2015 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2015 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \random_question_loader */ -class random_question_loader_test extends \advanced_testcase { +final class random_question_loader_test extends \advanced_testcase { use \quiz_question_helper_test_trait; - public function test_empty_category_gives_null() { + public function test_empty_category_gives_null(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertNull($loader->get_next_filtered_question_id($filters)); @@ -52,26 +58,26 @@ class random_question_loader_test extends \advanced_testcase { $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_unknown_category_behaves_like_empty() { + public function test_unknown_category_behaves_like_empty(): void { // It is up the caller to make sure the category id is valid. - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([-1], 1); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_descriptions_not_returned() { + public function test_descriptions_not_returned(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $info = $generator->create_question('description', null, ['category' => $cat->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_hidden_questions_not_returned() { + public function test_hidden_questions_not_returned(): void { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); @@ -79,27 +85,27 @@ class random_question_loader_test extends \advanced_testcase { $cat = $generator->create_question_category(); $question1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); $DB->set_field('question_versions', 'status', - \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN, ['questionid' => $question1->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN, ['questionid' => $question1->id]); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_cloze_subquestions_not_returned() { + public function test_cloze_subquestions_not_returned(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $question1 = $generator->create_question('multianswer', null, ['category' => $cat->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertEquals($question1->id, $loader->get_next_filtered_question_id($filters)); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_random_questions_not_returned() { + public function test_random_questions_not_returned(): void { $this->resetAfterTest(); $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); @@ -108,19 +114,19 @@ class random_question_loader_test extends \advanced_testcase { $course = $this->getDataGenerator()->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course]); $this->add_random_questions($quiz->id, 1, $cat->id, 1); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_one_question_category_returns_that_q_then_null() { + public function test_one_question_category_returns_that_q_then_null(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $question1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id], 1); $this->assertEquals($question1->id, $loader->get_next_filtered_question_id($filters)); @@ -129,14 +135,14 @@ class random_question_loader_test extends \advanced_testcase { $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_two_question_category_returns_both_then_null() { + public function test_two_question_category_returns_both_then_null(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $question1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); $question2 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $questionids = []; $filters = question_filter_test_helper::create_filters([$cat->id]); @@ -149,7 +155,7 @@ class random_question_loader_test extends \advanced_testcase { $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_nested_categories() { + public function test_nested_categories(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); @@ -157,7 +163,7 @@ class random_question_loader_test extends \advanced_testcase { $cat2 = $generator->create_question_category(['parent' => $cat1->id]); $question1 = $generator->create_question('shortanswer', null, ['category' => $cat1->id]); $question2 = $generator->create_question('shortanswer', null, ['category' => $cat2->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat2->id], 1); $this->assertEquals($question2->id, $loader->get_next_filtered_question_id($filters)); @@ -168,22 +174,22 @@ class random_question_loader_test extends \advanced_testcase { $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_used_question_not_returned_until_later() { + public function test_used_question_not_returned_until_later(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $question1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); $question2 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([]), - array($question2->id => 2)); + $loader = new random_question_loader(new qubaid_list([]), + [$question2->id => 2]); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertEquals($question1->id, $loader->get_next_filtered_question_id($filters)); $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_previously_used_question_not_returned_until_later() { + public function test_previously_used_question_not_returned_until_later(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); @@ -198,7 +204,7 @@ class random_question_loader_test extends \advanced_testcase { $quba->start_all_questions(); question_engine::save_questions_usage_by_activity($quba); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list(array($quba->get_id()))); + $loader = new random_question_loader(new qubaid_list([$quba->get_id()])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertEquals($question1->id, $loader->get_next_filtered_question_id($filters)); @@ -206,12 +212,12 @@ class random_question_loader_test extends \advanced_testcase { $this->assertNull($loader->get_next_filtered_question_id($filters)); } - public function test_empty_category_does_not_have_question_available() { + public function test_empty_category_does_not_have_question_available(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list(array())); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertFalse($loader->is_filtered_question_available($filters, 1)); @@ -219,13 +225,13 @@ class random_question_loader_test extends \advanced_testcase { $this->assertFalse($loader->is_filtered_question_available($filters, 1)); } - public function test_descriptions_not_available() { + public function test_descriptions_not_available(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); - $info = $generator->create_question('description', null, array('category' => $cat->id)); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list(array())); + $info = $generator->create_question('description', null, ['category' => $cat->id]); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertFalse($loader->is_filtered_question_available($filters, $info->id)); @@ -233,13 +239,13 @@ class random_question_loader_test extends \advanced_testcase { $this->assertFalse($loader->is_filtered_question_available($filters, $info->id)); } - public function test_existing_question_is_available_but_then_marked_used() { + public function test_existing_question_is_available_but_then_marked_used(): void { $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); - $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id)); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list(array())); + $question1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$cat->id]); $this->assertTrue($loader->is_filtered_question_available($filters, $question1->id)); @@ -250,8 +256,10 @@ class random_question_loader_test extends \advanced_testcase { /** * Data provider for the get_questions test. + * + * @return array testcases. */ - public function get_questions_test_cases() { + public static function get_questions_test_cases(): array { return [ 'empty category' => [ 'categoryindex' => 'emptycat', @@ -334,7 +342,7 @@ class random_question_loader_test extends \advanced_testcase { $includesubcategories, $usetagnames, $expectedquestionindexes - ) { + ): void { $this->resetAfterTest(); $categories = []; @@ -350,22 +358,22 @@ class random_question_loader_test extends \advanced_testcase { $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); // First category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['cat1', 'foo']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['cat1', 'foo']); $categories['cat1'] = $category; $questions['cat1q1'] = $categoryquestions[0]; $questions['cat1q2'] = $categoryquestions[1]; // Second category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['cat2', 'foo']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['cat2', 'foo']); $categories['cat2'] = $category; $questions['cat2q1'] = $categoryquestions[0]; $questions['cat2q2'] = $categoryquestions[1]; // Sub category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['subcat', 'foo'], $categories['cat1']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['subcat', 'foo'], $categories['cat1']); $categories['subcat'] = $category; $questions['subcatq1'] = $categoryquestions[0]; $questions['subcatq2'] = $categoryquestions[1]; // Empty category. - list($category, $categoryquestions) = $this->create_category_and_questions(0); + [$category, $categoryquestions] = $this->create_category_and_questions(0); $categories['emptycat'] = $category; // Generate the arguments for the get_questions function. @@ -374,7 +382,7 @@ class random_question_loader_test extends \advanced_testcase { return $tags[$tagname]->id; }, $usetagnames); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $filters = question_filter_test_helper::create_filters([$category->id], $includesubcategories, $tagids); $result = $loader->get_filtered_questions($filters); // Generate the expected question set. @@ -393,15 +401,15 @@ class random_question_loader_test extends \advanced_testcase { /** * get_questions should allow limiting and offsetting of the result set. */ - public function test_get_questions_with_limit_and_offset() { + public function test_get_questions_with_limit_and_offset(): void { $this->resetAfterTest(); $numberofquestions = 5; $includesubcategories = false; $tagids = []; $limit = 1; $offset = 0; - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); - list($category, $questions) = $this->create_category_and_questions($numberofquestions); + $loader = new random_question_loader(new qubaid_list([])); + [$category, $questions] = $this->create_category_and_questions($numberofquestions); // Add questionid as key to find them easily later. $questionsbyid = []; @@ -428,15 +436,15 @@ class random_question_loader_test extends \advanced_testcase { * get_questions should allow retrieving questions with only a subset of * fields populated. */ - public function test_get_questions_with_restricted_fields() { + public function test_get_questions_with_restricted_fields(): void { $this->resetAfterTest(); $includesubcategories = false; $tagids = []; $limit = 10; $offset = 0; $fields = ['id', 'name']; - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); - list($category, $questions) = $this->create_category_and_questions(1); + $loader = new random_question_loader(new qubaid_list([])); + [$category, $questions] = $this->create_category_and_questions(1); $filters = question_filter_test_helper::create_filters([$category->id], $includesubcategories, $tagids); $result = $loader->get_filtered_questions( @@ -458,8 +466,10 @@ class random_question_loader_test extends \advanced_testcase { /** * Data provider for the count_questions test. + * + * @return array testcases. */ - public function count_questions_test_cases() { + public static function count_questions_test_cases(): array { return [ 'empty category' => [ 'categoryindex' => 'emptycat', @@ -542,7 +552,7 @@ class random_question_loader_test extends \advanced_testcase { $includesubcategories, $usetagnames, $expectedcount - ) { + ): void { $this->resetAfterTest(); $categories = []; @@ -558,22 +568,22 @@ class random_question_loader_test extends \advanced_testcase { $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); // First category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['cat1', 'foo']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['cat1', 'foo']); $categories['cat1'] = $category; $questions['cat1q1'] = $categoryquestions[0]; $questions['cat1q2'] = $categoryquestions[1]; // Second category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['cat2', 'foo']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['cat2', 'foo']); $categories['cat2'] = $category; $questions['cat2q1'] = $categoryquestions[0]; $questions['cat2q2'] = $categoryquestions[1]; // Sub category and questions. - list($category, $categoryquestions) = $this->create_category_and_questions(2, ['subcat', 'foo'], $categories['cat1']); + [$category, $categoryquestions] = $this->create_category_and_questions(2, ['subcat', 'foo'], $categories['cat1']); $categories['subcat'] = $category; $questions['subcatq1'] = $categoryquestions[0]; $questions['subcatq2'] = $categoryquestions[1]; // Empty category. - list($category, $categoryquestions) = $this->create_category_and_questions(0); + [$category, $categoryquestions] = $this->create_category_and_questions(0); $categories['emptycat'] = $category; // Generate the arguments for the get_questions function. @@ -583,7 +593,7 @@ class random_question_loader_test extends \advanced_testcase { }, $usetagnames); $filters = question_filter_test_helper::create_filters([$category->id], $includesubcategories, $tagids); - $loader = new \core_question\local\bank\random_question_loader(new qubaid_list([])); + $loader = new random_question_loader(new qubaid_list([])); $result = $loader->count_filtered_questions($filters); // Ensure the result matches what was expected. @@ -599,7 +609,7 @@ class random_question_loader_test extends \advanced_testcase { * @param stdClass|null $parentcategory The category to set as the parent of the created category. * @return array The category and questions. */ - protected function create_category_and_questions($questioncount, $tagnames = [], $parentcategory = null) { + protected function create_category_and_questions($questioncount, $tagnames = [], $parentcategory = null): array { $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); if ($parentcategory) {