View source  
  <?php
namespace Drupal\Tests\quickedit\Kernel;
use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
use Drupal\Core\Language\LanguageInterface;
use Drupal\editor\Entity\Editor;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\quickedit\MetadataGenerator;
use Drupal\quickedit_test\MockQuickEditEntityFieldAccessCheck;
use Drupal\editor\EditorController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Drupal\filter\Entity\FilterFormat;
class EditorIntegrationTest extends QuickEditTestBase {
  
  protected static $modules = [
    'editor',
    'editor_test',
  ];
  
  protected $editorManager;
  
  protected $metadataGenerator;
  
  protected $editorSelector;
  
  protected $accessChecker;
  
  protected $fieldName;
  protected function setUp() : void {
    parent::setUp();
    
    $this->fieldName = 'field_textarea';
    $this
      ->createFieldWithStorage($this->fieldName, 'text', 1, 'Long text field', [], 'text_textarea', [
      'size' => 42,
    ], 'text_default', []);
    
    $full_html_format = FilterFormat::create([
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => [],
    ]);
    $full_html_format
      ->save();
    
    $editor = Editor::create([
      'format' => $full_html_format
        ->id(),
      'editor' => 'unicorn',
    ]);
    $editor
      ->save();
    
    FilterFormat::create([
      'format' => 'no_editor',
      'name' => 'No Text Editor',
      'weight' => 2,
      'filters' => [],
    ])
      ->save();
  }
  
  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 testEditorSelection() {
    $this->editorManager = $this->container
      ->get('plugin.manager.quickedit.editor');
    $this->editorSelector = $this->container
      ->get('quickedit.editor.selector');
    
    $entity = EntityTest::create();
    $entity->{$this->fieldName}->value = 'Hello, world!';
    $entity->{$this->fieldName}->format = 'filtered_html';
    $entity
      ->save();
    
    $this
      ->assertEquals('form', $this
      ->getSelectedEditor($entity
      ->id(), $this->fieldName), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected.");
    
    $entity->{$this->fieldName}->format = 'full_html';
    $entity
      ->save();
    $this
      ->assertEquals('editor', $this
      ->getSelectedEditor($entity
      ->id(), $this->fieldName), "With cardinality 1, and the full_html text format, the 'editor' 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(), $this->fieldName), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
  }
  
  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);
    
    $entity = EntityTest::create();
    $entity->{$this->fieldName}->value = 'Test';
    $entity->{$this->fieldName}->format = 'full_html';
    $entity
      ->save();
    $entity = EntityTest::load($entity
      ->id());
    
    $items = $entity
      ->get($this->fieldName);
    $metadata = $this->metadataGenerator
      ->generateFieldMetadata($items, 'default');
    $expected = [
      'access' => TRUE,
      'label' => 'Long text field',
      'editor' => 'editor',
      'custom' => [
        'format' => 'full_html',
        'formatHasTransformations' => FALSE,
      ],
    ];
    $this
      ->assertEquals($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
  }
  
  public function testAttachments() {
    $this->editorSelector = $this->container
      ->get('quickedit.editor.selector');
    $editors = [
      'editor',
    ];
    $attachments = $this->editorSelector
      ->getEditorAttachments($editors);
    $this
      ->assertSame([
      'library' => [
        'editor/quickedit.inPlaceEditor.formattedText',
      ],
    ], $attachments, "Expected attachments for Editor module's in-place editor found.");
  }
  
  public function testGetUntransformedTextCommand() {
    
    $entity = EntityTest::create();
    $entity->{$this->fieldName}->value = 'Test';
    $entity->{$this->fieldName}->format = 'full_html';
    $entity
      ->save();
    $entity = EntityTest::load($entity
      ->id());
    
    $controller = new EditorController();
    $request = new Request();
    $response = $controller
      ->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
    $expected = [
      [
        'command' => 'editorGetUntransformedText',
        'data' => 'Test',
      ],
    ];
    $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
    $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
    $event = new ResponseEvent(\Drupal::service('http_kernel'), $request, HttpKernelInterface::MASTER_REQUEST, $response);
    $subscriber
      ->onResponse($event);
    $this
      ->assertEquals(Json::encode($expected), $response
      ->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');
  }
}