function FormBuilderTest::providerTestChildAccessInheritance

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::providerTestChildAccessInheritance()
  2. 8.9.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::providerTestChildAccessInheritance()
  3. 11.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::providerTestChildAccessInheritance()

Data provider for testChildAccessInheritance.

Return value

array

File

core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php, line 681

Class

FormBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormBuilder @group Form

Namespace

Drupal\Tests\Core\Form

Code

public static function providerTestChildAccessInheritance() {
    $data = [];
    $element = [
        'child0' => [
            '#type' => 'checkbox',
        ],
        'child1' => [
            '#type' => 'checkbox',
        ],
        'child2' => [
            '#type' => 'fieldset',
            'child2.0' => [
                '#type' => 'checkbox',
            ],
            'child2.1' => [
                '#type' => 'checkbox',
            ],
            'child2.2' => [
                '#type' => 'checkbox',
            ],
        ],
    ];
    // Sets access FALSE on the root level, this should be inherited completely.
    $clone = $element;
    $clone['#access'] = FALSE;
    $expected_access = [];
    $expected_access[] = [
        [],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        FALSE,
    ];
    $data['access-false-root'] = [
        $clone,
        $expected_access,
    ];
    $clone = $element;
    $access_result = AccessResult::forbidden();
    $clone['#access'] = $access_result;
    $expected_access = [];
    $expected_access[] = [
        [],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        $access_result,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        $access_result,
    ];
    $data['access-forbidden-root'] = [
        $clone,
        $expected_access,
    ];
    // Allow access on the most outer level but set FALSE otherwise.
    $clone = $element;
    $clone['#access'] = TRUE;
    $clone['child0']['#access'] = FALSE;
    $expected_access = [];
    $expected_access[] = [
        [],
        TRUE,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        NULL,
    ];
    $data['access-true-root'] = [
        $clone,
        $expected_access,
    ];
    // Allow access on the most outer level but forbid otherwise.
    $clone = $element;
    $access_result_allowed = AccessResult::allowed();
    $clone['#access'] = $access_result_allowed;
    $access_result_forbidden = AccessResult::forbidden();
    $clone['child0']['#access'] = $access_result_forbidden;
    $expected_access = [];
    $expected_access[] = [
        [],
        $access_result_allowed,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        $access_result_forbidden,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        NULL,
    ];
    $data['access-allowed-root'] = [
        $clone,
        $expected_access,
    ];
    // Allow access on the most outer level, deny access on a parent, and allow
    // on a child. The denying should be inherited.
    $clone = $element;
    $clone['#access'] = TRUE;
    $clone['child2']['#access'] = FALSE;
    $clone['child2.0']['#access'] = TRUE;
    $clone['child2.1']['#access'] = TRUE;
    $clone['child2.2']['#access'] = TRUE;
    $expected_access = [];
    $expected_access[] = [
        [],
        TRUE,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        FALSE,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        FALSE,
    ];
    $data['access-mixed-parents'] = [
        $clone,
        $expected_access,
    ];
    $clone = $element;
    $clone['#access'] = $access_result_allowed;
    $clone['child2']['#access'] = $access_result_forbidden;
    $clone['child2.0']['#access'] = $access_result_allowed;
    $clone['child2.1']['#access'] = $access_result_allowed;
    $clone['child2.2']['#access'] = $access_result_allowed;
    $expected_access = [];
    $expected_access[] = [
        [],
        $access_result_allowed,
    ];
    $expected_access[] = [
        [
            'child0',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child1',
        ],
        NULL,
    ];
    $expected_access[] = [
        [
            'child2',
        ],
        $access_result_forbidden,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.0',
        ],
        $access_result_forbidden,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.1',
        ],
        $access_result_forbidden,
    ];
    $expected_access[] = [
        [
            'child2',
            'child2.2',
        ],
        $access_result_forbidden,
    ];
    $data['access-mixed-parents-object'] = [
        $clone,
        $expected_access,
    ];
    return $data;
}

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