FormatterPluginManagerTest.php

Same filename in other branches
  1. 9 core/modules/field/tests/src/Kernel/FormatterPluginManagerTest.php
  2. 8.9.x core/modules/field/tests/src/Kernel/FormatterPluginManagerTest.php
  3. 10 core/modules/field/tests/src/Kernel/FormatterPluginManagerTest.php

Namespace

Drupal\Tests\field\Kernel

File

core/modules/field/tests/src/Kernel/FormatterPluginManagerTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\field\Kernel;

use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Tests the field formatter plugin manager.
 *
 * @group field
 */
class FormatterPluginManagerTest extends FieldKernelTestBase {
    
    /**
     * Tests that getInstance falls back on default if current is not applicable.
     *
     * @see \Drupal\field\Tests\WidgetPluginManagerTest::testNotApplicableFallback()
     */
    public function testNotApplicableFallback() : void {
        
        /** @var \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager */
        $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter');
        $base_field_definition = BaseFieldDefinition::create('test_field')->setName('field_test_field');
        $formatter_options = [
            'field_definition' => $base_field_definition,
            'view_mode' => 'default',
            'configuration' => [
                'type' => 'field_test_applicable',
            ],
        ];
        $instance = $formatter_plugin_manager->getInstance($formatter_options);
        $this->assertEquals('field_test_applicable', $instance->getPluginId());
        // Now set name to something that makes isApplicable() return FALSE.
        $base_field_definition->setName('deny_applicable');
        $instance = $formatter_plugin_manager->getInstance($formatter_options);
        // Instance should be default widget.
        $this->assertNotSame('field_test_applicable', $instance->getPluginId());
        $this->assertEquals('field_test_default', $instance->getPluginId());
    }

}

Classes

Title Deprecated Summary
FormatterPluginManagerTest Tests the field formatter plugin manager.

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