From e52604b4d22192442450df9f9e134030790d05b0 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 14 May 2019 20:54:16 +0100
Subject: [PATCH] Issue #3052147 by Sam152, amateescu, Dropa, alexpott,
 larowlan, catch, xjm: comment_update_8701 fails if there are comments without
 field_name

---
 core/modules/comment/comment.install          |  27 ++++
 .../drupal-8.empty-comment-fields.3052147.php | 128 ++++++++++++++++++
 .../Functional/Update/CommentUpdateTest.php   |  21 +++
 3 files changed, 176 insertions(+)
 create mode 100644 core/modules/comment/tests/fixtures/update/drupal-8.empty-comment-fields.3052147.php

diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install
index 266904bf18ea..b02b432ecbeb 100644
--- a/core/modules/comment/comment.install
+++ b/core/modules/comment/comment.install
@@ -11,6 +11,33 @@
 use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Drupal\field\Entity\FieldStorageConfig;
 
+/**
+ * Implements hook_requirements().
+ */
+function comment_requirements($phase) {
+  $requirements = [];
+  if ($phase === 'update' && drupal_get_installed_schema_version('comment') < 8701) {
+    $has_empty_columns = \Drupal::entityQuery('comment', 'OR')
+      ->condition('entity_type', NULL, 'IS NULL')
+      ->condition('field_name', NULL, 'IS NULL')
+      ->range(0, 1)
+      ->accessCheck(FALSE)
+      ->execute();
+    if ($has_empty_columns) {
+      $requirements['comment_update_8701'] = [
+        'title' => t('Comment required fields update'),
+        'description' => t('The comment_update_8701() function requires that the %field_1 and %field_2 fields have values for all comment entities. See the <a href=":change_record">change record</a> for more information.', [
+          '%field_1' => 'entity_type',
+          '%field_2' => 'field_name',
+          ':change_record' => 'https://www.drupal.org/node/3053046',
+        ]),
+        'severity' => REQUIREMENT_ERROR,
+      ];
+    }
+  }
+  return $requirements;
+}
+
 /**
  * Implements hook_uninstall().
  */
diff --git a/core/modules/comment/tests/fixtures/update/drupal-8.empty-comment-fields.3052147.php b/core/modules/comment/tests/fixtures/update/drupal-8.empty-comment-fields.3052147.php
new file mode 100644
index 000000000000..b57a784b1a2c
--- /dev/null
+++ b/core/modules/comment/tests/fixtures/update/drupal-8.empty-comment-fields.3052147.php
@@ -0,0 +1,128 @@
+<?php
+
+/**
+ * @file
+ * Contains database additions to drupal-8-rc1.filled.standard.php.gz for the
+ * upgrade path in https://www.drupal.org/project/drupal/issues/2885809.
+ */
+
+use Drupal\Core\Database\Database;
+
+$connection = Database::getConnection();
+
+$connection->insert('comment')
+  ->fields([
+    'cid',
+    'comment_type',
+    'uuid',
+    'langcode',
+  ])
+  ->values([
+    'cid' => '5',
+    'comment_type' => 'comment',
+    'uuid' => '2f0505ad-fdc7-49fc-9d39-571bfc3e0f88',
+    'langcode' => 'en',
+  ])
+  ->values([
+    'cid' => '6',
+    'comment_type' => 'comment',
+    'uuid' => '3be94e6b-4506-488a-a861-9742a18f0507',
+    'langcode' => 'en',
+  ])
+  ->execute();
+
+$connection->insert('comment__comment_body')
+  ->fields([
+    'bundle',
+    'deleted',
+    'entity_id',
+    'revision_id',
+    'langcode',
+    'delta',
+    'comment_body_value',
+    'comment_body_format',
+  ])
+  ->values([
+    'bundle' => 'comment',
+    'deleted' => '0',
+    'entity_id' => '5',
+    'revision_id' => '5',
+    'langcode' => 'en',
+    'delta' => '0',
+    'comment_body_value' => "<p>Comment body</p>\r\n",
+    'comment_body_format' => 'basic_html',
+  ])
+  ->values([
+    'bundle' => 'comment',
+    'deleted' => '0',
+    'entity_id' => '6',
+    'revision_id' => '6',
+    'langcode' => 'en',
+    'delta' => '0',
+    'comment_body_value' => "<p>Comment body</p>\r\n",
+    'comment_body_format' => 'basic_html',
+  ])
+  ->execute();
+
+$connection->insert('comment_field_data')
+  ->fields([
+    'cid',
+    'comment_type',
+    'langcode',
+    'pid',
+    'entity_id',
+    'subject',
+    'uid',
+    'name',
+    'mail',
+    'homepage',
+    'hostname',
+    'created',
+    'changed',
+    'status',
+    'thread',
+    'entity_type',
+    'field_name',
+    'default_langcode',
+  ])
+  ->values([
+    'cid' => '5',
+    'comment_type' => 'comment',
+    'langcode' => 'en',
+    'pid' => NULL,
+    'entity_id' => '8',
+    'subject' => 'Comment with no entity_type',
+    'uid' => '1',
+    'name' => 'drupal',
+    'mail' => NULL,
+    'homepage' => NULL,
+    'hostname' => '127.0.0.1',
+    'created' => '1557218256',
+    'changed' => '1557218256',
+    'status' => '1',
+    'thread' => '02/',
+    'entity_type' => NULL,
+    'field_name' => 'field_test_2',
+    'default_langcode' => '1',
+  ])
+  ->values([
+    'cid' => '6',
+    'comment_type' => 'comment',
+    'langcode' => 'en',
+    'pid' => NULL,
+    'entity_id' => '8',
+    'subject' => 'Comment with no field_name',
+    'uid' => '1',
+    'name' => 'drupal',
+    'mail' => NULL,
+    'homepage' => NULL,
+    'hostname' => '127.0.0.1',
+    'created' => '1557218266',
+    'changed' => '1557218266',
+    'status' => '1',
+    'thread' => '03/',
+    'entity_type' => 'node',
+    'field_name' => NULL,
+    'default_langcode' => '1',
+  ])
+  ->execute();
diff --git a/core/modules/comment/tests/src/Functional/Update/CommentUpdateTest.php b/core/modules/comment/tests/src/Functional/Update/CommentUpdateTest.php
index 73b68cdd7035..b800e47f4ee1 100644
--- a/core/modules/comment/tests/src/Functional/Update/CommentUpdateTest.php
+++ b/core/modules/comment/tests/src/Functional/Update/CommentUpdateTest.php
@@ -118,4 +118,25 @@ public function testCommentEntityTypeAndFieldNameRequired() {
     }
   }
 
+  /**
+   * Test the update hook requirements check for 8701.
+   *
+   * @see comment_update_8701()
+   * @see comment_requirements()
+   */
+  public function testCommentEntityTypeAndFieldUpdateRequirementsCheck() {
+    require_once __DIR__ . '/../../../fixtures/update/drupal-8.empty-comment-fields.3052147.php';
+    $this->writeSettings([
+      'settings' => [
+        'update_free_access' => (object) [
+          'value' => TRUE,
+          'required' => TRUE,
+        ],
+      ],
+    ]);
+    $this->drupalGet($this->updateUrl);
+    $this->assertSession()->pageTextContains('Errors found');
+    $this->assertSession()->elementContains('css', '.system-status-report__entry--error', 'The comment_update_8701() function requires that the <em class="placeholder">entity_type</em> and <em class="placeholder">field_name</em> fields have values for all comment entities.');
+  }
+
 }
-- 
GitLab