Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 test_component\courseformat;
18
 
19
use core_courseformat\base as course_format;
20
use core_courseformat\output\local\content\section\controlmenu;
21
use core_courseformat\sectiondelegate as sectiondelegatebase;
22
use core_courseformat\stateupdates;
23
use section_info;
24
use renderer_base;
25
use action_menu;
26
 
27
/**
28
 * Test class for section delegate.
29
 *
30
 * @package    core_courseformat
31
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class sectiondelegate extends sectiondelegatebase {
35
 
36
    /** @var string force the default parent action menu. */
37
    public const MENUPARENT = 'parent';
38
 
39
    /** @var string force an empty action menu. */
40
    public const MENUEMPTY = 'empty';
41
 
42
    /** @var string force a null action menu. */
43
    public const MENUNULL = 'null';
44
 
1441 ariadna 45
    /** @var int The itemid to use to simulate disabled component. */
46
    public const DISABLEDITEMID = 999;
47
 
1 efrain 48
    /**
49
     * @var string|null Status to define which action menu to return when calling get_section_action_menu().
50
     * Alternatively, different testing classes could be created, but it wasn't worth it for this case.
51
     */
52
    protected ?string $actionmenustatus = self::MENUPARENT;
53
 
54
    /**
55
     * Test method to fake preprocesses the section name by appending a suffix to it.
56
     *
57
     * @param section_info $section The section information.
58
     * @param string|null $newname The new name for the section.
59
     * @return string|null The preprocessed section name with the suffix appended.
60
     */
61
    public function preprocess_section_name(section_info $section, ?string $newname): ?string {
62
        if (empty($newname)) {
63
            return 'null_name';
64
        }
65
        return $newname . '_suffix';
66
    }
67
 
68
    /**
69
     * Test method to add state updates of a section with additional information.
70
     *
71
     * @param section_info $section The section to update.
72
     * @param stateupdates $updates The state updates to apply.
73
     * @return void
74
     */
75
    public function put_section_state_extra_updates(section_info $section, stateupdates $updates): void {
76
        $updates->add_cm_put($section->itemid);
77
    }
78
 
79
    /**
80
     * Helper to change the behaviour of the get_section_action_menu(), for testing purposes, to return a different action menu
81
     * based on the value of $actionmenustatus. For instance:
82
     * - 'parent' returns the parent action menu.
83
     * - 'empty' returns an empty action menu.
84
     * - 'null' or null returns null action menu.
85
     *
86
     * @param string|null $actionmenustatus The status of the action menu.
87
     */
88
    public function set_section_action_menu(
89
        ?string $actionmenustatus,
90
    ) {
91
        $this->actionmenustatus = $actionmenustatus;
92
    }
93
 
94
    /**
95
     * Allow delegate plugin to modify the available section menu.
96
     * By default, it returns the parent action menu.
97
     *
98
     * @param course_format $format The course format instance.
99
     * @param controlmenu $controlmenu The control menu instance.
100
     * @param renderer_base $output The renderer instance.
101
     * @return action_menu|null The new action menu with the list of edit control items or null if no action menu is available.
102
     */
103
    public function get_section_action_menu(
104
        course_format $format,
105
        controlmenu $controlmenu,
106
        renderer_base $output,
107
    ): ?action_menu {
108
        switch ($this->actionmenustatus) {
109
            case self::MENUPARENT:
110
                return parent::get_section_action_menu($format, $controlmenu, $output);
111
 
112
            case self::MENUEMPTY:
113
                return new action_menu();
114
 
115
            case self::MENUNULL:
116
            default:
117
                return null;
118
        }
119
    }
1441 ariadna 120
 
121
    /**
122
     * Check if the delegate is enabled.
123
     *
124
     * To simulate a disabled component, the itemid is set to DISABLEDITEMID.
125
     *
126
     * @return bool
127
     */
128
    public function is_enabled(): bool {
129
        if ($this->sectioninfo->itemid === self::DISABLEDITEMID) {
130
            return false;
131
        }
132
        return true;
133
    }
1 efrain 134
}