View source
<?php
namespace Drupal\Tests\quickedit\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\quickedit\EditorSelector;
use Drupal\quickedit\MetadataGenerator;
use Drupal\quickedit_test\MockQuickEditEntityFieldAccessCheck;
use Drupal\filter\Entity\FilterFormat;
class MetadataGeneratorTest extends QuickEditTestBase {
protected static $modules = [
'quickedit_test',
];
protected $editorManager;
protected $metadataGenerator;
protected $editorSelector;
protected $accessChecker;
protected function setUp() : void {
parent::setUp();
$this->editorManager = $this->container
->get('plugin.manager.quickedit.editor');
$this->accessChecker = new MockQuickEditEntityFieldAccessCheck();
$this->editorSelector = new EditorSelector($this->editorManager, $this->container
->get('plugin.manager.field.formatter'));
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
}
public function testSimpleEntityType() {
$field_1_name = 'field_text';
$field_1_label = 'Plain text field';
$this
->createFieldWithStorage($field_1_name, 'string', 1, $field_1_label, [], 'string_textfield', [
'size' => 42,
], 'string', []);
$field_2_name = 'field_nr';
$field_2_label = 'Simple number field';
$this
->createFieldWithStorage($field_2_name, 'integer', 1, $field_2_label, [], 'number', [], 'number_integer', []);
$entity = EntityTest::create();
$entity->{$field_1_name}->value = 'Test';
$entity->{$field_2_name}->value = 42;
$entity
->save();
$entity = EntityTest::load($entity
->id());
$items_1 = $entity
->get($field_1_name);
$metadata_1 = $this->metadataGenerator
->generateFieldMetadata($items_1, 'default');
$expected_1 = [
'access' => TRUE,
'label' => 'Plain text field',
'editor' => 'plain_text',
];
$this
->assertEquals($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');
$items_2 = $entity
->get($field_2_name);
$metadata_2 = $this->metadataGenerator
->generateFieldMetadata($items_2, 'default');
$expected_2 = [
'access' => TRUE,
'label' => 'Simple number field',
'editor' => 'form',
];
$this
->assertEquals($expected_2, $metadata_2, 'The correct metadata is generated for the second field.');
}
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);
$field_name = 'field_rich';
$field_label = 'Rich text field';
$this
->createFieldWithStorage($field_name, 'text', 1, $field_label, [], 'text_textfield', [
'size' => 42,
], 'text_default', []);
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => [
'filter_htmlcorrector' => [
'status' => 1,
],
],
]);
$full_html_format
->save();
$entity = EntityTest::create();
$entity->{$field_name}->value = 'Test';
$entity->{$field_name}->format = 'full_html';
$entity
->save();
$entity = EntityTest::load($entity
->id());
$items = $entity
->get($field_name);
$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.');
}
}