3 namespace Drupal\Tests\comment\Kernel\Migrate\d7;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 use Drupal\node\NodeInterface;
10 * Tests the migration of comments from Drupal 7.
13 * @group migrate_drupal_7
15 class MigrateCommentTest extends MigrateDrupal7TestBase {
20 public static $modules = [
22 'content_translation',
29 // Required for translation migrations.
30 'migrate_drupal_multilingual',
40 protected function setUp() {
43 $this->installEntitySchema('node');
44 $this->installEntitySchema('comment');
45 $this->installEntitySchema('taxonomy_term');
46 $this->installConfig(['comment', 'node']);
47 $this->installSchema('comment', ['comment_entity_statistics']);
48 $this->installSchema('node', ['node_access']);
49 $this->executeMigrations([
52 'd7_language_content_settings',
57 'd7_node_translation',
60 'd7_comment_field_instance',
61 'd7_comment_entity_display',
62 'd7_comment_entity_form_display',
63 'd7_taxonomy_vocabulary',
67 'd7_entity_translation_settings',
68 'd7_comment_entity_translation',
73 * Tests the migrated comments.
75 public function testMigration() {
76 $comment = Comment::load(1);
77 $this->assertInstanceOf(Comment::class, $comment);
78 $this->assertSame('Subject field in English', $comment->getSubject());
79 $this->assertSame('1421727536', $comment->getCreatedTime());
80 $this->assertSame('1421727536', $comment->getChangedTime());
81 $this->assertTrue($comment->getStatus());
82 $this->assertSame('admin', $comment->getAuthorName());
83 $this->assertSame('admin@local.host', $comment->getAuthorEmail());
84 $this->assertSame('This is a comment', $comment->comment_body->value);
85 $this->assertSame('filtered_html', $comment->comment_body->format);
86 $this->assertSame('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', $comment->getHostname());
87 $this->assertSame('en', $comment->language()->getId());
88 $this->assertSame('1000000', $comment->field_integer->value);
90 $node = $comment->getCommentedEntity();
91 $this->assertInstanceOf(NodeInterface::class, $node);
92 $this->assertSame('1', $node->id());
94 // Tests that comments that used the Drupal 7 Title module and that have
95 // their subject replaced by a real field are correctly migrated.
96 $comment = Comment::load(2);
97 $this->assertInstanceOf(Comment::class, $comment);
98 $this->assertSame('TNG for the win!', $comment->getSubject());
99 $this->assertSame('TNG is better than DS9.', $comment->comment_body->value);
100 $this->assertSame('en', $comment->language()->getId());
102 // Tests that the commented entity is correctly migrated when the comment
103 // was posted to a node translation.
104 $comment = Comment::load(3);
105 $this->assertInstanceOf(Comment::class, $comment);
106 $this->assertSame('Comment to IS translation', $comment->getSubject());
107 $this->assertSame('This is a comment to an Icelandic translation.', $comment->comment_body->value);
108 $this->assertSame('2', $comment->getCommentedEntityId());
109 $this->assertSame('node', $comment->getCommentedEntityTypeId());
110 $this->assertSame('is', $comment->language()->getId());
112 $node = $comment->getCommentedEntity();
113 $this->assertInstanceOf(NodeInterface::class, $node);
114 $this->assertSame('2', $node->id());
118 * Tests the migration of comment entity translations.
120 public function testCommentEntityTranslations() {
121 $manager = $this->container->get('content_translation.manager');
123 // Get the comment and its translations.
124 $comment = Comment::load(1);
125 $comment_fr = $comment->getTranslation('fr');
126 $comment_is = $comment->getTranslation('is');
128 // Test that fields translated with Entity Translation are migrated.
129 $this->assertSame('Subject field in English', $comment->getSubject());
130 $this->assertSame('Subject field in French', $comment_fr->getSubject());
131 $this->assertSame('Subject field in Icelandic', $comment_is->getSubject());
132 $this->assertSame('1000000', $comment->field_integer->value);
133 $this->assertSame('2000000', $comment_fr->field_integer->value);
134 $this->assertSame('3000000', $comment_is->field_integer->value);
136 // Test that the French translation metadata is correctly migrated.
137 $metadata_fr = $manager->getTranslationMetadata($comment_fr);
138 $this->assertFalse($metadata_fr->isPublished());
139 $this->assertSame('en', $metadata_fr->getSource());
140 $this->assertSame('1', $metadata_fr->getAuthor()->uid->value);
141 $this->assertSame('1531837764', $metadata_fr->getCreatedTime());
142 $this->assertSame('1531837764', $metadata_fr->getChangedTime());
143 $this->assertFalse($metadata_fr->isOutdated());
145 // Test that the Icelandic translation metadata is correctly migrated.
146 $metadata_is = $manager->getTranslationMetadata($comment_is);
147 $this->assertTrue($metadata_is->isPublished());
148 $this->assertSame('en', $metadata_is->getSource());
149 $this->assertSame('2', $metadata_is->getAuthor()->uid->value);
150 $this->assertSame('1531838064', $metadata_is->getCreatedTime());
151 $this->assertSame('1531838064', $metadata_is->getChangedTime());
152 $this->assertTrue($metadata_is->isOutdated());