You are here

public function MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated()
  2. 10 core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated()

Tests making an entity field value unique only for migrated entities.

File

core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php, line 179

Class

MakeUniqueEntityFieldTest
@coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField @group migrate

Namespace

Drupal\Tests\migrate\Unit\process

Code

public function testMakeUniqueEntityFieldMigrated() {
  $configuration = [
    'entity_type' => 'test_entity_type',
    'field' => 'test_field',
    'migrated' => TRUE,
  ];
  $plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this
    ->getMigration(), $this->entityTypeManager);

  // Setup the entityQuery used in MakeUniqueEntityFieldEntity::exists. The
  // map, $map, is an array consisting of the four input parameters to the
  // query condition method and then the query to return. Both 'forum' and
  // 'test_vocab' are existing entities. There is no 'test_vocab1'.
  $map = [];
  foreach ([
    'forums',
    'test_vocab',
    'test_vocab1',
  ] as $id) {
    $query = $this
      ->prophesize(QueryInterface::class);
    $query
      ->willBeConstructedWith([]);
    $query
      ->accessCheck()
      ->willReturn($query);
    $query
      ->execute()
      ->willReturn($id === 'test_vocab1' ? [] : [
      $id,
    ]);
    $map[] = [
      'test_field',
      $id,
      NULL,
      NULL,
      $query
        ->reveal(),
    ];
  }
  $this->entityQuery
    ->method('condition')
    ->willReturnMap($map);

  // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
  $this->idMap
    ->method('lookupSourceId')
    ->willReturnMap([
    [
      [
        'test_field' => 'forums',
      ],
      FALSE,
    ],
    [
      [
        'test_field' => 'test_vocab',
      ],
      [
        'source_id' => 42,
      ],
    ],
  ]);

  // Existing entity 'forums' was not migrated, value should not be unique.
  $actual = $plugin
    ->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
  $this
    ->assertEquals('forums', $actual, 'Pre-existing name is re-used');

  // Entity 'test_vocab' was migrated, value should be unique.
  $actual = $plugin
    ->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
  $this
    ->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
}