3 namespace Drupal\Tests\node\Kernel\Migrate\d7;
5 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\NodeInterface;
10 * Tests node migration.
14 class MigrateNodeTest extends MigrateDrupal7TestBase {
19 public static $modules = [
20 'content_translation',
37 protected function setUp() {
40 $this->installEntitySchema('node');
41 $this->installEntitySchema('comment');
42 $this->installEntitySchema('taxonomy_term');
43 $this->installEntitySchema('file');
44 $this->installConfig(static::$modules);
45 $this->installSchema('node', ['node_access']);
46 $this->installSchema('system', ['sequences']);
48 $this->executeMigrations([
53 'd7_language_content_settings',
55 'd7_taxonomy_vocabulary',
59 'd7_node_translation',
64 * Asserts various aspects of a node.
70 * @param string $langcode
71 * The expected language code.
72 * @param string $title
75 * The expected author ID.
77 * The expected status of the node.
79 * The expected creation time.
81 * The expected modification time.
82 * @param bool $promoted
83 * Whether the node is expected to be promoted to the front page.
85 * Whether the node is expected to be sticky.
87 protected function assertEntity($id, $type, $langcode, $title, $uid, $status, $created, $changed, $promoted, $sticky) {
88 /** @var \Drupal\node\NodeInterface $node */
89 $node = Node::load($id);
90 $this->assertTrue($node instanceof NodeInterface);
91 $this->assertIdentical($type, $node->getType());
92 $this->assertIdentical($langcode, $node->langcode->value);
93 $this->assertIdentical($title, $node->getTitle());
94 $this->assertIdentical($uid, $node->getOwnerId());
95 $this->assertIdentical($status, $node->isPublished());
96 $this->assertIdentical($created, $node->getCreatedTime());
97 if (isset($changed)) {
98 $this->assertIdentical($changed, $node->getChangedTime());
100 $this->assertIdentical($promoted, $node->isPromoted());
101 $this->assertIdentical($sticky, $node->isSticky());
105 * Asserts various aspects of a node revision.
109 * @param string $title
110 * The expected title.
112 * The revision author ID.
114 * The revision log message.
115 * @param int $timestamp
116 * The revision's time stamp.
118 protected function assertRevision($id, $title, $uid, $log, $timestamp) {
119 $revision = \Drupal::entityManager()->getStorage('node')->loadRevision($id);
120 $this->assertTrue($revision instanceof NodeInterface);
121 $this->assertIdentical($title, $revision->getTitle());
122 $this->assertIdentical($uid, $revision->getRevisionUser()->id());
123 $this->assertIdentical($log, $revision->revision_log->value);
124 $this->assertIdentical($timestamp, $revision->getRevisionCreationTime());
128 * Test node migration from Drupal 7 to 8.
130 public function testNode() {
131 $this->assertEntity(1, 'test_content_type', 'en', 'A Node', '2', TRUE, '1421727515', '1441032132', TRUE, FALSE);
132 $this->assertRevision(1, 'A Node', '1', NULL, '1441032132');
134 $node = Node::load(1);
135 $this->assertTrue($node->field_boolean->value);
136 $this->assertIdentical('99-99-99-99', $node->field_phone->value);
137 // Use assertEqual() here instead, since SQLite interprets floats strictly.
138 $this->assertEqual('1', $node->field_float->value);
139 $this->assertIdentical('5', $node->field_integer->value);
140 $this->assertIdentical('Some more text', $node->field_text_list[0]->value);
141 $this->assertIdentical('7', $node->field_integer_list[0]->value);
142 $this->assertIdentical('qwerty', $node->field_text->value);
143 $this->assertIdentical('2', $node->field_file->target_id);
144 $this->assertIdentical('file desc', $node->field_file->description);
145 $this->assertTrue($node->field_file->display);
146 $this->assertIdentical('1', $node->field_images->target_id);
147 $this->assertIdentical('alt text', $node->field_images->alt);
148 $this->assertIdentical('title text', $node->field_images->title);
149 $this->assertIdentical('93', $node->field_images->width);
150 $this->assertIdentical('93', $node->field_images->height);
151 $this->assertIdentical('http://google.com', $node->field_link->uri);
152 $this->assertIdentical('Click Here', $node->field_link->title);
153 // Test that an email field is migrated.
154 $this->assertSame('default@example.com', $node->field_email->value);
155 $this->assertSame('another@example.com', $node->field_email[1]->value);
157 $node = Node::load(2);
158 $this->assertSame('en', $node->langcode->value);
159 $this->assertIdentical("...is that it's the absolute best show ever. Trust me, I would know.", $node->body->value);
160 $this->assertSame('The thing about Deep Space 9', $node->label());
161 $this->assertIdentical('internal:/', $node->field_link->uri);
162 $this->assertIdentical('Home', $node->field_link->title);
163 $this->assertTrue($node->hasTranslation('is'), "Node 2 has an Icelandic translation");
165 $translation = $node->getTranslation('is');
166 $this->assertSame('is', $translation->langcode->value);
167 $this->assertSame("is - ...is that it's the absolute best show ever. Trust me, I would know.", $translation->body->value);
168 $this->assertSame('is - The thing about Deep Space 9', $translation->label());
169 $this->assertSame('internal:/', $translation->field_link->uri);
170 $this->assertSame('Home', $translation->field_link->title);
172 // Test that content_translation_source is set.
173 $manager = $this->container->get('content_translation.manager');
174 $this->assertSame('en', $manager->getTranslationMetadata($node->getTranslation('is'))->getSource());
176 // Node 3 is a translation of node 2, and should not be imported separately.
177 $this->assertNull(Node::load(3), "Node 3 doesn't exist in D8, it was a translation");
179 // Test that content_translation_source for a source other than English.
180 $node = Node::load(4);
181 $this->assertSame('is', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());