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\MakeUniqueEntityField;
11 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
14 class MakeUniqueEntityFieldTest extends MigrateProcessTestCase {
17 * The mock entity query.
19 * @var \Drupal\Core\Entity\Query\QueryInterface
21 protected $entityQuery;
24 * The mocked entity type manager.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $entityTypeManager;
31 * The migration configuration, initialized to set the ID to test.
35 protected $migrationConfiguration = [
42 protected function setUp() {
43 $this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
44 ->disableOriginalConstructor()
46 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
48 $storage = $this->getMock(EntityStorageInterface::class);
49 $storage->expects($this->any())
51 ->willReturn($this->entityQuery);
52 $this->entityTypeManager->expects($this->any())
53 ->method('getStorage')
54 ->with('test_entity_type')
55 ->willReturn($storage);
60 * Tests making an entity field value unique.
62 * @dataProvider providerTestMakeUniqueEntityField
64 public function testMakeUniqueEntityField($count, $postfix = '', $start = NULL, $length = NULL) {
66 'entity_type' => 'test_entity_type',
67 'field' => 'test_field',
70 $configuration['postfix'] = $postfix;
72 $configuration['start'] = isset($start) ? $start : NULL;
73 $configuration['length'] = isset($length) ? $length : NULL;
74 $plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
75 $this->entityQueryExpects($count);
76 $value = $this->randomMachineName(32);
77 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
78 $expected = mb_substr($value, $start, $length);
79 $expected .= $count ? $postfix . $count : '';
80 $this->assertSame($expected, $actual);
84 * Tests that invalid start position throws an exception.
86 public function testMakeUniqueEntityFieldEntityInvalidStart() {
88 'entity_type' => 'test_entity_type',
89 'field' => 'test_field',
92 $plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
93 $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.');
94 $plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
98 * Tests that invalid length option throws an exception.
100 public function testMakeUniqueEntityFieldEntityInvalidLength() {
102 'entity_type' => 'test_entity_type',
103 'field' => 'test_field',
104 'length' => 'foobar',
106 $plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
107 $this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
108 $plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
112 * Data provider for testMakeUniqueEntityField().
114 public function providerTestMakeUniqueEntityField() {
116 // Tests no duplication.
118 // Tests no duplication and start position.
120 // Tests no duplication, start position, and length.
122 // Tests no duplication and length.
124 // Tests duplication.
126 // Tests duplication and start position.
128 // Tests duplication, start position, and length.
130 // Tests duplication and length.
132 // Tests no duplication and postfix.
134 // Tests no duplication, postfix, and start position.
136 // Tests no duplication, postfix, start position, and length.
138 // Tests no duplication, postfix, and length.
140 // Tests duplication and postfix.
142 // Tests duplication, postfix, and start position.
144 // Tests duplication, postfix, start position, and length.
146 // Tests duplication, postfix, and length.
152 * Helper function to add expectations to the mock entity query object.
155 * The number of unique values to be set up.
157 protected function entityQueryExpects($count) {
158 $this->entityQuery->expects($this->exactly($count + 1))
159 ->method('condition')
160 ->will($this->returnValue($this->entityQuery));
161 $this->entityQuery->expects($this->exactly($count + 1))
163 ->will($this->returnValue($this->entityQuery));
164 $this->entityQuery->expects($this->exactly($count + 1))
166 ->will($this->returnCallback(function () use (&$count) {
172 * Tests making an entity field value unique only for migrated entities.
174 public function testMakeUniqueEntityFieldMigrated() {
176 'entity_type' => 'test_entity_type',
177 'field' => 'test_field',
180 $plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
182 // Setup the entityQuery used in MakeUniqueEntityFieldEntity::exists. The
183 // map, $map, is an array consisting of the four input parameters to the
184 // query condition method and then the query to return. Both 'forum' and
185 // 'test_vocab' are existing entities. There is no 'test_vocab1'.
187 foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
188 $query = $this->prophesize(QueryInterface::class);
189 $query->willBeConstructedWith([]);
190 $query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
191 $map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
194 ->method('condition')
195 ->will($this->returnValueMap($map));
197 // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
199 ->method('lookupSourceId')
200 ->will($this->returnValueMap([
201 [['test_field' => 'forums'], FALSE],
202 [['test_field' => 'test_vocab'], ['source_id' => 42]],
205 // Existing entity 'forums' was not migrated, value should not be unique.
206 $actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
207 $this->assertEquals('forums', $actual, 'Pre-existing name is re-used');
209 // Entity 'test_vocab' was migrated, value should be unique.
210 $actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
211 $this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');