View source  
  <?php
namespace Drupal\Tests\quickedit\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\quickedit\EditorSelector;
class EditorSelectionTest extends QuickEditTestBase {
  
  protected $editorManager;
  
  protected $editorSelector;
  protected function setUp() : void {
    parent::setUp();
    $this->editorManager = $this->container
      ->get('plugin.manager.quickedit.editor');
    $this->editorSelector = new EditorSelector($this->editorManager, $this->container
      ->get('plugin.manager.field.formatter'));
  }
  
  protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('entity_test');
    $storage
      ->resetCache([
      $entity_id,
    ]);
    $entity = $storage
      ->load($entity_id);
    $items = $entity
      ->get($field_name);
    $options = \Drupal::service('entity_display.repository')
      ->getViewDisplay('entity_test', 'entity_test', $view_mode)
      ->getComponent($field_name);
    return $this->editorSelector
      ->getEditor($options['type'], $items);
  }
  
  public function testText() {
    $field_name = 'field_text';
    $this
      ->createFieldWithStorage($field_name, 'string', 1, 'Simple text field', [], 'string_textfield', [
      'size' => 42,
    ], 'string', []);
    
    $entity = EntityTest::create();
    $entity->{$field_name}->value = 'Hello, world!';
    $entity
      ->save();
    
    $this
      ->assertEquals('plain_text', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality 1, the 'plain_text' editor is selected.");
    
    $this->fields->field_text_field_storage
      ->setCardinality(2);
    $this->fields->field_text_field_storage
      ->save();
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
  }
  
  public function testTextWysiwyg() {
    
    $this
      ->enableModules([
      'quickedit_test',
    ]);
    $this->editorManager = $this->container
      ->get('plugin.manager.quickedit.editor');
    $this->editorSelector = new EditorSelector($this->editorManager, $this->container
      ->get('plugin.manager.field.formatter'));
    $field_name = 'field_textarea';
    $this
      ->createFieldWithStorage($field_name, 'text', 1, 'Long text field', [], 'text_textarea', [
      'size' => 42,
    ], 'text_default', []);
    
    $entity = EntityTest::create();
    $entity->{$field_name}->value = 'Hello, world!';
    $entity->{$field_name}->format = 'filtered_html';
    $entity
      ->save();
    
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected.");
    
    $entity->{$field_name}->format = 'full_html';
    $entity
      ->save();
    $this
      ->assertEquals('wysiwyg', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected.");
    
    $this->fields->field_textarea_field_storage
      ->setCardinality(2);
    $this->fields->field_textarea_field_storage
      ->save();
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
  }
  
  public function testNumber() {
    $field_name = 'field_nr';
    $this
      ->createFieldWithStorage($field_name, 'integer', 1, 'Simple number field', [], 'number', [], 'number_integer', []);
    
    $entity = EntityTest::create();
    $entity->{$field_name}->value = 42;
    $entity
      ->save();
    
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality 1, the 'form' editor is selected.");
    
    $this->fields->field_nr_field_storage
      ->setCardinality(2);
    $this->fields->field_nr_field_storage
      ->save();
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
  }
}