function FieldNoteItemTest::setUp

Same name in other branches
  1. 3.x modules/field_permission_example/tests/src/Kernel/FieldNoteItemTest.php \Drupal\Tests\field_permission_example\Kernel\FieldNoteItemTest::setUp()

This sets up the entity_test and user types to use our example field plugins.

Overrides FieldKernelTestBase::setUp

File

modules/field_permission_example/tests/src/Kernel/FieldNoteItemTest.php, line 45

Class

FieldNoteItemTest
Tests our sticky-note field type.

Namespace

Drupal\Tests\field_permission_example\Kernel

Code

protected function setUp() : void {
    parent::setUp();
    $type_manager = $this->container
        ->get('entity_type.manager');
    // Set up our entity_type and user type for our new field:
    $type_manager->getStorage('field_storage_config')
        ->create([
        'field_name' => 'field_fieldnote',
        'entity_type' => 'entity_test',
        'type' => 'field_permission_example_fieldnote',
    ])
        ->save();
    $type_manager->getStorage('field_config')
        ->create([
        'entity_type' => 'entity_test',
        'field_name' => 'field_fieldnote',
        'bundle' => 'entity_test',
    ])
        ->save();
    // Create a form display for the default form mode, and
    // add our field type.
    $type_manager->getStorage('entity_form_display')
        ->create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'default',
        'status' => TRUE,
    ])
        ->setComponent('field_fieldnote', [
        'type' => 'field_permission_example_widget',
    ])
        ->save();
    // Now do this for the user type.
    $type_manager->getStorage('field_storage_config')
        ->create([
        'field_name' => 'user_fieldnote',
        'entity_type' => 'user',
        'type' => 'field_permission_example_fieldnote',
    ])
        ->save();
    $type_manager->getStorage('field_config')
        ->create([
        'entity_type' => 'user',
        'field_name' => 'user_fieldnote',
        'bundle' => 'user',
    ])
        ->save();
    // Fetch a form display for a user. This may already exist, so check as
    // Core does.
    // @see https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_get_form_display/8
    $entity_form_display = $type_manager->getStorage('entity_form_display')
        ->load('user.user.default');
    if (empty($entity_form_display)) {
        $entity_form_display = $type_manager->getStorage('entity_form_display')
            ->create([
            'targetEntityType' => 'user',
            'bundle' => 'user',
            'mode' => 'default',
            'status' => TRUE,
        ]);
    }
    // And add our fancy field to that display:
    $entity_form_display->setComponent('field_fieldnote', [
        'type' => 'field_permission_example_widget',
    ])
        ->save();
}