You are here

public function FieldLayoutBuilderTest::testBuildForm in Drupal 8

Same name and namespace in other branches
  1. 9 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 167

Class

FieldLayoutBuilderTest
@coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder @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',
        ],
      ],
      '#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);
}