function QuickEditIntegrationTest::testMetadata

Tests (custom) metadata when the formatted text editor is used.

File

core/modules/editor/tests/src/Kernel/QuickEditIntegrationTest.php, line 168

Class

QuickEditIntegrationTest
Tests Edit module integration (Editor module's inline editing support).

Namespace

Drupal\Tests\editor\Kernel

Code

public function testMetadata() {
    $this->editorManager = $this->container
        ->get('plugin.manager.quickedit.editor');
    $this->accessChecker = new MockQuickEditEntityFieldAccessCheck();
    $this->editorSelector = $this->container
        ->get('quickedit.editor.selector');
    $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
    // Create an entity with values for the field.
    $entity = EntityTest::create();
    $entity->{$this->fieldName}->value = 'Test';
    $entity->{$this->fieldName}->format = 'full_html';
    $entity->save();
    $entity = EntityTest::load($entity->id());
    // Verify metadata.
    $items = $entity->get($this->fieldName);
    \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' => 'Long text field',
        'editor' => 'editor',
        'custom' => [
            'format' => 'full_html',
            'formatHasTransformations' => FALSE,
        ],
    ];
    $this->assertEqual($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.