function HookAlterOrderTest::testProceduralModuleImplementsAlterOrder

Tests procedural implementations of module implements alter ordering.

File

core/tests/Drupal/KernelTests/Core/Hook/HookAlterOrderTest.php, line 35

Class

HookAlterOrderTest
@group Hook @group legacy

Namespace

Drupal\KernelTests\Core\Hook

Code

public function testProceduralModuleImplementsAlterOrder() : void {
    $this->assertAlterCallOrder($main_unaltered = [
        'aaa_hook_order_test_procedural_alter',
        'bbb_hook_order_test_procedural_alter',
        'ccc_hook_order_test_procedural_alter',
    ], 'procedural');
    $this->assertAlterCallOrder($sub_unaltered = [
        'aaa_hook_order_test_procedural_subtype_alter',
        'bbb_hook_order_test_procedural_subtype_alter',
        'ccc_hook_order_test_procedural_subtype_alter',
    ], 'procedural_subtype');
    $this->assertAlterCallOrder($combined_unaltered = [
        'aaa_hook_order_test_procedural_alter',
        'aaa_hook_order_test_procedural_subtype_alter',
        'bbb_hook_order_test_procedural_alter',
        'bbb_hook_order_test_procedural_subtype_alter',
        'ccc_hook_order_test_procedural_alter',
        'ccc_hook_order_test_procedural_subtype_alter',
    ], [
        'procedural',
        'procedural_subtype',
    ]);
    $move_b_down = function (array &$implementations) : void {
        // Move B to the end, no matter which hook.
        $group = $implementations['bbb_hook_order_test'];
        unset($implementations['bbb_hook_order_test']);
        $implementations['bbb_hook_order_test'] = $group;
    };
    $modules = [
        'aaa_hook_order_test',
        'bbb_hook_order_test',
        'ccc_hook_order_test',
    ];
    // Test with module B moved to the end for both hooks.
    ModuleImplementsAlter::set(function (array &$implementations, string $hook) use ($modules, $move_b_down) : void {
        if (!in_array($hook, [
            'procedural_alter',
            'procedural_subtype_alter',
        ])) {
            return;
        }
        $this->assertSame($modules, array_keys($implementations));
        $move_b_down($implementations);
    });
    \Drupal::service('kernel')->rebuildContainer();
    $this->assertAlterCallOrder($main_altered = [
        'aaa_hook_order_test_procedural_alter',
        'ccc_hook_order_test_procedural_alter',
        // The implementation of B has been moved.
'bbb_hook_order_test_procedural_alter',
    ], 'procedural');
    $this->assertAlterCallOrder($sub_altered = [
        'aaa_hook_order_test_procedural_subtype_alter',
        'ccc_hook_order_test_procedural_subtype_alter',
        // The implementation of B has been moved.
'bbb_hook_order_test_procedural_subtype_alter',
    ], 'procedural_subtype');
    $this->assertAlterCallOrder($combined_altered = [
        'aaa_hook_order_test_procedural_alter',
        'aaa_hook_order_test_procedural_subtype_alter',
        'ccc_hook_order_test_procedural_alter',
        'ccc_hook_order_test_procedural_subtype_alter',
        // The implementation of B has been moved.
'bbb_hook_order_test_procedural_alter',
        'bbb_hook_order_test_procedural_subtype_alter',
    ], [
        'procedural',
        'procedural_subtype',
    ]);
    // If the altered hook is not the first one, implementations are back in
    // their unaltered order.
    $this->assertAlterCallOrder($main_unaltered, [
        'other_main_type',
        'procedural',
    ]);
    $this->assertAlterCallOrder($sub_unaltered, [
        'other_main_type',
        'procedural_subtype',
    ]);
    $this->assertAlterCallOrder($combined_unaltered, [
        'other_main_type',
        'procedural',
        'procedural_subtype',
    ]);
    // Test with module B moved to the end for the main hook.
    ModuleImplementsAlter::set(function (array &$implementations, string $hook) use ($modules, $move_b_down) : void {
        if (!in_array($hook, [
            'procedural_alter',
            'procedural_subtype_alter',
        ])) {
            return;
        }
        $this->assertSame($modules, array_keys($implementations));
        if ($hook !== 'procedural_alter') {
            return;
        }
        $move_b_down($implementations);
    });
    \Drupal::service('kernel')->rebuildContainer();
    $this->assertAlterCallOrder($main_altered, 'procedural');
    $this->assertAlterCallOrder($sub_unaltered, 'procedural_subtype');
    $this->assertAlterCallOrder($combined_altered, [
        'procedural',
        'procedural_subtype',
    ]);
    // Test with module B moved to the end for the subtype hook.
    ModuleImplementsAlter::set(function (array &$implementations, string $hook) use ($modules, $move_b_down) : void {
        if (!in_array($hook, [
            'procedural_alter',
            'procedural_subtype_alter',
        ])) {
            return;
        }
        $this->assertSameCallList($modules, array_keys($implementations));
        if ($hook !== 'procedural_subtype_alter') {
            return;
        }
        $move_b_down($implementations);
    });
    \Drupal::service('kernel')->rebuildContainer();
    $this->assertAlterCallOrder($main_unaltered, 'procedural');
    $this->assertAlterCallOrder($sub_altered, 'procedural_subtype');
    $this->assertAlterCallOrder($combined_unaltered, [
        'procedural',
        'procedural_subtype',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.