annotate core/modules/migrate/tests/src/Unit/RowTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\migrate\Unit;
Chris@0 4
Chris@0 5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
Chris@0 6 use Drupal\migrate\Row;
Chris@0 7 use Drupal\Tests\UnitTestCase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * @coversDefaultClass \Drupal\migrate\Row
Chris@0 11 * @group migrate
Chris@0 12 */
Chris@0 13 class RowTest extends UnitTestCase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * The source IDs.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 protected $testSourceIds = [
Chris@0 21 'nid' => 'Node ID',
Chris@0 22 ];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The test values.
Chris@0 26 *
Chris@0 27 * @var array
Chris@0 28 */
Chris@0 29 protected $testValues = [
Chris@0 30 'nid' => 1,
Chris@0 31 'title' => 'node 1',
Chris@0 32 ];
Chris@0 33
Chris@0 34 /**
Chris@18 35 * Test source properties for testing get and getMultiple.
Chris@18 36 *
Chris@18 37 * @var array
Chris@18 38 */
Chris@18 39 protected $testGetSourceProperties = [
Chris@18 40 'source_key_1' => 'source_value_1',
Chris@18 41 'source_key_2' => 'source_value_2',
Chris@18 42 '@source_key_3' => 'source_value_3',
Chris@18 43 'shared_key_1' => 'source_shared_value_1',
Chris@18 44 '@shared_key_2' => 'source_shared_value_2',
Chris@18 45 '@@@@shared_key_3' => 'source_shared_value_3',
Chris@18 46 ];
Chris@18 47
Chris@18 48 /**
Chris@18 49 * Test source keys for testing get and getMultiple.
Chris@18 50 *
Chris@18 51 * @var array
Chris@18 52 */
Chris@18 53 protected $testGetSourceIds = [
Chris@18 54 'source_key_1' => [],
Chris@18 55 ];
Chris@18 56
Chris@18 57 /**
Chris@18 58 * Test destination properties for testing get and getMultiple.
Chris@18 59 *
Chris@18 60 * @var array
Chris@18 61 */
Chris@18 62 protected $testGetDestinationProperties = [
Chris@18 63 'destination_key_1' => 'destination_value_1',
Chris@18 64 'destination_key_2' => 'destination_value_2',
Chris@18 65 '@destination_key_3' => 'destination_value_3',
Chris@18 66 'shared_key_1' => 'destination_shared_value_1',
Chris@18 67 '@shared_key_2' => 'destination_shared_value_2',
Chris@18 68 '@@@@shared_key_3' => 'destination_shared_value_3',
Chris@18 69 ];
Chris@18 70
Chris@18 71 /**
Chris@0 72 * The test hash.
Chris@0 73 *
Chris@0 74 * @var string
Chris@0 75 */
Chris@0 76 protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
Chris@0 77
Chris@0 78 /**
Chris@0 79 * The test hash after changing title value to 'new title'.
Chris@0 80 *
Chris@0 81 * @var string
Chris@0 82 */
Chris@0 83 protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Tests object creation: empty.
Chris@0 87 */
Chris@0 88 public function testRowWithoutData() {
Chris@0 89 $row = new Row();
Chris@0 90 $this->assertSame([], $row->getSource(), 'Empty row');
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Tests object creation: basic.
Chris@0 95 */
Chris@0 96 public function testRowWithBasicData() {
Chris@0 97 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 98 $this->assertSame($this->testValues, $row->getSource(), 'Row with data, simple id.');
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * Tests object creation: multiple source IDs.
Chris@0 103 */
Chris@0 104 public function testRowWithMultipleSourceIds() {
Chris@0 105 $multi_source_ids = $this->testSourceIds + ['vid' => 'Node revision'];
Chris@0 106 $multi_source_ids_values = $this->testValues + ['vid' => 1];
Chris@0 107 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 108 $this->assertSame($multi_source_ids_values, $row->getSource(), 'Row with data, multifield id.');
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Tests object creation: invalid values.
Chris@0 113 */
Chris@0 114 public function testRowWithInvalidData() {
Chris@0 115 $invalid_values = [
Chris@0 116 'title' => 'node X',
Chris@0 117 ];
Chris@0 118 $this->setExpectedException(\Exception::class);
Chris@0 119 $row = new Row($invalid_values, $this->testSourceIds);
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Tests source immutability after freeze.
Chris@0 124 */
Chris@0 125 public function testSourceFreeze() {
Chris@0 126 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 127 $row->rehash();
Chris@0 128 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
Chris@0 129 $row->setSourceProperty('title', 'new title');
Chris@0 130 $row->rehash();
Chris@0 131 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
Chris@0 132 $row->freezeSource();
Chris@0 133 $this->setExpectedException(\Exception::class);
Chris@0 134 $row->setSourceProperty('title', 'new title');
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Tests setting on a frozen row.
Chris@0 139 */
Chris@0 140 public function testSetFrozenRow() {
Chris@0 141 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 142 $row->freezeSource();
Chris@0 143 $this->setExpectedException(\Exception::class, "The source is frozen and can't be changed any more");
Chris@0 144 $row->setSourceProperty('title', 'new title');
Chris@0 145 }
Chris@0 146
Chris@0 147 /**
Chris@0 148 * Tests hashing.
Chris@0 149 */
Chris@0 150 public function testHashing() {
Chris@0 151 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 152 $this->assertSame('', $row->getHash(), 'No hash at creation');
Chris@0 153 $row->rehash();
Chris@0 154 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
Chris@0 155 $row->rehash();
Chris@0 156 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.');
Chris@0 157
Chris@0 158 // Set the map to needs update.
Chris@0 159 $test_id_map = [
Chris@0 160 'original_hash' => '',
Chris@0 161 'hash' => '',
Chris@0 162 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 163 ];
Chris@0 164 $row->setIdMap($test_id_map);
Chris@0 165 $this->assertTrue($row->needsUpdate());
Chris@0 166
Chris@0 167 $row->rehash();
Chris@0 168 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.');
Chris@0 169 $row->setSourceProperty('title', 'new title');
Chris@0 170 $row->rehash();
Chris@0 171 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
Chris@0 172 // Check hash calculation algorithm.
Chris@0 173 $hash = hash('sha256', serialize($row->getSource()));
Chris@0 174 $this->assertSame($hash, $row->getHash());
Chris@0 175 // Check length of generated hash used for mapping schema.
Chris@0 176 $this->assertSame(64, strlen($row->getHash()));
Chris@0 177
Chris@0 178 // Set the map to successfully imported.
Chris@0 179 $test_id_map = [
Chris@0 180 'original_hash' => '',
Chris@0 181 'hash' => '',
Chris@0 182 'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
Chris@0 183 ];
Chris@0 184 $row->setIdMap($test_id_map);
Chris@0 185 $this->assertFalse($row->needsUpdate());
Chris@0 186
Chris@0 187 // Set the same hash value and ensure it was not changed.
Chris@0 188 $random = $this->randomMachineName();
Chris@0 189 $test_id_map = [
Chris@0 190 'original_hash' => $random,
Chris@0 191 'hash' => $random,
Chris@0 192 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 193 ];
Chris@0 194 $row->setIdMap($test_id_map);
Chris@0 195 $this->assertFalse($row->changed());
Chris@0 196
Chris@0 197 // Set different has values to ensure it is marked as changed.
Chris@0 198 $test_id_map = [
Chris@0 199 'original_hash' => $this->randomMachineName(),
Chris@0 200 'hash' => $this->randomMachineName(),
Chris@0 201 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 202 ];
Chris@0 203 $row->setIdMap($test_id_map);
Chris@0 204 $this->assertTrue($row->changed());
Chris@0 205 }
Chris@0 206
Chris@0 207 /**
Chris@0 208 * Tests getting/setting the ID Map.
Chris@0 209 *
Chris@0 210 * @covers ::setIdMap
Chris@0 211 * @covers ::getIdMap
Chris@0 212 */
Chris@0 213 public function testGetSetIdMap() {
Chris@0 214 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 215 $test_id_map = [
Chris@0 216 'original_hash' => '',
Chris@0 217 'hash' => '',
Chris@0 218 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 219 ];
Chris@0 220 $row->setIdMap($test_id_map);
Chris@0 221 $this->assertEquals($test_id_map, $row->getIdMap());
Chris@0 222 }
Chris@0 223
Chris@0 224 /**
Chris@0 225 * Tests the source ID.
Chris@0 226 */
Chris@0 227 public function testSourceIdValues() {
Chris@0 228 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 229 $this->assertSame(['nid' => $this->testValues['nid']], $row->getSourceIdValues());
Chris@0 230 }
Chris@0 231
Chris@0 232 /**
Chris@0 233 * Tests the multiple source IDs.
Chris@0 234 */
Chris@0 235 public function testMultipleSourceIdValues() {
Chris@0 236 // Set values in same order as ids.
Chris@0 237 $multi_source_ids = $this->testSourceIds + [
Chris@0 238 'vid' => 'Node revision',
Chris@0 239 'type' => 'Node type',
Chris@0 240 'langcode' => 'Node language',
Chris@0 241 ];
Chris@0 242 $multi_source_ids_values = $this->testValues + [
Chris@0 243 'vid' => 1,
Chris@0 244 'type' => 'page',
Chris@0 245 'langcode' => 'en',
Chris@0 246 ];
Chris@0 247 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 248 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
Chris@0 249
Chris@0 250 // Set values in different order.
Chris@0 251 $multi_source_ids = $this->testSourceIds + [
Chris@0 252 'vid' => 'Node revision',
Chris@0 253 'type' => 'Node type',
Chris@0 254 'langcode' => 'Node language',
Chris@0 255 ];
Chris@0 256 $multi_source_ids_values = $this->testValues + [
Chris@0 257 'langcode' => 'en',
Chris@0 258 'type' => 'page',
Chris@0 259 'vid' => 1,
Chris@0 260 ];
Chris@0 261 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 262 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
Chris@0 263 }
Chris@0 264
Chris@0 265 /**
Chris@0 266 * Tests getting the source property.
Chris@0 267 *
Chris@0 268 * @covers ::getSourceProperty
Chris@0 269 */
Chris@0 270 public function testGetSourceProperty() {
Chris@0 271 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 272 $this->assertSame($this->testValues['nid'], $row->getSourceProperty('nid'));
Chris@0 273 $this->assertSame($this->testValues['title'], $row->getSourceProperty('title'));
Chris@0 274 $this->assertNull($row->getSourceProperty('non_existing'));
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * Tests setting and getting the destination.
Chris@0 279 */
Chris@0 280 public function testDestination() {
Chris@0 281 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 282 $this->assertEmpty($row->getDestination());
Chris@0 283 $this->assertFalse($row->hasDestinationProperty('nid'));
Chris@0 284
Chris@0 285 // Set a destination.
Chris@0 286 $row->setDestinationProperty('nid', 2);
Chris@0 287 $this->assertTrue($row->hasDestinationProperty('nid'));
Chris@0 288 $this->assertEquals(['nid' => 2], $row->getDestination());
Chris@0 289 }
Chris@0 290
Chris@0 291 /**
Chris@0 292 * Tests setting/getting multiple destination IDs.
Chris@0 293 */
Chris@0 294 public function testMultipleDestination() {
Chris@0 295 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 296 // Set some deep nested values.
Chris@0 297 $row->setDestinationProperty('image/alt', 'alt text');
Chris@0 298 $row->setDestinationProperty('image/fid', 3);
Chris@0 299
Chris@0 300 $this->assertTrue($row->hasDestinationProperty('image'));
Chris@0 301 $this->assertFalse($row->hasDestinationProperty('alt'));
Chris@0 302 $this->assertFalse($row->hasDestinationProperty('fid'));
Chris@0 303
Chris@0 304 $destination = $row->getDestination();
Chris@0 305 $this->assertEquals('alt text', $destination['image']['alt']);
Chris@0 306 $this->assertEquals(3, $destination['image']['fid']);
Chris@0 307 $this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
Chris@0 308 $this->assertEquals(3, $row->getDestinationProperty('image/fid'));
Chris@0 309 }
Chris@0 310
Chris@18 311 /**
Chris@18 312 * Test getting source and destination properties.
Chris@18 313 *
Chris@18 314 * @param string $key
Chris@18 315 * The key to look up.
Chris@18 316 * @param string $expected_value
Chris@18 317 * The expected value.
Chris@18 318 *
Chris@18 319 * @dataProvider getDataProvider
Chris@18 320 * @covers ::get
Chris@18 321 */
Chris@18 322 public function testGet($key, $expected_value) {
Chris@18 323 $row = $this->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
Chris@18 324 $this->assertSame($expected_value, $row->get($key));
Chris@18 325 }
Chris@18 326
Chris@18 327 /**
Chris@18 328 * Data Provider for testGet.
Chris@18 329 *
Chris@18 330 * @return array
Chris@18 331 * The keys and expected values.
Chris@18 332 */
Chris@18 333 public function getDataProvider() {
Chris@18 334 return [
Chris@18 335 ['source_key_1', 'source_value_1'],
Chris@18 336 ['source_key_2', 'source_value_2'],
Chris@18 337 ['@@source_key_3', 'source_value_3'],
Chris@18 338 ['shared_key_1', 'source_shared_value_1'],
Chris@18 339 ['@@shared_key_2', 'source_shared_value_2'],
Chris@18 340 ['@@@@@@@@shared_key_3', 'source_shared_value_3'],
Chris@18 341 ['@destination_key_1', 'destination_value_1'],
Chris@18 342 ['@destination_key_2', 'destination_value_2'],
Chris@18 343 ['@@@destination_key_3', 'destination_value_3'],
Chris@18 344 ['@shared_key_1', 'destination_shared_value_1'],
Chris@18 345 ['@@@shared_key_2', 'destination_shared_value_2'],
Chris@18 346 ['@@@@@@@@@shared_key_3', 'destination_shared_value_3'],
Chris@18 347 ['destination_key_1', NULL],
Chris@18 348 ['@shared_key_2', NULL],
Chris@18 349 ['@source_key_1', NULL],
Chris@18 350 ['random_source_key', NULL],
Chris@18 351 ['@random_destination_key', NULL],
Chris@18 352 ];
Chris@18 353 }
Chris@18 354
Chris@18 355 /**
Chris@18 356 * Test getting multiple source and destination properties.
Chris@18 357 *
Chris@18 358 * @param array $keys
Chris@18 359 * An array of keys to look up.
Chris@18 360 * @param array $expected_values
Chris@18 361 * An array of expected values.
Chris@18 362 *
Chris@18 363 * @covers::getMultiple
Chris@18 364 * @dataProvider getMultipleDataProvider
Chris@18 365 */
Chris@18 366 public function testGetMultiple(array $keys, array $expected_values) {
Chris@18 367 $row = $this->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
Chris@18 368 $this->assertArrayEquals(array_combine($keys, $expected_values), $row->getMultiple($keys));
Chris@18 369 }
Chris@18 370
Chris@18 371 /**
Chris@18 372 * Data Provider for testGetMultiple.
Chris@18 373 *
Chris@18 374 * @return array
Chris@18 375 * The keys and expected values.
Chris@18 376 */
Chris@18 377 public function getMultipleDataProvider() {
Chris@18 378 return [
Chris@18 379 'Single Key' => [
Chris@18 380 'keys' => ['source_key_1'],
Chris@18 381 'values' => ['source_value_1'],
Chris@18 382 ],
Chris@18 383 'All Source Keys' => [
Chris@18 384 'keys' => [
Chris@18 385 'source_key_1',
Chris@18 386 'source_key_2',
Chris@18 387 '@@source_key_3',
Chris@18 388 ],
Chris@18 389 'values' => [
Chris@18 390 'source_value_1',
Chris@18 391 'source_value_2',
Chris@18 392 'source_value_3',
Chris@18 393 ],
Chris@18 394 ],
Chris@18 395 'All Destination Keys' => [
Chris@18 396 'keys' => [
Chris@18 397 '@destination_key_1',
Chris@18 398 '@destination_key_2',
Chris@18 399 '@@@destination_key_3',
Chris@18 400 ],
Chris@18 401 'values' => [
Chris@18 402 'destination_value_1',
Chris@18 403 'destination_value_2',
Chris@18 404 'destination_value_3',
Chris@18 405 ],
Chris@18 406 ],
Chris@18 407 'Mix of keys including non-existant' => [
Chris@18 408 'keys' => [
Chris@18 409 'shared_key_1',
Chris@18 410 '@shared_key_1',
Chris@18 411 '@@shared_key_2',
Chris@18 412 '@@@shared_key_2',
Chris@18 413 '@@@@@@@@@shared_key_3',
Chris@18 414 'non_existant_source_key',
Chris@18 415 '@non_existant_destination_key',
Chris@18 416 ],
Chris@18 417 'values' => [
Chris@18 418 'source_shared_value_1',
Chris@18 419 'destination_shared_value_1',
Chris@18 420 'source_shared_value_2',
Chris@18 421 'destination_shared_value_2',
Chris@18 422 'destination_shared_value_3',
Chris@18 423 NULL,
Chris@18 424 NULL,
Chris@18 425 ],
Chris@18 426 ],
Chris@18 427 ];
Chris@18 428 }
Chris@18 429
Chris@18 430 /**
Chris@18 431 * Create a row and load it with destination properties.
Chris@18 432 *
Chris@18 433 * @param array $source_properties
Chris@18 434 * The source property array.
Chris@18 435 * @param array $source_ids
Chris@18 436 * The source ids array.
Chris@18 437 * @param array $destination_properties
Chris@18 438 * The destination properties to load.
Chris@18 439 * @param bool $is_stub
Chris@18 440 * Whether this row is a stub row, defaults to FALSE.
Chris@18 441 *
Chris@18 442 * @return \Drupal\migrate\Row
Chris@18 443 * The row, populated with destination properties.
Chris@18 444 */
Chris@18 445 protected function createRowWithDestinationProperties(array $source_properties, array $source_ids, array $destination_properties, $is_stub = FALSE) {
Chris@18 446 $row = new Row($source_properties, $source_ids, $is_stub);
Chris@18 447 foreach ($destination_properties as $key => $property) {
Chris@18 448 $row->setDestinationProperty($key, $property);
Chris@18 449 }
Chris@18 450 return $row;
Chris@18 451 }
Chris@18 452
Chris@0 453 }