Commit e58d1de5 authored by catch's avatar catch

Issue #2981000 by maxocub, masipila: Migrate Drupal 7 comment entity translations data to Drupal 8

parent f321b4aa
......@@ -36,8 +36,20 @@ public function prepareRow(Row $row) {
$comment_type = 'comment_node_' . $node_type;
$row->setSourceProperty('comment_type', 'comment_node_' . $node_type);
foreach (array_keys($this->getFields('comment', $comment_type)) as $field) {
$row->setSourceProperty($field, $this->getFieldValues('comment', $field, $cid));
// If this entity was translated using Entity Translation, we need to get
// its source language to get the field values in the right language.
// The translations will be migrated by the d7_comment_entity_translation
// migration.
$entity_translatable = $this->isEntityTranslatable('comment') && (int) $this->variableGet('language_content_type_' . $node_type, 0) === 4;
$source_language = $this->getEntityTranslationSourceLanguage('comment', $cid);
$language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
// Get Field API field values.
foreach ($this->getFields('comment', $comment_type) as $field_name => $field) {
// Ensure we're using the right language if the entity and the field are
// translatable.
$field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
$row->setSourceProperty($field_name, $this->getFieldValues('comment', $field_name, $cid, NULL, $field_language));
}
// If the comment subject was replaced by a real field using the Drupal 7
......
<?php
namespace Drupal\comment\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Provides Drupal 7 comment entity translation source plugin.
*
* @MigrateSource(
* id = "d7_comment_entity_translation",
* source_module = "entity_translation"
* )
*/
class CommentEntityTranslation extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('entity_translation', 'et')
->fields('et')
->fields('c', [
'subject',
])
->condition('et.entity_type', 'comment')
->condition('et.source', '', '<>');
$query->innerJoin('comment', 'c', 'c.cid = et.entity_id');
$query->innerJoin('node', 'n', 'n.nid = c.nid');
$query->addField('n', 'type', 'node_type');
$query->orderBy('et.created');
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$cid = $row->getSourceProperty('entity_id');
$language = $row->getSourceProperty('language');
$node_type = $row->getSourceProperty('node_type');
$comment_type = 'comment_node_' . $node_type;
// Get Field API field values.
foreach ($this->getFields('comment', $comment_type) as $field_name => $field) {
// Ensure we're using the right language if the entity is translatable.
$field_language = $field['translatable'] ? $language : NULL;
$row->setSourceProperty($field_name, $this->getFieldValues('comment', $field_name, $cid, NULL, $field_language));
}
// If the comment subject was replaced by a real field using the Drupal 7
// Title module, use the field value instead of the comment subject.
if ($this->moduleExists('title')) {
$subject_field = $row->getSourceProperty('subject_field');
if (isset($subject_field[0]['value'])) {
$row->setSourceProperty('subject', $subject_field[0]['value']);
}
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'entity_type' => $this->t('The entity type this translation relates to'),
'entity_id' => $this->t('The entity ID this translation relates to'),
'revision_id' => $this->t('The entity revision ID this translation relates to'),
'language' => $this->t('The target language for this translation.'),
'source' => $this->t('The source language from which this translation was created.'),
'uid' => $this->t('The author of this translation.'),
'status' => $this->t('Boolean indicating whether the translation is published (visible to non-administrators).'),
'translate' => $this->t('A boolean indicating whether this translation needs to be updated.'),
'created' => $this->t('The Unix timestamp when the translation was created.'),
'changed' => $this->t('The Unix timestamp when the translation was most recently saved.'),
'subject' => $this->t('The comment title.'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
return [
'entity_id' => [
'type' => 'integer',
'alias' => 'et',
],
'language' => [
'type' => 'string',
'alias' => 'et',
],
];
}
}
......@@ -26,6 +26,8 @@ class MigrateCommentTest extends MigrateDrupal7TestBase {
'language',
'link',
'menu_ui',
// Required for translation migrations.
'migrate_drupal_multilingual',
'node',
'taxonomy',
'telephone',
......@@ -40,6 +42,7 @@ protected function setUp() {
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installEntitySchema('taxonomy_term');
$this->installConfig(['comment', 'node']);
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installSchema('node', ['node_access']);
......@@ -61,6 +64,8 @@ protected function setUp() {
'd7_field',
'd7_field_instance',
'd7_comment',
'd7_entity_translation_settings',
'd7_comment_entity_translation',
]);
}
......@@ -70,7 +75,7 @@ protected function setUp() {
public function testMigration() {
$comment = Comment::load(1);
$this->assertInstanceOf(Comment::class, $comment);
$this->assertSame('A comment', $comment->getSubject());
$this->assertSame('Subject field in English', $comment->getSubject());
$this->assertSame('1421727536', $comment->getCreatedTime());
$this->assertSame('1421727536', $comment->getChangedTime());
$this->assertTrue($comment->getStatus());
......@@ -79,7 +84,7 @@ public function testMigration() {
$this->assertSame('This is a comment', $comment->comment_body->value);
$this->assertSame('filtered_html', $comment->comment_body->format);
$this->assertSame('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', $comment->getHostname());
$this->assertSame('und', $comment->language()->getId());
$this->assertSame('en', $comment->language()->getId());
$this->assertSame('1000000', $comment->field_integer->value);
$node = $comment->getCommentedEntity();
......@@ -109,4 +114,42 @@ public function testMigration() {
$this->assertSame('2', $node->id());
}
/**
* Tests the migration of comment entity translations.
*/
public function testCommentEntityTranslations() {
$manager = $this->container->get('content_translation.manager');
// Get the comment and its translations.
$comment = Comment::load(1);
$comment_fr = $comment->getTranslation('fr');
$comment_is = $comment->getTranslation('is');
// Test that fields translated with Entity Translation are migrated.
$this->assertSame('Subject field in English', $comment->getSubject());
$this->assertSame('Subject field in French', $comment_fr->getSubject());
$this->assertSame('Subject field in Icelandic', $comment_is->getSubject());
$this->assertSame('1000000', $comment->field_integer->value);
$this->assertSame('2000000', $comment_fr->field_integer->value);
$this->assertSame('3000000', $comment_is->field_integer->value);
// Test that the French translation metadata is correctly migrated.
$metadata_fr = $manager->getTranslationMetadata($comment_fr);
$this->assertFalse($metadata_fr->isPublished());
$this->assertSame('en', $metadata_fr->getSource());
$this->assertSame('1', $metadata_fr->getAuthor()->uid->value);
$this->assertSame('1531837764', $metadata_fr->getCreatedTime());
$this->assertSame('1531837764', $metadata_fr->getChangedTime());
$this->assertFalse($metadata_fr->isOutdated());
// Test that the Icelandic translation metadata is correctly migrated.
$metadata_is = $manager->getTranslationMetadata($comment_is);
$this->assertTrue($metadata_is->isPublished());
$this->assertSame('en', $metadata_is->getSource());
$this->assertSame('2', $metadata_is->getAuthor()->uid->value);
$this->assertSame('1531838064', $metadata_is->getCreatedTime());
$this->assertSame('1531838064', $metadata_is->getChangedTime());
$this->assertTrue($metadata_is->isOutdated());
}
}
<?php
namespace Drupal\Tests\comment\Kernel\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D7 comment entity translation source plugin.
*
* @covers \Drupal\comment\Plugin\migrate\source\d7\CommentEntityTranslation
* @group comment
*/
class CommentEntityTranslationTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['comment', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['comment'] = [
[
'cid' => '1',
'pid' => '0',
'nid' => '1',
'uid' => '1',
'subject' => 'A comment',
'hostname' => '::1',
'created' => '1421727536',
'changed' => '1421727536',
'status' => '1',
'thread' => '01/',
'name' => 'admin',
'mail' => '',
'homepage' => '',
'language' => 'en',
],
];
$tests[0]['source_data']['entity_translation'] = [
[
'entity_type' => 'comment',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'en',
'source' => '',
'uid' => 1,
'status' => 1,
'translate' => 0,
'created' => '1421727536',
'changed' => '1421727536',
],
[
'entity_type' => 'comment',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'fr',
'source' => 'en',
'uid' => 1,
'status' => 0,
'translate' => 0,
'created' => 1531343508,
'changed' => 1531343508,
],
[
'entity_type' => 'comment',
'entity_id' => 1,
'revision_id' => 1,
'language' => 'es',
'source' => 'en',
'uid' => 2,
'status' => 1,
'translate' => 1,
'created' => 1531343528,
'changed' => 1531343528,
],
];
$tests[0]['source_data']['field_config'] = [
[
'id' => 1,
'field_name' => 'field_test',
'type' => 'text',
'module' => 'text',
'active' => 1,
'storage_type' => 'field_sql_storage',
'storage_module' => 'field_sql_storage',
'storage_active' => 1,
'locked' => 1,
'data' => 'a:0:{}',
'cardinality' => 1,
'translatable' => 1,
'deleted' => 0,
],
[
'id' => 2,
'field_name' => 'subject_field',
'type' => 'text',
'module' => 'text',
'active' => 1,
'storage_type' => 'field_sql_storage',
'storage_module' => 'field_sql_storage',
'storage_active' => 1,
'locked' => 1,
'data' => 'a:0:{}',
'cardinality' => 1,
'translatable' => 1,
'deleted' => 0,
],
];
$tests[0]['source_data']['field_config_instance'] = [
[
'id' => '1',
'field_id' => '1',
'field_name' => 'field_test',
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'data' => 'a:0:{}',
'deleted' => '0',
],
[
'id' => '2',
'field_id' => '2',
'field_name' => 'subject_field',
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'data' => 'a:0:{}',
'deleted' => '0',
],
];
$tests[0]['source_data']['field_data_field_test'] = [
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'en',
'delta' => '0',
'field_test_value' => 'This is an English comment',
'field_test_format' => NULL,
],
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'field_test_value' => 'This is a French comment',
'field_test_format' => NULL,
],
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'es',
'delta' => '0',
'field_test_value' => 'This is a Spanish comment',
'field_test_format' => NULL,
],
];
$tests[0]['source_data']['field_data_subject_field'] = [
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'en',
'delta' => '0',
'subject_field_value' => 'Comment subject in English',
'subject_field_format' => NULL,
],
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'subject_field_value' => 'Comment subject in French',
'subject_field_format' => NULL,
],
[
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'es',
'delta' => '0',
'subject_field_value' => 'Comment subject in Spanish',
'subject_field_format' => NULL,
],
];
$tests[0]['source_data']['node'] = [
[
'nid' => '1',
'vid' => '1',
'type' => 'test_content_type',
'language' => 'en',
'title' => 'A Node',
'uid' => '1',
'status' => '1',
'created' => '1421727515',
'changed' => '1421727515',
'comment' => '2',
'promote' => '1',
'sticky' => '0',
'tnid' => '0',
'translate' => '0',
],
];
// The expected results.
$tests[0]['expected_data'] = [
[
'subject' => 'A comment',
'entity_type' => 'comment',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'source' => 'en',
'uid' => '1',
'status' => '0',
'translate' => '0',
'created' => '1531343508',
'changed' => '1531343508',
'field_test' => [
[
'value' => 'This is a French comment',
'format' => NULL,
],
],
'subject_field' => [
[
'value' => 'Comment subject in French',
'format' => NULL,
],
],
],
[
'subject' => 'A comment',
'entity_type' => 'comment',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'es',
'source' => 'en',
'uid' => '2',
'status' => '1',
'translate' => '1',
'created' => '1531343528',
'changed' => '1531343528',
'field_test' => [
[
'value' => 'This is a Spanish comment',
'format' => NULL,
],
],
'subject_field' => [
[
'value' => 'Comment subject in Spanish',
'format' => NULL,
],
],
],
];
return $tests;
}
}
id: d7_comment_entity_translation
label: Comment entity translations
migration_tags:
- Drupal 7
- translation
- Content
class: Drupal\comment\Plugin\migrate\D7Comment
source:
plugin: d7_comment_entity_translation
process:
cid: entity_id
subject: subject
langcode: language
uid: uid
status: status
created: created
changed: changed
content_translation_source: source
content_translation_outdated: translate
destination:
plugin: entity:comment
translations: true
destination_module: content_translation
migration_dependencies:
required:
- language
- d7_entity_translation_settings
- d7_comment
</
......@@ -2548,7 +2548,7 @@
'name' => 'admin',
'mail' => '',
'homepage' => '',
'language' => 'und',
'language' => 'en',
))
->values(array(
'cid' => '2',
......@@ -3131,7 +3131,7 @@
'entity_type' => 'comment',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'und',
'language' => 'en',
'source' => '',
'uid' => '1',
'status' => '1',
......@@ -3139,6 +3139,30 @@
'created' => '1421727536',
'changed' => '1421727536',
))
->values(array(
'entity_type' => 'comment',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'source' => 'en',
'uid' => '1',
'status' => '0',
'translate' => '0',
'created' => '1531837764',
'changed' => '1531837764',
))
->values(array(
'entity_type' => 'comment',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'source' => 'en',
'uid' => '2',
'status' => '1',
'translate' => '1',
'created' => '1531838064',
'changed' => '1531838064',
))
->values(array(
'entity_type' => 'node',
'entity_id' => '1',
......@@ -4725,6 +4749,15 @@
'data' => 'a:6:{s:8:"required";b:0;s:5:"label";s:11:"Description";s:11:"description";s:0:"";s:8:"settings";a:5:{s:15:"text_processing";i:1;s:10:"hide_label";a:2:{s:4:"page";b:0;s:6:"entity";b:0;}s:15:"display_summary";i:0;s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:6:"widget";a:4:{s:6:"weight";i:-5;s:4:"type";s:26:"text_textarea_with_summary";s:8:"settings";a:2:{s:4:"rows";i:20;s:12:"summary_rows";i:5;}s:6:"module";s:4:"text";}s:7:"display";a:1:{s:7:"default";a:4:{s:4:"type";s:6:"hidden";s:5:"label";s:5:"above";s:8:"settings";a:0:{}s:6:"weight";i:15;}}}',
'deleted' => '0',
))
->values(array(
'id' => '71',
'field_id' => '41',
'field_name' => 'subject_field',
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'data' => 'a:6:{s:5:"label";s:7:"Subject";s:11:"description";s:0:"";s:8:"required";b:1;s:8:"settings";a:4:{s:15:"text_processing";i:0;s:10:"hide_label";a:2:{s:4:"page";b:0;s:6:"entity";b:0;}s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:6:"widget";a:4:{s:6:"weight";i:-5;s:4:"type";s:14:"text_textfield";s:8:"settings";a:1:{s:4:"size";i:60;}s:6:"module";s:4:"text";}s:7:"display";a:1:{s:7:"default";a:4:{s:4:"type";s:6:"hidden";s:5:"label";s:5:"above";s:8:"settings";a:0:{}s:6:"weight";i:1;}}}',
'deleted' => '0',
))
->execute();
$connection->schema()->createTable('field_data_body', array(
......@@ -6209,10 +6242,30 @@
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'und',
'language' => 'en',
'delta' => '0',
'field_integer_value' => '1000000',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'field_integer_value' => '2000000',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'delta' => '0',
'field_integer_value' => '3000000',
))
->values(array(
'entity_type' => 'node',
'bundle' => 'test_content_type',
......@@ -8855,6 +8908,39 @@
'subject_field_value',
'subject_field_format',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'en',
'delta' => '0',
'subject_field_value' => 'Subject field in English',
'subject_field_format' => NULL,
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'subject_field_value' => 'Subject field in French',
'subject_field_format' => NULL,
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'delta' => '0',
'subject_field_value' => 'Subject field in Icelandic',
'subject_field_format' => NULL,
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_article',
......@@ -10629,10 +10715,30 @@
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'und',
'language' => 'en',
'delta' => '0',
'field_integer_value' => '1000000',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'fr',
'delta' => '0',
'field_integer_value' => '2000000',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',
'language' => 'is',
'delta' => '0',
'field_integer_value' => '3000000',
))
->values(array(
'entity_type' => 'node',
'bundle' => 'test_content_type',
......@@ -13289,6 +13395,39 @@
'subject_field_value',
'subject_field_format',
))
->values(array(
'entity_type' => 'comment',
'bundle' => 'comment_node_test_content_type',
'deleted' => '0',
'entity_id' => '1',
'revision_id' => '1',