function WidgetPluginManagerTest::testNotApplicableFallback

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/WidgetPluginManagerTest.php \Drupal\Tests\field\Kernel\WidgetPluginManagerTest::testNotApplicableFallback()
  2. 8.9.x core/modules/field/tests/src/Kernel/WidgetPluginManagerTest.php \Drupal\Tests\field\Kernel\WidgetPluginManagerTest::testNotApplicableFallback()
  3. 10 core/modules/field/tests/src/Kernel/WidgetPluginManagerTest.php \Drupal\Tests\field\Kernel\WidgetPluginManagerTest::testNotApplicableFallback()

Tests that getInstance falls back on default if current is not applicable.

See also

\Drupal\field\Tests\FormatterPluginManagerTest::testNotApplicableFallback()

File

core/modules/field/tests/src/Kernel/WidgetPluginManagerTest.php, line 33

Class

WidgetPluginManagerTest
Tests the field widget manager.

Namespace

Drupal\Tests\field\Kernel

Code

public function testNotApplicableFallback() : void {
    
    /** @var \Drupal\Core\Field\WidgetPluginManager $widget_plugin_manager */
    $widget_plugin_manager = \Drupal::service('plugin.manager.field.widget');
    $base_field_definition = BaseFieldDefinition::create('test_field')->setName('field_multi_widget_field');
    $widget_options = [
        'field_definition' => $base_field_definition,
        'form_mode' => 'default',
        'configuration' => [
            'type' => 'test_field_widget_multiple',
        ],
    ];
    $instance = $widget_plugin_manager->getInstance($widget_options);
    $this->assertEquals('test_field_widget_multiple', $instance->getPluginId());
    // Now do the same but with machine name field_onewidgetfield, because that
    // makes isApplicable() return FALSE.
    $base_field_definition->setName('field_onewidgetfield');
    $instance = $widget_plugin_manager->getInstance($widget_options);
    // Instance should be default widget.
    $this->assertNotSame('test_field_widget_multiple', $instance->getPluginId());
    $this->assertEquals('test_field_widget', $instance->getPluginId());
}

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