4bbd6ef49c393f5ee724e28a007dfb6c053a62b7
[yaffs-website] / web / core / modules / node / tests / src / Kernel / Migrate / d7 / MigrateNodeTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Kernel\Migrate\d7;
4
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;
10
11 /**
12  * Tests node migration.
13  *
14  * @group node
15  */
16 class MigrateNodeTest extends MigrateDrupal7TestBase {
17
18   use FileMigrationSetupTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = [
24     'content_translation',
25     'comment',
26     'datetime',
27     'file',
28     'filter',
29     'forum',
30     'image',
31     'language',
32     'link',
33     'menu_ui',
34     // Required for translation migrations.
35     'migrate_drupal_multilingual',
36     'node',
37     'taxonomy',
38     'telephone',
39     'text',
40   ];
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47
48     $this->fileMigrationSetup();
49
50     $this->installEntitySchema('node');
51     $this->installEntitySchema('comment');
52     $this->installEntitySchema('taxonomy_term');
53     $this->installConfig(static::$modules);
54     $this->installSchema('comment', ['comment_entity_statistics']);
55     $this->installSchema('forum', ['forum', 'forum_index']);
56     $this->installSchema('node', ['node_access']);
57     $this->installSchema('system', ['sequences']);
58
59     $this->executeMigrations([
60       'language',
61       'd7_user_role',
62       'd7_user',
63       'd7_node_type',
64       'd7_language_content_settings',
65       'd7_comment_type',
66       'd7_comment_field',
67       'd7_comment_field_instance',
68       'd7_taxonomy_vocabulary',
69       'd7_field',
70       'd7_field_instance',
71       'd7_node',
72       'd7_node_translation',
73       'd7_entity_translation_settings',
74       'd7_node_entity_translation',
75     ]);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function getFileMigrationInfo() {
82     return [
83       'path' => 'public://sites/default/files/cube.jpeg',
84       'size' => '3620',
85       'base_path' => 'public://',
86       'plugin_id' => 'd7_file',
87     ];
88   }
89
90   /**
91    * Asserts various aspects of a node.
92    *
93    * @param string $id
94    *   The node ID.
95    * @param string $type
96    *   The node type.
97    * @param string $langcode
98    *   The expected language code.
99    * @param string $title
100    *   The expected title.
101    * @param int $uid
102    *   The expected author ID.
103    * @param bool $status
104    *   The expected status of the node.
105    * @param int $created
106    *   The expected creation time.
107    * @param int $changed
108    *   The expected modification time.
109    * @param bool $promoted
110    *   Whether the node is expected to be promoted to the front page.
111    * @param bool $sticky
112    *   Whether the node is expected to be sticky.
113    */
114   protected function assertEntity($id, $type, $langcode, $title, $uid, $status, $created, $changed, $promoted, $sticky) {
115     /** @var \Drupal\node\NodeInterface $node */
116     $node = Node::load($id);
117     $this->assertInstanceOf(NodeInterface::class, $node);
118     $this->assertEquals($type, $node->getType());
119     $this->assertEquals($langcode, $node->langcode->value);
120     $this->assertEquals($title, $node->getTitle());
121     $this->assertEquals($uid, $node->getOwnerId());
122     $this->assertEquals($status, $node->isPublished());
123     $this->assertEquals($created, $node->getCreatedTime());
124     if (isset($changed)) {
125       $this->assertEquals($changed, $node->getChangedTime());
126     }
127     $this->assertEquals($promoted, $node->isPromoted());
128     $this->assertEquals($sticky, $node->isSticky());
129   }
130
131   /**
132    * Asserts various aspects of a node revision.
133    *
134    * @param int $id
135    *   The revision ID.
136    * @param string $title
137    *   The expected title.
138    * @param int $uid
139    *   The revision author ID.
140    * @param string $log
141    *   The revision log message.
142    * @param int $timestamp
143    *   The revision's time stamp.
144    */
145   protected function assertRevision($id, $title, $uid, $log, $timestamp) {
146     $revision = \Drupal::entityManager()->getStorage('node')->loadRevision($id);
147     $this->assertInstanceOf(NodeInterface::class, $revision);
148     $this->assertEquals($title, $revision->getTitle());
149     $this->assertEquals($uid, $revision->getRevisionUser()->id());
150     $this->assertEquals($log, $revision->revision_log->value);
151     $this->assertEquals($timestamp, $revision->getRevisionCreationTime());
152   }
153
154   /**
155    * Test node migration from Drupal 7 to 8.
156    */
157   public function testNode() {
158     $this->assertEntity(1, 'test_content_type', 'en', 'An English Node', '2', TRUE, '1421727515', '1441032132', TRUE, FALSE);
159     $this->assertRevision(1, 'An English Node', '1', NULL, '1441032132');
160
161     $node = Node::load(1);
162     $this->assertTrue($node->field_boolean->value);
163     $this->assertEquals('99-99-99-99', $node->field_phone->value);
164     $this->assertEquals('1', $node->field_float->value);
165     $this->assertEquals('5', $node->field_integer->value);
166     $this->assertEquals('Some more text', $node->field_text_list[0]->value);
167     $this->assertEquals('7', $node->field_integer_list[0]->value);
168     $this->assertEquals('qwerty', $node->field_text->value);
169     $this->assertEquals('2', $node->field_file->target_id);
170     $this->assertEquals('file desc', $node->field_file->description);
171     $this->assertTrue($node->field_file->display);
172     $this->assertEquals('1', $node->field_images->target_id);
173     $this->assertEquals('alt text', $node->field_images->alt);
174     $this->assertEquals('title text', $node->field_images->title);
175     $this->assertEquals('93', $node->field_images->width);
176     $this->assertEquals('93', $node->field_images->height);
177     $this->assertEquals('http://google.com', $node->field_link->uri);
178     $this->assertEquals('Click Here', $node->field_link->title);
179     // Test that an email field is migrated.
180     $this->assertEquals('default@example.com', $node->field_email->value);
181     $this->assertEquals('another@example.com', $node->field_email[1]->value);
182     $this->assertEquals(CommentItemInterface::OPEN, $node->comment_node_test_content_type->status);
183     $this->assertEquals('3.1416', $node->field_float_list[0]->value);
184
185     $node = Node::load(2);
186     $this->assertEquals('en', $node->langcode->value);
187     $this->assertEquals("...is that it's the absolute best show ever. Trust me, I would know.", $node->body->value);
188     $this->assertEquals('The thing about Deep Space 9', $node->label());
189     $this->assertEquals('internal:/', $node->field_link->uri);
190     $this->assertEquals('Home', $node->field_link->title);
191     $this->assertEquals(CommentItemInterface::OPEN, $node->comment_node_article->status);
192     $this->assertTrue($node->hasTranslation('is'), "Node 2 has an Icelandic translation");
193
194     $translation = $node->getTranslation('is');
195     $this->assertEquals('is', $translation->langcode->value);
196     $this->assertEquals("is - ...is that it's the absolute best show ever. Trust me, I would know.", $translation->body->value);
197     $this->assertEquals('is - The thing about Deep Space 9', $translation->label());
198     $this->assertEquals('internal:/', $translation->field_link->uri);
199     $this->assertEquals(CommentItemInterface::OPEN, $translation->comment_node_article->status);
200     $this->assertEquals('Home', $translation->field_link->title);
201
202     // Test that content_translation_source is set.
203     $manager = $this->container->get('content_translation.manager');
204     $this->assertEquals('en', $manager->getTranslationMetadata($node->getTranslation('is'))->getSource());
205
206     // Node 3 is a translation of node 2, and should not be imported separately.
207     $this->assertNull(Node::load(3), "Node 3 doesn't exist in D8, it was a translation");
208
209     // Test that content_translation_source for a source other than English.
210     $node = Node::load(4);
211     $this->assertEquals('is', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
212     $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_node_article->status);
213
214     $translation = $node->getTranslation('en');
215     $this->assertEquals(CommentItemInterface::CLOSED, $translation->comment_node_article->status);
216
217     $node = Node::load(6);
218     $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_forum->status);
219
220     $node = Node::load(7);
221     $this->assertEquals(CommentItemInterface::OPEN, $node->comment_forum->status);
222   }
223
224   /**
225    * Test node entity translations migration from Drupal 7 to 8.
226    */
227   public function testNodeEntityTranslations() {
228     $manager = $this->container->get('content_translation.manager');
229
230     // Get the node and its translations.
231     $node = Node::load(1);
232     $node_fr = $node->getTranslation('fr');
233     $node_is = $node->getTranslation('is');
234
235     // Test that fields translated with Entity Translation are migrated.
236     $this->assertSame('An English Node', $node->getTitle());
237     $this->assertSame('A French Node', $node_fr->getTitle());
238     $this->assertSame('An Icelandic Node', $node_is->getTitle());
239     $this->assertSame('5', $node->field_integer->value);
240     $this->assertSame('6', $node_fr->field_integer->value);
241     $this->assertSame('7', $node_is->field_integer->value);
242
243     // Test that the French translation metadata is correctly migrated.
244     $metadata_fr = $manager->getTranslationMetadata($node_fr);
245     $this->assertSame('en', $metadata_fr->getSource());
246     $this->assertTrue($metadata_fr->isOutdated());
247     $this->assertSame('2', $node_fr->getOwnerId());
248     $this->assertSame('1529615802', $node_fr->getCreatedTime());
249     $this->assertSame('1529615802', $node_fr->getChangedTime());
250     $this->assertTrue($node_fr->isPublished());
251
252     // Test that the Icelandic translation metadata is correctly migrated.
253     $metadata_is = $manager->getTranslationMetadata($node_is);
254     $this->assertSame('en', $metadata_is->getSource());
255     $this->assertFalse($metadata_is->isOutdated());
256     $this->assertSame('1', $node_is->getOwnerId());
257     $this->assertSame('1529615813', $node_is->getCreatedTime());
258     $this->assertSame('1529615813', $node_is->getChangedTime());
259     $this->assertFalse($node_is->isPublished());
260   }
261
262 }