3 namespace Drupal\Tests\migrate\Unit\process;
5 @trigger_error('The ' . __NAMESPACE__ . '\DedupeEntityTest is deprecated in
6 Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\MakeUniqueEntityFieldTest', E_USER_DEPRECATED);
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Entity\Query\QueryInterface;
11 use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
12 use Drupal\Component\Utility\Unicode;
15 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\DedupeEntity
18 class DedupeEntityTest extends MigrateProcessTestCase {
21 * The mock entity query.
23 * @var \Drupal\Core\Entity\Query\QueryInterface
24 * @var \Drupal\Core\Entity\Query\QueryFactory
26 protected $entityQuery;
29 * The mocked entity type manager.
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33 protected $entityTypeManager;
36 * The migration configuration, initialized to set the ID to test.
40 protected $migrationConfiguration = [
47 protected function setUp() {
48 $this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
49 ->disableOriginalConstructor()
51 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
53 $storage = $this->getMock(EntityStorageInterface::class);
54 $storage->expects($this->any())
56 ->willReturn($this->entityQuery);
57 $this->entityTypeManager->expects($this->any())
58 ->method('getStorage')
59 ->with('test_entity_type')
60 ->willReturn($storage);
65 * Tests entity based deduplication based on providerTestDedupe() values.
67 * @dataProvider providerTestDedupe
69 public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
71 'entity_type' => 'test_entity_type',
72 'field' => 'test_field',
75 $configuration['postfix'] = $postfix;
77 $configuration['start'] = isset($start) ? $start : NULL;
78 $configuration['length'] = isset($length) ? $length : NULL;
79 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
80 $this->entityQueryExpects($count);
81 $value = $this->randomMachineName(32);
82 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
83 $expected = Unicode::substr($value, $start, $length);
84 $expected .= $count ? $postfix . $count : '';
85 $this->assertSame($expected, $actual);
89 * Tests that invalid start position throws an exception.
91 public function testDedupeEntityInvalidStart() {
93 'entity_type' => 'test_entity_type',
94 'field' => 'test_field',
97 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
98 $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.');
99 $plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
103 * Tests that invalid length option throws an exception.
105 public function testDedupeEntityInvalidLength() {
107 'entity_type' => 'test_entity_type',
108 'field' => 'test_field',
109 'length' => 'foobar',
111 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
112 $this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
113 $plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
117 * Data provider for testDedupe().
119 public function providerTestDedupe() {
121 // Tests no duplication.
123 // Tests no duplication and start position.
125 // Tests no duplication, start position, and length.
127 // Tests no duplication and length.
129 // Tests duplication.
131 // Tests duplication and start position.
133 // Tests duplication, start position, and length.
135 // Tests duplication and length.
137 // Tests no duplication and postfix.
139 // Tests no duplication, postfix, and start position.
141 // Tests no duplication, postfix, start position, and length.
143 // Tests no duplication, postfix, and length.
145 // Tests duplication and postfix.
147 // Tests duplication, postfix, and start position.
149 // Tests duplication, postfix, start position, and length.
151 // Tests duplication, postfix, and length.
157 * Helper function to add expectations to the mock entity query object.
160 * The number of deduplications to be set up.
162 protected function entityQueryExpects($count) {
163 $this->entityQuery->expects($this->exactly($count + 1))
164 ->method('condition')
165 ->will($this->returnValue($this->entityQuery));
166 $this->entityQuery->expects($this->exactly($count + 1))
168 ->will($this->returnValue($this->entityQuery));
169 $this->entityQuery->expects($this->exactly($count + 1))
171 ->will($this->returnCallback(function () use (&$count) { return $count--;}));
175 * Test deduplicating only migrated entities.
177 public function testDedupeMigrated() {
179 'entity_type' => 'test_entity_type',
180 'field' => 'test_field',
183 $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
185 // Setup the entityQuery used in DedupeEntity::exists. The map, $map, is
186 // an array consisting of the four input parameters to the query condition
187 // method and then the query to return. Both 'forum' and
188 // 'test_vocab' are existing entities. There is no 'test_vocab1'.
190 foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
191 $query = $this->prophesize(QueryInterface::class);
192 $query->willBeConstructedWith([]);
193 $query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
194 $map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
197 ->method('condition')
198 ->will($this->returnValueMap($map));
200 // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
202 ->method('lookupSourceID')
203 ->will($this->returnValueMap([
204 [['test_field' => 'forums'], FALSE],
205 [['test_field' => 'test_vocab'], ['source_id' => 42]],
208 // Existing entity 'forums' was not migrated, it should not be deduplicated.
209 $actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
210 $this->assertEquals('forums', $actual, 'Pre-existing name is re-used');
212 // Entity 'test_vocab' was migrated, should be deduplicated.
213 $actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
214 $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');