Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 21... Línea 21...
21
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
21
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
23
 */
24
namespace core_privacy\privacy;
24
namespace core_privacy\privacy;
Línea 25... Línea -...
25
 
-
 
26
defined('MOODLE_INTERNAL') || die();
-
 
27
 
25
 
28
use core_privacy\manager;
26
use core_privacy\manager;
29
use core_privacy\local\metadata\collection;
27
use core_privacy\local\metadata\collection;
30
use core_privacy\local\metadata\types\type;
28
use core_privacy\local\metadata\types\type;
31
use core_privacy\local\metadata\types\database_table;
-
 
32
use core_privacy\local\metadata\types\external_location;
-
 
33
use core_privacy\local\metadata\types\plugin_type_link;
29
use core_privacy\local\metadata\types\database_table;
-
 
30
use core_privacy\local\metadata\types\subsystem_link;
34
use core_privacy\local\metadata\types\subsystem_link;
31
 
Línea 35... Línea 32...
35
use core_privacy\local\metadata\types\user_preference;
32
// phpcs:disable moodle.PHPUnit.TestCaseProvider.dataProviderSyntaxMethodNotFound
36
 
33
 
37
/**
34
/**
38
 * Unit tests for all Privacy Providers.
35
 * Unit tests for all Privacy Providers.
39
 *
36
 *
40
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
37
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
41
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
39
 */
43
class provider_test extends \advanced_testcase {
40
final class provider_test extends \core\tests\plugin_checks_testcase {
44
    /**
41
    /**
45
     * Returns a list of frankenstyle names of core components (plugins and subsystems).
42
     * Returns a list of frankenstyle names of core components (plugins and subsystems).
46
     *
43
     *
47
     * @return array the array of frankenstyle component names with the relevant class name.
44
     * @return array the array of frankenstyle component names with the relevant class name.
48
     */
45
     */
49
    public function get_component_list() {
46
    public static function get_component_list(): array {
50
        $components = ['core' => [
47
        $components = ['core' => [
51
            'component' => 'core',
48
            'component' => 'core',
52
            'classname' => manager::get_provider_classname_for_component('core')
49
            'classname' => manager::get_provider_classname_for_component('core'),
53
        ]];
50
        ]];
54
        // Get all plugins.
51
        // Get all plugins.
55
        $plugintypes = \core_component::get_plugin_types();
52
        $plugintypes = \core_component::get_plugin_types();
Línea 78... Línea 75...
78
    }
75
    }
Línea 79... Línea 76...
79
 
76
 
80
    /**
77
    /**
81
     * Test that the specified null_provider works as expected.
78
     * Test that the specified null_provider works as expected.
-
 
79
     *
82
     *
80
     * @group        plugin_checks
83
     * @dataProvider null_provider_provider
81
     * @dataProvider null_provider_provider
84
     * @param   string  $component The name of the component.
82
     * @param   string  $component The name of the component.
85
     * @param   string  $classname The name of the class for privacy
83
     * @param   string  $classname The name of the class for privacy
86
     */
84
     */
87
    public function test_null_provider($component, $classname): void {
85
    public function test_null_provider($component, $classname): void {
88
        $reason = $classname::get_reason();
86
        $reason = $classname::get_reason();
Línea 89... Línea 87...
89
        $this->assertIsString($reason);
87
        $this->assertIsString($reason);
90
 
-
 
91
        $this->assertIsString(get_string($reason, $component));
88
 
Línea 92... Línea 89...
92
        $this->assertDebuggingNotCalled();
89
        $this->assertIsString(get_string($reason, $component));
93
    }
90
    }
94
 
91
 
95
    /**
92
    /**
96
     * Data provider for the null_provider tests.
93
     * Data provider for the null_provider tests.
97
     *
94
     *
98
     * @return array
95
     * @return array
99
     */
96
     */
100
    public function null_provider_provider() {
97
    public static function null_provider_provider(): array {
101
        return array_filter($this->get_component_list(), function($component) {
98
        return array_filter(self::get_component_list(), function($component): bool {
102
                return static::component_implements(
99
            return static::component_implements(
103
                    $component['classname'],
100
                $component['classname'],
104
                    \core_privacy\local\metadata\null_provider::class
101
                \core_privacy\local\metadata\null_provider::class
Línea 105... Línea 102...
105
                );
102
            );
106
        });
103
        });
107
    }
104
    }
-
 
105
 
108
 
106
    /**
109
    /**
107
     * Test that the specified metadata_provider works as expected.
110
     * Test that the specified metadata_provider works as expected.
108
     *
111
     *
109
     * @group        plugin_checks
112
     * @dataProvider metadata_provider_provider
110
     * @dataProvider metadata_provider_provider
Línea 148... Línea 146...
148
                // Summary is optional, but when provided must be a valid string identifier.
146
                // Summary is optional, but when provided must be a valid string identifier.
149
                $this->assertIsString($summary);
147
                $this->assertIsString($summary);
Línea 150... Línea 148...
150
 
148
 
151
                // Check that the string is also correctly defined.
149
                // Check that the string is also correctly defined.
152
                $this->assertIsString(get_string($summary, $component));
-
 
153
                $this->assertDebuggingNotCalled();
150
                $this->assertIsString(get_string($summary, $component));
Línea 154... Línea 151...
154
            }
151
            }
155
 
152
 
156
            if ($fields = $item->get_privacy_fields()) {
153
            if ($fields = $item->get_privacy_fields()) {
157
                // Privacy fields are optional, but when provided must be a valid string identifier.
154
                // Privacy fields are optional, but when provided must be a valid string identifier.
158
                foreach ($fields as $field => $identifier) {
155
                foreach ($fields as $field => $identifier) {
Línea 159... Línea 156...
159
                    $this->assertIsString($field);
156
                    $this->assertIsString($field);
160
                    $this->assertIsString($identifier);
157
                    $this->assertIsString($identifier);
161
 
-
 
162
                    // Check that the string is also correctly defined.
158
 
163
                    $this->assertIsString(get_string($identifier, $component));
159
                    // Check that the string is also correctly defined.
164
                    $this->assertDebuggingNotCalled();
160
                    $this->assertIsString(get_string($identifier, $component));
165
                }
161
                }
Línea 166... Línea 162...
166
            }
162
            }
167
        }
163
        }
168
    }
164
    }
-
 
165
 
169
 
166
    /**
170
    /**
167
     * Test that all providers implement some form of compliant provider.
171
     * Test that all providers implement some form of compliant provider.
168
     *
172
     *
169
     * @group        plugin_checks
173
     * @dataProvider get_component_list
170
     * @dataProvider get_component_list
Línea 180... Línea 177...
180
    }
177
    }
Línea 181... Línea 178...
181
 
178
 
182
    /**
179
    /**
183
     * Ensure that providers do not throw an error when processing a deleted user.
180
     * Ensure that providers do not throw an error when processing a deleted user.
184
     *
-
 
185
     * @dataProvider    is_user_data_provider
181
     *
186
     * @param   string  $component
-
 
187
     */
-
 
188
    public function test_component_understands_deleted_users($component): void {
-
 
189
        $this->resetAfterTest();
-
 
190
 
-
 
191
        // Create a user.
-
 
192
        $user = $this->getDataGenerator()->create_user();
-
 
193
 
-
 
194
        // Delete the user and their context.
-
 
195
        delete_user($user);
-
 
196
        $usercontext = \context_user::instance($user->id);
-
 
197
        $usercontext->delete();
-
 
198
 
-
 
199
        $contextlist = manager::component_class_callback($component, \core_privacy\local\request\core_user_data_provider::class,
-
 
200
                'get_contexts_for_userid', [$user->id]);
-
 
201
 
-
 
202
        $this->assertInstanceOf(\core_privacy\local\request\contextlist::class, $contextlist);
-
 
203
    }
-
 
204
 
-
 
205
    /**
-
 
206
     * Ensure that providers do not throw an error when processing a deleted user.
-
 
207
     *
182
     * @group           plugin_checks
208
     * @dataProvider    is_user_data_provider
183
     * @dataProvider    is_user_data_provider
209
     * @param   string  $component
184
     * @param   string  $component
210
     */
185
     */
211
    public function test_userdata_provider_implements_userlist($component): void {
186
    public function test_userdata_provider_implements_userlist($component): void {
Línea 216... Línea 191...
216
    /**
191
    /**
217
     * Data provider for the metadata\provider tests.
192
     * Data provider for the metadata\provider tests.
218
     *
193
     *
219
     * @return array
194
     * @return array
220
     */
195
     */
221
    public function metadata_provider_provider() {
196
    public static function metadata_provider_provider(): array {
222
        return array_filter($this->get_component_list(), function($component) {
197
        return array_filter(self::get_component_list(), function($component): bool {
223
                return static::component_implements(
198
            return static::component_implements(
224
                    $component['classname'],
199
                $component['classname'],
225
                    \core_privacy\local\metadata\provider::class
200
                \core_privacy\local\metadata\provider::class
226
                );
201
            );
227
        });
202
        });
228
    }
203
    }
Línea 229... Línea 204...
229
 
204
 
230
    /**
205
    /**
231
     * List of providers which implement the core_user_data_provider.
206
     * List of providers which implement the core_user_data_provider.
232
     *
207
     *
233
     * @return array
208
     * @return array
234
     */
209
     */
-
 
210
    public static function is_user_data_provider(): array {
-
 
211
        return array_map(
235
    public function is_user_data_provider() {
212
            fn ($data) => ['component' => $data['component']],
236
        return array_filter($this->get_component_list(), function($component) {
213
            array_filter(self::get_component_list(), function($component): bool {
237
                return static::component_implements(
214
                return static::component_implements(
238
                    $component['classname'],
215
                    $component['classname'],
239
                    \core_privacy\local\request\core_user_data_provider::class
216
                    \core_privacy\local\request\core_user_data_provider::class
-
 
217
                );
240
                );
218
            }),
241
        });
219
        );
Línea 242... Línea 220...
242
    }
220
    }
243
 
221
 
244
    /**
222
    /**
Línea 287... Línea 265...
287
        return $userfields;
265
        return $userfields;
288
    }
266
    }
Línea 289... Línea 267...
289
 
267
 
290
    /**
268
    /**
-
 
269
     * Test that all tables with user fields are covered by metadata providers
-
 
270
     *
-
 
271
     * @group        plugin_checks
-
 
272
     * @dataProvider get_component_list
-
 
273
     * @coversNothing
-
 
274
     * @param string $component frankenstyle component name, e.g. 'mod_assign'
291
     * Test that all tables with user fields are covered by metadata providers
275
     * @param string $classname the fully qualified provider classname
292
     */
276
     */
293
    public function test_table_coverage(): void {
277
    public function test_table_coverage(string $component, string $classname): void {
294
        global $DB;
278
        global $DB;
295
        $dbman = $DB->get_manager();
279
        $dbman = $DB->get_manager(); // Load DDL classes.
Línea 296... Línea 280...
296
        $tables = [];
280
        $tables = [];
297
 
281
 
298
        foreach ($dbman->get_install_xml_files() as $filename) {
282
        $filename = \core_component::get_component_directory($component) . '/db/install.xml';
299
            $xmldbfile = new \xmldb_file($filename);
283
        if (!file_exists($filename)) {
300
            if (!$xmldbfile->loadXMLStructure()) {
284
            $this->expectNotToPerformAssertions();
301
                continue;
285
            return;
302
            }
286
        }
Línea -... Línea 287...
-
 
287
        $xmldbfile = new \xmldb_file($filename);
-
 
288
        $this->assertTrue($xmldbfile->loadXMLStructure());;
-
 
289
 
303
            $structure = $xmldbfile->getStructure();
290
        $structure = $xmldbfile->getStructure();
304
            $tablelist = $structure->getTables();
291
        $tablelist = $structure->getTables();
305
 
292
 
306
            foreach ($tablelist as $table) {
-
 
307
                if ($fields = $this->get_userid_fields($table)) {
293
        foreach ($tablelist as $table) {
308
                    $tables[$table->getName()] = '  - ' . $table->getName() . ' (' . join(', ', $fields) . ')';
294
            if ($fields = $this->get_userid_fields($table)) {
Línea 309... Línea 295...
309
                }
295
                $tables[$table->getName()] = '  - ' . $table->getName() . ' (' . join(', ', $fields) . ')';
310
            }
296
            }
311
        }
297
        }
312
 
298
 
313
        $componentlist = $this->metadata_provider_provider();
299
        $componentlist = self::metadata_provider_provider();
314
        foreach ($componentlist as $componentarray) {
300
        foreach ($componentlist as $componentarray) {