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
declare(strict_types=1);
18
 
19
namespace core\plugininfo;
20
 
21
use advanced_testcase;
22
 
23
/**
24
 * Unit tests for the media plugininfo class.
25
 *
26
 * @package     core
27
 * @covers      \core\plugininfo\media
28
 * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class media_test extends advanced_testcase {
1 efrain 32
 
33
    /**
34
     * Test the get_enabled_plugins method.
35
     */
36
    public function test_get_enabled_plugins(): void {
37
        $this->resetAfterTest();
38
 
39
        $plugins = media::get_enabled_plugins();
40
        $this->assertArrayHasKey('videojs', $plugins);
41
        $this->assertArrayHasKey('youtube', $plugins);
42
 
43
        // Disable a plugin.
44
        media::enable_plugin('youtube', 0);
45
 
46
        $plugins = media::get_enabled_plugins();
47
        $this->assertArrayNotHasKey('youtube', $plugins);
48
        $this->assertArrayHasKey('videojs', $plugins);
49
    }
50
 
51
    /**
52
     * Test the is_uninstall_allowed method.
53
     *
54
     * @dataProvider is_uninstall_allowed_provider
55
     * @param string $plugin
56
     */
57
    public function test_is_uninstall_allowed(
58
        string $plugin,
59
    ): void {
60
        $pluginmanager = \core_plugin_manager::instance();
61
        $plugininfo = $pluginmanager->get_plugin_info("media_{$plugin}");
62
        $this->assertTrue($plugininfo->is_uninstall_allowed());
63
    }
64
 
65
    /**
66
     * Data provider for the is_uninstall_allowed tests.
67
     *
68
     * @return array
69
     */
1441 ariadna 70
    public static function is_uninstall_allowed_provider(): array {
1 efrain 71
        $plugins = media::get_enabled_plugins();
72
        return array_map(function ($plugin) {
73
            return [
74
                'plugin' => $plugin,
75
            ];
76
        }, array_keys($plugins));
77
    }
78
 
79
    /**
80
     * Ensure that change_plugin_order() changes the order of the plugins.
81
     *
82
     * @dataProvider change_plugin_order_provider
83
     * @param string $initialorder
84
     * @param string $pluginname
85
     * @param int $direction
1441 ariadna 86
     * @param array $expected
1 efrain 87
     */
88
    public function test_change_plugin_order(
89
        array $initialorder,
90
        string $pluginname,
91
        int $direction,
1441 ariadna 92
        array $expected,
1 efrain 93
    ): void {
94
        $this->resetAfterTest(true);
95
 
96
        media::set_enabled_plugins($initialorder);
97
        media::change_plugin_order($pluginname, $direction);
98
 
99
        $this->assertSame(
1441 ariadna 100
            $expected,
1 efrain 101
            array_keys(media::get_sorted_plugins()),
102
        );
103
    }
104
 
1441 ariadna 105
    public static function change_plugin_order_provider(): array {
1 efrain 106
        $pluginmanager = \core_plugin_manager::instance();
107
        $allplugins = $pluginmanager->get_plugins_of_type('media');
108
        \core_collator::asort_objects_by_method($allplugins, 'get_rank', \core_collator::SORT_NUMERIC);
109
        $getorder = function (array $plugins) use ($allplugins) {
110
            return array_merge(
111
                $plugins,
112
                array_diff(array_reverse(array_keys($allplugins)), array_values($plugins)),
113
            );
114
        };
115
 
116
        return [
117
            'Top item down one' => [
118
                'initialorder' => [
119
                    'videojs',
120
                    'html5audio',
121
                    'html5video',
122
                    'youtube',
123
                ],
124
                'pluginname' => 'videojs',
125
                'direction' => base::MOVE_DOWN,
126
                'expected' => $getorder([
127
                    'html5audio',
128
                    'videojs',
129
                    'html5video',
130
                    'youtube',
131
                ]),
132
            ],
133
            'Bottom item down one' => [
134
                'initialorder' => [
135
                    'videojs',
136
                    'html5audio',
137
                    'html5video',
138
                    'youtube',
139
                ],
140
                'pluginname' => 'youtube',
141
                'direction' => base::MOVE_DOWN,
142
                'expected' => $getorder([
143
                    'videojs',
144
                    'html5audio',
145
                    'html5video',
146
                    'youtube',
147
                ]),
148
            ],
149
            'Top item up' => [
150
                'initialorder' => [
151
                    'videojs',
152
                    'html5audio',
153
                    'html5video',
154
                    'youtube',
155
                ],
156
                'pluginname' => 'videojs',
157
                'direction' => base::MOVE_UP,
158
                'expected' => $getorder([
159
                    'videojs',
160
                    'html5audio',
161
                    'html5video',
162
                    'youtube',
163
                ]),
164
            ],
165
            'Disabled plugin' => [
166
                'initialorder' => [
167
                    'videojs',
168
                    'html5audio',
169
                    'html5video',
170
                ],
171
                'pluginname' => 'youtube',
172
                'direction' => base::MOVE_UP,
173
                'expected' => $getorder([
174
                    'videojs',
175
                    'html5audio',
176
                    'html5video',
177
                ]),
178
            ],
179
            'Non-existent plugin' => [
180
                'initialorder' => [
181
                    'videojs',
182
                    'html5audio',
183
                    'html5video',
184
                    'youtube',
185
                ],
186
                'pluginname' => 'moodletube',
187
                'direction' => base::MOVE_UP,
188
                'expected' => $getorder([
189
                    'videojs',
190
                    'html5audio',
191
                    'html5video',
192
                    'youtube',
193
                ]),
194
            ],
195
        ];
196
    }
197
}