Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 15... Línea 15...
15
// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
namespace core\hook;
17
namespace core\hook;
-
 
18
 
Línea 18... Línea 19...
18
 
19
use core\di;
19
use core\di;
20
use core\tests\fake_plugins_test_trait;
20
 
21
 
21
/**
22
/**
Línea 26... Línea 27...
26
 * @copyright 2022 Open LMS
27
 * @copyright 2022 Open LMS
27
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core\hook\manager
29
 * @covers \core\hook\manager
29
 */
30
 */
30
final class manager_test extends \advanced_testcase {
31
final class manager_test extends \advanced_testcase {
-
 
32
 
-
 
33
    use fake_plugins_test_trait;
-
 
34
 
31
    /**
35
    /**
32
     * Test public factory method to get hook manager.
36
     * Test public factory method to get hook manager.
33
     */
37
     */
34
    public function test_get_instance(): void {
38
    public function test_get_instance(): void {
35
        $manager = manager::get_instance();
39
        $manager = manager::get_instance();
Línea 346... Línea 350...
346
        // Confirm the deprecated callback is called as expected.
350
        // Confirm the deprecated callback is called as expected.
347
        $this->assertEquals(
351
        $this->assertEquals(
348
            'Called deprecated callback',
352
            'Called deprecated callback',
349
            component_callback('fake_hooktest', 'old_callback', [], null, true)
353
            component_callback('fake_hooktest', 'old_callback', [], null, true)
350
        );
354
        );
-
 
355
        $this->assertDebuggingNotCalled();
-
 
356
 
-
 
357
        // Forcefully modify the PHPUnit flag on the manager to ensure the debugging message is output.
-
 
358
        $manager = di::get(manager::class);
-
 
359
        $rp = new \ReflectionProperty($manager, 'phpunit');
-
 
360
        $rp->setValue($manager, false);
-
 
361
 
-
 
362
        component_callback('fake_hooktest', 'old_callback', [], null, true);
-
 
363
 
351
        $this->assertDebuggingCalled(
364
        $this->assertDebuggingCalled(
352
            'Callback old_callback in fake_hooktest component should be migrated to new hook ' .
365
            'Callback old_callback in fake_hooktest component should be migrated to new hook ' .
353
                'callback for fake_hooktest\hook\hook_replacing_callback'
366
                'callback for fake_hooktest\hook\hook_replacing_callback',
354
        );
367
        );
355
    }
368
    }
Línea 356... Línea 369...
356
 
369
 
357
    /**
370
    /**
Línea 419... Línea 432...
419
        // Confirm the deprecated class callback is called as expected.
432
        // Confirm the deprecated class callback is called as expected.
420
        $this->assertEquals(
433
        $this->assertEquals(
421
            'Called deprecated class callback',
434
            'Called deprecated class callback',
422
            component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true)
435
            component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true)
423
        );
436
        );
-
 
437
        $this->assertDebuggingNotCalled();
-
 
438
 
-
 
439
        // Forcefully modify the PHPUnit flag on the manager to ensure the debugging message is output.
-
 
440
        $manager = di::get(manager::class);
-
 
441
        $rp = new \ReflectionProperty($manager, 'phpunit');
-
 
442
        $rp->setValue($manager, false);
-
 
443
 
-
 
444
        component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true);
424
        $this->assertDebuggingCalled(
445
        $this->assertDebuggingCalled(
425
            'Callback callbacks::old_class_callback in fake_hooktest component should be migrated to new hook ' .
446
            'Callback callbacks::old_class_callback in fake_hooktest component should be migrated to new hook ' .
426
                'callback for fake_hooktest\hook\hook_replacing_class_callback'
447
                'callback for fake_hooktest\hook\hook_replacing_class_callback',
427
        );
448
        );
428
    }
449
    }
Línea 429... Línea 450...
429
 
450
 
430
    /**
451
    /**
Línea 463... Línea 484...
463
        $this->assertNull(component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true));
484
        $this->assertNull(component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true));
464
        $this->assertDebuggingNotCalled();
485
        $this->assertDebuggingNotCalled();
465
    }
486
    }
Línea 466... Línea 487...
466
 
487
 
-
 
488
    /**
-
 
489
     * Test verifying that callbacks for deprecated plugins are not returned and hook dispatching won't call into these plugins.
-
 
490
     *
-
 
491
     * @runInSeparateProcess
-
 
492
     * @return void
-
 
493
     */
-
 
494
    public function test_get_callbacks_for_hook_deprecated_plugintype(): void {
-
 
495
        $this->resetAfterTest();
-
 
496
 
-
 
497
        // Inject the fixture 'fake' plugin type into component sources, which includes a single 'fake_fullfeatured' plugin.
-
 
498
        // This 'fake_fullfeatured' plugin is an available plugin at this stage (not yet deprecated).
-
 
499
        $this->add_full_mocked_plugintype(
-
 
500
            plugintype: 'fake',
-
 
501
            path: 'lib/tests/fixtures/fakeplugins/fake',
-
 
502
        );
-
 
503
 
-
 
504
        // Force reset the static instance cache \core\hook\manager::$instance so that a fresh instance is instantiated, ensuring
-
 
505
        // the component lists are re-run and the hook manager can see the injected mock plugin and it's callbacks.
-
 
506
        // Note: we can't use \core\hook\manager::phpunit_get_instance() because that doesn't load in component callbacks from disk.
-
 
507
        $hookmanrc = new \ReflectionClass(\core\hook\manager::class);
-
 
508
        $hookmanrc->setStaticPropertyValue('instance', null);
-
 
509
        $manager = \core\hook\manager::get_instance();
-
 
510
 
-
 
511
        // Get all registered callbacks for the hook listened to by the mock plugin (after_course_created).
-
 
512
        $listeners = $manager->get_callbacks_for_hook(\core_course\hook\after_course_created::class);
-
 
513
        $componentswithcallbacks = array_column($listeners, 'component');
-
 
514
 
-
 
515
        // Verify the available mock plugin is returned as a listener.
-
 
516
        $this->assertContains('fake_fullfeatured', $componentswithcallbacks);
-
 
517
 
-
 
518
        // Deprecate the 'fake' plugin type.
-
 
519
        $this->deprecate_full_mocked_plugintype('fake');
-
 
520
 
-
 
521
        // Force a fresh plugin manager instance, again to ensure the up-to-date component lists are used.
-
 
522
        $hookmanrc->setStaticPropertyValue('instance', null);
-
 
523
        $manager = \core\hook\manager::get_instance();
-
 
524
 
-
 
525
        // And verify the plugin is now not returned as a listener, since it's deprecated.
-
 
526
        $listeners = $manager->get_callbacks_for_hook(\core_course\hook\after_course_created::class);
-
 
527
        $componentswithcallbacks = array_column($listeners, 'component');
-
 
528
        $this->assertNotContains('fake_fullfeatured', $componentswithcallbacks);
-
 
529
    }
-
 
530
 
467
    /**
531
    /**
468
     * Normalise the sort order of callbacks to help with asserts.
532
     * Normalise the sort order of callbacks to help with asserts.
469
     *
533
     *
470
     * @param array $callbacks
534
     * @param array $callbacks
471
     */
535
     */