d15446fbad90974c1184aa7a72f814c29764716d
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / Plugin / EntityRevisionTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel\Plugin;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\node\Entity\Node;
7 use Drupal\Tests\migrate\Kernel\MigrateTestBase;
8 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
9
10 /**
11  * Tests the EntityRevision destination plugin.
12  *
13  * @group migrate
14  */
15 class EntityRevisionTest extends MigrateTestBase {
16
17   use ContentTypeCreationTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'content_translation',
24     'field',
25     'filter',
26     'language',
27     'node',
28     'system',
29     'text',
30     'user',
31   ];
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->installConfig('node');
39     $this->installSchema('node', ['node_access']);
40     $this->installEntitySchema('node');
41     $this->installEntitySchema('user');
42   }
43
44   /**
45    * Tests that EntityRevision correctly handles revision translations.
46    */
47   public function testRevisionTranslation() {
48     ConfigurableLanguage::createFromLangcode('fr')->save();
49
50     /** @var \Drupal\node\NodeInterface $node */
51     $node = Node::create([
52       'type' => $this->createContentType()->id(),
53       'title' => 'Default 1',
54     ]);
55     $node->addTranslation('fr', [
56       'title' => 'French 1',
57     ]);
58     $node->save();
59     $node->setNewRevision();
60     $node->setTitle('Default 2');
61     $node->getTranslation('fr')->setTitle('French 2');
62     $node->save();
63
64     $migration = [
65       'source' => [
66         'plugin' => 'embedded_data',
67         'data_rows' => [
68           [
69             'nid' => $node->id(),
70             'vid' => $node->getRevisionId(),
71             'langcode' => 'fr',
72             'title' => 'Titre nouveau, tabarnak!',
73           ],
74         ],
75         'ids' => [
76           'nid' => [
77             'type' => 'integer',
78           ],
79           'vid' => [
80             'type' => 'integer',
81           ],
82           'langcode' => [
83             'type' => 'string',
84           ],
85         ],
86       ],
87       'process' => [
88         'nid' => 'nid',
89         'vid' => 'vid',
90         'langcode' => 'langcode',
91         'title' => 'title',
92       ],
93       'destination' => [
94         'plugin' => 'entity_revision:node',
95         'translations' => TRUE,
96       ],
97     ];
98
99     /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
100     $migration = $this->container
101       ->get('plugin.manager.migration')
102       ->createStubMigration($migration);
103
104     $this->executeMigration($migration);
105
106     // The entity_revision destination uses the revision ID and langcode as its
107     // keys (the langcode is only used if the destination is configured for
108     // translation), so we should be able to look up the source IDs by revision
109     // ID and langcode.
110     $source_ids = $migration->getIdMap()->lookupSourceID([
111       'vid' => $node->getRevisionId(),
112       'langcode' => 'fr',
113     ]);
114     $this->assertNotEmpty($source_ids);
115     $this->assertSame($node->id(), $source_ids['nid']);
116     $this->assertSame($node->getRevisionId(), $source_ids['vid']);
117     $this->assertSame('fr', $source_ids['langcode']);
118
119     // Confirm the french revision was used in the migration, instead of the
120     // default revision.
121     /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
122     $entity_type_manager = \Drupal::entityTypeManager();
123     $revision = $entity_type_manager->getStorage('node')->loadRevision(1);
124     $this->assertSame('Default 1', $revision->label());
125     $this->assertSame('French 1', $revision->getTranslation('fr')->label());
126     $revision = $entity_type_manager->getStorage('node')->loadRevision(2);
127     $this->assertSame('Default 2', $revision->label());
128     $this->assertSame('Titre nouveau, tabarnak!', $revision->getTranslation('fr')->label());
129   }
130
131 }