You are here

public function MigrateEntityContentValidationTest::test1 in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php \Drupal\Tests\migrate\Kernel\MigrateEntityContentValidationTest::test1()

Tests an import with invalid data and checks error messages.

File

core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php, line 48

Class

MigrateEntityContentValidationTest
Tests validation of an entity during migration.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function test1() {

  // Make sure that a user with uid 2 exists.
  $this->container
    ->get('entity_type.manager')
    ->getStorage('user')
    ->create([
    'uid' => 2,
    'name' => $this
      ->randomMachineName(),
    'status' => 1,
  ])
    ->save();
  $this
    ->runImport([
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'id' => '1',
          'name' => $this
            ->randomString(256),
          'user_id' => '1',
        ],
        [
          'id' => '2',
          'name' => $this
            ->randomString(32),
          'user_id' => '1',
        ],
        [
          'id' => '3',
          'name' => $this
            ->randomString(32),
          'user_id' => '2',
        ],
      ],
      'ids' => [
        'id' => [
          'type' => 'integer',
        ],
      ],
    ],
    'process' => [
      'id' => 'id',
      'name' => 'name',
      'user_id' => 'user_id',
    ],
    'destination' => [
      'plugin' => 'entity:entity_test',
      'validate' => TRUE,
    ],
  ]);
  $this
    ->assertSame('1: [entity_test: 1]: name.0.value=<em class="placeholder">Name</em>: may not be longer than 32 characters.||user_id.0.target_id=The referenced entity (<em class="placeholder">user</em>: <em class="placeholder">1</em>) does not exist.', $this->messages[0], 'First message should have 2 validation errors.');
  $this
    ->assertSame('2: [entity_test: 2]: user_id.0.target_id=The referenced entity (<em class="placeholder">user</em>: <em class="placeholder">1</em>) does not exist.', $this->messages[1], 'Second message should have 1 validation error.');
  $this
    ->assertArrayNotHasKey(2, $this->messages, 'Third message should not exist.');
}