Version 1
[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\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\NodeInterface;
8
9 /**
10  * Tests node migration.
11  *
12  * @group node
13  */
14 class MigrateNodeTest extends MigrateDrupal7TestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'content_translation',
21     'comment',
22     'datetime',
23     'filter',
24     'image',
25     'language',
26     'link',
27     'menu_ui',
28     'node',
29     'taxonomy',
30     'telephone',
31     'text',
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
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']);
47
48     $this->executeMigrations([
49       'language',
50       'd7_user_role',
51       'd7_user',
52       'd7_node_type',
53       'd7_language_content_settings',
54       'd7_comment_type',
55       'd7_taxonomy_vocabulary',
56       'd7_field',
57       'd7_field_instance',
58       'd7_node',
59       'd7_node_translation',
60     ]);
61   }
62
63   /**
64    * Asserts various aspects of a node.
65    *
66    * @param string $id
67    *   The node ID.
68    * @param string $type
69    *   The node type.
70    * @param string $langcode
71    *   The expected language code.
72    * @param string $title
73    *   The expected title.
74    * @param int $uid
75    *   The expected author ID.
76    * @param bool $status
77    *   The expected status of the node.
78    * @param int $created
79    *   The expected creation time.
80    * @param int $changed
81    *   The expected modification time.
82    * @param bool $promoted
83    *   Whether the node is expected to be promoted to the front page.
84    * @param bool $sticky
85    *   Whether the node is expected to be sticky.
86    */
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());
99     }
100     $this->assertIdentical($promoted, $node->isPromoted());
101     $this->assertIdentical($sticky, $node->isSticky());
102   }
103
104   /**
105    * Asserts various aspects of a node revision.
106    *
107    * @param int $id
108    *   The revision ID.
109    * @param string $title
110    *   The expected title.
111    * @param int $uid
112    *   The revision author ID.
113    * @param string $log
114    *   The revision log message.
115    * @param int $timestamp
116    *   The revision's time stamp.
117    */
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());
125   }
126
127   /**
128    * Test node migration from Drupal 7 to 8.
129    */
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');
133
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);
156
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");
164
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);
171
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());
175
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");
178
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());
182
183   }
184
185 }