3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Entity\Query\QueryInterface;
8 use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
9 use Drupal\Component\Utility\Unicode;
12 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\DedupeEntity
16 class DedupeEntityTest extends MigrateProcessTestCase {
19 * The mock entity query.
21 * @var \Drupal\Core\Entity\Query\QueryInterface|\Drupal\Core\Entity\Query\QueryFactory
23 protected $entityQuery;
26 * The mocked entity type manager.
28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
30 protected $entityTypeManager;
33 * The migration configuration, initialized to set the ID to test.
37 protected $migrationConfiguration = [
44 protected function setUp() {
45 $this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
46 ->disableOriginalConstructor()
48 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
50 $storage = $this->getMock(EntityStorageInterface::class);
51 $storage->expects($this->any())
53 ->willReturn($this->entityQuery);
54 $this->entityTypeManager->expects($this->any())
55 ->method('getStorage')
56 ->with('test_entity_type')
57 ->willReturn($storage);
62 * Tests entity based deduplication based on providerTestDedupe() values.
64 * @dataProvider providerTestDedupe
66 public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
68 'entity_type' => 'test_entity_type',
69 'field' => 'test_field',
72 $configuration['postfix'] = $postfix;
74 $configuration['start'] = isset($start) ? $start : NULL;
75 $configuration['length'] = isset($length) ? $length : NULL;
76 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
77 $this->entityQueryExpects($count);
78 $value = $this->randomMachineName(32);
79 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
80 $expected = Unicode::substr($value, $start, $length);
81 $expected .= $count ? $postfix . $count : '';
82 $this->assertSame($expected, $actual);
86 * Tests that invalid start position throws an exception.
88 public function testDedupeEntityInvalidStart() {
90 'entity_type' => 'test_entity_type',
91 'field' => 'test_field',
94 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
95 $this->setExpectedException('Drupal\migrate\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
96 $plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
100 * Tests that invalid length option throws an exception.
102 public function testDedupeEntityInvalidLength() {
104 'entity_type' => 'test_entity_type',
105 'field' => 'test_field',
106 'length' => 'foobar',
108 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
109 $this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
110 $plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
114 * Data provider for testDedupe().
116 public function providerTestDedupe() {
118 // Tests no duplication.
120 // Tests no duplication and start position.
122 // Tests no duplication, start position, and length.
124 // Tests no duplication and length.
126 // Tests duplication.
128 // Tests duplication and start position.
130 // Tests duplication, start position, and length.
132 // Tests duplication and length.
134 // Tests no duplication and postfix.
136 // Tests no duplication, postfix, and start position.
138 // Tests no duplication, postfix, start position, and length.
140 // Tests no duplication, postfix, and length.
142 // Tests duplication and postfix.
144 // Tests duplication, postfix, and start position.
146 // Tests duplication, postfix, start position, and length.
148 // Tests duplication, postfix, and length.
154 * Helper function to add expectations to the mock entity query object.
157 * The number of deduplications to be set up.
159 protected function entityQueryExpects($count) {
160 $this->entityQuery->expects($this->exactly($count + 1))
161 ->method('condition')
162 ->will($this->returnValue($this->entityQuery));
163 $this->entityQuery->expects($this->exactly($count + 1))
165 ->will($this->returnValue($this->entityQuery));
166 $this->entityQuery->expects($this->exactly($count + 1))
168 ->will($this->returnCallback(function () use (&$count) {
174 * Test deduplicating only migrated entities.
176 public function testDedupeMigrated() {
178 'entity_type' => 'test_entity_type',
179 'field' => 'test_field',
182 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
184 // Setup the entityQuery used in DedupeEntity::exists. The map, $map, is
185 // an array consisting of the four input parameters to the query condition
186 // method and then the query to return. Both 'forum' and
187 // 'test_vocab' are existing entities. There is no 'test_vocab1'.
189 foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
190 $query = $this->prophesize(QueryInterface::class);
191 $query->willBeConstructedWith([]);
192 $query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
193 $map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
196 ->method('condition')
197 ->will($this->returnValueMap($map));
199 // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
201 ->method('lookupSourceID')
202 ->will($this->returnValueMap([
203 [['test_field' => 'forums'], FALSE],
204 [['test_field' => 'test_vocab'], ['source_id' => 42]],
207 // Existing entity 'forums' was not migrated, it should not be deduplicated.
208 $actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
209 $this->assertEquals('forums', $actual, 'Pre-existing name is re-used');
211 // Entity 'test_vocab' was migrated, should be deduplicated.
212 $actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
213 $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');