From 60531a745e731ea93284fd315202d09a863435e2 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 19 Mar 2015 10:29:17 +0000
Subject: [PATCH] Issue #2452619 by vasike, amateescu, joshtaylor: Move
 entity_reference_create_field function to EntityReferenceTestTrait

---
 .../entity_reference/entity_reference.module  | 52 --------------
 .../Tests/EntityReferenceIntegrationTest.php  |  5 +-
 .../src/Tests/EntityReferenceTestTrait.php    | 69 +++++++++++++++++++
 .../EntityReferenceFormatterTest.php          |  5 +-
 .../EntityReferenceItemTest.php               |  8 ++-
 .../Tests/Field/EntityReferenceRdfaTest.php   |  5 +-
 .../Tests/Entity/EntityReferenceFieldTest.php |  7 +-
 .../Tests/Entity/EntityViewBuilderTest.php    |  5 +-
 .../src/Tests/UserEntityReferenceTest.php     |  5 +-
 9 files changed, 100 insertions(+), 61 deletions(-)
 create mode 100644 core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php

diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module
index 28011aca2300..e6d764bb8d34 100644
--- a/core/modules/entity_reference/entity_reference.module
+++ b/core/modules/entity_reference/entity_reference.module
@@ -202,55 +202,3 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
   $handler = $query->getMetadata('entity_reference_selection_handler');
   $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();
-  }
-}
-
diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
index 2203fd8f00cb..53fbccee388c 100644
--- a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
@@ -10,6 +10,7 @@
 use Drupal\Component\Utility\String;
 use Drupal\config\Tests\AssertConfigEntityImportTrait;
 use Drupal\Core\Config\Entity\ConfigEntityBase;
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\simpletest\WebTestBase;
 
@@ -19,7 +20,9 @@
  * @group entity_reference
  */
 class EntityReferenceIntegrationTest extends WebTestBase {
+
   use AssertConfigEntityImportTrait;
+  use EntityReferenceTestTrait;
 
   /**
    * The entity type used in this test.
@@ -68,7 +71,7 @@ public function testSupportedEntityTypesAndWidgets() {
       $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
 
       // 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.
       entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php b/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php
new file mode 100644
index 000000000000..18820792bec0
--- /dev/null
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php
@@ -0,0 +1,69 @@
+<?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();
+    }
+  }
+
+}
diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php
index 31ec4fd6bb7b..41f3fea0f81f 100644
--- a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php
+++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php
@@ -9,6 +9,7 @@
 
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\filter\Entity\FilterFormat;
 use Drupal\system\Tests\Entity\EntityUnitTestBase;
@@ -22,6 +23,8 @@
  */
 class EntityReferenceFormatterTest extends EntityUnitTestBase {
 
+  use EntityReferenceTestTrait;
+
   /**
    * The entity type used in this test.
    *
@@ -78,7 +81,7 @@ protected function setUp() {
     $this->installSchema('system', 'router');
     $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
     // cache tag when rendering it entirely.
diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php
index 6c0b4a8db39d..859551dbbda6 100644
--- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php
+++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php
@@ -11,12 +11,14 @@
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Field\FieldItemInterface;
 use Drupal\Core\Language\LanguageInterface;
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\field\Tests\FieldUnitTestBase;
 use Drupal\taxonomy\Entity\Term;
 use Drupal\taxonomy\Entity\Vocabulary;
 
+
 /**
  * Tests the new entity API for the entity reference field type.
  *
@@ -24,6 +26,8 @@
  */
 class EntityReferenceItemTest extends FieldUnitTestBase {
 
+  use EntityReferenceTestTrait;
+
   /**
    * Modules to install.
    *
@@ -68,8 +72,8 @@ protected function setUp() {
     $this->term->save();
 
     // 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');
-    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_term', 'Test content entity reference', 'taxonomy_term');
+    $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
   }
 
   /**
diff --git a/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
index cf25170fcdbd..31b8465f5a93 100644
--- a/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
+++ b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
@@ -6,6 +6,7 @@
 
 namespace Drupal\rdf\Tests\Field;
 
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\user\Entity\Role;
 use Drupal\user\RoleInterface;
 
@@ -16,6 +17,8 @@
  */
 class EntityReferenceRdfaTest extends FieldRdfaTestBase {
 
+  use EntityReferenceTestTrait;
+
   /**
    * {@inheritdoc}
    */
@@ -58,7 +61,7 @@ protected function setUp() {
       ->grantPermission('view test entity')
       ->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.
     $mapping = rdf_get_mapping('entity_test', 'entity_test');
diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
index cf920939c6d8..a0b8ea03569b 100644
--- a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
+++ b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
@@ -12,6 +12,7 @@
 use Drupal\Core\Entity\EntityStorageException;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\user\Entity\Role;
 use Drupal\user\Entity\User;
@@ -24,7 +25,9 @@
  * @group Entity
  */
 class EntityReferenceFieldTest extends EntityUnitTestBase {
+
   use SchemaCheckTestTrait;
+  use EntityReferenceTestTrait;
 
   /**
    * The entity type used in this test.
@@ -70,7 +73,7 @@ protected function setUp() {
     $this->installEntitySchema('entity_test_rev');
 
     // Create a field.
-    entity_reference_create_field(
+    $this->createEntityReferenceField(
       $this->entityType,
       $this->bundle,
       $this->fieldName,
@@ -178,7 +181,7 @@ public function testReferencedEntitiesMultipleLoad() {
   public function testReferencedEntitiesStringId() {
     $field_name = 'entity_reference_string_id';
     $this->installEntitySchema('entity_test_string_id');
-    entity_reference_create_field(
+    $this->createEntityReferenceField(
       $this->entityType,
       $this->bundle,
       $field_name,
diff --git a/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php b/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php
index eba7809c6271..15881508d5ea 100644
--- a/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php
+++ b/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\Entity;
 
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\user\Entity\Role;
 use Drupal\user\RoleInterface;
 
@@ -17,6 +18,8 @@
  */
 class EntityViewBuilderTest extends EntityUnitTestBase {
 
+  use EntityReferenceTestTrait;
+
   /**
    * Modules to enable.
    *
@@ -98,7 +101,7 @@ public function testEntityViewBuilderCacheWithReferences() {
     $request->setMethod('GET');
 
     // 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', [
       'type' => 'entity_reference_entity_view',
       'settings' => ['link' => FALSE],
diff --git a/core/modules/user/src/Tests/UserEntityReferenceTest.php b/core/modules/user/src/Tests/UserEntityReferenceTest.php
index c62b69b7c79d..1323fcd636b4 100644
--- a/core/modules/user/src/Tests/UserEntityReferenceTest.php
+++ b/core/modules/user/src/Tests/UserEntityReferenceTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\user\Tests;
 
+use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\system\Tests\Entity\EntityUnitTestBase;
 
@@ -17,6 +18,8 @@
  */
 class UserEntityReferenceTest extends EntityUnitTestBase {
 
+  use EntityReferenceTestTrait;
+
   /**
    * A randomly-generated role for testing purposes.
    *
@@ -56,7 +59,7 @@ protected function setUp() {
     ));
     $this->role2->save();
 
-    entity_reference_create_field('user', 'user', 'user_reference', 'User reference', 'user');
+    $this->createEntityReferenceField('user', 'user', 'user_reference', 'User reference', 'user');
   }
 
   /**
-- 
GitLab