View source
<?php
namespace Drupal\Tests\migrate\Unit;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
class RowTest extends UnitTestCase {
protected $testSourceIds = [
'nid' => 'Node ID',
];
protected $testValues = [
'nid' => 1,
'title' => 'node 1',
];
protected $testGetSourceProperties = [
'source_key_1' => 'source_value_1',
'source_key_2' => 'source_value_2',
'@source_key_3' => 'source_value_3',
'shared_key_1' => 'source_shared_value_1',
'@shared_key_2' => 'source_shared_value_2',
'@@@@shared_key_3' => 'source_shared_value_3',
];
protected $testGetSourceIds = [
'source_key_1' => [],
];
protected $testGetDestinationProperties = [
'destination_key_1' => 'destination_value_1',
'destination_key_2' => 'destination_value_2',
'@destination_key_3' => 'destination_value_3',
'shared_key_1' => 'destination_shared_value_1',
'@shared_key_2' => 'destination_shared_value_2',
'@@@@shared_key_3' => 'destination_shared_value_3',
];
protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
public function testRowWithoutData() {
$row = new Row();
$this
->assertSame([], $row
->getSource(), 'Empty row');
}
public function testRowWithBasicData() {
$row = new Row($this->testValues, $this->testSourceIds);
$this
->assertSame($this->testValues, $row
->getSource(), 'Row with data, simple id.');
}
public function testRowWithMultipleSourceIds() {
$multi_source_ids = $this->testSourceIds + [
'vid' => 'Node revision',
];
$multi_source_ids_values = $this->testValues + [
'vid' => 1,
];
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this
->assertSame($multi_source_ids_values, $row
->getSource(), 'Row with data, multifield id.');
}
public function testRowWithInvalidData() {
$invalid_values = [
'title' => 'node X',
];
$this
->expectException(\Exception::class);
new Row($invalid_values, $this->testSourceIds);
}
public function testSourceFreeze() {
$row = new Row($this->testValues, $this->testSourceIds);
$row
->rehash();
$this
->assertSame($this->testHash, $row
->getHash(), 'Correct hash.');
$row
->setSourceProperty('title', 'new title');
$row
->rehash();
$this
->assertSame($this->testHashMod, $row
->getHash(), 'Hash changed correctly.');
$row
->freezeSource();
$this
->expectException(\Exception::class);
$row
->setSourceProperty('title', 'new title');
}
public function testSetFrozenRow() {
$row = new Row($this->testValues, $this->testSourceIds);
$row
->freezeSource();
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage("The source is frozen and can't be changed any more");
$row
->setSourceProperty('title', 'new title');
}
public function testHashing() {
$row = new Row($this->testValues, $this->testSourceIds);
$this
->assertSame('', $row
->getHash(), 'No hash at creation');
$row
->rehash();
$this
->assertSame($this->testHash, $row
->getHash(), 'Correct hash.');
$row
->rehash();
$this
->assertSame($this->testHash, $row
->getHash(), 'Correct hash even doing it twice.');
$test_id_map = [
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
];
$row
->setIdMap($test_id_map);
$this
->assertTrue($row
->needsUpdate());
$row
->rehash();
$this
->assertSame($this->testHash, $row
->getHash(), 'Correct hash even if id_mpa have changed.');
$row
->setSourceProperty('title', 'new title');
$row
->rehash();
$this
->assertSame($this->testHashMod, $row
->getHash(), 'Hash changed correctly.');
$hash = hash('sha256', serialize($row
->getSource()));
$this
->assertSame($hash, $row
->getHash());
$this
->assertSame(64, strlen($row
->getHash()));
$test_id_map = [
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
];
$row
->setIdMap($test_id_map);
$this
->assertFalse($row
->needsUpdate());
$random = $this
->randomMachineName();
$test_id_map = [
'original_hash' => $random,
'hash' => $random,
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
];
$row
->setIdMap($test_id_map);
$this
->assertFalse($row
->changed());
$test_id_map = [
'original_hash' => $this
->randomMachineName(),
'hash' => $this
->randomMachineName(),
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
];
$row
->setIdMap($test_id_map);
$this
->assertTrue($row
->changed());
}
public function testGetSetIdMap() {
$row = new Row($this->testValues, $this->testSourceIds);
$test_id_map = [
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
];
$row
->setIdMap($test_id_map);
$this
->assertEquals($test_id_map, $row
->getIdMap());
}
public function testSourceIdValues() {
$row = new Row($this->testValues, $this->testSourceIds);
$this
->assertSame([
'nid' => $this->testValues['nid'],
], $row
->getSourceIdValues());
}
public function testMultipleSourceIdValues() {
$multi_source_ids = $this->testSourceIds + [
'vid' => 'Node revision',
'type' => 'Node type',
'langcode' => 'Node language',
];
$multi_source_ids_values = $this->testValues + [
'vid' => 1,
'type' => 'page',
'langcode' => 'en',
];
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this
->assertSame(array_keys($multi_source_ids), array_keys($row
->getSourceIdValues()));
$multi_source_ids = $this->testSourceIds + [
'vid' => 'Node revision',
'type' => 'Node type',
'langcode' => 'Node language',
];
$multi_source_ids_values = $this->testValues + [
'langcode' => 'en',
'type' => 'page',
'vid' => 1,
];
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this
->assertSame(array_keys($multi_source_ids), array_keys($row
->getSourceIdValues()));
}
public function testGetSourceProperty() {
$row = new Row($this->testValues, $this->testSourceIds);
$this
->assertSame($this->testValues['nid'], $row
->getSourceProperty('nid'));
$this
->assertSame($this->testValues['title'], $row
->getSourceProperty('title'));
$this
->assertNull($row
->getSourceProperty('non_existing'));
}
public function testDestination() {
$row = new Row($this->testValues, $this->testSourceIds);
$this
->assertEmpty($row
->getDestination());
$this
->assertFalse($row
->hasDestinationProperty('nid'));
$row
->setDestinationProperty('nid', 2);
$this
->assertTrue($row
->hasDestinationProperty('nid'));
$this
->assertEquals([
'nid' => 2,
], $row
->getDestination());
}
public function testMultipleDestination() {
$row = new Row($this->testValues, $this->testSourceIds);
$row
->setDestinationProperty('image/alt', 'alt text');
$row
->setDestinationProperty('image/fid', 3);
$this
->assertTrue($row
->hasDestinationProperty('image'));
$this
->assertFalse($row
->hasDestinationProperty('alt'));
$this
->assertFalse($row
->hasDestinationProperty('fid'));
$destination = $row
->getDestination();
$this
->assertEquals('alt text', $destination['image']['alt']);
$this
->assertEquals(3, $destination['image']['fid']);
$this
->assertEquals('alt text', $row
->getDestinationProperty('image/alt'));
$this
->assertEquals(3, $row
->getDestinationProperty('image/fid'));
}
public function testGet($key, $expected_value) {
$row = $this
->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
$this
->assertSame($expected_value, $row
->get($key));
}
public function getDataProvider() {
return [
[
'source_key_1',
'source_value_1',
],
[
'source_key_2',
'source_value_2',
],
[
'@@source_key_3',
'source_value_3',
],
[
'shared_key_1',
'source_shared_value_1',
],
[
'@@shared_key_2',
'source_shared_value_2',
],
[
'@@@@@@@@shared_key_3',
'source_shared_value_3',
],
[
'@destination_key_1',
'destination_value_1',
],
[
'@destination_key_2',
'destination_value_2',
],
[
'@@@destination_key_3',
'destination_value_3',
],
[
'@shared_key_1',
'destination_shared_value_1',
],
[
'@@@shared_key_2',
'destination_shared_value_2',
],
[
'@@@@@@@@@shared_key_3',
'destination_shared_value_3',
],
[
'destination_key_1',
NULL,
],
[
'@shared_key_2',
NULL,
],
[
'@source_key_1',
NULL,
],
[
'random_source_key',
NULL,
],
[
'@random_destination_key',
NULL,
],
];
}
public function testGetMultiple(array $keys, array $expected_values) {
$row = $this
->createRowWithDestinationProperties($this->testGetSourceProperties, $this->testGetSourceIds, $this->testGetDestinationProperties);
$this
->assertEquals(array_combine($keys, $expected_values), $row
->getMultiple($keys));
}
public function getMultipleDataProvider() {
return [
'Single Key' => [
'keys' => [
'source_key_1',
],
'values' => [
'source_value_1',
],
],
'All Source Keys' => [
'keys' => [
'source_key_1',
'source_key_2',
'@@source_key_3',
],
'values' => [
'source_value_1',
'source_value_2',
'source_value_3',
],
],
'All Destination Keys' => [
'keys' => [
'@destination_key_1',
'@destination_key_2',
'@@@destination_key_3',
],
'values' => [
'destination_value_1',
'destination_value_2',
'destination_value_3',
],
],
'Mix of keys including non-existent' => [
'keys' => [
'shared_key_1',
'@shared_key_1',
'@@shared_key_2',
'@@@shared_key_2',
'@@@@@@@@@shared_key_3',
'non_existent_source_key',
'@non_existent_destination_key',
],
'values' => [
'source_shared_value_1',
'destination_shared_value_1',
'source_shared_value_2',
'destination_shared_value_2',
'destination_shared_value_3',
NULL,
NULL,
],
],
];
}
protected function createRowWithDestinationProperties(array $source_properties, array $source_ids, array $destination_properties, $is_stub = FALSE) {
$row = new Row($source_properties, $source_ids, $is_stub);
foreach ($destination_properties as $key => $property) {
$row
->setDestinationProperty($key, $property);
}
return $row;
}
}