From 2de46154c98c1d8e1079d651f3f100f580173b01 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 18 Jun 2013 12:38:11 +0200
Subject: [PATCH] Issue #1981306 by aspilicious, andypost, pcambra, swentel,
 amateescu: Drop procedural usage of fields in [a-e] modules.

---
 .../block/custom_block/custom_block.module    |  12 +-
 .../Tests/CustomBlockFieldTest.php            |  20 +--
 core/modules/comment/comment.install          |   2 +-
 core/modules/comment/comment.module           |  12 +-
 .../comment/Tests/CommentFieldsTest.php       |   2 +-
 .../comment/Tests/CommentLanguageTest.php     |   2 +-
 .../Tests/CommentTranslationUITest.php        |   2 +-
 .../contact/Tests/Views/ContactFieldsTest.php |  19 +--
 .../datetime/Tests/DatetimeFieldTest.php      | 140 ++++++++++--------
 .../lib/Drupal/edit/Tests/EditTestBase.php    |  12 +-
 .../Drupal/edit/Tests/EditorSelectionTest.php |  24 +--
 .../editor/Tests/EditIntegrationTest.php      |   4 +-
 .../lib/Drupal/email/Tests/EmailFieldTest.php |  39 +++--
 .../lib/Drupal/email/Tests/EmailItemTest.php  |  10 +-
 .../Drupal/entity/Tests/EntityDisplayTest.php |  48 +++---
 .../entity/Tests/EntityFormDisplayTest.php    |  37 ++---
 .../entity_reference/entity_reference.module  |   9 +-
 .../Tests/EntityReferenceAutoCreateTest.php   |  12 +-
 .../EntityReferenceSelectionSortTest.php      |  10 +-
 19 files changed, 217 insertions(+), 199 deletions(-)

diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module
index 7a5f08bf533a..e290cd4b904b 100644
--- a/core/modules/block/custom_block/custom_block.module
+++ b/core/modules/block/custom_block/custom_block.module
@@ -183,22 +183,22 @@ function custom_block_add_body_field($block_type_id, $label = 'Block body') {
   $field = field_info_field('block_body');
   $instance = field_info_instance('custom_block', 'block_body', $block_type_id);
   if (empty($field)) {
-    $field = array(
+    $field = entity_create('field_entity', array(
       'field_name' => 'block_body',
       'type' => 'text_with_summary',
       'entity_types' => array('custom_block'),
-    );
-    $field = field_create_field($field);
+    ));
+    $field->save();
   }
   if (empty($instance)) {
-    $instance = array(
+    $instance = entity_create('field_instance', array(
       'field_name' => 'block_body',
       'entity_type' => 'custom_block',
       'bundle' => $block_type_id,
       'label' => $label,
       'settings' => array('display_summary' => FALSE),
-    );
-    $instance = field_create_instance($instance);
+    ));
+    $instance->save();
 
     // Assign widget settings for the 'default' form mode.
     entity_get_form_display('custom_block', $block_type_id, 'default')
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
index 909dd128afbd..da5ac4f06205 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
@@ -25,14 +25,14 @@ class CustomBlockFieldTest extends CustomBlockTestBase {
   /**
    * The created field.
    *
-   * @var array
+   * @var \Drupal\field\Plugin\Core\Entity\Field
    */
   protected $field;
 
   /**
-   * The created instance
+   * The created instance.
    *
-   * @var array
+   * @var \Drupal\field\Plugin\Core\Entity\FieldInstance
    */
   protected $instance;
 
@@ -64,21 +64,21 @@ public function testBlockFields() {
     $this->blockType = $this->createCustomBlockType('link');
 
     // Create a field with settings to validate.
-    $this->field = array(
+    $this->field = entity_create('field_entity', array(
       'field_name' => drupal_strtolower($this->randomName()),
       'type' => 'link',
       'cardinality' => 2,
-    );
-    field_create_field($this->field);
-    $this->instance = array(
-      'field_name' => $this->field['field_name'],
+    ));
+    $this->field->save();
+    $this->instance = entity_create('field_instance', array(
+      'field_name' => $this->field->id(),
       'entity_type' => 'custom_block',
       'bundle' => 'link',
       'settings' => array(
         'title' => DRUPAL_OPTIONAL,
       ),
-    );
-    field_create_instance($this->instance);
+    ));
+    $this->instance->save();
     entity_get_form_display('custom_block', 'link', 'default')
       ->setComponent($this->field['field_name'], array(
         'type' => 'link_default',
diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install
index 0361b509e828..936159095d97 100644
--- a/core/modules/comment/comment.install
+++ b/core/modules/comment/comment.install
@@ -10,7 +10,7 @@
  */
 function comment_uninstall() {
   // Delete comment_body field.
-  field_delete_field('comment_body');
+  field_info_field('comment_body')->delete();
 
   // Remove variables.
   variable_del('comment_block_count');
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 0b45d879043e..ec495127fb47 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -345,26 +345,26 @@ function comment_node_type_delete($info) {
 function _comment_body_field_create($info) {
   // Create the field if needed.
   if (!field_read_field('comment_body', array('include_inactive' => TRUE))) {
-    $field = array(
+    $field = entity_create('field_entity', array(
       'field_name' => 'comment_body',
       'type' => 'text_long',
       'entity_types' => array('comment'),
-    );
-    field_create_field($field);
+    ));
+    $field->save();
   }
   // Create the instance if needed.
   if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) {
     entity_invoke_bundle_hook('create', 'comment', 'comment_node_' . $info->type);
     // Attaches the body field by default.
-    $instance = array(
+    $instance = entity_create('field_instance', array(
       'field_name' => 'comment_body',
       'label' => 'Comment',
       'entity_type' => 'comment',
       'bundle' => 'comment_node_' . $info->type,
       'settings' => array('text_processing' => 1),
       'required' => TRUE,
-    );
-    field_create_instance($instance);
+    ));
+    $instance->save();
 
     // Assign widget settings for the 'default' form mode.
     entity_get_form_display('comment', 'comment_node_' . $info->type, 'default')
diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
index c92d2abe59a0..ecd464a0bd95 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
@@ -41,7 +41,7 @@ function testCommentDefaultFields() {
       $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
 
       // Delete the instance along the way.
-      field_delete_instance($instances['comment_node_' . $type_name]['comment_body']);
+      $instances['comment_node_' . $type_name]['comment_body']->delete();
     }
 
     // Check that the 'comment_body' field is deleted.
diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php
index 0fdbb617c21a..47efe82b6827 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php
@@ -73,7 +73,7 @@ function setUp() {
     // Make comment body translatable.
     $field = field_info_field('comment_body');
     $field['translatable'] = TRUE;
-    field_update_field($field);
+    $field->save();
     $this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.');
   }
 
diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php
index 100e02fe5b14..2a147915341a 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTranslationUITest.php
@@ -65,7 +65,7 @@ function setupTestFields() {
     parent::setupTestFields();
     $field = field_info_field('comment_body');
     $field['translatable'] = TRUE;
-    field_update_field($field);
+    $field->save();
   }
 
   /**
diff --git a/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php
index 0ce00711c0d1..166a64bb43b6 100644
--- a/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php
+++ b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php
@@ -24,7 +24,7 @@ class ContactFieldsTest extends ViewTestBase {
   /**
    * Contains the field definition array attached to contact used for this test.
    *
-   * @var array
+   * @var \Drupal\field\Plugin\Core\Entity\Field
    */
   protected $field;
 
@@ -39,19 +39,17 @@ public static function getInfo() {
   protected function setUp() {
     parent::setUp();
 
-    $field = array(
+    $this->field = entity_create('field_entity', array(
       'field_name' => strtolower($this->randomName()),
       'type' => 'text'
-    );
+    ));
+    $this->field->save();
 
-    $this->field = field_create_field($field);
-
-    $instance = array(
-      'field_name' => $field['field_name'],
+    entity_create('field_instance', array(
+      'field_name' => $this->field->id(),
       'entity_type' => 'contact_message',
       'bundle' => 'contact_message',
-    );
-    field_create_instance($instance);
+    ))->save();
 
     $this->container->get('views.views_data')->clear();
   }
@@ -60,7 +58,6 @@ protected function setUp() {
    * Tests the views data generation.
    */
   public function testViewsData() {
-    $field_name = $this->field['field_name'];
     $table_name = _field_sql_storage_tablename($this->field);
     $data = $this->container->get('views.views_data')->get($table_name);
 
@@ -68,7 +65,7 @@ public function testViewsData() {
     $expected = array('', '_value', '_format');
     $this->assertEqual(count($data), count($expected), 'The expected amount of array keys were found.');
     foreach ($expected as $suffix) {
-      $this->assertTrue(isset($data[$field_name . $suffix]));
+      $this->assertTrue(isset($data[$this->field->id() . $suffix]));
     }
     $this->assertTrue(empty($data['table']['join']), 'The field is not joined to the non existent contact message base table.');
   }
diff --git a/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php b/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php
index 9a7892cebc96..a0538d766222 100644
--- a/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php
+++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php
@@ -26,10 +26,17 @@ class DatetimeFieldTest extends WebTestBase {
   /**
    * A field to use in this test class.
    *
-   * @var \Drupal\Core\Datetime\DrupalDateTime
+   * @var \Drupal\field\Plugin\Core\Entity\Field
    */
   protected $field;
 
+  /**
+   * The instance used in this test class.
+   *
+   * @var \Drupal\field\Plugin\Core\Entity\FieldInstance
+   */
+  protected $instance;
+
   public static function getInfo() {
     return array(
       'name'  => 'Datetime Field',
@@ -49,22 +56,24 @@ function setUp() {
     $this->drupalLogin($web_user);
 
     // Create a field with settings to validate.
-    $this->field = field_create_field(array(
+    $this->field = entity_create('field_entity', array(
       'field_name' => drupal_strtolower($this->randomName()),
       'type' => 'datetime',
       'settings' => array('datetime_type' => 'date'),
     ));
-    $this->instance = field_create_instance(array(
-      'field_name' => $this->field['field_name'],
+    $this->field->save();
+    $this->instance = entity_create('field_instance', array(
+      'field_name' => $this->field->id(),
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
       'settings' => array(
         'default_value' => 'blank',
       ),
     ));
+    $this->instance->save();
 
-    entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
-      ->setComponent($this->field['field_name'], array(
+    entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')
+      ->setComponent($this->field->id(), array(
         'type' => 'datetime_default',
       ))
       ->save();
@@ -74,8 +83,8 @@ function setUp() {
       'label' => 'hidden',
       'settings' => array('format_type' => 'medium'),
     );
-    entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
-      ->setComponent($this->field['field_name'], $this->display_options)
+    entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
+      ->setComponent($this->field->id(), $this->display_options)
       ->save();
   }
 
@@ -83,12 +92,13 @@ function setUp() {
    * Tests date field functionality.
    */
   function testDateField() {
+    $field_name = $this->field->id();
 
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $langcode = Language::LANGCODE_NOT_SPECIFIED;
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
-    $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element not found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
+    $this->assertNoFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element not found.');
 
     // Submit a valid date and ensure it is accepted.
     $value = '2012-12-31 00:00:00';
@@ -100,7 +110,7 @@ function testDateField() {
     $edit = array(
       'user_id' => 1,
       'name' => $this->randomName(),
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format),
+      "{$field_name}[$langcode][0][value][date]" => $date->format($date_format),
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
@@ -120,8 +130,8 @@ function testDateField() {
       foreach ($values as $new_value) {
         // Update the entity display settings.
         $this->display_options['settings'] = array($setting => $new_value);
-        $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
-          ->setComponent($this->instance['field_name'], $this->display_options)
+        entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
+          ->setComponent($field_name, $this->display_options)
           ->save();
 
         $this->renderTestEntity($id);
@@ -138,8 +148,8 @@ function testDateField() {
 
     // Verify that the plain formatter works.
     $this->display_options['type'] = 'datetime_plain';
-    $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
-      ->setComponent($this->instance['field_name'], $this->display_options)
+    entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
+      ->setComponent($field_name, $this->display_options)
       ->save();
     $expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
     $this->renderTestEntity($id);
@@ -150,16 +160,16 @@ function testDateField() {
    * Tests date and time field.
    */
   function testDatetimeField() {
-
+    $field_name = $this->field->id();
     // Change the field to a datetime field.
     $this->field['settings']['datetime_type'] = 'datetime';
-    field_update_field($this->field);
+    $this->field->save();
 
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $langcode = Language::LANGCODE_NOT_SPECIFIED;
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element found.');
 
     // Submit a valid date and ensure it is accepted.
     $value = '2012-12-31 00:00:00';
@@ -171,8 +181,8 @@ function testDatetimeField() {
     $edit = array(
       'user_id' => 1,
       'name' => $this->randomName(),
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format),
-      "{$this->field['field_name']}[$langcode][0][value][time]" => $date->format($time_format),
+      "{$field_name}[$langcode][0][value][date]" => $date->format($date_format),
+      "{$field_name}[$langcode][0][value][time]" => $date->format($time_format),
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
@@ -189,8 +199,8 @@ function testDatetimeField() {
       foreach ($values as $new_value) {
         // Update the entity display settings.
         $this->display_options['settings'] = array($setting => $new_value);
-        $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
-          ->setComponent($this->instance['field_name'], $this->display_options)
+        entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
+          ->setComponent($field_name, $this->display_options)
           ->save();
 
         $this->renderTestEntity($id);
@@ -207,8 +217,8 @@ function testDatetimeField() {
 
     // Verify that the plain formatter works.
     $this->display_options['type'] = 'datetime_plain';
-    $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
-      ->setComponent($this->instance['field_name'], $this->display_options)
+    entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
+      ->setComponent($field_name, $this->display_options)
       ->save();
     $expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $this->renderTestEntity($id);
@@ -219,14 +229,14 @@ function testDatetimeField() {
    * Tests Date List Widget functionality.
    */
   function testDatelistWidget() {
-
+    $field_name = $this->field->id();
     // Change the field to a datetime field.
-    $this->field['settings']['datetime_type'] = 'datetime';
-    field_update_field($this->field);
+    $this->field->settings['datetime_type'] = 'datetime';
+    $this->field->save();
 
     // Change the widget to a datelist widget.
-    entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
-      ->setComponent($this->instance['field_name'], array(
+    entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')
+      ->setComponent($field_name, array(
         'type' => 'datetime_datelist',
         'settings' => array(
           'increment' => 1,
@@ -239,7 +249,6 @@ function testDatelistWidget() {
 
     // Display creation form.
     $this->drupalGet('entity_test/add');
-    $field_name = $this->field['field_name'];
     $langcode = Language::LANGCODE_NOT_SPECIFIED;
 
     $this->assertFieldByXPath("//*[@id=\"edit-$field_name-$langcode-0-value-year\"]", NULL, 'Year element found.');
@@ -258,7 +267,6 @@ function testDatelistWidget() {
 
     // Submit a valid date and ensure it is accepted.
     $date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15);
-    $date = new DrupalDateTime($date_value);
 
     $edit = array(
       'user_id' => 1,
@@ -267,7 +275,7 @@ function testDatelistWidget() {
     // Add the ampm indicator since we are testing 12 hour time.
     $date_value['ampm'] = 'am';
     foreach ($date_value as $part => $value) {
-      $edit["{$this->field['field_name']}[$langcode][0][value][$part]"] = $value;
+      $edit["{$field_name}[$langcode][0][value][$part]"] = $value;
     }
 
     $this->drupalPost(NULL, $edit, t('Save'));
@@ -289,13 +297,14 @@ function testDatelistWidget() {
   function testDefaultValue() {
 
     // Change the field to a datetime field.
-    $this->field['settings']['datetime_type'] = 'datetime';
-    field_update_field($this->field);
+    $this->field->settings['datetime_type'] = 'datetime';
+    $this->field->save();
+    $field_name = $this->field->id();
 
     // Set the default value to 'now'.
-    $this->instance['settings']['default_value'] = 'now';
-    $this->instance['default_value_function'] = 'datetime_default_value';
-    field_update_instance($this->instance);
+    $this->instance->settings['default_value'] = 'now';
+    $this->instance->default_value_function = 'datetime_default_value';
+    $this->instance->save();
 
     // Display creation form.
     $date = new DrupalDateTime();
@@ -307,21 +316,21 @@ function testDefaultValue() {
     // it may be a few seconds between the time the comparison date is created
     // and the form date, so we just test the date and that the time is not
     // empty.
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", $date->format($date_format), 'Date element found.');
-    $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", $date->format($date_format), 'Date element found.');
+    $this->assertNoFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element found.');
 
     // Set the default value to 'blank'.
-    $this->instance['settings']['default_value'] = 'blank';
-    $this->instance['default_value_function'] = 'datetime_default_value';
-    field_update_instance($this->instance);
+    $this->instance->settings['default_value'] = 'blank';
+    $this->instance->default_value_function = 'datetime_default_value';
+    $this->instance->save();
 
     // Display creation form.
     $date = new DrupalDateTime();
     $this->drupalGet('entity_test/add');
 
     // See that no date is set.
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element found.');
   }
 
   /**
@@ -330,44 +339,45 @@ function testDefaultValue() {
   function testInvalidField() {
 
     // Change the field to a datetime field.
-    $this->field['settings']['datetime_type'] = 'datetime';
-    field_update_field($this->field);
+    $this->field->settings['datetime_type'] = 'datetime';
+    $this->field->save();
+    $field_name = $this->field->id();
 
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $langcode = Language::LANGCODE_NOT_SPECIFIED;
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][date]", '', 'Date element found.');
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element found.');
 
     // Submit invalid dates and ensure they is not accepted.
     $date_value = '';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => '12:00:00',
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => '12:00:00',
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', 'Empty date value has been caught.');
 
     $date_value = 'aaaa-12-01';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => '00:00:00',
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => '00:00:00',
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array('%date' => $date_value)));
 
     $date_value = '2012-75-01';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => '00:00:00',
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => '00:00:00',
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array('%date' => $date_value)));
 
     $date_value = '2012-12-99';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => '00:00:00',
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => '00:00:00',
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array('%date' => $date_value)));
@@ -375,8 +385,8 @@ function testInvalidField() {
     $date_value = '2012-12-01';
     $time_value = '';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => $time_value,
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => $time_value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', 'Empty time value has been caught.');
@@ -384,8 +394,8 @@ function testInvalidField() {
     $date_value = '2012-12-01';
     $time_value = '49:00:00';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => $time_value,
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => $time_value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array('%time' => $time_value)));
@@ -393,8 +403,8 @@ function testInvalidField() {
     $date_value = '2012-12-01';
     $time_value = '12:99:00';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => $time_value,
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => $time_value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array('%time' => $time_value)));
@@ -402,8 +412,8 @@ function testInvalidField() {
     $date_value = '2012-12-01';
     $time_value = '12:15:99';
     $edit = array(
-      "{$this->field['field_name']}[$langcode][0][value][date]" => $date_value,
-      "{$this->field['field_name']}[$langcode][0][value][time]" => $time_value,
+      "{$field_name}[$langcode][0][value][date]" => $date_value,
+      "{$field_name}[$langcode][0][value][time]" => $time_value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value)));
diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php
index 30aec3fd530a..649f03327d51 100644
--- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php
+++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php
@@ -54,15 +54,15 @@ function setUp() {
    */
   function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
     $field = $field_name . '_field';
-    $this->field = array(
+    $this->$field = entity_create('field_entity', array(
       'field_name' => $field_name,
       'type' => $type,
       'cardinality' => $cardinality,
-    );
-    $this->$field = field_create_field($this->field);
+    ));
+    $this->$field->save();
 
     $instance = $field_name . '_instance';
-    $this->$instance = array(
+    $this->$instance = entity_create('field_instance', array(
       'field_name' => $field_name,
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
@@ -70,8 +70,8 @@ function createFieldWithInstance($field_name, $type, $cardinality, $label, $inst
       'description' => $label,
       'weight' => mt_rand(0, 127),
       'settings' => $instance_settings,
-    );
-    field_create_instance($this->$instance);
+    ));
+    $this->$instance->save();
 
     entity_get_form_display('entity_test', 'entity_test', 'default')
       ->setComponent($field_name, array(
diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php
index 432c2bea21a7..c1982c5b6f4f 100644
--- a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php
+++ b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php
@@ -79,24 +79,24 @@ function testText() {
     $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'direct' editor is selected.");
 
     // Editor selection with text processing, cardinality 1.
-    $this->field_text_instance['settings']['text_processing'] = 1;
-    field_update_instance($this->field_text_instance);
+    $this->field_text_instance->settings['text_processing'] = 1;
+    $this->field_text_instance->save();
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected.");
 
     // Editor selection without text processing, cardinality 1 (again).
-    $this->field_text_instance['settings']['text_processing'] = 0;
-    field_update_instance($this->field_text_instance);
+    $this->field_text_instance->settings['text_processing'] = 0;
+    $this->field_text_instance->save();
     $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'direct' editor is selected.");
 
     // Editor selection without text processing, cardinality >1
-    $this->field_text_field['cardinality'] = 2;
-    field_update_field($this->field_text_field);
+    $this->field_text_field->cardinality = 2;
+    $this->field_text_field->save();
     $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected.");
 
     // Editor selection with text processing, cardinality >1
-    $this->field_text_instance['settings']['text_processing'] = 1;
-    field_update_instance($this->field_text_instance);
+    $this->field_text_instance->settings['text_processing'] = 1;
+    $this->field_text_instance->save();
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected.");
   }
 
@@ -133,8 +133,8 @@ function testTextWysiwyg() {
     $this->assertEqual('wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected.");
 
     // Editor selection with text processing, cardinality >1
-    $this->field_textarea_field['cardinality'] = 2;
-    field_update_field($this->field_textarea_field);
+    $this->field_textarea_field->cardinality = 2;
+    $this->field_textarea_field->save();
     $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
   }
@@ -163,8 +163,8 @@ function testNumber() {
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected.");
 
     // Editor selection with cardinality >1.
-    $this->field_nr_field['cardinality'] = 2;
-    field_update_field($this->field_nr_field);
+    $this->field_nr_field->cardinality = 2;
+    $this->field_nr_field->save();
     $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected.");
   }
 
diff --git a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php
index e1ee984fed87..e9e3418428eb 100644
--- a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php
+++ b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php
@@ -137,8 +137,8 @@ function testEditorSelection() {
     $this->assertEqual('editor', $this->getSelectedEditor($items, $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected.");
 
     // Editor selection with text processing, cardinality >1
-    $this->field_textarea_field['cardinality'] = 2;
-    field_update_field($this->field_textarea_field);
+    $this->field_textarea_field->cardinality = 2;
+    $this->field_textarea_field->save();
     $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
     $this->assertEqual('form', $this->getSelectedEditor($items, $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
   }
diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php
index 699944154cc8..2d76d7616851 100644
--- a/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php
+++ b/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php
@@ -22,6 +22,20 @@ class EmailFieldTest extends WebTestBase {
    */
   public static $modules = array('node', 'entity_test', 'email', 'field_ui');
 
+  /**
+   * A field to use in this test class.
+   *
+   * @var \Drupal\field\Plugin\Core\Entity\Field
+   */
+  protected $field;
+
+  /**
+   * The instance used in this test class.
+   *
+   * @var \Drupal\field\Plugin\Core\Entity\FieldInstance
+   */
+  protected $instance;
+
   public static function getInfo() {
     return array(
       'name'  => 'E-mail field',
@@ -46,21 +60,22 @@ function setUp() {
    */
   function testEmailField() {
     // Create a field with settings to validate.
-    $this->field = array(
-      'field_name' => drupal_strtolower($this->randomName()),
+    $field_name = drupal_strtolower($this->randomName());
+    $this->field = entity_create('field_entity', array(
+      'field_name' => $field_name,
       'type' => 'email',
-    );
-    field_create_field($this->field);
-    $this->instance = array(
-      'field_name' => $this->field['field_name'],
+    ));
+    $this->field->save();
+    $this->instance = entity_create('field_instance', array(
+      'field_name' => $field_name,
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
-    );
-    field_create_instance($this->instance);
+    ));
+    $this->instance->save();
 
     // Create a form display for the default form mode.
     entity_get_form_display('entity_test', 'entity_test', 'default')
-      ->setComponent($this->field['field_name'], array(
+      ->setComponent($field_name, array(
         'type' => 'email_default',
         'settings' => array(
           'placeholder' => 'example@example.com',
@@ -69,7 +84,7 @@ function testEmailField() {
       ->save();
     // Create a display for the full view mode.
     entity_get_display('entity_test', 'entity_test', 'full')
-      ->setComponent($this->field['field_name'], array(
+      ->setComponent($field_name, array(
         'type' => 'email_mailto',
       ))
       ->save();
@@ -77,7 +92,7 @@ function testEmailField() {
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $langcode = Language::LANGCODE_NOT_SPECIFIED;
-    $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget found.');
+    $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget found.');
     $this->assertRaw('placeholder="example@example.com"');
 
     // Submit a valid e-mail address and ensure it is accepted.
@@ -85,7 +100,7 @@ function testEmailField() {
     $edit = array(
       'user_id' => 1,
       'name' => $this->randomName(),
-      "{$this->field['field_name']}[$langcode][0][value]" => $value,
+      "{$field_name}[$langcode][0][value]" => $value,
     );
     $this->drupalPost(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
diff --git a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
index 1555b8d7b20c..7e721636254c 100644
--- a/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
+++ b/core/modules/email/lib/Drupal/email/Tests/EmailItemTest.php
@@ -35,17 +35,15 @@ public function setUp() {
     parent::setUp();
 
     // Create an email field and instance for validation.
-    $this->field = array(
+    entity_create('field_entity', array(
       'field_name' => 'field_email',
       'type' => 'email',
-    );
-    field_create_field($this->field);
-    $this->instance = array(
+    ))->save();
+    entity_create('field_instance', array(
       'entity_type' => 'entity_test',
       'field_name' => 'field_email',
       'bundle' => 'entity_test',
-    );
-    field_create_instance($this->instance);
+    ))->save();
 
     // Create a form display for the default form mode.
     entity_get_form_display('entity_test', 'entity_test', 'default')
diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php
index 051fb76f87e0..24cb092ec7c0 100644
--- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php
+++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php
@@ -139,22 +139,23 @@ public function testFieldComponent() {
       'mode' => 'default',
     ));
 
+    $field_name = 'test_field';
     // Create a field and an instance.
-    $field = array(
-      'field_name' => 'test_field',
+    $field = entity_create('field_entity', array(
+      'field_name' => $field_name,
       'type' => 'test_field'
-    );
-    field_create_field($field);
-    $instance = array(
-      'field_name' => $field['field_name'],
+    ));
+    $field->save();
+    $instance = entity_create('field_instance', array(
+      'field_name' => $field_name,
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
-    );
-    field_create_instance($instance);
+    ));
+    $instance->save();
 
     // Check that providing no options results in default values being used.
-    $display->setComponent($field['field_name']);
-    $field_type_info = field_info_field_types($field['type']);
+    $display->setComponent($field_name);
+    $field_type_info = field_info_field_types($field->type);
     $default_formatter = $field_type_info['default_formatter'];
     $default_settings = field_info_formatter_settings($default_formatter);
     $expected = array(
@@ -163,10 +164,10 @@ public function testFieldComponent() {
       'type' => $default_formatter,
       'settings' => $default_settings,
     );
-    $this->assertEqual($display->getComponent($field['field_name']), $expected);
+    $this->assertEqual($display->getComponent($field_name), $expected);
 
     // Check that the getFormatter() method returns the correct formatter plugin.
-    $formatter = $display->getFormatter($field['field_name']);
+    $formatter = $display->getFormatter($field_name);
     $this->assertEqual($formatter->getPluginId(), $default_formatter);
     $this->assertEqual($formatter->getSettings(), $default_settings);
 
@@ -174,26 +175,26 @@ public function testFieldComponent() {
     // arbitrary property and reading it back.
     $random_value = $this->randomString();
     $formatter->randomValue = $random_value;
-    $formatter = $display->getFormatter($field['field_name']);
+    $formatter = $display->getFormatter($field_name);
     $this->assertEqual($formatter->randomValue, $random_value);
 
     // Check that changing the definition creates a new formatter.
-    $display->setComponent($field['field_name'], array(
+    $display->setComponent($field_name, array(
       'type' => 'field_test_multiple',
     ));
-    $formatter = $display->getFormatter($field['field_name']);
+    $formatter = $display->getFormatter($field_name);
     $this->assertEqual($formatter->getPluginId(), 'field_test_multiple');
     $this->assertFalse(isset($formatter->randomValue));
 
     // Check that specifying an unknown formatter (e.g. case of a disabled
     // module) gets stored as is in the display, but results in the default
     // formatter being used.
-    $display->setComponent($field['field_name'], array(
+    $display->setComponent($field_name, array(
       'type' => 'unknown_formatter',
     ));
-    $options = $display->getComponent($field['field_name']);
+    $options = $display->getComponent($field_name);
     $this->assertEqual($options['type'], 'unknown_formatter');
-    $formatter = $display->getFormatter($field['field_name']);
+    $formatter = $display->getFormatter($field_name);
     $this->assertEqual($formatter->getPluginId(), $default_formatter);
   }
 
@@ -232,32 +233,33 @@ public function testRenameDeleteBundle() {
   public function testDeleteFieldInstance() {
     $this->enableModules(array('field_sql_storage', 'field_test'));
 
+    $field_name = 'test_field';
     // Create a field and an instance.
     $field = entity_create('field_entity', array(
-      'field_name' => 'test_field',
+      'field_name' => $field_name,
       'type' => 'test_field'
     ));
     $field->save();
     $instance = entity_create('field_instance', array(
-      'field_name' => $field['field_name'],
+      'field_name' => $field_name,
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
     ));
     $instance->save();
 
     // Create an entity display.
-    $display = entity_create('entity_display', array(
+    entity_create('entity_display', array(
       'targetEntityType' => 'entity_test',
       'bundle' => 'entity_test',
       'viewMode' => 'default',
-    ))->setComponent($field['field_name'])->save();
+    ))->setComponent($field_name)->save();
 
     // Delete the instance.
     $instance->delete();
 
     // Check that the component has been removed from the entity display.
     $display = entity_get_display('entity_test', 'entity_test', 'default');
-    $this->assertFalse($display->getComponent($field['field_name']));
+    $this->assertFalse($display->getComponent($field_name));
   }
 
 }
diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php
index b92cfbff36ab..e7cb19dc91d6 100644
--- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php
+++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php
@@ -62,21 +62,22 @@ public function testFieldComponent() {
     ));
 
     // Create a field and an instance.
-    $field = array(
-      'field_name' => 'test_field',
+    $field_name = 'test_field';
+    $field = entity_create('field_entity', array(
+      'field_name' => $field_name,
       'type' => 'test_field'
-    );
-    field_create_field($field);
-    $instance = array(
-      'field_name' => $field['field_name'],
+    ));
+    $field->save();
+    $instance = entity_create('field_instance', array(
+      'field_name' => $field_name,
       'entity_type' => 'entity_test',
       'bundle' => 'entity_test',
-    );
-    field_create_instance($instance);
+    ));
+    $instance->save();
 
     // Check that providing no options results in default values being used.
-    $form_display->setComponent($field['field_name']);
-    $field_type_info = field_info_field_types($field['type']);
+    $form_display->setComponent($field_name);
+    $field_type_info = field_info_field_types($field->type);
     $default_widget = $field_type_info['default_widget'];
     $default_settings = field_info_widget_settings($default_widget);
     $expected = array(
@@ -84,10 +85,10 @@ public function testFieldComponent() {
       'type' => $default_widget,
       'settings' => $default_settings,
     );
-    $this->assertEqual($form_display->getComponent($field['field_name']), $expected);
+    $this->assertEqual($form_display->getComponent($field_name), $expected);
 
     // Check that the getWidget() method returns the correct widget plugin.
-    $widget = $form_display->getWidget($field['field_name']);
+    $widget = $form_display->getWidget($field_name);
     $this->assertEqual($widget->getPluginId(), $default_widget);
     $this->assertEqual($widget->getSettings(), $default_settings);
 
@@ -95,26 +96,26 @@ public function testFieldComponent() {
     // arbitrary property and reading it back.
     $random_value = $this->randomString();
     $widget->randomValue = $random_value;
-    $widget = $form_display->getWidget($field['field_name']);
+    $widget = $form_display->getWidget($field_name);
     $this->assertEqual($widget->randomValue, $random_value);
 
     // Check that changing the definition creates a new widget.
-    $form_display->setComponent($field['field_name'], array(
+    $form_display->setComponent($field_name, array(
       'type' => 'field_test_multiple',
     ));
-    $widget = $form_display->getWidget($field['field_name']);
+    $widget = $form_display->getWidget($field_name);
     $this->assertEqual($widget->getPluginId(), 'test_field_widget');
     $this->assertFalse(isset($widget->randomValue));
 
     // Check that specifying an unknown widget (e.g. case of a disabled module)
     // gets stored as is in the display, but results in the default widget being
     // used.
-    $form_display->setComponent($field['field_name'], array(
+    $form_display->setComponent($field_name, array(
       'type' => 'unknown_widget',
     ));
-    $options = $form_display->getComponent($field['field_name']);
+    $options = $form_display->getComponent($field_name);
     $this->assertEqual($options['type'], 'unknown_widget');
-    $widget = $form_display->getWidget($field['field_name']);
+    $widget = $form_display->getWidget($field_name);
     $this->assertEqual($widget->getPluginId(), $default_widget);
   }
 
diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module
index cd302e92227e..84d4743ce1bd 100644
--- a/core/modules/entity_reference/entity_reference.module
+++ b/core/modules/entity_reference/entity_reference.module
@@ -194,8 +194,8 @@ function entity_reference_field_update_field($field, $prior_field) {
   foreach ($field['bundles'] as $entity_type => $bundles) {
     foreach ($bundles as $bundle) {
       $instance = field_info_instance($entity_type, $field_name, $bundle);
-      $instance['settings']['handler_settings'] = array();
-      field_update_instance($instance);
+      $instance->settings['handler_settings'] = array();
+      $instance->save();
     }
   }
 }
@@ -430,7 +430,7 @@ function entity_reference_create_instance($entity_type, $bundle, $field_name, $f
         'target_type' => $target_entity_type,
       ),
     );
-    field_create_field($field);
+    entity_create('field_entity', $field)->save();
   }
 
   if (empty($instance)) {
@@ -444,6 +444,7 @@ function entity_reference_create_instance($entity_type, $bundle, $field_name, $f
         'handler_settings' => $selection_handler_settings,
       ),
     );
-    field_create_instance($instance);
+    entity_create('field_instance', $instance)->save();
   }
 }
+
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php
index b6bb5aaf69e7..2d1f3bbeb4c9 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php
@@ -35,7 +35,7 @@ function setUp() {
     $referenced = $this->drupalCreateContentType();
     $this->referenced_type = $referenced->type;
 
-    $field = array(
+    entity_create('field_entity', array(
       'translatable' => FALSE,
       'entity_types' => array(),
       'settings' => array(
@@ -44,11 +44,9 @@ function setUp() {
       'field_name' => 'test_field',
       'type' => 'entity_reference',
       'cardinality' => FIELD_CARDINALITY_UNLIMITED,
-    );
-
-    field_create_field($field);
+    ))->save();
 
-    $instance = array(
+    entity_create('field_instance', array(
       'label' => 'Entity reference field',
       'field_name' => 'test_field',
       'entity_type' => 'node',
@@ -64,9 +62,7 @@ function setUp() {
           'auto_create' => TRUE,
         ),
       ),
-    );
-
-    field_create_instance($instance);
+    ))->save();
 
     entity_get_form_display('node', $referencing->type, 'default')
       ->setComponent('test_field', array(
diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php
index 9fdb2d4f63ca..09dd2c877c86 100644
--- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php
@@ -36,22 +36,20 @@ function setUp() {
    */
   public function testSort() {
     // Add text field to entity, to sort by.
-    $field_info = array(
+    entity_create('field_entity', array(
       'field_name' => 'field_text',
       'type' => 'text',
       'entity_types' => array('node'),
-    );
-    field_create_field($field_info);
+    ))->save();
 
-    $instance_info = array(
+    entity_create('field_instance', array(
       'label' => 'Text Field',
       'field_name' => 'field_text',
       'entity_type' => 'node',
       'bundle' => 'article',
       'settings' => array(),
       'required' => FALSE,
-    );
-    field_create_instance($instance_info);
+    ))->save();
 
 
     // Create a field and instance.
-- 
GitLab