Security update for Core, with self-updated composer
[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     'node',
35     'taxonomy',
36     'telephone',
37     'text',
38   ];
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp() {
44     parent::setUp();
45
46     $this->fileMigrationSetup();
47
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']);
56
57     $this->executeMigrations([
58       'language',
59       'd7_user_role',
60       'd7_user',
61       'd7_node_type',
62       'd7_language_content_settings',
63       'd7_comment_type',
64       'd7_comment_field',
65       'd7_comment_field_instance',
66       'd7_taxonomy_vocabulary',
67       'd7_field',
68       'd7_field_instance',
69       'd7_node',
70       'd7_node_translation',
71     ]);
72   }
73
74   /**
75    * Asserts various aspects of a node.
76    *
77    * @param string $id
78    *   The node ID.
79    * @param string $type
80    *   The node type.
81    * @param string $langcode
82    *   The expected language code.
83    * @param string $title
84    *   The expected title.
85    * @param int $uid
86    *   The expected author ID.
87    * @param bool $status
88    *   The expected status of the node.
89    * @param int $created
90    *   The expected creation time.
91    * @param int $changed
92    *   The expected modification time.
93    * @param bool $promoted
94    *   Whether the node is expected to be promoted to the front page.
95    * @param bool $sticky
96    *   Whether the node is expected to be sticky.
97    */
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());
110     }
111     $this->assertEquals($promoted, $node->isPromoted());
112     $this->assertEquals($sticky, $node->isSticky());
113   }
114
115   /**
116    * Asserts various aspects of a node revision.
117    *
118    * @param int $id
119    *   The revision ID.
120    * @param string $title
121    *   The expected title.
122    * @param int $uid
123    *   The revision author ID.
124    * @param string $log
125    *   The revision log message.
126    * @param int $timestamp
127    *   The revision's time stamp.
128    */
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());
136   }
137
138   /**
139    * Test node migration from Drupal 7 to 8.
140    */
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');
144
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);
167
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");
176
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);
184
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());
188
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");
191
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);
196
197     $translation = $node->getTranslation('en');
198     $this->assertEquals(CommentItemInterface::CLOSED, $translation->comment_node_article->status);
199
200     $node = Node::load(6);
201     $this->assertEquals(CommentItemInterface::CLOSED, $node->comment_forum->status);
202
203     $node = Node::load(7);
204     $this->assertEquals(CommentItemInterface::OPEN, $node->comment_forum->status);
205   }
206
207 }