Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 core_privacy\privacy;
18
 
19
use core_privacy\manager;
20
 
21
/**
22
 * Slow unit tests for all Privacy Providers that require database modifications.
23
 *
24
 * @package     core_privacy
25
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
26
 * @copyright   2025 Petr Skoda
27
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
final class provider_advanced_test extends \advanced_testcase {
30
    /**
31
     * Returns a list of frankenstyle names of core components (plugins and subsystems).
32
     *
33
     * @return array the array of frankenstyle component names with the relevant class name.
34
     */
35
    public static function get_component_list(): array {
36
        $components = ['core' => [
37
            'component' => 'core',
38
            'classname' => manager::get_provider_classname_for_component('core'),
39
        ]];
40
        // Get all plugins.
41
        $plugintypes = \core_component::get_plugin_types();
42
        foreach ($plugintypes as $plugintype => $typedir) {
43
            $plugins = \core_component::get_plugin_list($plugintype);
44
            foreach ($plugins as $pluginname => $plugindir) {
45
                $frankenstyle = $plugintype . '_' . $pluginname;
46
                $components[$frankenstyle] = [
47
                    'component' => $frankenstyle,
48
                    'classname' => manager::get_provider_classname_for_component($frankenstyle),
49
                ];
50
 
51
            }
52
        }
53
        // Get all subsystems.
54
        foreach (\core_component::get_core_subsystems() as $name => $path) {
55
            if (isset($path)) {
56
                $frankenstyle = 'core_' . $name;
57
                $components[$frankenstyle] = [
58
                    'component' => $frankenstyle,
59
                    'classname' => manager::get_provider_classname_for_component($frankenstyle),
60
                ];
61
            }
62
        }
63
        return $components;
64
    }
65
 
66
    /**
67
     * Ensure that providers do not throw an error when processing a deleted user.
68
     *
69
     * @group           plugin_checks
70
     * @dataProvider    is_user_data_provider
71
     * @coversNothing
72
     * @param   string  $component
73
     */
74
    public function test_component_understands_deleted_users($component): void {
75
        $this->resetAfterTest();
76
 
77
        // Create a user.
78
        $user = $this->getDataGenerator()->create_user();
79
 
80
        // Delete the user and their context.
81
        delete_user($user);
82
        $usercontext = \context_user::instance($user->id);
83
        $usercontext->delete();
84
 
85
        $contextlist = manager::component_class_callback($component, \core_privacy\local\request\core_user_data_provider::class,
86
            'get_contexts_for_userid', [$user->id]);
87
 
88
        $this->assertInstanceOf(\core_privacy\local\request\contextlist::class, $contextlist);
89
    }
90
 
91
    /**
92
     * List of providers which implement the core_user_data_provider.
93
     *
94
     * @return array
95
     */
96
    public static function is_user_data_provider(): array {
97
        return array_map(
98
            fn ($data) => ['component' => $data['component']],
99
            array_filter(self::get_component_list(), function($component): bool {
100
                return static::component_implements(
101
                    $component['classname'],
102
                    \core_privacy\local\request\core_user_data_provider::class
103
                );
104
            }),
105
        );
106
    }
107
 
108
    /**
109
     * Checks whether the component's provider class implements the specified interface, either directly or as a grandchild.
110
     *
111
     * @param   string  $providerclass The name of the class to test.
112
     * @param   string  $interface the name of the interface we want to check.
113
     * @return  bool    Whether the class implements the interface.
114
     */
115
    protected static function component_implements($providerclass, $interface) {
116
        if (class_exists($providerclass) && interface_exists($interface)) {
117
            return is_subclass_of($providerclass, $interface);
118
        }
119
 
120
        return false;
121
    }
122
}