class MakeUniqueEntityFieldTest
Same name in other branches
- 9 core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest
- 8.9.x core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest
- 11.x core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest
@coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField @group migrate
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\migrate\Unit\MigrateTestCase extends \Drupal\Tests\UnitTestCase
- class \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase extends \Drupal\Tests\migrate\Unit\MigrateTestCase
- class \Drupal\Tests\migrate\Unit\process\MakeUniqueEntityFieldTest extends \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase
- class \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase extends \Drupal\Tests\migrate\Unit\MigrateTestCase
- class \Drupal\Tests\migrate\Unit\MigrateTestCase extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of MakeUniqueEntityFieldTest
File
-
core/
modules/ migrate/ tests/ src/ Unit/ process/ MakeUniqueEntityFieldTest.php, line 16
Namespace
Drupal\Tests\migrate\Unit\processView source
class MakeUniqueEntityFieldTest extends MigrateProcessTestCase {
/**
* The mock entity query.
*
* @var \Drupal\Core\Entity\Query\QueryInterface
*/
protected $entityQuery;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $entityTypeManager;
/**
* The migration configuration, initialized to set the ID to test.
*
* @var array
*/
protected $migrationConfiguration = [
'id' => 'test',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->entityQuery = $this->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryInterface')
->disableOriginalConstructor()
->getMock();
$this->entityQuery
->expects($this->any())
->method('accessCheck')
->willReturnSelf();
$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$storage = $this->createMock(EntityStorageInterface::class);
$storage->expects($this->any())
->method('getQuery')
->willReturn($this->entityQuery);
$this->entityTypeManager
->expects($this->any())
->method('getStorage')
->with('test_entity_type')
->willReturn($storage);
parent::setUp();
}
/**
* Tests making an entity field value unique.
*
* @dataProvider providerTestMakeUniqueEntityField
*/
public function testMakeUniqueEntityField($count, $postfix = '', $start = NULL, $length = NULL) : void {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
];
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = $start;
$configuration['length'] = $length;
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->entityQueryExpects($count);
$value = $this->randomMachineName(32);
$actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'foo');
$expected = mb_substr($value, $start ?? 0, $length);
$expected .= $count ? $postfix . $count : '';
$this->assertSame($expected, $actual);
}
/**
* Tests that invalid start position throws an exception.
*/
public function testMakeUniqueEntityFieldEntityInvalidStart() : void {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
];
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->expectException('Drupal\\migrate\\MigrateException');
$this->expectExceptionMessage('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
$plugin->transform('test_start', $this->migrateExecutable, $this->row, 'foo');
}
/**
* Tests that invalid length option throws an exception.
*/
public function testMakeUniqueEntityFieldEntityInvalidLength() : void {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
];
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->expectException('Drupal\\migrate\\MigrateException');
$this->expectExceptionMessage('The character length configuration key should be an integer. Omit this key to capture the entire string.');
$plugin->transform('test_length', $this->migrateExecutable, $this->row, 'foo');
}
/**
* Data provider for testMakeUniqueEntityField().
*/
public static function providerTestMakeUniqueEntityField() {
return [
// Tests no duplication.
[
0,
],
// Tests no duplication and start position.
[
0,
NULL,
10,
],
// Tests no duplication, start position, and length.
[
0,
NULL,
5,
10,
],
// Tests no duplication and length.
[
0,
NULL,
NULL,
10,
],
// Tests duplication.
[
3,
],
// Tests duplication and start position.
[
3,
NULL,
10,
],
// Tests duplication, start position, and length.
[
3,
NULL,
5,
10,
],
// Tests duplication and length.
[
3,
NULL,
NULL,
10,
],
// Tests no duplication and postfix.
[
0,
'_',
],
// Tests no duplication, postfix, and start position.
[
0,
'_',
5,
],
// Tests no duplication, postfix, start position, and length.
[
0,
'_',
5,
10,
],
// Tests no duplication, postfix, and length.
[
0,
'_',
NULL,
10,
],
// Tests duplication and postfix.
[
2,
'_',
],
// Tests duplication, postfix, and start position.
[
2,
'_',
5,
],
// Tests duplication, postfix, start position, and length.
[
2,
'_',
5,
10,
],
// Tests duplication, postfix, and length.
[
2,
'_',
NULL,
10,
],
];
}
/**
* Helper function to add expectations to the mock entity query object.
*
* @param int $count
* The number of unique values to be set up.
*/
protected function entityQueryExpects($count) {
$this->entityQuery
->expects($this->exactly($count + 1))
->method('condition')
->willReturn($this->entityQuery);
$this->entityQuery
->expects($this->exactly($count + 1))
->method('count')
->willReturn($this->entityQuery);
$this->entityQuery
->expects($this->exactly($count + 1))
->method('execute')
->willReturnCallback(function () use (&$count) {
return $count--;
});
}
/**
* Tests making an entity field value unique only for migrated entities.
*/
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');
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
MakeUniqueEntityFieldTest::$entityQuery | protected | property | The mock entity query. | |||
MakeUniqueEntityFieldTest::$entityTypeManager | protected | property | The mocked entity type manager. | |||
MakeUniqueEntityFieldTest::$migrationConfiguration | protected | property | The migration configuration, initialized to set the ID to test. | Overrides MigrateTestCase::$migrationConfiguration | ||
MakeUniqueEntityFieldTest::entityQueryExpects | protected | function | Helper function to add expectations to the mock entity query object. | |||
MakeUniqueEntityFieldTest::providerTestMakeUniqueEntityField | public static | function | Data provider for testMakeUniqueEntityField(). | |||
MakeUniqueEntityFieldTest::setUp | protected | function | Overrides MigrateProcessTestCase::setUp | |||
MakeUniqueEntityFieldTest::testMakeUniqueEntityField | public | function | Tests making an entity field value unique. | |||
MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldEntityInvalidLength | public | function | Tests that invalid length option throws an exception. | |||
MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldEntityInvalidStart | public | function | Tests that invalid start position throws an exception. | |||
MakeUniqueEntityFieldTest::testMakeUniqueEntityFieldMigrated | public | function | Tests making an entity field value unique only for migrated entities. | |||
MigrateProcessTestCase::$migrateExecutable | protected | property | ||||
MigrateProcessTestCase::$plugin | protected | property | 1 | |||
MigrateProcessTestCase::$row | protected | property | ||||
MigrateTestCase::$idMap | protected | property | The migration ID map. | |||
MigrateTestCase::$migrationStatus | protected | property | Local store for mocking setStatus()/getStatus(). | |||
MigrateTestCase::createSchemaFromRow | protected | function | Generates a table schema from a row. | |||
MigrateTestCase::getDatabase | protected | function | Gets an SQLite database connection object for use in tests. | |||
MigrateTestCase::getMigration | protected | function | Retrieves a mocked migration. | |||
MigrateTestCase::getValue | protected | function | Gets the value on a row for a given key. | |||
MigrateTestCase::queryResultTest | public | function | Tests a query. | |||
MigrateTestCase::retrievalAssertHelper | protected | function | Asserts tested values during test retrieval. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.