function MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated

Same name in other branches
  1. 9 core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated()
  2. 8.9.x core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated()
  3. 11.x 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 181

Class

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

Namespace

Drupal\Tests\migrate\Unit\process

Code

public function testMakeUniqueEntityFieldMigrated() : void {
    $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, 'foo');
    $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, 'foo');
    $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.