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
/**
18
 * Subplugin test helper trait
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2023 - present, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Laurent David (laurent@call-learning.fr)
24
 */
25
 
26
namespace mod_bigbluebuttonbn\test;
27
 
28
use core_component;
29
use core_h5p\core;
30
use core_plugin_manager;
31
use mod_bigbluebuttonbn\extension;
32
use ReflectionClass;
33
 
34
trait subplugins_test_helper_trait {
35
    /**
36
     * Setup a fake extension plugin
37
     *
38
     * This is intended to behave in most case like a real subplugina and will
39
     * allow most functionalities to be tested.
40
     *
41
     * @param string $pluginname plugin name
42
     * @return void
43
     */
44
    protected function setup_fake_plugin(string $pluginname): void {
45
        global $CFG;
46
        require_once("$CFG->libdir/upgradelib.php");
47
        $bbbextpath = "{$CFG->dirroot}/mod/bigbluebuttonbn/tests/fixtures/extension";
48
        // This is similar to accesslib_test::setup_fake_plugin.
49
        $mockedcomponent = new ReflectionClass(core_component::class);
50
 
51
        $mockedplugins = $mockedcomponent->getProperty('plugins');
52
        $plugins = $mockedplugins->getValue();
53
        $plugins[extension::BBB_EXTENSION_PLUGIN_NAME] = [$pluginname => $bbbextpath . "/$pluginname"];
54
        $mockedplugins->setValue(null, $plugins);
55
 
56
        $mockedplugintypes = $mockedcomponent->getProperty('plugintypes');
57
        $pluginstypes = $mockedplugintypes->getValue();
58
        $pluginstypes[extension::BBB_EXTENSION_PLUGIN_NAME] = $bbbextpath;
59
        $mockedplugintypes->setValue(null, $pluginstypes);
60
 
61
        $fillclassmap = $mockedcomponent->getMethod('fill_classmap_cache');
62
        $fillclassmap->invoke(null);
63
 
64
        $fillfilemap = $mockedcomponent->getMethod('fill_filemap_cache');
65
        $fillfilemap->invoke(null);
66
 
67
        $mockedsubplugins = $mockedcomponent->getProperty('subplugins');
68
        $subplugins = $mockedsubplugins->getValue();
69
        $subplugins['mod_bigbluebuttonbn'][extension::BBB_EXTENSION_PLUGIN_NAME][] = $pluginname;
70
        $mockedsubplugins->setValue(null, $subplugins);
71
 
72
        // Now write the content of the cache in a file so we can use it later.
73
        $content = core_component::get_cache_content();
74
        self::write_fake_component_cache($content);
75
 
76
        // Make sure the plugin is installed.
77
        ob_start();
78
        upgrade_noncore(false);
79
        upgrade_finished();
80
        ob_end_clean();
81
 
82
        // Cache has been cleared so let's write it again.
83
        self::write_fake_component_cache($content);
84
 
85
    }
86
 
87
    /**
88
     * Write the content of the cache in a file for later use.
89
     *
90
     * This is used exclusively in behat test as the cache is filled with new values at each session/page load.
91
     *
92
     * @param string $content content of the cache
93
     * @return void
94
     */
95
    protected function write_fake_component_cache($content) {
96
        global $CFG;
97
        $cachefile = "$CFG->cachedir/core_component.php";
98
        if (file_exists($cachefile)) {
99
            // Stale cache detected!
100
            unlink($cachefile);
101
        }
102
 
103
        // Permissions might not be setup properly in installers.
104
        $dirpermissions = !isset($CFG->directorypermissions) ? 02777 : $CFG->directorypermissions;
105
        $filepermissions = !isset($CFG->filepermissions) ? ($dirpermissions & 0666) : $CFG->filepermissions;
106
 
107
        clearstatcache();
108
        $cachedir = dirname($cachefile);
109
        if (!is_dir($cachedir)) {
110
            mkdir($cachedir, $dirpermissions, true);
111
        }
112
 
113
        if ($fp = @fopen($cachefile . '.tmp', 'xb')) {
114
            fwrite($fp, $content);
115
            fclose($fp);
116
            @rename($cachefile . '.tmp', $cachefile);
117
            @chmod($cachefile, $filepermissions);
118
        }
119
        @unlink($cachefile . '.tmp'); // Just in case anything fails (race condition).
120
        core_component::invalidate_opcode_php_cache($cachefile);
121
 
122
    }
123
    /**
124
     * Uninstall a fake extension plugin
125
     *
126
     * This is intended to behave in most case like a real subplugina and will
127
     * allow most functionalities to be tested.
128
     *
129
     * @param string $pluginname plugin name
130
     * @return void
131
     */
132
    protected function uninstall_fake_plugin(string $pluginname): void {
133
        global $CFG;
134
        require_once("$CFG->libdir/adminlib.php");
135
        // We just need access to fill_all_caches so everything goes back to normal.
136
        // If we don't do this, there are some side effects that will make other test fails
137
        // (such as mod_bigbluebuttonbn\task\upgrade_recordings_task_test::test_upgrade_recordings_imported_basic).
138
        $cachefile = "$CFG->cachedir/core_component.php";
139
        if (file_exists($cachefile)) {
140
            // Stale cache detected!
141
            unlink($cachefile);
142
        }
143
        $mockedcomponent = new ReflectionClass(core_component::class);
144
        // Here we reset the plugin caches.
145
        $mockedplugintypes = $mockedcomponent->getProperty('plugintypes');
146
        $mockedplugintypes->setValue(null, null);
147
        $fillclassmap = $mockedcomponent->getMethod('init');
148
        $fillclassmap->invoke(null);
149
 
150
        // Now uninstall the plugin and clean everything up for other tests.
151
        $pluginman = core_plugin_manager::instance();
152
        $plugininfo = $pluginman->get_plugins();
153
        foreach ($plugininfo as $type => $plugins) {
154
            foreach ($plugins as $name => $plugin) {
155
                if ($name === $pluginname) {
156
                    ob_start();
157
                    uninstall_plugin($type, $name);
158
                    ob_end_clean();
159
                }
160
            }
161
        }
162
    }
163
}