function MetadataGeneratorTest::testEditorWithCustomMetadata

Same name in other branches
  1. 8.9.x core/modules/quickedit/tests/src/Kernel/MetadataGeneratorTest.php \Drupal\Tests\quickedit\Kernel\MetadataGeneratorTest::testEditorWithCustomMetadata()

Tests a field whose associated in-place editor generates custom metadata.

File

core/modules/quickedit/tests/src/Kernel/MetadataGeneratorTest.php, line 136

Class

MetadataGeneratorTest
Tests in-place field editing metadata.

Namespace

Drupal\Tests\quickedit\Kernel

Code

public function testEditorWithCustomMetadata() {
    $this->editorManager = $this->container
        ->get('plugin.manager.quickedit.editor');
    $this->editorSelector = new EditorSelector($this->editorManager, $this->container
        ->get('plugin.manager.field.formatter'));
    $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
    $this->editorManager = $this->container
        ->get('plugin.manager.quickedit.editor');
    $this->editorSelector = new EditorSelector($this->editorManager, $this->container
        ->get('plugin.manager.field.formatter'));
    $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
    // Create a rich text field.
    $field_name = 'field_rich';
    $field_label = 'Rich text field';
    $this->createFieldWithStorage($field_name, 'text', 1, $field_label, [], 'text_textfield', [
        'size' => 42,
    ], 'text_default', []);
    // Create a text format.
    $full_html_format = FilterFormat::create([
        'format' => 'full_html',
        'name' => 'Full HTML',
        'weight' => 1,
        'filters' => [
            'filter_htmlcorrector' => [
                'status' => 1,
            ],
        ],
    ]);
    $full_html_format->save();
    // Create an entity with values for this rich text field.
    $entity = EntityTest::create();
    $entity->{$field_name}->value = 'Test';
    $entity->{$field_name}->format = 'full_html';
    $entity->save();
    $entity = EntityTest::load($entity->id());
    // Verify metadata.
    $items = $entity->get($field_name);
    \Drupal::state()->set('quickedit_test_field_access', 'forbidden');
    $this->assertSame([
        'access' => FALSE,
    ], $this->metadataGenerator
        ->generateFieldMetadata($items, 'default'));
    \Drupal::state()->set('quickedit_test_field_access', 'neutral');
    $this->assertSame([
        'access' => FALSE,
    ], $this->metadataGenerator
        ->generateFieldMetadata($items, 'default'));
    \Drupal::state()->set('quickedit_test_field_access', 'allowed');
    $metadata = $this->metadataGenerator
        ->generateFieldMetadata($items, 'default');
    $expected = [
        'access' => TRUE,
        'label' => 'Rich text field',
        'editor' => 'wysiwyg',
        'custom' => [
            'format' => 'full_html',
        ],
    ];
    $this->assertEquals($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
}

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