From 902098c7650766c2110965642c386838c628d6ab Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 14 May 2014 16:10:04 -0500
Subject: [PATCH] Issue #2249113 by tstoeckler, jessebeach: Use an entity save
 instead of db_insert() in user_install().

---
 core/includes/install.core.inc                |  2 +-
 .../lib/Drupal/user/Tests/UserInstallTest.php | 14 ++++++----
 .../user/lib/Drupal/user/UserStorage.php      |  4 ++-
 core/modules/user/user.info.yml               |  4 +++
 core/modules/user/user.install                | 27 +++++++++----------
 5 files changed, 30 insertions(+), 21 deletions(-)

diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index b00d23b21d38..79c310fbd3f1 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -965,7 +965,7 @@ function install_base_system(&$install_state) {
 
   // Enable the user module so that sessions can be recorded during the
   // upcoming bootstrap step.
-  \Drupal::moduleHandler()->install(array('user'), FALSE);
+  \Drupal::moduleHandler()->install(array('field', 'user'), FALSE);
 
   // Save the list of other modules to install for the upcoming tasks.
   // State can be set to the database now that system.module is installed.
diff --git a/core/modules/user/lib/Drupal/user/Tests/UserInstallTest.php b/core/modules/user/lib/Drupal/user/Tests/UserInstallTest.php
index 4e54ff85bdc3..5d4114884d21 100644
--- a/core/modules/user/lib/Drupal/user/Tests/UserInstallTest.php
+++ b/core/modules/user/lib/Drupal/user/Tests/UserInstallTest.php
@@ -19,7 +19,7 @@ class UserInstallTest extends DrupalUnitTestBase {
    *
    * @var array
    */
-  public static $modules = array('user');
+  public static $modules = array('field', 'user');
 
   /**
    * {@inheritdoc}
@@ -51,11 +51,15 @@ public function testUserInstall() {
     $this->assertFalse(empty($anon->uuid), 'Anon user has a UUID');
     $this->assertFalse(empty($admin->uuid), 'Admin user has a UUID');
 
-    $this->assertEqual($anon->langcode, \Drupal::languageManager()->getDefaultLanguage()->id, 'Anon user language is the default.');
-    $this->assertEqual($admin->langcode, \Drupal::languageManager()->getDefaultLanguage()->id, 'Admin user language is the default.');
+    // Test that the anonymous and administrators languages are equal to the
+    // site's default language.
+    $this->assertEqual($anon->langcode, \Drupal::languageManager()->getDefaultLanguage()->id);
+    $this->assertEqual($admin->langcode, \Drupal::languageManager()->getDefaultLanguage()->id);
 
-    $this->assertEqual($admin->status, 1, 'Admin user is active.');
-    $this->assertEqual($anon->status, 0, 'Anon user is blocked.');
+    // Test that the administrator is active.
+    $this->assertEqual($admin->status, 1);
+    // Test that the anonymous user is blocked.
+    $this->assertEqual($anon->status, 0);
   }
 
 }
diff --git a/core/modules/user/lib/Drupal/user/UserStorage.php b/core/modules/user/lib/Drupal/user/UserStorage.php
index afd53b3fcea3..bad10dab1ea8 100644
--- a/core/modules/user/lib/Drupal/user/UserStorage.php
+++ b/core/modules/user/lib/Drupal/user/UserStorage.php
@@ -99,7 +99,9 @@ function mapFromStorageRecords(array $records) {
    * {@inheritdoc}
    */
   public function save(EntityInterface $entity) {
-    if (!$entity->id()) {
+    // The anonymous user account is saved with the fixed user ID of 0.
+    // Therefore we need to check for NULL explicitly.
+    if ($entity->id() === NULL) {
       $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
       $entity->enforceIsNew();
     }
diff --git a/core/modules/user/user.info.yml b/core/modules/user/user.info.yml
index 45a421a5ade5..9800f7ff2ac9 100644
--- a/core/modules/user/user.info.yml
+++ b/core/modules/user/user.info.yml
@@ -6,3 +6,7 @@ version: VERSION
 core: 8.x
 required: true
 configure: user.admin_index
+# @todo Remove this field dependency
+#   @see https://drupal.org/node/2116363
+dependencies:
+  - field
diff --git a/core/modules/user/user.install b/core/modules/user/user.install
index 97515a251fce..090b235018e3 100644
--- a/core/modules/user/user.install
+++ b/core/modules/user/user.install
@@ -220,28 +220,27 @@ function user_schema() {
  * Implements hook_install().
  */
 function user_install() {
+  $storage = \Drupal::entityManager()->getStorage('user');
+  // @todo Rely on the default value for langcode in
+  //   https://drupal.org/node/1966436
+  $langcode = \Drupal::languageManager()->getDefaultLanguage()->id;
   // Insert a row for the anonymous user.
-  db_insert('users')
-    ->fields(array(
+  $storage
+    ->create(array(
       'uid' => 0,
-      'uuid' => \Drupal::service('uuid')->generate(),
-      'name' => '',
-      'mail' => '',
-      'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
+      'status' => 0,
+      'langcode' => $langcode,
     ))
-    ->execute();
+    ->save();
 
   // We need some placeholders here as name and mail are uniques.
   // This will be changed by the settings form in the installer.
-  db_insert('users')
-    ->fields(array(
+  $storage
+    ->create(array(
       'uid' => 1,
-      'uuid' => \Drupal::service('uuid')->generate(),
       'name' => 'placeholder-for-uid-1',
       'mail' => 'placeholder-for-uid-1',
-      'created' => REQUEST_TIME,
-      'status' => 1,
-      'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
+      'langcode' => $langcode,
     ))
-    ->execute();
+    ->save();
 }
-- 
GitLab