Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 26... Línea 26...
26
 * @category   test
26
 * @category   test
27
 * @copyright  2021 Sara Arjona (sara@moodle.com)
27
 * @copyright  2021 Sara Arjona (sara@moodle.com)
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @coversDefaultClass \core_adminpresets\manager
29
 * @coversDefaultClass \core_adminpresets\manager
30
 */
30
 */
31
class manager_test extends \advanced_testcase {
31
final class manager_test extends \advanced_testcase {
32
    /**
32
    /**
33
     * Include required libraries.
33
     * Include required libraries.
34
     */
34
     */
35
    public static function setUpBeforeClass(): void {
35
    public static function setUpBeforeClass(): void {
36
        global $CFG;
36
        global $CFG;
37
        require_once($CFG->libdir.'/adminlib.php');
37
        require_once($CFG->libdir.'/adminlib.php');
-
 
38
        parent::setUpBeforeClass();
38
    }
39
    }
Línea 39... Línea 40...
39
 
40
 
40
    /**
41
    /**
41
     * Test the behaviour of protected get_site_settings method.
42
     * Test the behaviour of protected get_site_settings method.
Línea 365... Línea 366...
365
    /**
366
    /**
366
     * Data provider for test_export_preset().
367
     * Data provider for test_export_preset().
367
     *
368
     *
368
     * @return array
369
     * @return array
369
     */
370
     */
370
    public function export_preset_provider(): array {
371
    public static function export_preset_provider(): array {
371
        return [
372
        return [
372
            'Export settings and plugins, excluding sensible' => [
373
            'Export settings and plugins, excluding sensible' => [
373
                'includesensible' => false,
374
                'includesensible' => false,
374
            ],
375
            ],
375
            'Export settings and plugins, including sensible' => [
376
            'Export settings and plugins, including sensible' => [
Línea 421... Línea 422...
421
     * @param bool $expecteddebugging Whether debugging message will be thrown or not.
422
     * @param bool $expecteddebugging Whether debugging message will be thrown or not.
422
     * @param string|null $expectedexception Expected exception class (if that's the case).
423
     * @param string|null $expectedexception Expected exception class (if that's the case).
423
     * @param string|null $expectedpresetname Expected preset name.
424
     * @param string|null $expectedpresetname Expected preset name.
424
     */
425
     */
425
    public function test_import_preset(string $filecontents, bool $expectedpreset, bool $expectedsettings = false,
426
    public function test_import_preset(string $filecontents, bool $expectedpreset, bool $expectedsettings = false,
426
            bool $expectedplugins = false, bool $expecteddebugging = false, string $expectedexception = null,
427
            bool $expectedplugins = false, bool $expecteddebugging = false, ?string $expectedexception = null,
427
            string $expectedpresetname = 'Imported preset'): void {
428
            string $expectedpresetname = 'Imported preset'): void {
428
        global $DB;
429
        global $DB;
Línea 429... Línea 430...
429
 
430
 
430
        $this->resetAfterTest();
431
        $this->resetAfterTest();
Línea 435... Línea 436...
435
        $currentadvitems = $DB->count_records('adminpresets_it_a');
436
        $currentadvitems = $DB->count_records('adminpresets_it_a');
Línea 436... Línea 437...
436
 
437
 
437
        // Call the method to be tested.
438
        // Call the method to be tested.
438
        $manager = new manager();
439
        $manager = new manager();
-
 
440
        try {
-
 
441
            $invokable = self::get_invokable();
439
        try {
442
            set_error_handler($invokable, E_WARNING);
-
 
443
            list($xml, $preset, $settingsfound, $pluginsfound) = $manager->import_preset($filecontents);
440
            list($xml, $preset, $settingsfound, $pluginsfound) = $manager->import_preset($filecontents);
444
            restore_error_handler();
441
        } catch (\exception $e) {
445
        } catch (\exception $e) {
442
            if ($expectedexception) {
446
            if ($expectedexception) {
443
                $this->assertInstanceOf($expectedexception, $e);
447
                $this->assertInstanceOf($expectedexception, $e);
444
            }
448
            }
Línea 503... Línea 507...
503
                        'block' => [
507
                        'block' => [
504
                            'html' => 0,
508
                            'html' => 0,
505
                            'activity_modules' => 1,
509
                            'activity_modules' => 1,
506
                        ],
510
                        ],
507
                        'mod' => [
511
                        'mod' => [
508
                            'chat' => 0,
512
                            'page' => 0,
509
                            'data' => 0,
513
                            'data' => 0,
510
                            'lesson' => 1,
514
                            'lesson' => 1,
511
                        ],
515
                        ],
512
                    ];
516
                    ];
513
                    foreach ($plugins as $plugin) {
517
                    foreach ($plugins as $plugin) {
Línea 528... Línea 532...
528
    /**
532
    /**
529
     * Data provider for test_import_preset().
533
     * Data provider for test_import_preset().
530
     *
534
     *
531
     * @return array
535
     * @return array
532
     */
536
     */
533
    public function import_preset_provider(): array {
537
    public static function import_preset_provider(): array {
534
        return [
538
        return [
535
            'Import settings from an empty file' => [
539
            'Import settings from an empty file' => [
536
                'filecontents' => '',
540
                'filecontents' => '',
537
                'expectedpreset' => false,
541
                'expectedpreset' => false,
538
            ],
542
            ],
539
            'Import settings and plugins from a valid XML file' => [
543
            'Import settings and plugins from a valid XML file' => [
-
 
544
                'filecontents' => file_get_contents(
540
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_settings_plugins.xml'),
545
                    filename: self::get_fixture_path(__NAMESPACE__, 'import_settings_plugins.xml')
-
 
546
                ),
541
                'expectedpreset' => true,
547
                'expectedpreset' => true,
542
                'expectedsettings' => true,
548
                'expectedsettings' => true,
543
                'expectedplugins' => true,
549
                'expectedplugins' => true,
544
            ],
550
            ],
545
            'Import only settings from a valid XML file' => [
551
            'Import only settings from a valid XML file' => [
-
 
552
                'filecontents' => file_get_contents(
546
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_settings.xml'),
553
                    filename: self::get_fixture_path(__NAMESPACE__, 'import_settings.xml')
-
 
554
                ),
547
                'expectedpreset' => true,
555
                'expectedpreset' => true,
548
                'expectedsettings' => true,
556
                'expectedsettings' => true,
549
                'expectedplugins' => false,
557
                'expectedplugins' => false,
550
            ],
558
            ],
551
            'Import settings and plugins from a valid XML file with Starter name, which will be marked as non-core' => [
559
            'Import settings and plugins from a valid XML file with Starter name, which will be marked as non-core' => [
-
 
560
                'filecontents' => file_get_contents(
552
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_starter_name.xml'),
561
                    filename: self::get_fixture_path(__NAMESPACE__, 'import_starter_name.xml')
-
 
562
                ),
553
                'expectedpreset' => true,
563
                'expectedpreset' => true,
554
                'expectedsettings' => true,
564
                'expectedsettings' => true,
555
                'expectedplugins' => true,
565
                'expectedplugins' => true,
556
                'expecteddebugging' => false,
566
                'expecteddebugging' => false,
557
                'expectedexception' => null,
567
                'expectedexception' => null,
558
                'expectedpresetname' => 'Starter',
568
                'expectedpresetname' => 'Starter',
559
            ],
569
            ],
560
            'Import settings from an invalid XML file' => [
570
            'Import settings from an invalid XML file' => [
-
 
571
                'filecontents' => file_get_contents(
561
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/invalid_xml_file.xml'),
572
                    filename: self::get_fixture_path(__NAMESPACE__, 'invalid_xml_file.xml')
-
 
573
                ),
562
                'expectedpreset' => false,
574
                'expectedpreset' => false,
563
                'expectedsettings' => false,
575
                'expectedsettings' => false,
564
                'expectedplugins' => false,
576
                'expectedplugins' => false,
565
                'expecteddebugging' => false,
577
                'expecteddebugging' => false,
566
                'expectedexception' => \Exception::class,
578
                'expectedexception' => \Exception::class,
567
            ],
579
            ],
568
            'Import unexisting settings category' => [
580
            'Import unexisting settings category' => [
-
 
581
                'filecontents' => file_get_contents(
569
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/unexisting_category.xml'),
582
                    filename: self::get_fixture_path(__NAMESPACE__, 'unexisting_category.xml')
-
 
583
                ),
570
                'expectedpreset' => false,
584
                'expectedpreset' => false,
571
                'expectedsettings' => false,
585
                'expectedsettings' => false,
572
                'expectedplugins' => false,
586
                'expectedplugins' => false,
573
            ],
587
            ],
574
            'Import unexisting setting' => [
588
            'Import unexisting setting' => [
-
 
589
                'filecontents' => file_get_contents(
575
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/unexisting_setting.xml'),
590
                    filename: self::get_fixture_path(__NAMESPACE__, 'unexisting_setting.xml')
-
 
591
                ),
576
                'expectedpreset' => false,
592
                'expectedpreset' => false,
577
                'expectedsettings' => false,
593
                'expectedsettings' => false,
578
                'expectedplugins' => false,
594
                'expectedplugins' => false,
579
                'expecteddebugging' => true,
595
                'expecteddebugging' => true,
580
            ],
596
            ],
581
            'Import valid settings with one unexisting setting too' => [
597
            'Import valid settings with one unexisting setting too' => [
-
 
598
                'filecontents' => file_get_contents(
582
                'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_settings_with_unexisting_setting.xml'),
599
                    filename: self::get_fixture_path(__NAMESPACE__, 'import_settings_with_unexisting_setting.xml'),
-
 
600
                ),
583
                'expectedpreset' => true,
601
                'expectedpreset' => true,
584
                'expectedsettings' => false,
602
                'expectedsettings' => false,
585
                'expectedplugins' => false,
603
                'expectedplugins' => false,
586
                'expecteddebugging' => true,
604
                'expecteddebugging' => true,
587
            ],
605
            ],
588
        ];
606
        ];
589
    }
607
    }
Línea 590... Línea -...
590
 
-
 
591
 
608
 
592
    /**
609
    /**
593
     * Test the behaviour of delete_preset() method when the preset id doesn't exist.
610
     * Test the behaviour of delete_preset() method when the preset id doesn't exist.
594
     *
611
     *
595
     * @covers ::delete_preset
612
     * @covers ::delete_preset
Línea 793... Línea 810...
793
        $this->assertArrayHasKey('glossary', $enabledplugins);
810
        $this->assertArrayHasKey('glossary', $enabledplugins);
794
        // This plugin won't change (because it had the same value than before the preset was applied).
811
        // This plugin won't change (because it had the same value than before the preset was applied).
795
        $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
812
        $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
796
        $this->assertArrayHasKey('truefalse', $enabledplugins);
813
        $this->assertArrayHasKey('truefalse', $enabledplugins);
797
    }
814
    }
-
 
815
 
-
 
816
    /**
-
 
817
     * Test apply_preset for a deprecated plugin type.
-
 
818
     *
-
 
819
     * @runInSeparateProcess
-
 
820
     *
-
 
821
     * @covers ::apply_preset
-
 
822
     * @return void
-
 
823
     */
-
 
824
    public function test_apply_preset_deprecated_plugintype(): void {
-
 
825
        global $CFG;
-
 
826
        $this->resetAfterTest();
-
 
827
 
-
 
828
        // Create a preset.
-
 
829
        $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
-
 
830
        $presetid = $generator->create_preset();
-
 
831
 
-
 
832
        // Add a mock plugin into the preset. This plugin will be mocked and deprecated below.
-
 
833
        helper::add_plugin($presetid, 'fake', 'fullfeatured', true);
-
 
834
 
-
 
835
        // Inject the mock plugin type + plugin, and deprecate it.
-
 
836
        $this->add_mocked_plugintype('fake', "$CFG->libdir/tests/fixtures/fakeplugins/fake", true);
-
 
837
        $this->add_mocked_plugin('fake', 'fullfeatured', $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured');
-
 
838
 
-
 
839
        // Pre-flight check to ensure that the plugin is deprecated.
-
 
840
        $pluginman = \core\plugin_manager::instance();
-
 
841
        $plugininfo = $pluginman->get_plugin_info('fake_fullfeatured');
-
 
842
        // Expected, unit-test-only debugging, since \core\plugin_manager isn't aware of the mock plugininfo class for this type.
-
 
843
        $this->assertDebuggingCalled();
-
 
844
        $this->assertTrue($plugininfo->is_deprecated());
-
 
845
 
-
 
846
        // Finally, check apply_preset method, verifying the deprecated plugin is skipped.
-
 
847
        $manager = new manager();
-
 
848
        [$applied, $skipped] = $manager->apply_preset($presetid);
-
 
849
        $this->assertdebuggingcalledcount(2); // Expected unit-test-only debugging, as above.
-
 
850
        $this->assertContains('fake', array_column($skipped, 'plugin'));
-
 
851
    }
798
}
852
}