Commit 60531a74 authored by alexpott's avatar alexpott
Browse files

Issue #2452619 by vasike, amateescu, joshtaylor: Move...

Issue #2452619 by vasike, amateescu, joshtaylor: Move entity_reference_create_field function to EntityReferenceTestTrait
parent f3f12e54
...@@ -202,55 +202,3 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query ...@@ -202,55 +202,3 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
$handler = $query->getMetadata('entity_reference_selection_handler'); $handler = $query->getMetadata('entity_reference_selection_handler');
$handler->entityQueryAlter($query); $handler->entityQueryAlter($query);
} }
/**
* Creates a field of an entity reference field storage on the specified bundle.
*
* @param string $entity_type
* The type of entity the field will be attached to.
* @param string $bundle
* The bundle name of the entity the field will be attached to.
* @param string $field_name
* The name of the field; if it already exists, a new instance of the existing
* field will be created.
* @param string $field_label
* The label of the field.
* @param string $target_entity_type
* The type of the referenced entity.
* @param string $selection_handler
* The selection handler used by this field.
* @param array $selection_handler_settings
* An array of settings supported by the selection handler specified above.
* (e.g. 'target_bundles', 'sort', 'auto_create', etc).
* @param int $cardinality
* The cardinality of the field.
*
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
*/
function entity_reference_create_field($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) {
// Look for or add the specified field to the requested entity bundle.
if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
entity_create('field_storage_config', array(
'field_name' => $field_name,
'type' => 'entity_reference',
'entity_type' => $entity_type,
'cardinality' => $cardinality,
'settings' => array(
'target_type' => $target_entity_type,
),
))->save();
}
if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
entity_create('field_config', array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field_label,
'settings' => array(
'handler' => $selection_handler,
'handler_settings' => $selection_handler_settings,
),
))->save();
}
}
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\config\Tests\AssertConfigEntityImportTrait; use Drupal\config\Tests\AssertConfigEntityImportTrait;
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
...@@ -19,7 +20,9 @@ ...@@ -19,7 +20,9 @@
* @group entity_reference * @group entity_reference
*/ */
class EntityReferenceIntegrationTest extends WebTestBase { class EntityReferenceIntegrationTest extends WebTestBase {
use AssertConfigEntityImportTrait; use AssertConfigEntityImportTrait;
use EntityReferenceTestTrait;
/** /**
* The entity type used in this test. * The entity type used in this test.
...@@ -68,7 +71,7 @@ public function testSupportedEntityTypesAndWidgets() { ...@@ -68,7 +71,7 @@ public function testSupportedEntityTypesAndWidgets() {
$this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId(); $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
// Create an Entity reference field. // Create an Entity reference field.
entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2); $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
// Test the default 'entity_reference_autocomplete' widget. // Test the default 'entity_reference_autocomplete' widget.
entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save(); entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
......
<?php
/**
* @file
* Contains \Drupal\entity_reference\Tests\EntityReferenceTestTrait.
*/
namespace Drupal\entity_reference\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Provides common functionality for the EntityReference test classes.
*/
trait EntityReferenceTestTrait {
/**
* Creates a field of an entity reference field storage on the specified bundle.
*
* @param string $entity_type
* The type of entity the field will be attached to.
* @param string $bundle
* The bundle name of the entity the field will be attached to.
* @param string $field_name
* The name of the field; if it already exists, a new instance of the existing
* field will be created.
* @param string $field_label
* The label of the field.
* @param string $target_entity_type
* The type of the referenced entity.
* @param string $selection_handler
* The selection handler used by this field.
* @param array $selection_handler_settings
* An array of settings supported by the selection handler specified above.
* (e.g. 'target_bundles', 'sort', 'auto_create', etc).
* @param int $cardinality
* The cardinality of the field.
*
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
*/
protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) {
// Look for or add the specified field to the requested entity bundle.
if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
FieldStorageConfig::create(array(
'field_name' => $field_name,
'type' => 'entity_reference',
'entity_type' => $entity_type,
'cardinality' => $cardinality,
'settings' => array(
'target_type' => $target_entity_type,
),
))->save();
}
if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
FieldConfig::create(array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field_label,
'settings' => array(
'handler' => $selection_handler,
'handler_settings' => $selection_handler_settings,
),
))->save();
}
}
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat; use Drupal\filter\Entity\FilterFormat;
use Drupal\system\Tests\Entity\EntityUnitTestBase; use Drupal\system\Tests\Entity\EntityUnitTestBase;
...@@ -22,6 +23,8 @@ ...@@ -22,6 +23,8 @@
*/ */
class EntityReferenceFormatterTest extends EntityUnitTestBase { class EntityReferenceFormatterTest extends EntityUnitTestBase {
use EntityReferenceTestTrait;
/** /**
* The entity type used in this test. * The entity type used in this test.
* *
...@@ -78,7 +81,7 @@ protected function setUp() { ...@@ -78,7 +81,7 @@ protected function setUp() {
$this->installSchema('system', 'router'); $this->installSchema('system', 'router');
$this->container->get('router.builder')->rebuild(); $this->container->get('router.builder')->rebuild();
entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
// Set up a field, so that the entity that'll be referenced bubbles up a // Set up a field, so that the entity that'll be referenced bubbles up a
// cache tag when rendering it entirely. // cache tag when rendering it entirely.
......
...@@ -11,12 +11,14 @@ ...@@ -11,12 +11,14 @@
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Tests\FieldUnitTestBase; use Drupal\field\Tests\FieldUnitTestBase;
use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
/** /**
* Tests the new entity API for the entity reference field type. * Tests the new entity API for the entity reference field type.
* *
...@@ -24,6 +26,8 @@ ...@@ -24,6 +26,8 @@
*/ */
class EntityReferenceItemTest extends FieldUnitTestBase { class EntityReferenceItemTest extends FieldUnitTestBase {
use EntityReferenceTestTrait;
/** /**
* Modules to install. * Modules to install.
* *
...@@ -68,8 +72,8 @@ protected function setUp() { ...@@ -68,8 +72,8 @@ protected function setUp() {
$this->term->save(); $this->term->save();
// Use the util to create an instance. // Use the util to create an instance.
entity_reference_create_field('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term');
entity_reference_create_field('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
} }
/** /**
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
namespace Drupal\rdf\Tests\Field; namespace Drupal\rdf\Tests\Field;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
...@@ -16,6 +17,8 @@ ...@@ -16,6 +17,8 @@
*/ */
class EntityReferenceRdfaTest extends FieldRdfaTestBase { class EntityReferenceRdfaTest extends FieldRdfaTestBase {
use EntityReferenceTestTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -58,7 +61,7 @@ protected function setUp() { ...@@ -58,7 +61,7 @@ protected function setUp() {
->grantPermission('view test entity') ->grantPermission('view test entity')
->save(); ->save();
entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType); $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
// Add the mapping. // Add the mapping.
$mapping = rdf_get_mapping('entity_test', 'entity_test'); $mapping = rdf_get_mapping('entity_test', 'entity_test');
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\Entity\User; use Drupal\user\Entity\User;
...@@ -24,7 +25,9 @@ ...@@ -24,7 +25,9 @@
* @group Entity * @group Entity
*/ */
class EntityReferenceFieldTest extends EntityUnitTestBase { class EntityReferenceFieldTest extends EntityUnitTestBase {
use SchemaCheckTestTrait; use SchemaCheckTestTrait;
use EntityReferenceTestTrait;
/** /**
* The entity type used in this test. * The entity type used in this test.
...@@ -70,7 +73,7 @@ protected function setUp() { ...@@ -70,7 +73,7 @@ protected function setUp() {
$this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_rev');
// Create a field. // Create a field.
entity_reference_create_field( $this->createEntityReferenceField(
$this->entityType, $this->entityType,
$this->bundle, $this->bundle,
$this->fieldName, $this->fieldName,
...@@ -178,7 +181,7 @@ public function testReferencedEntitiesMultipleLoad() { ...@@ -178,7 +181,7 @@ public function testReferencedEntitiesMultipleLoad() {
public function testReferencedEntitiesStringId() { public function testReferencedEntitiesStringId() {
$field_name = 'entity_reference_string_id'; $field_name = 'entity_reference_string_id';
$this->installEntitySchema('entity_test_string_id'); $this->installEntitySchema('entity_test_string_id');
entity_reference_create_field( $this->createEntityReferenceField(
$this->entityType, $this->entityType,
$this->bundle, $this->bundle,
$field_name, $field_name,
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\system\Tests\Entity; namespace Drupal\system\Tests\Entity;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
...@@ -17,6 +18,8 @@ ...@@ -17,6 +18,8 @@
*/ */
class EntityViewBuilderTest extends EntityUnitTestBase { class EntityViewBuilderTest extends EntityUnitTestBase {
use EntityReferenceTestTrait;
/** /**
* Modules to enable. * Modules to enable.
* *
...@@ -98,7 +101,7 @@ public function testEntityViewBuilderCacheWithReferences() { ...@@ -98,7 +101,7 @@ public function testEntityViewBuilderCacheWithReferences() {
$request->setMethod('GET'); $request->setMethod('GET');
// Create an entity reference field and an entity that will be referenced. // Create an entity reference field and an entity that will be referenced.
entity_reference_create_field('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test'); $this->createEntityReferenceField('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test');
entity_get_display('entity_test', 'entity_test', 'full')->setComponent('reference_field', [ entity_get_display('entity_test', 'entity_test', 'full')->setComponent('reference_field', [
'type' => 'entity_reference_entity_view', 'type' => 'entity_reference_entity_view',
'settings' => ['link' => FALSE], 'settings' => ['link' => FALSE],
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\user\Tests; namespace Drupal\user\Tests;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\system\Tests\Entity\EntityUnitTestBase; use Drupal\system\Tests\Entity\EntityUnitTestBase;
...@@ -17,6 +18,8 @@ ...@@ -17,6 +18,8 @@
*/ */
class UserEntityReferenceTest extends EntityUnitTestBase { class UserEntityReferenceTest extends EntityUnitTestBase {
use EntityReferenceTestTrait;
/** /**
* A randomly-generated role for testing purposes. * A randomly-generated role for testing purposes.
* *
...@@ -56,7 +59,7 @@ protected function setUp() { ...@@ -56,7 +59,7 @@ protected function setUp() {
)); ));
$this->role2->save(); $this->role2->save();
entity_reference_create_field('user', 'user', 'user_reference', 'User reference', 'user'); $this->createEntityReferenceField('user', 'user', 'user_reference', 'User reference', 'user');
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment