Skip to content

Commit 706480a

Browse files
committed
fixup! MDL-87177 question: Update page types returns for 5.2
1 parent 8f5f928 commit 706480a

2 files changed

Lines changed: 63 additions & 15 deletions

File tree

public/lib/questionlib.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,9 @@ function core_question_question_preview_pluginfile($previewcontext, $questionid,
18411841

18421842
/**
18431843
* Return a list of page types for questions and the page types for the current module/context.
1844+
*
1845+
* This list is used when displaying blocks on a question page, to provide the list of possible page type patterns for the block.
1846+
*
18441847
* @param string $pagetype current page type
18451848
* @param stdClass $parentcontext Block's parent context
18461849
* @param stdClass $currentcontext Current context of block
@@ -1854,6 +1857,7 @@ function question_page_type_list($pagetype, $parentcontext, $currentcontext): ar
18541857
'question-bank-exportquestions-export' => get_string('page-question-export', 'question'),
18551858
'question-bank-importquestions-import' => get_string('page-question-import', 'question'),
18561859
];
1860+
// If current page is in a module context, include the list of page types for that module, if it provides one.
18571861
if ($currentcontext && $currentcontext->contextlevel == CONTEXT_MODULE) {
18581862
[, $cm] = get_course_and_cm_from_cmid($currentcontext->instanceid);
18591863
$directory = core_component::get_plugin_directory('mod', $cm->modname);

public/lib/tests/questionlib_test.php

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use core\context\module;
2121
use core_question\local\bank\question_bank_helper;
2222
use mod_quiz\quiz_settings;
23+
use PHPUnit\Framework\Attributes\DataProvider;
2324
use question_bank;
2425

2526
defined('MOODLE_INTERNAL') || die();
@@ -1781,31 +1782,74 @@ public function test_move_question_set_references_category(): void {
17811782
}
17821783

17831784
/**
1784-
* Check the page type list is correct based on the context of the module the question is in.
1785+
* Provide examples of different modules and the page types returned by their _page_type_list function.
1786+
*
1787+
* @return array[] Module name, and a list of page types.
1788+
*/
1789+
public static function module_page_types(): array {
1790+
return [
1791+
'quiz' => [
1792+
'module' => 'quiz',
1793+
'pagetypes' => [
1794+
'mod-quiz-*',
1795+
'mod-quiz-view',
1796+
'mod-quiz-attempt',
1797+
'mod-quiz-summary',
1798+
'mod-quiz-review',
1799+
'mod-quiz-edit',
1800+
'mod-quiz-report',
1801+
],
1802+
],
1803+
'forum' => [
1804+
'module' => 'forum',
1805+
'pagetypes' => [
1806+
'mod-forum-*',
1807+
'mod-forum-view',
1808+
'mod-forum-discuss',
1809+
],
1810+
],
1811+
'scorm' => [
1812+
'module' => 'scorm',
1813+
'pagetypes' => [
1814+
'mod-scorm-*',
1815+
],
1816+
],
1817+
'lti' => [ // LTI has no additional page types.
1818+
'module' => 'lti',
1819+
'pagetypes' => [],
1820+
],
1821+
];
1822+
}
1823+
1824+
/**
1825+
* Calling question_page_type_list should return the question page types, plus the page types for the module context.
17851826
*
17861827
* @covers ::question_page_type_list()
1828+
* @param string $module The module name.
1829+
* @param string[] $pagetypes A list of the page type patterns this module should add.
17871830
*/
1788-
public function test_question_page_type_list(): void {
1831+
#[DataProvider('module_page_types')]
1832+
public function test_question_page_type_list(string $module, array $pagetypes): void {
17891833
$this->resetAfterTest();
17901834
$this->setAdminUser();
17911835

17921836
$course = $this->getDataGenerator()->create_course();
17931837
$coursecontext = course::instance($course->id);
17941838

1795-
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
1796-
$quizcontext = module::instance($quiz->cmid);
1839+
$modulerecord = $this->getDataGenerator()->create_module($module, ['course' => $course->id]);
1840+
$modulecontext = module::instance($modulerecord->cmid);
17971841

1798-
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
1799-
$forumcontext = module::instance($forum->cmid);
1800-
1801-
$scorm = $this->getDataGenerator()->create_module('scorm', ['course' => $course->id]);
1802-
$scormcontext = module::instance($scorm->cmid);
1842+
$questionpagetypes = [
1843+
'question-*',
1844+
'question-edit',
1845+
'question-bank-managecategories-category',
1846+
'question-bank-exportquestions-export',
1847+
'question-bank-importquestions-import',
1848+
];
18031849

1804-
// Function quiz_page_type_list has 7 additional page types.
1805-
$this->assertCount(12, question_page_type_list('question-edit', $coursecontext, $quizcontext));
1806-
// Function forum_page_type_list has 3 additional page types.
1807-
$this->assertCount(8, question_page_type_list('question-edit', $coursecontext, $forumcontext));
1808-
// Function scorm_page_type_list has 1 additional page types.
1809-
$this->assertCount(6, question_page_type_list('question-edit', $coursecontext, $scormcontext));
1850+
$expectedpagetypes = array_merge($pagetypes, $questionpagetypes);
1851+
$actualpagetypes = question_page_type_list('question-edit', $coursecontext, $modulecontext);
1852+
// Check that the expected page type patterns match those returned for this module.
1853+
$this->assertEquals($expectedpagetypes, array_keys($actualpagetypes));
18101854
}
18111855
}

0 commit comments

Comments
 (0)