View source
<?php
namespace Drupal\quickedit\Tests;
use Drupal\quickedit\EditorSelector;
class EditorSelectionTest extends QuickEditTestBase {
protected $editorManager;
protected $editorSelector;
protected function setUp() {
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') {
$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 testText() {
$field_name = 'field_text';
$this
->createFieldWithStorage($field_name, 'string', 1, 'Simple text field', array(), 'string_textfield', array(
'size' => 42,
), 'string', array());
$entity = entity_create('entity_test');
$entity->{$field_name}->value = 'Hello, world!';
$entity
->save();
$this
->assertEqual('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
->assertEqual('form', $this
->getSelectedEditor($entity
->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
}
public function testTextWysiwyg() {
$this
->enableModules(array(
'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', array(), 'text_textarea', array(
'size' => 42,
), 'text_default', array());
$entity = entity_create('entity_test');
$entity->{$field_name}->value = 'Hello, world!';
$entity->{$field_name}->format = 'filtered_html';
$entity
->save();
$this
->assertEqual('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
->assertEqual('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
->assertEqual('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', array(), 'number', array(), 'number_integer', array());
$entity = entity_create('entity_test');
$entity->{$field_name}->value = 42;
$entity
->save();
$this
->assertEqual('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
->assertEqual('form', $this
->getSelectedEditor($entity
->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
}
}