class FieldLayoutBuilderTest

Same name in other branches
  1. 9 core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest
  2. 8.9.x core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest
  3. 10 core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest

@coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder @group field_layout

Hierarchy

Expanded class hierarchy of FieldLayoutBuilderTest

File

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

Namespace

Drupal\Tests\field_layout\Unit
View source
class FieldLayoutBuilderTest extends UnitTestCase {
    
    /**
     * @var \Drupal\Core\Layout\LayoutPluginManager|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $layoutPluginManager;
    
    /**
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $entityFieldManager;
    
    /**
     * @var \Drupal\field_layout\FieldLayoutBuilder
     */
    protected $fieldLayoutBuilder;
    
    /**
     * @var \Drupal\Core\Layout\LayoutInterface
     */
    protected $layoutPlugin;
    
    /**
     * @var \Drupal\Core\Layout\LayoutDefinition
     */
    protected $pluginDefinition;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->pluginDefinition = new LayoutDefinition([
            'library' => 'field_layout/drupal.layout.twocol',
            'theme_hook' => 'layout__twocol',
            'regions' => [
                'left' => [
                    'label' => 'Left',
                ],
                'right' => [
                    'label' => 'Right',
                ],
            ],
        ]);
        $this->layoutPlugin = new LayoutDefault([], 'two_column', $this->pluginDefinition);
        $this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
        $this->layoutPluginManager
            ->getDefinition('unknown', FALSE)
            ->willReturn(NULL);
        $this->layoutPluginManager
            ->getDefinition('two_column', FALSE)
            ->willReturn($this->pluginDefinition);
        $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
        $this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager
            ->reveal(), $this->entityFieldManager
            ->reveal());
    }
    
    /**
     * @covers ::buildView
     * @covers ::getFields
     */
    public function testBuildView() : void {
        $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',
                    ],
                ],
                '#in_preview' => FALSE,
                '#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);
    }
    
    /**
     * @covers ::buildForm
     * @covers ::getFields
     */
    public function testBuildForm() : void {
        $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\\RenderElementBase::processGroup',
                    ],
                    '#pre_render' => [
                        '\\Drupal\\Core\\Render\\Element\\RenderElementBase::preRenderGroup',
                    ],
                ],
                'right' => [
                    '#process' => [
                        '\\Drupal\\Core\\Render\\Element\\RenderElementBase::processGroup',
                    ],
                    '#pre_render' => [
                        '\\Drupal\\Core\\Render\\Element\\RenderElementBase::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);
    }
    
    /**
     * @covers ::buildForm
     */
    public function testBuildFormEmpty() : void {
        $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 = [
            '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',
            ],
            'non_configurable_field' => [
                'region' => 'left',
            ],
        ]);
        $expected = [
            'non_configurable_field' => [
                '#markup' => 'Non-configurable',
            ],
        ];
        $this->fieldLayoutBuilder
            ->buildForm($build, $display->reveal());
        $this->assertSame($expected, $build);
    }
    
    /**
     * @covers ::buildForm
     */
    public function testBuildFormNoLayout() : void {
        $this->entityFieldManager
            ->getFieldDefinitions(Argument::any(), Argument::any())
            ->shouldNotBeCalled();
        $build = [
            'test1' => [
                '#markup' => 'Test1',
            ],
        ];
        $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
        $display->getLayoutId()
            ->willReturn('unknown');
        $display->getLayoutSettings()
            ->willReturn([]);
        $display->getComponents()
            ->shouldNotBeCalled();
        $expected = [
            'test1' => [
                '#markup' => 'Test1',
            ],
        ];
        $this->fieldLayoutBuilder
            ->buildForm($build, $display->reveal());
        $this->assertSame($expected, $build);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FieldLayoutBuilderTest::$entityFieldManager protected property
FieldLayoutBuilderTest::$fieldLayoutBuilder protected property
FieldLayoutBuilderTest::$layoutPlugin protected property
FieldLayoutBuilderTest::$layoutPluginManager protected property
FieldLayoutBuilderTest::$pluginDefinition protected property
FieldLayoutBuilderTest::setUp protected function Overrides UnitTestCase::setUp
FieldLayoutBuilderTest::testBuildForm public function @covers ::buildForm
@covers ::getFields
FieldLayoutBuilderTest::testBuildFormEmpty public function @covers ::buildForm
FieldLayoutBuilderTest::testBuildFormNoLayout public function @covers ::buildForm
FieldLayoutBuilderTest::testBuildView public function @covers ::buildView
@covers ::getFields
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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