comparison core/modules/migrate/tests/src/Unit/RowTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
27 * @var array 27 * @var array
28 */ 28 */
29 protected $testValues = [ 29 protected $testValues = [
30 'nid' => 1, 30 'nid' => 1,
31 'title' => 'node 1', 31 'title' => 'node 1',
32 ];
33
34 /**
35 * Test source properties for testing get and getMultiple.
36 *
37 * @var array
38 */
39 protected $testGetSourceProperties = [
40 'source_key_1' => 'source_value_1',
41 'source_key_2' => 'source_value_2',
42 '@source_key_3' => 'source_value_3',
43 'shared_key_1' => 'source_shared_value_1',
44 '@shared_key_2' => 'source_shared_value_2',
45 '@@@@shared_key_3' => 'source_shared_value_3',
46 ];
47
48 /**
49 * Test source keys for testing get and getMultiple.
50 *
51 * @var array
52 */
53 protected $testGetSourceIds = [
54 'source_key_1' => [],
55 ];
56
57 /**
58 * Test destination properties for testing get and getMultiple.
59 *
60 * @var array
61 */
62 protected $testGetDestinationProperties = [
63 'destination_key_1' => 'destination_value_1',
64 'destination_key_2' => 'destination_value_2',
65 '@destination_key_3' => 'destination_value_3',
66 'shared_key_1' => 'destination_shared_value_1',
67 '@shared_key_2' => 'destination_shared_value_2',
68 '@@@@shared_key_3' => 'destination_shared_value_3',
32 ]; 69 ];
33 70
34 /** 71 /**
35 * The test hash. 72 * The test hash.
36 * 73 *
269 $this->assertEquals(3, $destination['image']['fid']); 306 $this->assertEquals(3, $destination['image']['fid']);
270 $this->assertEquals('alt text', $row->getDestinationProperty('image/alt')); 307 $this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
271 $this->assertEquals(3, $row->getDestinationProperty('image/fid')); 308 $this->assertEquals(3, $row->getDestinationProperty('image/fid'));
272 } 309 }
273 310
311 /**
312 * Test getting source and destination properties.
313 *
314 * @param string $key
315 * The key to look up.
316 * @param string $expected_value
317 * The expected value.
318 *
319 * @dataProvider getDataProvider
320 * @covers ::get
321 */
322 public function testGet($key, $expected_value) {
323 $row = $this->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
324 $this->assertSame($expected_value, $row->get($key));
325 }
326
327 /**
328 * Data Provider for testGet.
329 *
330 * @return array
331 * The keys and expected values.
332 */
333 public function getDataProvider() {
334 return [
335 ['source_key_1', 'source_value_1'],
336 ['source_key_2', 'source_value_2'],
337 ['@@source_key_3', 'source_value_3'],
338 ['shared_key_1', 'source_shared_value_1'],
339 ['@@shared_key_2', 'source_shared_value_2'],
340 ['@@@@@@@@shared_key_3', 'source_shared_value_3'],
341 ['@destination_key_1', 'destination_value_1'],
342 ['@destination_key_2', 'destination_value_2'],
343 ['@@@destination_key_3', 'destination_value_3'],
344 ['@shared_key_1', 'destination_shared_value_1'],
345 ['@@@shared_key_2', 'destination_shared_value_2'],
346 ['@@@@@@@@@shared_key_3', 'destination_shared_value_3'],
347 ['destination_key_1', NULL],
348 ['@shared_key_2', NULL],
349 ['@source_key_1', NULL],
350 ['random_source_key', NULL],
351 ['@random_destination_key', NULL],
352 ];
353 }
354
355 /**
356 * Test getting multiple source and destination properties.
357 *
358 * @param array $keys
359 * An array of keys to look up.
360 * @param array $expected_values
361 * An array of expected values.
362 *
363 * @covers::getMultiple
364 * @dataProvider getMultipleDataProvider
365 */
366 public function testGetMultiple(array $keys, array $expected_values) {
367 $row = $this->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
368 $this->assertArrayEquals(array_combine($keys, $expected_values), $row->getMultiple($keys));
369 }
370
371 /**
372 * Data Provider for testGetMultiple.
373 *
374 * @return array
375 * The keys and expected values.
376 */
377 public function getMultipleDataProvider() {
378 return [
379 'Single Key' => [
380 'keys' => ['source_key_1'],
381 'values' => ['source_value_1'],
382 ],
383 'All Source Keys' => [
384 'keys' => [
385 'source_key_1',
386 'source_key_2',
387 '@@source_key_3',
388 ],
389 'values' => [
390 'source_value_1',
391 'source_value_2',
392 'source_value_3',
393 ],
394 ],
395 'All Destination Keys' => [
396 'keys' => [
397 '@destination_key_1',
398 '@destination_key_2',
399 '@@@destination_key_3',
400 ],
401 'values' => [
402 'destination_value_1',
403 'destination_value_2',
404 'destination_value_3',
405 ],
406 ],
407 'Mix of keys including non-existant' => [
408 'keys' => [
409 'shared_key_1',
410 '@shared_key_1',
411 '@@shared_key_2',
412 '@@@shared_key_2',
413 '@@@@@@@@@shared_key_3',
414 'non_existant_source_key',
415 '@non_existant_destination_key',
416 ],
417 'values' => [
418 'source_shared_value_1',
419 'destination_shared_value_1',
420 'source_shared_value_2',
421 'destination_shared_value_2',
422 'destination_shared_value_3',
423 NULL,
424 NULL,
425 ],
426 ],
427 ];
428 }
429
430 /**
431 * Create a row and load it with destination properties.
432 *
433 * @param array $source_properties
434 * The source property array.
435 * @param array $source_ids
436 * The source ids array.
437 * @param array $destination_properties
438 * The destination properties to load.
439 * @param bool $is_stub
440 * Whether this row is a stub row, defaults to FALSE.
441 *
442 * @return \Drupal\migrate\Row
443 * The row, populated with destination properties.
444 */
445 protected function createRowWithDestinationProperties(array $source_properties, array $source_ids, array $destination_properties, $is_stub = FALSE) {
446 $row = new Row($source_properties, $source_ids, $is_stub);
447 foreach ($destination_properties as $key => $property) {
448 $row->setDestinationProperty($key, $property);
449 }
450 return $row;
451 }
452
274 } 453 }