Proyectos de Subversion Moodle

Rev

| 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
 
45
    /**
46
     * @var string|null Status to define which action menu to return when calling get_section_action_menu().
47
     * Alternatively, different testing classes could be created, but it wasn't worth it for this case.
48
     */
49
    protected ?string $actionmenustatus = self::MENUPARENT;
50
 
51
    /**
52
     * Test method to fake preprocesses the section name by appending a suffix to it.
53
     *
54
     * @param section_info $section The section information.
55
     * @param string|null $newname The new name for the section.
56
     * @return string|null The preprocessed section name with the suffix appended.
57
     */
58
    public function preprocess_section_name(section_info $section, ?string $newname): ?string {
59
        if (empty($newname)) {
60
            return 'null_name';
61
        }
62
        return $newname . '_suffix';
63
    }
64
 
65
    /**
66
     * Test method to add state updates of a section with additional information.
67
     *
68
     * @param section_info $section The section to update.
69
     * @param stateupdates $updates The state updates to apply.
70
     * @return void
71
     */
72
    public function put_section_state_extra_updates(section_info $section, stateupdates $updates): void {
73
        $updates->add_cm_put($section->itemid);
74
    }
75
 
76
    /**
77
     * Helper to change the behaviour of the get_section_action_menu(), for testing purposes, to return a different action menu
78
     * based on the value of $actionmenustatus. For instance:
79
     * - 'parent' returns the parent action menu.
80
     * - 'empty' returns an empty action menu.
81
     * - 'null' or null returns null action menu.
82
     *
83
     * @param string|null $actionmenustatus The status of the action menu.
84
     */
85
    public function set_section_action_menu(
86
        ?string $actionmenustatus,
87
    ) {
88
        $this->actionmenustatus = $actionmenustatus;
89
    }
90
 
91
    /**
92
     * Allow delegate plugin to modify the available section menu.
93
     * By default, it returns the parent action menu.
94
     *
95
     * @param course_format $format The course format instance.
96
     * @param controlmenu $controlmenu The control menu instance.
97
     * @param renderer_base $output The renderer instance.
98
     * @return action_menu|null The new action menu with the list of edit control items or null if no action menu is available.
99
     */
100
    public function get_section_action_menu(
101
        course_format $format,
102
        controlmenu $controlmenu,
103
        renderer_base $output,
104
    ): ?action_menu {
105
        switch ($this->actionmenustatus) {
106
            case self::MENUPARENT:
107
                return parent::get_section_action_menu($format, $controlmenu, $output);
108
 
109
            case self::MENUEMPTY:
110
                return new action_menu();
111
 
112
            case self::MENUNULL:
113
            default:
114
                return null;
115
        }
116
    }
117
}