3 namespace Drupal\Tests\migrate\Unit;
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6 use Drupal\migrate\Row;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\migrate\Row
13 class RowTest extends UnitTestCase {
20 protected $testSourceIds = [
29 protected $testValues = [
39 protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
42 * The test hash after changing title value to 'new title'.
46 protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
49 * Tests object creation: empty.
51 public function testRowWithoutData() {
53 $this->assertSame([], $row->getSource(), 'Empty row');
57 * Tests object creation: basic.
59 public function testRowWithBasicData() {
60 $row = new Row($this->testValues, $this->testSourceIds);
61 $this->assertSame($this->testValues, $row->getSource(), 'Row with data, simple id.');
65 * Tests object creation: multiple source IDs.
67 public function testRowWithMultipleSourceIds() {
68 $multi_source_ids = $this->testSourceIds + ['vid' => 'Node revision'];
69 $multi_source_ids_values = $this->testValues + ['vid' => 1];
70 $row = new Row($multi_source_ids_values, $multi_source_ids);
71 $this->assertSame($multi_source_ids_values, $row->getSource(), 'Row with data, multifield id.');
75 * Tests object creation: invalid values.
77 public function testRowWithInvalidData() {
81 $this->setExpectedException(\Exception::class);
82 $row = new Row($invalid_values, $this->testSourceIds);
86 * Tests source immutability after freeze.
88 public function testSourceFreeze() {
89 $row = new Row($this->testValues, $this->testSourceIds);
91 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
92 $row->setSourceProperty('title', 'new title');
94 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
96 $this->setExpectedException(\Exception::class);
97 $row->setSourceProperty('title', 'new title');
101 * Tests setting on a frozen row.
103 public function testSetFrozenRow() {
104 $row = new Row($this->testValues, $this->testSourceIds);
105 $row->freezeSource();
106 $this->setExpectedException(\Exception::class, "The source is frozen and can't be changed any more");
107 $row->setSourceProperty('title', 'new title');
113 public function testHashing() {
114 $row = new Row($this->testValues, $this->testSourceIds);
115 $this->assertSame('', $row->getHash(), 'No hash at creation');
117 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
119 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.');
121 // Set the map to needs update.
123 'original_hash' => '',
125 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
127 $row->setIdMap($test_id_map);
128 $this->assertTrue($row->needsUpdate());
131 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.');
132 $row->setSourceProperty('title', 'new title');
134 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
135 // Check hash calculation algorithm.
136 $hash = hash('sha256', serialize($row->getSource()));
137 $this->assertSame($hash, $row->getHash());
138 // Check length of generated hash used for mapping schema.
139 $this->assertSame(64, strlen($row->getHash()));
141 // Set the map to successfully imported.
143 'original_hash' => '',
145 'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
147 $row->setIdMap($test_id_map);
148 $this->assertFalse($row->needsUpdate());
150 // Set the same hash value and ensure it was not changed.
151 $random = $this->randomMachineName();
153 'original_hash' => $random,
155 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
157 $row->setIdMap($test_id_map);
158 $this->assertFalse($row->changed());
160 // Set different has values to ensure it is marked as changed.
162 'original_hash' => $this->randomMachineName(),
163 'hash' => $this->randomMachineName(),
164 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
166 $row->setIdMap($test_id_map);
167 $this->assertTrue($row->changed());
171 * Tests getting/setting the ID Map.
176 public function testGetSetIdMap() {
177 $row = new Row($this->testValues, $this->testSourceIds);
179 'original_hash' => '',
181 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
183 $row->setIdMap($test_id_map);
184 $this->assertEquals($test_id_map, $row->getIdMap());
188 * Tests the source ID.
190 public function testSourceIdValues() {
191 $row = new Row($this->testValues, $this->testSourceIds);
192 $this->assertSame(['nid' => $this->testValues['nid']], $row->getSourceIdValues());
196 * Tests the multiple source IDs.
198 public function testMultipleSourceIdValues() {
199 // Set values in same order as ids.
200 $multi_source_ids = $this->testSourceIds + [
201 'vid' => 'Node revision',
202 'type' => 'Node type',
203 'langcode' => 'Node language',
205 $multi_source_ids_values = $this->testValues + [
210 $row = new Row($multi_source_ids_values, $multi_source_ids);
211 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
213 // Set values in different order.
214 $multi_source_ids = $this->testSourceIds + [
215 'vid' => 'Node revision',
216 'type' => 'Node type',
217 'langcode' => 'Node language',
219 $multi_source_ids_values = $this->testValues + [
224 $row = new Row($multi_source_ids_values, $multi_source_ids);
225 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
229 * Tests getting the source property.
231 * @covers ::getSourceProperty
233 public function testGetSourceProperty() {
234 $row = new Row($this->testValues, $this->testSourceIds);
235 $this->assertSame($this->testValues['nid'], $row->getSourceProperty('nid'));
236 $this->assertSame($this->testValues['title'], $row->getSourceProperty('title'));
237 $this->assertNull($row->getSourceProperty('non_existing'));
241 * Tests setting and getting the destination.
243 public function testDestination() {
244 $row = new Row($this->testValues, $this->testSourceIds);
245 $this->assertEmpty($row->getDestination());
246 $this->assertFalse($row->hasDestinationProperty('nid'));
248 // Set a destination.
249 $row->setDestinationProperty('nid', 2);
250 $this->assertTrue($row->hasDestinationProperty('nid'));
251 $this->assertEquals(['nid' => 2], $row->getDestination());
255 * Tests setting/getting multiple destination IDs.
257 public function testMultipleDestination() {
258 $row = new Row($this->testValues, $this->testSourceIds);
259 // Set some deep nested values.
260 $row->setDestinationProperty('image/alt', 'alt text');
261 $row->setDestinationProperty('image/fid', 3);
263 $this->assertTrue($row->hasDestinationProperty('image'));
264 $this->assertFalse($row->hasDestinationProperty('alt'));
265 $this->assertFalse($row->hasDestinationProperty('fid'));
267 $destination = $row->getDestination();
268 $this->assertEquals('alt text', $destination['image']['alt']);
269 $this->assertEquals(3, $destination['image']['fid']);
270 $this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
271 $this->assertEquals(3, $row->getDestinationProperty('image/fid'));