class FieldLayoutBuilderTest

Same name in other branches
  1. 8.9.x core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest
  2. 10 core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php \Drupal\Tests\field_layout\Unit\FieldLayoutBuilderTest
  3. 11.x 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 19

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() {
        $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() {
        $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);
    }
    
    /**
     * @covers ::buildForm
     */
    public function testBuildFormEmpty() {
        $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() {
        $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 Deprecated Modifiers Object type Summary Overriden Title Overrides
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
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

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