Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / Migrate / d7 / MigrateCommentTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel\Migrate\d7;
4
5 use Drupal\comment\Entity\Comment;
6 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 use Drupal\node\NodeInterface;
8
9 /**
10  * Tests the migration of comments from Drupal 7.
11  *
12  * @group comment
13  * @group migrate_drupal_7
14  */
15 class MigrateCommentTest extends MigrateDrupal7TestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'comment',
22     'content_translation',
23     'datetime',
24     'filter',
25     'image',
26     'language',
27     'link',
28     'menu_ui',
29     // Required for translation migrations.
30     'migrate_drupal_multilingual',
31     'node',
32     'taxonomy',
33     'telephone',
34     'text',
35   ];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
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([
50       'language',
51       'd7_node_type',
52       'd7_language_content_settings',
53       'd7_user_role',
54       'd7_user',
55       'd7_node_type',
56       'd7_node',
57       'd7_node_translation',
58       'd7_comment_type',
59       'd7_comment_field',
60       'd7_comment_field_instance',
61       'd7_comment_entity_display',
62       'd7_comment_entity_form_display',
63       'd7_taxonomy_vocabulary',
64       'd7_field',
65       'd7_field_instance',
66       'd7_comment',
67       'd7_entity_translation_settings',
68       'd7_comment_entity_translation',
69     ]);
70   }
71
72   /**
73    * Tests the migrated comments.
74    */
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);
89
90     $node = $comment->getCommentedEntity();
91     $this->assertInstanceOf(NodeInterface::class, $node);
92     $this->assertSame('1', $node->id());
93
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());
101
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());
111
112     $node = $comment->getCommentedEntity();
113     $this->assertInstanceOf(NodeInterface::class, $node);
114     $this->assertSame('2', $node->id());
115   }
116
117   /**
118    * Tests the migration of comment entity translations.
119    */
120   public function testCommentEntityTranslations() {
121     $manager = $this->container->get('content_translation.manager');
122
123     // Get the comment and its translations.
124     $comment = Comment::load(1);
125     $comment_fr = $comment->getTranslation('fr');
126     $comment_is = $comment->getTranslation('is');
127
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);
135
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());
144
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());
153   }
154
155 }