MDL-82541 core_courseformat: delegated sections in disabled plugins
This commit is contained in:
@@ -59,7 +59,11 @@ abstract class sectiondelegate {
|
||||
if ($classname === null) {
|
||||
return null;
|
||||
}
|
||||
return new $classname($sectioninfo);
|
||||
$instance = new $classname($sectioninfo);
|
||||
if (!$instance->is_enabled()) {
|
||||
return null;
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,6 +88,16 @@ abstract class sectiondelegate {
|
||||
return self::get_delegate_class_name($pluginname) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the delegate is enabled.
|
||||
*
|
||||
* Usually this happens when the delegate plugin is disabled.
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the section final name.
|
||||
*
|
||||
|
||||
@@ -51,11 +51,26 @@ abstract class sectiondelegatemodule extends sectiondelegate {
|
||||
) {
|
||||
parent::__construct($sectioninfo);
|
||||
|
||||
[$this->course, $this->cm] = get_course_and_cm_from_instance(
|
||||
$this->sectioninfo->itemid,
|
||||
$this->get_module_name(),
|
||||
$this->sectioninfo->course,
|
||||
);
|
||||
try {
|
||||
// Disabled or missing plugins can throw exceptions.
|
||||
[$this->course, $this->cm] = get_course_and_cm_from_instance(
|
||||
$this->sectioninfo->itemid,
|
||||
$this->get_module_name(),
|
||||
$this->sectioninfo->course,
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->cm = null;
|
||||
$this->course = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the delegated component is enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled(): bool {
|
||||
return $this->cm !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,6 +76,33 @@ class sectiondelegate_test extends \advanced_testcase {
|
||||
$this->assertNull(sectiondelegate::instance($sectioninfos[3]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the instance method returns null when the delegate class is disabled.
|
||||
*
|
||||
* @covers ::instance
|
||||
*/
|
||||
public function test_instance_disabled(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3]);
|
||||
|
||||
// Section 2 has an existing delegate class.
|
||||
course_update_section(
|
||||
$course,
|
||||
$DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]),
|
||||
[
|
||||
'component' => 'test_component',
|
||||
'itemid' => testsectiondelegate::DISABLEDITEMID,
|
||||
]
|
||||
);
|
||||
|
||||
$modinfo = get_fast_modinfo($course->id);
|
||||
$sectioninfos = $modinfo->get_section_info_all();
|
||||
|
||||
$this->assertNull(sectiondelegate::instance($sectioninfos[2]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test has_delegate_class().
|
||||
*
|
||||
|
||||
@@ -2298,11 +2298,9 @@ class global_navigation extends navigation_node {
|
||||
$parentnode = $coursenode;
|
||||
|
||||
// Set the parent node to the parent section if this is a delegated section.
|
||||
if ($section->is_delegated()) {
|
||||
$parentsection = $section->get_component_instance()->get_parent_section();
|
||||
if ($parentsection) {
|
||||
$parentnode = $coursenode->find($parentsection->id, self::TYPE_SECTION) ?: $coursenode;
|
||||
}
|
||||
$parentsection = $section->get_component_instance()?->get_parent_section();
|
||||
if ($parentsection) {
|
||||
$parentnode = $coursenode->find($parentsection->id, self::TYPE_SECTION) ?: $coursenode;
|
||||
}
|
||||
|
||||
$sectionname = get_section_name($course, $section);
|
||||
|
||||
+17
@@ -42,6 +42,9 @@ class sectiondelegate extends sectiondelegatebase {
|
||||
/** @var string force a null action menu. */
|
||||
public const MENUNULL = 'null';
|
||||
|
||||
/** @var int The itemid to use to simulate disabled component. */
|
||||
public const DISABLEDITEMID = 999;
|
||||
|
||||
/**
|
||||
* @var string|null Status to define which action menu to return when calling get_section_action_menu().
|
||||
* Alternatively, different testing classes could be created, but it wasn't worth it for this case.
|
||||
@@ -114,4 +117,18 @@ class sectiondelegate extends sectiondelegatebase {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the delegate is enabled.
|
||||
*
|
||||
* To simulate a disabled component, the itemid is set to DISABLEDITEMID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled(): bool {
|
||||
if ($this->sectioninfo->itemid === self::DISABLEDITEMID) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,17 +186,21 @@ class manager {
|
||||
/**
|
||||
* Get the delegated section info.
|
||||
*
|
||||
* @return section_info the delegated section info
|
||||
* @return section_info|null the delegated section info
|
||||
*/
|
||||
public function get_delegated_section_info(): section_info {
|
||||
public function get_delegated_section_info(): ?section_info {
|
||||
$delegatedsection = $this->cm->get_delegated_section_info();
|
||||
if (!$delegatedsection) {
|
||||
// Some restorations can produce a situation where the section is not found.
|
||||
// In that case, we create a new one.
|
||||
$delegatedsection = formatactions::section($this->cm->course)->create_delegated(
|
||||
self::PLUGINNAME,
|
||||
$this->cm->id,
|
||||
(object) ['name' => $this->instance->name],
|
||||
$this->cm->instance,
|
||||
(object) [
|
||||
'name' => $this->cm->name,
|
||||
'visible' => $this->cm->visible,
|
||||
'availability' => (!empty($this->cm->availability)) ? $this->cm->availability : null,
|
||||
],
|
||||
);
|
||||
}
|
||||
return $delegatedsection;
|
||||
|
||||
@@ -109,4 +109,40 @@ final class sectiondelegatemodule_test extends \advanced_testcase {
|
||||
$this->assertInstanceOf(stdClass::class, $delegatedsectioncourse);
|
||||
$this->assertEquals($course->id, $delegatedsectioncourse->id);
|
||||
}
|
||||
|
||||
public function test_instance_plugin_disabled(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
||||
$manager::enable_plugin('subsection', 1);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
|
||||
$module = $this->getDataGenerator()->create_module(
|
||||
'subsection',
|
||||
(object) ['course' => $course->id, 'section' => 2]
|
||||
);
|
||||
|
||||
// Get the section info for the delegated section.
|
||||
$sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
|
||||
|
||||
/** @var testsectiondelegatemodule $delegated */
|
||||
$delegated = sectiondelegate::instance($sectioninfo);
|
||||
$this->assertTrue($delegated->is_enabled());
|
||||
|
||||
// Disabling the plugin should disable the delegate.
|
||||
$manager::enable_plugin('subsection', 0);
|
||||
rebuild_course_cache($course->id, true);
|
||||
|
||||
$sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
|
||||
|
||||
/** @var testsectiondelegatemodule $delegated */
|
||||
$delegated = sectiondelegate::instance($sectioninfo);
|
||||
// Delegated from a disabled plugin are considered orphaned, not delegated.
|
||||
$this->assertNull($delegated);
|
||||
|
||||
// Section delegate should not be created directly but we do it
|
||||
// here to validate the is_enabled() method neverthless.
|
||||
$delegatedinstance = new sectiondelegate($sectioninfo);
|
||||
$this->assertFalse($delegatedinstance->is_enabled());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
<?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 mod_subsection;
|
||||
|
||||
use availability_date\condition;
|
||||
use core_availability\tree;
|
||||
use core_courseformat\formatactions;
|
||||
|
||||
/**
|
||||
* Tests for Subsection manager class.
|
||||
*
|
||||
* @covers \mod_subsection\manager
|
||||
* @package mod_subsection
|
||||
* @category test
|
||||
* @copyright 2024 Ferran Recio <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class manager_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test get_delegated_section_info.
|
||||
*
|
||||
* @covers ::get_delegated_section_info
|
||||
* @dataProvider provider_test_get_delegated_section_info
|
||||
* @param bool $hasavailability Whether the module has access restrictions.
|
||||
* @param bool $visible Whether the module is visible.
|
||||
* @return void
|
||||
*/
|
||||
public function test_get_delegated_section_info(
|
||||
bool $hasavailability,
|
||||
bool $visible
|
||||
): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$pluginmanager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
||||
$pluginmanager::enable_plugin('subsection', 1);
|
||||
|
||||
// Set up the availability settings.
|
||||
$availabilityjson = null;
|
||||
if ($hasavailability) {
|
||||
$operation = condition::DIRECTION_FROM;
|
||||
$availabilityjson = json_encode(tree::get_root_json(
|
||||
[
|
||||
condition::get_json($operation, time() + 3600),
|
||||
],
|
||||
'&',
|
||||
true
|
||||
));
|
||||
}
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
|
||||
$module = $this->getDataGenerator()->create_module(
|
||||
'subsection',
|
||||
(object)['course' => $course->id, 'section' => 2],
|
||||
['visible' => $visible, 'availability' => $availabilityjson]
|
||||
);
|
||||
|
||||
$cm = get_coursemodule_from_id('subsection', $module->cmid, 0, false, MUST_EXIST);
|
||||
$manager = manager::create_from_coursemodule($cm);
|
||||
$sectioninfo = $manager->get_delegated_section_info();
|
||||
$this->assertInstanceOf(\section_info::class, $sectioninfo);
|
||||
$this->assertEquals($cm->instance, $sectioninfo->itemid);
|
||||
$this->assertEquals($cm->name, $sectioninfo->name);
|
||||
$this->assertEquals($cm->visible, $sectioninfo->visible);
|
||||
$this->assertEquals($cm->availability, $sectioninfo->availability);
|
||||
$initialid = $sectioninfo->id;
|
||||
|
||||
// When subsections are disabled, all subsections are considered orphaned
|
||||
// and can be removed without affecting the course_module. This should regenerate
|
||||
// the delegated section once the module is re-enabled.
|
||||
$pluginmanager::enable_plugin('subsection', 0);
|
||||
formatactions::section($course)->delete($sectioninfo);
|
||||
rebuild_course_cache($course->id, true);
|
||||
$pluginmanager::enable_plugin('subsection', 1);
|
||||
rebuild_course_cache($course->id, true);
|
||||
|
||||
$cm = get_coursemodule_from_id('subsection', $module->cmid, 0, false, MUST_EXIST);
|
||||
$manager = manager::create_from_coursemodule($cm);
|
||||
$sectioninfo = $manager->get_delegated_section_info();
|
||||
$this->assertInstanceOf(\section_info::class, $sectioninfo);
|
||||
$this->assertEquals($cm->instance, $sectioninfo->itemid);
|
||||
$this->assertEquals($cm->name, $sectioninfo->name);
|
||||
$this->assertEquals($cm->visible, $sectioninfo->visible);
|
||||
$this->assertEquals($cm->availability, $sectioninfo->availability);
|
||||
|
||||
// The section should be different from the previous one.
|
||||
$this->assertNotEquals($initialid, $sectioninfo->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_get_delegated_section_info.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function provider_test_get_delegated_section_info(): array {
|
||||
return [
|
||||
'Module is visible with no restrictions' => [
|
||||
'hasavailability' => false,
|
||||
'visible' => true,
|
||||
],
|
||||
'Module is visible with restrictions' => [
|
||||
'hasavailability' => true,
|
||||
'visible' => true,
|
||||
],
|
||||
'Module is hidden with no restrictions' => [
|
||||
'hasavailability' => false,
|
||||
'visible' => false,
|
||||
],
|
||||
'Module is hidden with restrictions' => [
|
||||
'hasavailability' => true,
|
||||
'visible' => false,
|
||||
],
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user