View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
class DedupeEntityTest extends MigrateProcessTestCase {
protected $entityQuery;
protected $entityTypeManager;
protected $migrationConfiguration = [
'id' => 'test',
];
protected function setUp() {
$this->entityQuery = $this
->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryInterface')
->disableOriginalConstructor()
->getMock();
$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();
}
public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
];
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = isset($start) ? $start : NULL;
$configuration['length'] = isset($length) ? $length : NULL;
$plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this
->getMigration(), $this->entityTypeManager);
$this
->entityQueryExpects($count);
$value = $this
->randomMachineName(32);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
$expected = mb_substr($value, $start, $length);
$expected .= $count ? $postfix . $count : '';
$this
->assertSame($expected, $actual);
}
public function testDedupeEntityInvalidStart() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
];
$plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $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, 'testproperty');
}
public function testDedupeEntityInvalidLength() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
];
$plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $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, 'testproperty');
}
public function providerTestDedupe() {
return [
[
0,
],
[
0,
NULL,
10,
],
[
0,
NULL,
5,
10,
],
[
0,
NULL,
NULL,
10,
],
[
3,
],
[
3,
NULL,
10,
],
[
3,
NULL,
5,
10,
],
[
3,
NULL,
NULL,
10,
],
[
0,
'_',
],
[
0,
'_',
5,
],
[
0,
'_',
5,
10,
],
[
0,
'_',
NULL,
10,
],
[
2,
'_',
],
[
2,
'_',
5,
],
[
2,
'_',
5,
10,
],
[
2,
'_',
NULL,
10,
],
];
}
protected function entityQueryExpects($count) {
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('condition')
->will($this
->returnValue($this->entityQuery));
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('count')
->will($this
->returnValue($this->entityQuery));
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('execute')
->will($this
->returnCallback(function () use (&$count) {
return $count--;
}));
}
public function testDedupeMigrated() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'migrated' => TRUE,
];
$plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this
->getMigration(), $this->entityTypeManager);
$map = [];
foreach ([
'forums',
'test_vocab',
'test_vocab1',
] as $id) {
$query = $this
->prophesize(QueryInterface::class);
$query
->willBeConstructedWith([]);
$query
->execute()
->willReturn($id === 'test_vocab1' ? [] : [
$id,
]);
$map[] = [
'test_field',
$id,
NULL,
NULL,
$query
->reveal(),
];
}
$this->entityQuery
->method('condition')
->will($this
->returnValueMap($map));
$this->idMap
->method('lookupSourceId')
->will($this
->returnValueMap([
[
[
'test_field' => 'forums',
],
FALSE,
],
[
[
'test_field' => 'test_vocab',
],
[
'source_id' => 42,
],
],
]));
$actual = $plugin
->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
$this
->assertEquals('forums', $actual, 'Pre-existing name is re-used');
$actual = $plugin
->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
$this
->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
}
}