3 namespace Drupal\Tests\node\Kernel\Migrate\d7;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\Tests\file\Kernel\Migrate\d7\FileMigrationSetupTrait;
7 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\NodeInterface;
12 * Tests node migration.
16 class MigrateNodeTest extends MigrateDrupal7TestBase {
18 use FileMigrationSetupTrait;
23 public static $modules = [
24 'content_translation',
43 protected function setUp() {
46 $this->fileMigrationSetup();
48 $this->installEntitySchema('node');
49 $this->installEntitySchema('comment');
50 $this->installEntitySchema('taxonomy_term');
51 $this->installConfig(static::$modules);
52 $this->installSchema('comment', ['comment_entity_statistics']);
53 $this->installSchema('forum', ['forum', 'forum_index']);
54 $this->installSchema('node', ['node_access']);
55 $this->installSchema('system', ['sequences']);
57 $this->executeMigrations([
62 'd7_language_content_settings',
65 'd7_comment_field_instance',
66 'd7_taxonomy_vocabulary',
70 'd7_node_translation',
75 * Asserts various aspects of a node.
81 * @param string $langcode
82 * The expected language code.
83 * @param string $title
86 * The expected author ID.
88 * The expected status of the node.
90 * The expected creation time.
92 * The expected modification time.
93 * @param bool $promoted
94 * Whether the node is expected to be promoted to the front page.
96 * Whether the node is expected to be sticky.
98 protected function assertEntity($id, $type, $langcode, $title, $uid, $status, $created, $changed, $promoted, $sticky) {
99 /** @var \Drupal\node\NodeInterface $node */
100 $node = Node::load($id);
101 $this->assertInstanceOf(NodeInterface::class, $node);
102 $this->assertEquals($type, $node->getType());
103 $this->assertEquals($langcode, $node->langcode->value);
104 $this->assertEquals($title, $node->getTitle());
105 $this->assertEquals($uid, $node->getOwnerId());
106 $this->assertEquals($status, $node->isPublished());
107 $this->assertEquals($created, $node->getCreatedTime());
108 if (isset($changed)) {
109 $this->assertEquals($changed, $node->getChangedTime());
111 $this->assertEquals($promoted, $node->isPromoted());
112 $this->assertEquals($sticky, $node->isSticky());
116 * Asserts various aspects of a node revision.
120 * @param string $title
121 * The expected title.
123 * The revision author ID.
125 * The revision log message.
126 * @param int $timestamp
127 * The revision's time stamp.
129 protected function assertRevision($id, $title, $uid, $log, $timestamp) {
130 $revision = \Drupal::entityManager()->getStorage('node')->loadRevision($id);
131 $this->assertInstanceOf(NodeInterface::class, $revision);
132 $this->assertEquals($title, $revision->getTitle());
133 $this->assertEquals($uid, $revision->getRevisionUser()->id());
134 $this->assertEquals($log, $revision->revision_log->value);
135 $this->assertEquals($timestamp, $revision->getRevisionCreationTime());
139 * Test node migration from Drupal 7 to 8.
141 public function testNode() {
142 $this->assertEntity(1, 'test_content_type', 'en', 'A Node', '2', TRUE, '1421727515', '1441032132', TRUE, FALSE);
143 $this->assertRevision(1, 'A Node', '1', NULL, '1441032132');
145 $node = Node::load(1);
146 $this->assertTrue($node->field_boolean->value);
147 $this->assertEquals('99-99-99-99', $node->field_phone->value);
148 $this->assertEquals('1', $node->field_float->value);
149 $this->assertEquals('5', $node->field_integer->value);
150 $this->assertEquals('Some more text', $node->field_text_list[0]->value);
151 $this->assertEquals('7', $node->field_integer_list[0]->value);
152 $this->assertEquals('qwerty', $node->field_text->value);
153 $this->assertEquals('2', $node->field_file->target_id);
154 $this->assertEquals('file desc', $node->field_file->description);
155 $this->assertTrue($node->field_file->display);
156 $this->assertEquals('1', $node->field_images->target_id);
157 $this->assertEquals('alt text', $node->field_images->alt);
158 $this->assertEquals('title text', $node->field_images->title);
159 $this->assertEquals('93', $node->field_images->width);
160 $this->assertEquals('93', $node->field_images->height);
161 $this->assertEquals('http://google.com', $node->field_link->uri);
162 $this->assertEquals('Click Here', $node->field_link->title);
163 // Test that an email field is migrated.
164 $this->assertEquals('default@example.com', $node->field_email->value);
165 $this->assertEquals('another@example.com', $node->field_email[1]->value);
166 $this->assertEquals(CommentItemInterface::OPEN, $node->comment_node_test_content_type->status);
168 $node = Node::load(2);
169 $this->assertEquals('en', $node->langcode->value);
170 $this->assertEquals("...is that it's the absolute best show ever. Trust me, I would know.", $node->body->value);
171 $this->assertEquals('The thing about Deep Space 9', $node->label());
172 $this->assertEquals('internal:/', $node->field_link->uri);
173 $this->assertEquals('Home', $node->field_link->title);
174 $this->assertEquals(CommentItemInterface::OPEN, $node->comment_node_article->status);
175 $this->assertTrue($node->hasTranslation('is'), "Node 2 has an Icelandic translation");
177 $translation = $node->getTranslation('is');
178 $this->assertEquals('is', $translation->langcode->value);
179 $this->assertEquals("is - ...is that it's the absolute best show ever. Trust me, I would know.", $translation->body->value);
180 $this->assertEquals('is - The thing about Deep Space 9', $translation->label());
181 $this->assertEquals('internal:/', $translation->field_link->uri);
182 $this->assertEquals(CommentItemInterface::OPEN, $translation->comment_node_article->status);
183 $this->assertEquals('Home', $translation->field_link->title);
185 // Test that content_translation_source is set.
186 $manager = $this->container->get('content_translation.manager');
187 $this->assertEquals('en', $manager->getTranslationMetadata($node->getTranslation('is'))->getSource());
189 // Node 3 is a translation of node 2, and should not be imported separately.
190 $this->assertNull(Node::load(3), "Node 3 doesn't exist in D8, it was a translation");
192 // Test that content_translation_source for a source other than English.
193 $node = Node::load(4);
194 $this->assertEquals('is', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
195 $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_node_article->status);
197 $translation = $node->getTranslation('en');
198 $this->assertEquals(CommentItemInterface::CLOSED, $translation->comment_node_article->status);
200 $node = Node::load(6);
201 $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_forum->status);
203 $node = Node::load(7);
204 $this->assertEquals(CommentItemInterface::OPEN, $node->comment_forum->status);