Version 1
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / DedupeEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
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);
7
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;
13
14 /**
15  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\DedupeEntity
16  * @group migrate
17  */
18 class DedupeEntityTest extends MigrateProcessTestCase {
19
20   /**
21    * The mock entity query.
22    *
23    * @var \Drupal\Core\Entity\Query\QueryInterface
24    * @var \Drupal\Core\Entity\Query\QueryFactory
25    */
26   protected $entityQuery;
27
28   /**
29    * The mocked entity type manager.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32    */
33   protected $entityTypeManager;
34
35   /**
36    * The migration configuration, initialized to set the ID to test.
37    *
38    * @var array
39    */
40   protected $migrationConfiguration = [
41     'id' => 'test',
42   ];
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUp() {
48     $this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
49       ->disableOriginalConstructor()
50       ->getMock();
51     $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
52
53     $storage = $this->getMock(EntityStorageInterface::class);
54     $storage->expects($this->any())
55       ->method('getQuery')
56       ->willReturn($this->entityQuery);
57     $this->entityTypeManager->expects($this->any())
58       ->method('getStorage')
59       ->with('test_entity_type')
60       ->willReturn($storage);
61     parent::setUp();
62   }
63
64   /**
65    * Tests entity based deduplication based on providerTestDedupe() values.
66    *
67    * @dataProvider providerTestDedupe
68    */
69   public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
70     $configuration = [
71       'entity_type' => 'test_entity_type',
72       'field' => 'test_field',
73     ];
74     if ($postfix) {
75       $configuration['postfix'] = $postfix;
76     }
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);
86   }
87
88   /**
89    * Tests that invalid start position throws an exception.
90    */
91   public function testDedupeEntityInvalidStart() {
92     $configuration = [
93       'entity_type' => 'test_entity_type',
94       'field' => 'test_field',
95       'start' => 'foobar',
96     ];
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');
100   }
101
102   /**
103    * Tests that invalid length option throws an exception.
104    */
105   public function testDedupeEntityInvalidLength() {
106     $configuration = [
107       'entity_type' => 'test_entity_type',
108       'field' => 'test_field',
109       'length' => 'foobar',
110     ];
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');
114   }
115
116   /**
117    * Data provider for testDedupe().
118    */
119   public function providerTestDedupe() {
120     return [
121       // Tests no duplication.
122       [0],
123       // Tests no duplication and start position.
124       [0, NULL, 10],
125       // Tests no duplication, start position, and length.
126       [0, NULL, 5, 10],
127       // Tests no duplication and length.
128       [0, NULL, NULL, 10],
129       // Tests duplication.
130       [3],
131       // Tests duplication and start position.
132       [3, NULL, 10],
133       // Tests duplication, start position, and length.
134       [3, NULL, 5, 10],
135       // Tests duplication and length.
136       [3, NULL, NULL, 10],
137       // Tests no duplication and postfix.
138       [0, '_'],
139       // Tests no duplication, postfix, and start position.
140       [0, '_', 5],
141       // Tests no duplication, postfix, start position, and length.
142       [0, '_', 5, 10],
143       // Tests no duplication, postfix, and length.
144       [0, '_', NULL, 10],
145       // Tests duplication and postfix.
146       [2, '_'],
147       // Tests duplication, postfix, and start position.
148       [2, '_', 5],
149       // Tests duplication, postfix, start position, and length.
150       [2, '_', 5, 10],
151       // Tests duplication, postfix, and length.
152       [2, '_', NULL, 10],
153     ];
154   }
155
156   /**
157    * Helper function to add expectations to the mock entity query object.
158    *
159    * @param int $count
160    *   The number of deduplications to be set up.
161    */
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))
167       ->method('count')
168       ->will($this->returnValue($this->entityQuery));
169     $this->entityQuery->expects($this->exactly($count + 1))
170       ->method('execute')
171       ->will($this->returnCallback(function () use (&$count) { return $count--;}));
172   }
173
174   /**
175    * Test deduplicating only migrated entities.
176    */
177   public function testDedupeMigrated() {
178     $configuration = [
179       'entity_type' => 'test_entity_type',
180       'field' => 'test_field',
181       'migrated' => TRUE,
182     ];
183     $plugin = new DedupeEntity($configuration, 'dedupe_entity', [], $this->getMigration(), $this->entityTypeManager);
184
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'.
189     $map = [];
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()];
195     }
196     $this->entityQuery
197       ->method('condition')
198       ->will($this->returnValueMap($map));
199
200     // Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
201     $this->idMap
202       ->method('lookupSourceID')
203       ->will($this->returnValueMap([
204         [['test_field' => 'forums'], FALSE],
205         [['test_field' => 'test_vocab'], ['source_id' => 42]],
206       ]));
207
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');
211
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');
215   }
216
217 }