View source
<?php
namespace Drupal\editor\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
use Drupal\Core\Language\LanguageInterface;
use Drupal\quickedit\MetadataGenerator;
use Drupal\quickedit\Tests\QuickEditTestBase;
use Drupal\quickedit_test\MockEditEntityFieldAccessCheck;
use Drupal\editor\EditorController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class QuickEditIntegrationTest extends QuickEditTestBase {
public static $modules = array(
'editor',
'editor_test',
);
protected $editorManager;
protected $metadataGenerator;
protected $editorSelector;
protected $accessChecker;
protected $fieldName;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'url_alias');
$this->fieldName = 'field_textarea';
$this
->createFieldWithStorage($this->fieldName, 'text', 1, 'Long text field', array(), 'text_textarea', array(
'size' => 42,
), 'text_default', array());
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
));
$full_html_format
->save();
$editor = entity_create('editor', array(
'format' => $full_html_format
->id(),
'editor' => 'unicorn',
));
$editor
->save();
entity_create('filter_format', array(
'format' => 'no_editor',
'name' => 'No Text Editor',
'weight' => 2,
'filters' => array(),
))
->save();
}
protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
$entity = entity_load('entity_test', $entity_id, TRUE);
$items = $entity
->get($field_name);
$options = entity_get_display('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 = entity_create('entity_test');
$entity->{$this->fieldName}->value = 'Hello, world!';
$entity->{$this->fieldName}->format = 'filtered_html';
$entity
->save();
$this
->assertEqual('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
->assertEqual('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
->assertEqual('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 MockEditEntityFieldAccessCheck();
$this->editorSelector = $this->container
->get('quickedit.editor.selector');
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
$entity = entity_create('entity_test');
$entity->{$this->fieldName}->value = 'Test';
$entity->{$this->fieldName}->format = 'full_html';
$entity
->save();
$entity = entity_load('entity_test', $entity
->id());
$items = $entity
->get($this->fieldName);
$metadata = $this->metadataGenerator
->generateFieldMetadata($items, 'default');
$expected = array(
'access' => TRUE,
'label' => 'Long text field',
'editor' => 'editor',
'custom' => array(
'format' => 'full_html',
'formatHasTransformations' => FALSE,
),
);
$this
->assertEqual($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
}
public function testAttachments() {
$this->editorSelector = $this->container
->get('quickedit.editor.selector');
$editors = array(
'editor',
);
$attachments = $this->editorSelector
->getEditorAttachments($editors);
$this
->assertIdentical($attachments, array(
'library' => array(
'editor/quickedit.inPlaceEditor.formattedText',
),
), "Expected attachments for Editor module's in-place editor found.");
}
public function testGetUntransformedTextCommand() {
$entity = entity_create('entity_test');
$entity->{$this->fieldName}->value = 'Test';
$entity->{$this->fieldName}->format = 'full_html';
$entity
->save();
$entity = entity_load('entity_test', $entity
->id());
$controller = new EditorController();
$request = new Request();
$response = $controller
->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
$expected = array(
array(
'command' => 'editorGetUntransformedText',
'data' => 'Test',
),
);
$ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
$subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
$event = new FilterResponseEvent(\Drupal::service('http_kernel'), $request, HttpKernelInterface::MASTER_REQUEST, $response);
$subscriber
->onResponse($event);
$this
->assertEqual(Json::encode($expected), $response
->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');
}
}