function FieldLayoutBuilderTest::testBuildForm

Same name and namespace in other branches
  1. 8.9.x core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest::testBuildForm()
  2. 10 core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest::testBuildForm()
  3. 11.x core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest::testBuildForm()

@covers ::buildForm @covers ::getFields

File

core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php, line 168

Class

FieldLayoutBuilderTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21field_layout%21src%21FieldLayoutBuilder.php/class/FieldLayoutBuilder/9" title="Builds a field layout." class="local">\Drupal\field_layout\FieldLayoutBuilder</a> @group field_layout

Namespace

Drupal\Tests\field_layout\Unit

Code

public function testBuildForm() {
    $definitions = [];
    $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $non_configurable_field_definition->isDisplayConfigurable('form')
        ->willReturn(FALSE);
    $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
    $this->entityFieldManager
        ->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')
        ->willReturn($definitions);
    $this->entityFieldManager
        ->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')
        ->willReturn([]);
    $build = [
        'test1' => [
            '#markup' => 'Test1',
        ],
        'test2' => [
            '#markup' => 'Test2',
            '#group' => 'existing_group',
        ],
        'test3' => [
            '#markup' => 'Test3',
        ],
        'field_layout' => [
            '#markup' => 'Field created through the UI happens to be named "Layout"',
        ],
        'non_configurable_field' => [
            '#markup' => 'Non-configurable',
        ],
    ];
    $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
    $display->getTargetEntityTypeId()
        ->willReturn('the_entity_type_id');
    $display->getTargetBundle()
        ->willReturn('the_entity_type_bundle');
    $display->getLayout()
        ->willReturn($this->layoutPlugin);
    $display->getLayoutId()
        ->willReturn('two_column');
    $display->getLayoutSettings()
        ->willReturn([]);
    $display->getComponents()
        ->willReturn([
        'test1' => [
            'region' => 'right',
        ],
        'test2' => [
            'region' => 'left',
        ],
        'test3' => [
            'region' => 'unknown_region',
        ],
        'field_layout' => [
            'region' => 'right',
        ],
        'non_configurable_field' => [
            'region' => 'left',
        ],
    ]);
    $expected = [
        'test1' => [
            '#markup' => 'Test1',
            '#group' => 'right',
        ],
        'test2' => [
            '#markup' => 'Test2',
            '#group' => 'existing_group',
        ],
        'test3' => [
            '#markup' => 'Test3',
        ],
        'field_layout' => [
            '#markup' => 'Field created through the UI happens to be named "Layout"',
            '#group' => 'right',
        ],
        'non_configurable_field' => [
            '#markup' => 'Non-configurable',
        ],
        '_field_layout' => [
            'left' => [
                '#process' => [
                    '\\Drupal\\Core\\Render\\Element\\RenderElement::processGroup',
                ],
                '#pre_render' => [
                    '\\Drupal\\Core\\Render\\Element\\RenderElement::preRenderGroup',
                ],
            ],
            'right' => [
                '#process' => [
                    '\\Drupal\\Core\\Render\\Element\\RenderElement::processGroup',
                ],
                '#pre_render' => [
                    '\\Drupal\\Core\\Render\\Element\\RenderElement::preRenderGroup',
                ],
            ],
            '#in_preview' => FALSE,
            '#settings' => [
                'label' => '',
            ],
            '#layout' => $this->pluginDefinition,
            '#theme' => 'layout__twocol',
            '#attached' => [
                'library' => [
                    'field_layout/drupal.layout.twocol',
                ],
            ],
        ],
    ];
    $this->fieldLayoutBuilder
        ->buildForm($build, $display->reveal());
    $this->assertEquals($expected, $build);
    $this->assertSame($expected, $build);
}

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