summaryrefslogtreecommitdiffstats
path: root/core/modules/migrate_drupal/tests/src/Kernel/d6/EntityContentBaseTest.php
blob: 5074473cecb3fd8b46789a199ca440af19c66ad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

namespace Drupal\Tests\migrate_drupal\Kernel\d6;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\user\Entity\User;
use Prophecy\Argument;

/**
 * @group migrate_drupal
 */
class EntityContentBaseTest extends MigrateDrupal6TestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['migrate_overwrite_test'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create a field on the user entity so that we can test nested property
    // overwrites.
    // @see static::testOverwriteSelectedNestedProperty()
    FieldStorageConfig::create([
      'field_name' => 'signature',
      'entity_type' => 'user',
      'type' => 'text_long',
    ])->save();

    FieldConfig::create([
      'field_name' => 'signature',
      'entity_type' => 'user',
      'bundle' => 'user',
    ])->save();

    User::create([
      'uid' => 2,
      'name' => 'Ford Prefect',
      'mail' => 'ford.prefect@localhost',
      'signature' => array(
        array(
          'value' => 'Bring a towel.',
          'format' => 'filtered_html',
        ),
      ),
      'init' => 'proto@zo.an',
    ])->save();

    $this->executeMigrations(['d6_filter_format', 'd6_user_role']);
  }

  /**
   * Tests overwriting all mapped properties in the destination entity (default
   * behavior).
   */
  public function testOverwriteAllMappedProperties() {
    $this->executeMigration('d6_user');
    /** @var \Drupal\user\UserInterface $account */
    $account = User::load(2);
    $this->assertIdentical('john.doe', $account->label());
    $this->assertIdentical('john.doe@example.com', $account->getEmail());
    $this->assertIdentical('doe@example.com', $account->getInitialEmail());
  }

  /**
   * Tests overwriting selected properties in the destination entity, specified
   * in the destination configuration.
   */
  public function testOverwriteProperties() {
    // Execute the migration in migrate_overwrite_test, which documents how
    // property overwrites work.
    $this->executeMigration('users');

    /** @var \Drupal\user\UserInterface $account */
    $account = User::load(2);
    $this->assertIdentical('john.doe', $account->label());
    $this->assertIdentical('john.doe@example.com', $account->getEmail());
    $this->assertIdentical('The answer is 42.', $account->signature->value);
    // This value is not overwritten because it's not listed in
    // overwrite_properties.
    $this->assertIdentical('proto@zo.an', $account->getInitialEmail());
  }

  /**
   * Test that translation destination fails for untranslatable entities.
   */
  public function testUntranslatable() {
    $this->enableModules(['language_test']);
    $this->installEntitySchema('no_language_entity_test');

    /** @var MigrationInterface $migration */
    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration([
      'source' => [
        'plugin' => 'embedded_data',
        'ids' => ['id' => ['type' => 'integer']],
        'data_rows' => [['id' => 1]],
      ],
      'process' => [
        'id' => 'id',
      ],
      'destination' => [
        'plugin' => 'entity:no_language_entity_test',
        'translations' => TRUE,
      ],
    ]);

    $message = $this->prophesize(MigrateMessageInterface::class);
    // Match the expected message. Can't use default argument types, because
    // we need to convert to string from TranslatableMarkup.
    $argument = Argument::that(function($msg) {
      return strpos((string) $msg, "This entity type does not support translation") !== FALSE;
    });
    $message->display($argument, Argument::any())
      ->shouldBeCalled();

    $executable = new MigrateExecutable($migration, $message->reveal());
    $executable->import();
  }

}