You are here

public function FieldLayoutBuilderTest::testBuildView 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::testBuildView()

@covers ::buildView @covers ::getFields

File

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

Class

FieldLayoutBuilderTest
@coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder @group field_layout

Namespace

Drupal\Tests\field_layout\Unit

Code

public function testBuildView() {
  $definitions = [];
  $non_configurable_field_definition = $this
    ->prophesize(FieldDefinitionInterface::class);
  $non_configurable_field_definition
    ->isDisplayConfigurable('view')
    ->willReturn(FALSE);
  $definitions['non_configurable_field'] = $non_configurable_field_definition
    ->reveal();
  $definitions['non_configurable_field_with_extra_field'] = $non_configurable_field_definition
    ->reveal();
  $this->entityFieldManager
    ->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')
    ->willReturn($definitions);
  $extra_fields = [];
  $extra_fields['non_configurable_field_with_extra_field'] = [
    'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
  ];
  $this->entityFieldManager
    ->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')
    ->willReturn($extra_fields);
  $build = [
    'test1' => [
      '#markup' => 'Test1',
    ],
    'test2' => [
      '#markup' => 'Test2',
    ],
    'non_configurable_field' => [
      '#markup' => 'Non-configurable',
    ],
    'non_configurable_field_with_extra_field' => [
      '#markup' => 'Non-configurable with extra field',
    ],
  ];
  $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' => 'unknown_region',
    ],
    'non_configurable_field' => [
      'region' => 'left',
    ],
    'non_configurable_field_with_extra_field' => [
      'region' => 'left',
    ],
  ]);
  $expected = [
    'test2' => [
      '#markup' => 'Test2',
    ],
    'non_configurable_field' => [
      '#markup' => 'Non-configurable',
    ],
    '_field_layout' => [
      'left' => [
        'non_configurable_field_with_extra_field' => [
          '#markup' => 'Non-configurable with extra field',
        ],
      ],
      'right' => [
        'test1' => [
          '#markup' => 'Test1',
        ],
      ],
      '#settings' => [
        'label' => '',
      ],
      '#layout' => $this->pluginDefinition,
      '#theme' => 'layout__twocol',
      '#attached' => [
        'library' => [
          'field_layout/drupal.layout.twocol',
        ],
      ],
    ],
  ];
  $this->fieldLayoutBuilder
    ->buildView($build, $display
    ->reveal());
  $this
    ->assertEquals($expected, $build);
  $this
    ->assertSame($expected, $build);
}