Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,6 @@
name: 'Quick Edit test'
type: module
description: 'Support module for the Quick Edit module tests.'
core: 8.x
package: Testing
version: VERSION

View file

@ -0,0 +1,33 @@
<?php
/**
* @file
* Helper module for the Quick Edit tests.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_quickedit_render_field().
*/
function quickedit_test_quickedit_render_field(EntityInterface $entity, $field_name, $view_mode_id, $langcode) {
$entity = \Drupal::entityManager()->getTranslationFromContext($entity, $langcode);
return array(
'#prefix' => '<div class="quickedit-test-wrapper">',
'field' => $entity->get($field_name)->view($view_mode_id),
'#suffix' => '</div>',
);
}
/**
* Implements hook_field_formatter_info_alter().
*
* @see quickedit_field_formatter_info_alter()
* @see editor_field_formatter_info_alter()
*/
function quickedit_test_field_formatter_info_alter(&$info) {
// Update \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter's
// annotation to indicate that it supports the 'wysiwyg' in-place editor
// provided by this module.
$info['text_default']['quickedit'] = ['editor' => 'wysiwyg'];
}

View file

@ -0,0 +1,20 @@
<?php
namespace Drupal\quickedit_test;
use Drupal\Core\Entity\EntityInterface;
use Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface;
/**
* Access check for editing entity fields.
*/
class MockEditEntityFieldAccessCheck implements EditEntityFieldAccessCheckInterface {
/**
* {@inheritdoc}
*/
public function accessEditEntityField(EntityInterface $entity, $field_name) {
return TRUE;
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Drupal\quickedit_test\Plugin\InPlaceEditor;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\quickedit\Plugin\InPlaceEditorBase;
/**
* Defines the 'wysiwyg' in-place editor.
*
* @InPlaceEditor(
* id = "wysiwyg",
* )
*/
class WysiwygEditor extends InPlaceEditorBase {
/**
* {@inheritdoc}
*/
public function isCompatible(FieldItemListInterface $items) {
$field_definition = $items->getFieldDefinition();
// This editor is incompatible with multivalued fields.
if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
return FALSE;
}
// This editor is compatible with formatted ("rich") text fields; but only
// if there is a currently active text format and that text format is the
// 'full_html' text format.
return $items[0]->format === 'full_html';
}
/**
* {@inheritdoc}
*/
public function getMetadata(FieldItemListInterface $items) {
$metadata['format'] = $items[0]->format;
return $metadata;
}
/**
* {@inheritdoc}
*/
public function getAttachments() {
return array(
'library' => array(
'quickedit_test/not-existing-wysiwyg',
),
);
}
}

View file

@ -0,0 +1,155 @@
<?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') {
$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 = 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.");
}
}

View file

@ -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 = EntityTest::load($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 = EntityTest::load($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.');
}
}

View 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();
}
}

View file

@ -0,0 +1,149 @@
<?php
namespace Drupal\Tests\quickedit\Unit\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\Container;
use Drupal\quickedit\Access\EditEntityFieldAccessCheck;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Language\LanguageInterface;
/**
* @coversDefaultClass \Drupal\quickedit\Access\EditEntityFieldAccessCheck
* @group Access
* @group quickedit
*/
class EditEntityFieldAccessCheckTest extends UnitTestCase {
/**
* The tested access checker.
*
* @var \Drupal\quickedit\Access\EditEntityFieldAccessCheck
*/
protected $editAccessCheck;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->editAccessCheck = new EditEntityFieldAccessCheck();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
$cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Provides test data for testAccess().
*
* @see \Drupal\Tests\edit\Unit\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess()
*/
public function providerTestAccess() {
$data = array();
$data[] = array(TRUE, TRUE, AccessResult::allowed());
$data[] = array(FALSE, TRUE, AccessResult::neutral());
$data[] = array(TRUE, FALSE, AccessResult::neutral());
$data[] = array(FALSE, FALSE, AccessResult::neutral());
return $data;
}
/**
* Tests the method for checking access to routes.
*
* @param bool $entity_is_editable
* Whether the subject entity is editable.
* @param bool $field_storage_is_accessible
* Whether the user has access to the field storage entity.
* @param \Drupal\Core\Access\AccessResult $expected_result
* The expected result of the access call.
*
* @dataProvider providerTestAccess
*/
public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) {
$entity = $this->createMockEntity();
$entity->expects($this->any())
->method('access')
->willReturn(AccessResult::allowedIf($entity_is_editable)->cachePerPermissions());
$field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
$field_storage->expects($this->any())
->method('access')
->willReturn(AccessResult::allowedIf($field_storage_is_accessible));
$expected_result->cachePerPermissions();
$field_name = 'valid';
$entity_with_field = clone $entity;
$entity_with_field->expects($this->any())
->method('get')
->with($field_name)
->will($this->returnValue($field_storage));
$entity_with_field->expects($this->once())
->method('hasTranslation')
->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->will($this->returnValue(TRUE));
$account = $this->getMock('Drupal\Core\Session\AccountInterface');
$access = $this->editAccessCheck->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
$this->assertEquals($expected_result, $access);
}
/**
* Tests checking access to routes that result in AccessResult::isForbidden().
*
* @dataProvider providerTestAccessForbidden
*/
public function testAccessForbidden($field_name, $langcode) {
$account = $this->getMock('Drupal\Core\Session\AccountInterface');
$entity = $this->createMockEntity();
$this->assertEquals(AccessResult::forbidden(), $this->editAccessCheck->access($entity, $field_name, $langcode, $account));
}
/**
* Provides test data for testAccessForbidden.
*/
public function providerTestAccessForbidden() {
$data = array();
// Tests the access method without a field_name.
$data[] = array(NULL, LanguageInterface::LANGCODE_NOT_SPECIFIED);
// Tests the access method with a non-existent field.
$data[] = array('not_valid', LanguageInterface::LANGCODE_NOT_SPECIFIED);
// Tests the access method without a langcode.
$data[] = array('valid', NULL);
// Tests the access method with an invalid langcode.
$data[] = array('valid', 'xx-lolspeak');
return $data;
}
/**
* Returns a mock entity.
*
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function createMockEntity() {
$entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest')
->disableOriginalConstructor()
->getMock();
$entity->expects($this->any())
->method('hasTranslation')
->will($this->returnValueMap(array(
array(LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE),
array('xx-lolspeak', FALSE),
)));
$entity->expects($this->any())
->method('hasField')
->will($this->returnValueMap(array(
array('valid', TRUE),
array('not_valid', FALSE),
)));
return $entity;
}
}