annotate core/modules/migrate/tests/src/Unit/RowTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
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@0 35 * The test hash.
Chris@0 36 *
Chris@0 37 * @var string
Chris@0 38 */
Chris@0 39 protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The test hash after changing title value to 'new title'.
Chris@0 43 *
Chris@0 44 * @var string
Chris@0 45 */
Chris@0 46 protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Tests object creation: empty.
Chris@0 50 */
Chris@0 51 public function testRowWithoutData() {
Chris@0 52 $row = new Row();
Chris@0 53 $this->assertSame([], $row->getSource(), 'Empty row');
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Tests object creation: basic.
Chris@0 58 */
Chris@0 59 public function testRowWithBasicData() {
Chris@0 60 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 61 $this->assertSame($this->testValues, $row->getSource(), 'Row with data, simple id.');
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Tests object creation: multiple source IDs.
Chris@0 66 */
Chris@0 67 public function testRowWithMultipleSourceIds() {
Chris@0 68 $multi_source_ids = $this->testSourceIds + ['vid' => 'Node revision'];
Chris@0 69 $multi_source_ids_values = $this->testValues + ['vid' => 1];
Chris@0 70 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 71 $this->assertSame($multi_source_ids_values, $row->getSource(), 'Row with data, multifield id.');
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Tests object creation: invalid values.
Chris@0 76 */
Chris@0 77 public function testRowWithInvalidData() {
Chris@0 78 $invalid_values = [
Chris@0 79 'title' => 'node X',
Chris@0 80 ];
Chris@0 81 $this->setExpectedException(\Exception::class);
Chris@0 82 $row = new Row($invalid_values, $this->testSourceIds);
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Tests source immutability after freeze.
Chris@0 87 */
Chris@0 88 public function testSourceFreeze() {
Chris@0 89 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 90 $row->rehash();
Chris@0 91 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
Chris@0 92 $row->setSourceProperty('title', 'new title');
Chris@0 93 $row->rehash();
Chris@0 94 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
Chris@0 95 $row->freezeSource();
Chris@0 96 $this->setExpectedException(\Exception::class);
Chris@0 97 $row->setSourceProperty('title', 'new title');
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * Tests setting on a frozen row.
Chris@0 102 */
Chris@0 103 public function testSetFrozenRow() {
Chris@0 104 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 105 $row->freezeSource();
Chris@0 106 $this->setExpectedException(\Exception::class, "The source is frozen and can't be changed any more");
Chris@0 107 $row->setSourceProperty('title', 'new title');
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * Tests hashing.
Chris@0 112 */
Chris@0 113 public function testHashing() {
Chris@0 114 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 115 $this->assertSame('', $row->getHash(), 'No hash at creation');
Chris@0 116 $row->rehash();
Chris@0 117 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
Chris@0 118 $row->rehash();
Chris@0 119 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.');
Chris@0 120
Chris@0 121 // Set the map to needs update.
Chris@0 122 $test_id_map = [
Chris@0 123 'original_hash' => '',
Chris@0 124 'hash' => '',
Chris@0 125 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 126 ];
Chris@0 127 $row->setIdMap($test_id_map);
Chris@0 128 $this->assertTrue($row->needsUpdate());
Chris@0 129
Chris@0 130 $row->rehash();
Chris@0 131 $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.');
Chris@0 132 $row->setSourceProperty('title', 'new title');
Chris@0 133 $row->rehash();
Chris@0 134 $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
Chris@0 135 // Check hash calculation algorithm.
Chris@0 136 $hash = hash('sha256', serialize($row->getSource()));
Chris@0 137 $this->assertSame($hash, $row->getHash());
Chris@0 138 // Check length of generated hash used for mapping schema.
Chris@0 139 $this->assertSame(64, strlen($row->getHash()));
Chris@0 140
Chris@0 141 // Set the map to successfully imported.
Chris@0 142 $test_id_map = [
Chris@0 143 'original_hash' => '',
Chris@0 144 'hash' => '',
Chris@0 145 'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
Chris@0 146 ];
Chris@0 147 $row->setIdMap($test_id_map);
Chris@0 148 $this->assertFalse($row->needsUpdate());
Chris@0 149
Chris@0 150 // Set the same hash value and ensure it was not changed.
Chris@0 151 $random = $this->randomMachineName();
Chris@0 152 $test_id_map = [
Chris@0 153 'original_hash' => $random,
Chris@0 154 'hash' => $random,
Chris@0 155 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 156 ];
Chris@0 157 $row->setIdMap($test_id_map);
Chris@0 158 $this->assertFalse($row->changed());
Chris@0 159
Chris@0 160 // Set different has values to ensure it is marked as changed.
Chris@0 161 $test_id_map = [
Chris@0 162 'original_hash' => $this->randomMachineName(),
Chris@0 163 'hash' => $this->randomMachineName(),
Chris@0 164 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 165 ];
Chris@0 166 $row->setIdMap($test_id_map);
Chris@0 167 $this->assertTrue($row->changed());
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Tests getting/setting the ID Map.
Chris@0 172 *
Chris@0 173 * @covers ::setIdMap
Chris@0 174 * @covers ::getIdMap
Chris@0 175 */
Chris@0 176 public function testGetSetIdMap() {
Chris@0 177 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 178 $test_id_map = [
Chris@0 179 'original_hash' => '',
Chris@0 180 'hash' => '',
Chris@0 181 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
Chris@0 182 ];
Chris@0 183 $row->setIdMap($test_id_map);
Chris@0 184 $this->assertEquals($test_id_map, $row->getIdMap());
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Tests the source ID.
Chris@0 189 */
Chris@0 190 public function testSourceIdValues() {
Chris@0 191 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 192 $this->assertSame(['nid' => $this->testValues['nid']], $row->getSourceIdValues());
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * Tests the multiple source IDs.
Chris@0 197 */
Chris@0 198 public function testMultipleSourceIdValues() {
Chris@0 199 // Set values in same order as ids.
Chris@0 200 $multi_source_ids = $this->testSourceIds + [
Chris@0 201 'vid' => 'Node revision',
Chris@0 202 'type' => 'Node type',
Chris@0 203 'langcode' => 'Node language',
Chris@0 204 ];
Chris@0 205 $multi_source_ids_values = $this->testValues + [
Chris@0 206 'vid' => 1,
Chris@0 207 'type' => 'page',
Chris@0 208 'langcode' => 'en',
Chris@0 209 ];
Chris@0 210 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 211 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
Chris@0 212
Chris@0 213 // Set values in different order.
Chris@0 214 $multi_source_ids = $this->testSourceIds + [
Chris@0 215 'vid' => 'Node revision',
Chris@0 216 'type' => 'Node type',
Chris@0 217 'langcode' => 'Node language',
Chris@0 218 ];
Chris@0 219 $multi_source_ids_values = $this->testValues + [
Chris@0 220 'langcode' => 'en',
Chris@0 221 'type' => 'page',
Chris@0 222 'vid' => 1,
Chris@0 223 ];
Chris@0 224 $row = new Row($multi_source_ids_values, $multi_source_ids);
Chris@0 225 $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
Chris@0 226 }
Chris@0 227
Chris@0 228 /**
Chris@0 229 * Tests getting the source property.
Chris@0 230 *
Chris@0 231 * @covers ::getSourceProperty
Chris@0 232 */
Chris@0 233 public function testGetSourceProperty() {
Chris@0 234 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 235 $this->assertSame($this->testValues['nid'], $row->getSourceProperty('nid'));
Chris@0 236 $this->assertSame($this->testValues['title'], $row->getSourceProperty('title'));
Chris@0 237 $this->assertNull($row->getSourceProperty('non_existing'));
Chris@0 238 }
Chris@0 239
Chris@0 240 /**
Chris@0 241 * Tests setting and getting the destination.
Chris@0 242 */
Chris@0 243 public function testDestination() {
Chris@0 244 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 245 $this->assertEmpty($row->getDestination());
Chris@0 246 $this->assertFalse($row->hasDestinationProperty('nid'));
Chris@0 247
Chris@0 248 // Set a destination.
Chris@0 249 $row->setDestinationProperty('nid', 2);
Chris@0 250 $this->assertTrue($row->hasDestinationProperty('nid'));
Chris@0 251 $this->assertEquals(['nid' => 2], $row->getDestination());
Chris@0 252 }
Chris@0 253
Chris@0 254 /**
Chris@0 255 * Tests setting/getting multiple destination IDs.
Chris@0 256 */
Chris@0 257 public function testMultipleDestination() {
Chris@0 258 $row = new Row($this->testValues, $this->testSourceIds);
Chris@0 259 // Set some deep nested values.
Chris@0 260 $row->setDestinationProperty('image/alt', 'alt text');
Chris@0 261 $row->setDestinationProperty('image/fid', 3);
Chris@0 262
Chris@0 263 $this->assertTrue($row->hasDestinationProperty('image'));
Chris@0 264 $this->assertFalse($row->hasDestinationProperty('alt'));
Chris@0 265 $this->assertFalse($row->hasDestinationProperty('fid'));
Chris@0 266
Chris@0 267 $destination = $row->getDestination();
Chris@0 268 $this->assertEquals('alt text', $destination['image']['alt']);
Chris@0 269 $this->assertEquals(3, $destination['image']['fid']);
Chris@0 270 $this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
Chris@0 271 $this->assertEquals(3, $row->getDestinationProperty('image/fid'));
Chris@0 272 }
Chris@0 273
Chris@0 274 }