Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Kernel / d6 / EntityContentBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_drupal\Kernel\d6;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\migrate\MigrateExecutable;
8 use Drupal\migrate\MigrateMessageInterface;
9 use Drupal\user\Entity\User;
10 use Prophecy\Argument;
11
12 /**
13  * @group migrate_drupal
14  */
15 class EntityContentBaseTest extends MigrateDrupal6TestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['migrate_overwrite_test'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     // Create a field on the user entity so that we can test nested property
29     // overwrites.
30     // @see static::testOverwriteSelectedNestedProperty()
31     FieldStorageConfig::create([
32       'field_name' => 'signature',
33       'entity_type' => 'user',
34       'type' => 'text_long',
35     ])->save();
36
37     FieldConfig::create([
38       'field_name' => 'signature',
39       'entity_type' => 'user',
40       'bundle' => 'user',
41     ])->save();
42
43     User::create([
44       'uid' => 2,
45       'name' => 'Ford Prefect',
46       'mail' => 'ford.prefect@localhost',
47       'signature' => [
48         [
49           'value' => 'Bring a towel.',
50           'format' => 'filtered_html',
51         ],
52       ],
53       'init' => 'proto@zo.an',
54     ])->save();
55
56     $this->executeMigrations(['d6_filter_format', 'd6_user_role']);
57   }
58
59   /**
60    * Tests overwriting all mapped properties in the destination entity (default
61    * behavior).
62    */
63   public function testOverwriteAllMappedProperties() {
64     $this->executeMigration('d6_user');
65     /** @var \Drupal\user\UserInterface $account */
66     $account = User::load(2);
67     $this->assertIdentical('john.doe', $account->label());
68     $this->assertIdentical('john.doe@example.com', $account->getEmail());
69     $this->assertIdentical('doe@example.com', $account->getInitialEmail());
70   }
71
72   /**
73    * Tests overwriting selected properties in the destination entity, specified
74    * in the destination configuration.
75    */
76   public function testOverwriteProperties() {
77     // Execute the migration in migrate_overwrite_test, which documents how
78     // property overwrites work.
79     $this->executeMigration('users');
80
81     /** @var \Drupal\user\UserInterface $account */
82     $account = User::load(2);
83     $this->assertIdentical('john.doe', $account->label());
84     $this->assertIdentical('john.doe@example.com', $account->getEmail());
85     $this->assertIdentical('The answer is 42.', $account->signature->value);
86     // This value is not overwritten because it's not listed in
87     // overwrite_properties.
88     $this->assertIdentical('proto@zo.an', $account->getInitialEmail());
89   }
90
91   /**
92    * Test that translation destination fails for untranslatable entities.
93    */
94   public function testUntranslatable() {
95     $this->enableModules(['language_test']);
96     $this->installEntitySchema('no_language_entity_test');
97
98     /** @var MigrationInterface $migration */
99     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration([
100       'source' => [
101         'plugin' => 'embedded_data',
102         'ids' => ['id' => ['type' => 'integer']],
103         'data_rows' => [['id' => 1]],
104       ],
105       'process' => [
106         'id' => 'id',
107       ],
108       'destination' => [
109         'plugin' => 'entity:no_language_entity_test',
110         'translations' => TRUE,
111       ],
112     ]);
113
114     $message = $this->prophesize(MigrateMessageInterface::class);
115     // Match the expected message. Can't use default argument types, because
116     // we need to convert to string from TranslatableMarkup.
117     $argument = Argument::that(function ($msg) {
118       return strpos((string) $msg, "This entity type does not support translation") !== FALSE;
119     });
120     $message->display($argument, Argument::any())
121       ->shouldBeCalled();
122
123     $executable = new MigrateExecutable($migration, $message->reveal());
124     $executable->import();
125   }
126
127 }