Commit b42529c4 authored by catch's avatar catch
Browse files

Issue #2981392 by masipila, maxocub: Comment migration corrupts data with multilingual sites

parent 808263b1
Loading
Loading
Loading
Loading
+13 −1
Changes for core/modules/comment/migrations/d6_comment.yml: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -16,7 +16,16 @@ process:
    plugin: migration_lookup
    migration: d6_comment
    source: pid
  entity_id: nid
  entity_id:
    -
      plugin: migration_lookup
      migration:
        - d6_node
        - d6_node_translation
      source: nid
    -
      plugin: skip_on_empty
      method: row
  entity_type: 'constants/entity_type'
  comment_type:
    -
@@ -26,6 +35,7 @@ process:
    -
      plugin: skip_on_empty
      method: row
  langcode: language
  field_name: '@comment_type'
  subject: subject
  uid: uid
@@ -52,3 +62,5 @@ migration_dependencies:
    - d6_comment_entity_form_display
    - d6_user
    - d6_filter_format
  optional:
    - d6_node_translation
+13 −1
Changes for core/modules/comment/migrations/d7_comment.yml: 13 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -17,7 +17,16 @@ process:
    plugin: migration_lookup
    migration: d7_comment
    source: pid
  entity_id: nid
  entity_id:
    -
      plugin: migration_lookup
      migration:
        - d7_node
        - d7_node_translation
      source: nid
    -
      plugin: skip_on_empty
      method: row
  entity_type: 'constants/entity_type'
  comment_type:
    -
@@ -27,6 +36,7 @@ process:
    -
      plugin: skip_on_empty
      method: row
  langcode: language
  field_name: '@comment_type'
  subject: subject
  uid: uid
@@ -45,3 +55,5 @@ migration_dependencies:
  required:
    - d7_node
    - d7_comment_type
  optional:
    - d7_node_translation
+9 −1
Changes for core/modules/comment/src/Plugin/migrate/source/d6/Comment.php: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ public function query() {
      'mail', 'homepage', 'format',
    ]);
    $query->innerJoin('node', 'n', 'c.nid = n.nid');
    $query->fields('n', ['type']);
    $query->fields('n', ['type', 'language']);
    $query->orderBy('c.timestamp');
    return $query;
  }
@@ -61,6 +61,13 @@ protected function prepareComment(Row $row) {
    // In D6, status=0 means published, while in D8 means the opposite.
    // See https://www.drupal.org/node/237636.
    $row->setSourceProperty('status', !$row->getSourceProperty('status'));

    // If node did not have a language, use site default language as a fallback.
    if (!$row->getSourceProperty('language')) {
      $language_default = $this->variableGet('language_default', NULL);
      $language = $language_default ? $language_default->language : 'en';
      $row->setSourceProperty('language', $language);
    }
    return $row;
  }

@@ -84,6 +91,7 @@ public function fields() {
      'mail' => $this->t("The comment author's email address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
      'homepage' => $this->t("The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
      'type' => $this->t("The {node}.type to which this comment is a reply."),
      'language' => $this->t("The {node}.language to which this comment is a reply. Site default language is used as a fallback if node does not have a language."),
    ];
  }

+1 −0
Changes for core/modules/comment/src/Plugin/migrate/source/d7/Comment.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ public function fields() {
      'name' => $this->t("The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form."),
      'mail' => $this->t("The comment author's email address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
      'homepage' => $this->t("The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
      'language' => $this->t('The comment language.'),
      'type' => $this->t("The {node}.type to which this comment is a reply."),
    ];
  }
+35 −1
Changes for core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentTest.php: 35 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -20,7 +20,12 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
  /**
   * {@inheritdoc}
   */
  public static $modules = ['comment', 'menu_ui'];
  public static $modules = [
    'comment',
    'content_translation',
    'language',
    'menu_ui',
  ];

  /**
   * {@inheritdoc}
@@ -31,6 +36,7 @@ protected function setUp() {
    $this->installEntitySchema('node');
    $this->installEntitySchema('comment');
    $this->installSchema('comment', ['comment_entity_statistics']);
    $this->installSchema('node', ['node_access']);
    $this->installConfig(['comment']);

    // The entity.node.canonical route must exist when the RDF hook is called.
@@ -38,7 +44,10 @@ protected function setUp() {

    $this->migrateContent();
    $this->executeMigrations([
      'language',
      'd6_language_content_settings',
      'd6_node',
      'd6_node_translation',
      'd6_comment_type',
      'd6_comment_field',
      'd6_comment_field_instance',
@@ -84,6 +93,31 @@ public function testMigration() {
    $node = $comment->getCommentedEntity();
    $this->assertInstanceOf(NodeInterface::class, $node);
    $this->assertSame('1', $node->id());

    // Tests that the language of the comment is migrated from the node.
    $comment = Comment::load(7);
    $this->assertSame('Comment to John Smith - EN', $comment->subject->value);
    $this->assertSame('This is an English comment.', $comment->comment_body->value);
    $this->assertSame('21', $comment->getCommentedEntityId());
    $this->assertSame('node', $comment->getCommentedEntityTypeId());
    $this->assertSame('en', $comment->language()->getId());

    $node = $comment->getCommentedEntity();
    $this->assertInstanceOf(NodeInterface::class, $node);
    $this->assertSame('21', $node->id());

    // Tests that the comment language is correct and that the commented entity
    // is correctly migrated when the comment was posted to a node translation.
    $comment = Comment::load(8);
    $this->assertSame('Comment to John Smith - FR', $comment->subject->value);
    $this->assertSame('This is a French comment.', $comment->comment_body->value);
    $this->assertSame('21', $comment->getCommentedEntityId());
    $this->assertSame('node', $comment->getCommentedEntityTypeId());
    $this->assertSame('fr', $comment->language()->getId());

    $node = $comment->getCommentedEntity();
    $this->assertInstanceOf(NodeInterface::class, $node);
    $this->assertSame('21', $node->id());
  }

}
Loading