1441 |
ariadna |
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 mod_subsection;
|
|
|
18 |
|
|
|
19 |
use availability_date\condition;
|
|
|
20 |
use core_availability\tree;
|
|
|
21 |
use core_courseformat\formatactions;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Tests for Subsection manager class.
|
|
|
25 |
*
|
|
|
26 |
* @covers \mod_subsection\manager
|
|
|
27 |
* @package mod_subsection
|
|
|
28 |
* @category test
|
|
|
29 |
* @copyright 2024 Ferran Recio <ferran@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
final class manager_test extends \advanced_testcase {
|
|
|
33 |
/**
|
|
|
34 |
* Test get_delegated_section_info.
|
|
|
35 |
*
|
|
|
36 |
* @covers ::get_delegated_section_info
|
|
|
37 |
* @dataProvider provider_test_get_delegated_section_info
|
|
|
38 |
* @param bool $hasavailability Whether the module has access restrictions.
|
|
|
39 |
* @param bool $visible Whether the module is visible.
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
public function test_get_delegated_section_info(
|
|
|
43 |
bool $hasavailability,
|
|
|
44 |
bool $visible
|
|
|
45 |
): void {
|
|
|
46 |
global $DB;
|
|
|
47 |
|
|
|
48 |
$this->resetAfterTest();
|
|
|
49 |
|
|
|
50 |
// Set up the availability settings.
|
|
|
51 |
$availabilityjson = null;
|
|
|
52 |
if ($hasavailability) {
|
|
|
53 |
$operation = condition::DIRECTION_FROM;
|
|
|
54 |
$availabilityjson = json_encode(tree::get_root_json(
|
|
|
55 |
[
|
|
|
56 |
condition::get_json($operation, time() + 3600),
|
|
|
57 |
],
|
|
|
58 |
'&',
|
|
|
59 |
true
|
|
|
60 |
));
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
|
|
|
64 |
$module = $this->getDataGenerator()->create_module(
|
|
|
65 |
'subsection',
|
|
|
66 |
(object)['course' => $course->id, 'section' => 2],
|
|
|
67 |
['visible' => $visible, 'availability' => $availabilityjson]
|
|
|
68 |
);
|
|
|
69 |
|
|
|
70 |
$cm = get_coursemodule_from_id('subsection', $module->cmid, 0, false, MUST_EXIST);
|
|
|
71 |
$manager = manager::create_from_coursemodule($cm);
|
|
|
72 |
$sectioninfo = $manager->get_delegated_section_info();
|
|
|
73 |
$this->assertInstanceOf(\section_info::class, $sectioninfo);
|
|
|
74 |
$this->assertEquals($cm->instance, $sectioninfo->itemid);
|
|
|
75 |
$this->assertEquals($cm->name, $sectioninfo->name);
|
|
|
76 |
$this->assertEquals($cm->visible, $sectioninfo->visible);
|
|
|
77 |
$this->assertEquals($cm->availability, $sectioninfo->availability);
|
|
|
78 |
$initialid = $sectioninfo->id;
|
|
|
79 |
|
|
|
80 |
// When subsections are disabled, all subsections are considered orphaned
|
|
|
81 |
// and can be removed without affecting the course_module. This should regenerate
|
|
|
82 |
// the delegated section once the module is re-enabled.
|
|
|
83 |
$pluginmanager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
84 |
$pluginmanager::enable_plugin('subsection', 0);
|
|
|
85 |
formatactions::section($course)->delete($sectioninfo);
|
|
|
86 |
rebuild_course_cache($course->id, true);
|
|
|
87 |
$pluginmanager::enable_plugin('subsection', 1);
|
|
|
88 |
rebuild_course_cache($course->id, true);
|
|
|
89 |
|
|
|
90 |
$cm = get_coursemodule_from_id('subsection', $module->cmid, 0, false, MUST_EXIST);
|
|
|
91 |
$manager = manager::create_from_coursemodule($cm);
|
|
|
92 |
$sectioninfo = $manager->get_delegated_section_info();
|
|
|
93 |
$this->assertInstanceOf(\section_info::class, $sectioninfo);
|
|
|
94 |
$this->assertEquals($cm->instance, $sectioninfo->itemid);
|
|
|
95 |
$this->assertEquals($cm->name, $sectioninfo->name);
|
|
|
96 |
$this->assertEquals($cm->visible, $sectioninfo->visible);
|
|
|
97 |
$this->assertEquals($cm->availability, $sectioninfo->availability);
|
|
|
98 |
|
|
|
99 |
// The section should be different from the previous one.
|
|
|
100 |
$this->assertNotEquals($initialid, $sectioninfo->id);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Data provider for test_get_delegated_section_info.
|
|
|
105 |
*
|
|
|
106 |
* @return array
|
|
|
107 |
*/
|
|
|
108 |
public static function provider_test_get_delegated_section_info(): array {
|
|
|
109 |
return [
|
|
|
110 |
'Module is visible with no restrictions' => [
|
|
|
111 |
'hasavailability' => false,
|
|
|
112 |
'visible' => true,
|
|
|
113 |
],
|
|
|
114 |
'Module is visible with restrictions' => [
|
|
|
115 |
'hasavailability' => true,
|
|
|
116 |
'visible' => true,
|
|
|
117 |
],
|
|
|
118 |
'Module is hidden with no restrictions' => [
|
|
|
119 |
'hasavailability' => false,
|
|
|
120 |
'visible' => false,
|
|
|
121 |
],
|
|
|
122 |
'Module is hidden with restrictions' => [
|
|
|
123 |
'hasavailability' => true,
|
|
|
124 |
'visible' => false,
|
|
|
125 |
],
|
|
|
126 |
|
|
|
127 |
];
|
|
|
128 |
}
|
|
|
129 |
}
|