Update to Drupal 8.1.1. For more information, see https://www.drupal.org/node/2718713
This commit is contained in:
parent
c0a0d5a94c
commit
9eae24d844
669 changed files with 3873 additions and 1553 deletions
|
@ -18,4 +18,3 @@ function quickedit_test_quickedit_render_field(EntityInterface $entity, $field_n
|
|||
'#suffix' => '</div>',
|
||||
);
|
||||
}
|
||||
|
||||
|
|
153
core/modules/quickedit/tests/src/Kernel/EditorSelectionTest.php
Normal file
153
core/modules/quickedit/tests/src/Kernel/EditorSelectionTest.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\quickedit\Kernel;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\quickedit\EditorSelector;
|
||||
|
||||
/**
|
||||
* Tests in-place field editor selection.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class EditorSelectionTest extends QuickEditTestBase {
|
||||
|
||||
/**
|
||||
* The manager for editor plugins.
|
||||
*
|
||||
* @var \Drupal\Component\Plugin\PluginManagerInterface
|
||||
*/
|
||||
protected $editorManager;
|
||||
|
||||
/**
|
||||
* The editor selector object to be tested.
|
||||
*
|
||||
* @var \Drupal\quickedit\EditorSelectorInterface
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the in-place editor that Quick Edit selects.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a string (plain text) field, with cardinality 1 and >1.
|
||||
*/
|
||||
public function testText() {
|
||||
$field_name = 'field_text';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'string', 1, 'Simple text field',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'string_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'string',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = EntityTest::create();
|
||||
$entity->{$field_name}->value = 'Hello, world!';
|
||||
$entity->save();
|
||||
|
||||
// With cardinality 1.
|
||||
$this->assertEqual('plain_text', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, the 'plain_text' editor is selected.");
|
||||
|
||||
// With cardinality >1
|
||||
$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.");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a textual field, with text filtering, with cardinality 1 and >1,
|
||||
* always with an Editor plugin present that supports textual fields with text
|
||||
* filtering, but with varying text format compatibility.
|
||||
*/
|
||||
public function testTextWysiwyg() {
|
||||
// Enable edit_test module so that the 'wysiwyg' editor becomes available.
|
||||
$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',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'text_textarea',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'text_default',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = EntityTest::create();
|
||||
$entity->{$field_name}->value = 'Hello, world!';
|
||||
$entity->{$field_name}->format = 'filtered_html';
|
||||
$entity->save();
|
||||
|
||||
// Editor selection w/ cardinality 1, text format w/o associated text editor.
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected.");
|
||||
|
||||
// Editor selection w/ cardinality 1, text format w/ associated text editor.
|
||||
$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.");
|
||||
|
||||
// Editor selection with text field, cardinality >1.
|
||||
$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.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a number field, with cardinality 1 and >1.
|
||||
*/
|
||||
public function testNumber() {
|
||||
$field_name = 'field_nr';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'integer', 1, 'Simple number field',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'number',
|
||||
array(),
|
||||
// 'default' formatter type & settings.
|
||||
'number_integer',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = EntityTest::create();
|
||||
$entity->{$field_name}->value = 42;
|
||||
$entity->save();
|
||||
|
||||
// Editor selection with cardinality 1.
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, the 'form' editor is selected.");
|
||||
|
||||
// Editor selection with cardinality >1.
|
||||
$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.");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\quickedit\Kernel;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\quickedit\EditorSelector;
|
||||
use Drupal\quickedit\MetadataGenerator;
|
||||
use Drupal\quickedit_test\MockEditEntityFieldAccessCheck;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
|
||||
/**
|
||||
* Tests in-place field editing metadata.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class MetadataGeneratorTest extends QuickEditTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('quickedit_test');
|
||||
|
||||
/**
|
||||
* The manager for editor plugins.
|
||||
*
|
||||
* @var \Drupal\Component\Plugin\PluginManagerInterface
|
||||
*/
|
||||
protected $editorManager;
|
||||
|
||||
/**
|
||||
* The metadata generator object to be tested.
|
||||
*
|
||||
* @var \Drupal\quickedit\MetadataGeneratorInterface.php
|
||||
*/
|
||||
protected $metadataGenerator;
|
||||
|
||||
/**
|
||||
* The editor selector object to be used by the metadata generator object.
|
||||
*
|
||||
* @var \Drupal\quickedit\EditorSelectorInterface
|
||||
*/
|
||||
protected $editorSelector;
|
||||
|
||||
/**
|
||||
* The access checker object to be used by the metadata generator object.
|
||||
*
|
||||
* @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface
|
||||
*/
|
||||
protected $accessChecker;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->accessChecker = new MockEditEntityFieldAccessCheck();
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a simple entity type, with two different simple fields.
|
||||
*/
|
||||
public function testSimpleEntityType() {
|
||||
$field_1_name = 'field_text';
|
||||
$field_1_label = 'Plain text field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_1_name, 'string', 1, $field_1_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'string_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'string',
|
||||
array()
|
||||
);
|
||||
$field_2_name = 'field_nr';
|
||||
$field_2_label = 'Simple number field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_2_name, 'integer', 1, $field_2_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'number',
|
||||
array(),
|
||||
// 'default' formatter type & settings.
|
||||
'number_integer',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = EntityTest::create();
|
||||
$entity->{$field_1_name}->value = 'Test';
|
||||
$entity->{$field_2_name}->value = 42;
|
||||
$entity->save();
|
||||
$entity = entity_load('entity_test', $entity->id());
|
||||
|
||||
// Verify metadata for field 1.
|
||||
$items_1 = $entity->get($field_1_name);
|
||||
$metadata_1 = $this->metadataGenerator->generateFieldMetadata($items_1, 'default');
|
||||
$expected_1 = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Plain text field',
|
||||
'editor' => 'plain_text',
|
||||
);
|
||||
$this->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');
|
||||
|
||||
// Verify metadata for field 2.
|
||||
$items_2 = $entity->get($field_2_name);
|
||||
$metadata_2 = $this->metadataGenerator->generateFieldMetadata($items_2, 'default');
|
||||
$expected_2 = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Simple number field',
|
||||
'editor' => 'form',
|
||||
);
|
||||
$this->assertEqual($expected_2, $metadata_2, 'The correct metadata is generated for the second field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a field whose associated in-place editor generates custom metadata.
|
||||
*/
|
||||
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);
|
||||
|
||||
// Create a rich text field.
|
||||
$field_name = 'field_rich';
|
||||
$field_label = 'Rich text field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'text', 1, $field_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'text_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'text_default',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create a text format.
|
||||
$full_html_format = FilterFormat::create(array(
|
||||
'format' => 'full_html',
|
||||
'name' => 'Full HTML',
|
||||
'weight' => 1,
|
||||
'filters' => array(
|
||||
'filter_htmlcorrector' => array('status' => 1),
|
||||
),
|
||||
));
|
||||
$full_html_format->save();
|
||||
|
||||
// Create an entity with values for this rich text field.
|
||||
$entity = EntityTest::create();
|
||||
$entity->{$field_name}->value = 'Test';
|
||||
$entity->{$field_name}->format = 'full_html';
|
||||
$entity->save();
|
||||
$entity = entity_load('entity_test', $entity->id());
|
||||
|
||||
// Verify metadata.
|
||||
$items = $entity->get($field_name);
|
||||
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
|
||||
$expected = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Rich text field',
|
||||
'editor' => 'wysiwyg',
|
||||
'custom' => array(
|
||||
'format' => 'full_html'
|
||||
),
|
||||
);
|
||||
$this->assertEqual($expected, $metadata); //, 'The correct metadata (including custom metadata) is generated.');
|
||||
}
|
||||
|
||||
}
|
105
core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php
Normal file
105
core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\quickedit\Kernel;
|
||||
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Base class for testing Quick Edit functionality.
|
||||
*/
|
||||
abstract class QuickEditTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['system', 'entity_test', 'field', 'field_test', 'filter', 'user', 'text', 'quickedit'];
|
||||
|
||||
/**
|
||||
* Bag of created fields.
|
||||
*
|
||||
* Allows easy access to test field names/IDs/objects via:
|
||||
* - $this->fields->{$field_name}_field_storage
|
||||
* - $this->fields->{$field_name}_instance
|
||||
*
|
||||
* @see \Drupal\quickedit\Tests\QuickEditTestBase::createFieldWithStorage()
|
||||
*
|
||||
* @var \ArrayObject
|
||||
*/
|
||||
protected $fields;
|
||||
|
||||
/**
|
||||
* Sets the default field storage backend for fields created during tests.
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->fields = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('entity_test');
|
||||
$this->installConfig(array('field', 'filter'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a field.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param string $type
|
||||
* The field type.
|
||||
* @param int $cardinality
|
||||
* The field's cardinality.
|
||||
* @param string $label
|
||||
* The field's label (used everywhere: widget label, formatter label).
|
||||
* @param array $field_settings
|
||||
* @param string $widget_type
|
||||
* The widget type.
|
||||
* @param array $widget_settings
|
||||
* The widget settings.
|
||||
* @param string $formatter_type
|
||||
* The formatter type.
|
||||
* @param array $formatter_settings
|
||||
* The formatter settings.
|
||||
*/
|
||||
protected function createFieldWithStorage($field_name, $type, $cardinality, $label, $field_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
|
||||
$field_storage = $field_name . '_field_storage';
|
||||
$this->fields->$field_storage = FieldStorageConfig::create(array(
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
'cardinality' => $cardinality,
|
||||
));
|
||||
$this->fields->$field_storage->save();
|
||||
|
||||
$field = $field_name . '_field';
|
||||
$this->fields->$field = FieldConfig::create([
|
||||
'field_storage' => $this->fields->$field_storage,
|
||||
'bundle' => 'entity_test',
|
||||
'label' => $label,
|
||||
'description' => $label,
|
||||
'weight' => mt_rand(0, 127),
|
||||
'settings' => $field_settings,
|
||||
]);
|
||||
$this->fields->$field->save();
|
||||
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, array(
|
||||
'type' => $widget_type,
|
||||
'settings' => $widget_settings,
|
||||
))
|
||||
->save();
|
||||
|
||||
entity_get_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, array(
|
||||
'label' => 'above',
|
||||
'type' => $formatter_type,
|
||||
'settings' => $formatter_settings
|
||||
))
|
||||
->save();
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue