Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / RowTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit;
4
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6 use Drupal\migrate\Row;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\migrate\Row
11  * @group migrate
12  */
13 class RowTest extends UnitTestCase {
14
15   /**
16    * The source IDs.
17    *
18    * @var array
19    */
20   protected $testSourceIds = [
21     'nid' => 'Node ID',
22   ];
23
24   /**
25    * The test values.
26    *
27    * @var array
28    */
29   protected $testValues = [
30     'nid' => 1,
31     'title' => 'node 1',
32   ];
33
34   /**
35    * The test hash.
36    *
37    * @var string
38    */
39   protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
40
41   /**
42    * The test hash after changing title value to 'new title'.
43    *
44    * @var string
45    */
46   protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
47
48   /**
49    * Tests object creation: empty.
50    */
51   public function testRowWithoutData() {
52     $row = new Row();
53     $this->assertSame([], $row->getSource(), 'Empty row');
54   }
55
56   /**
57    * Tests object creation: basic.
58    */
59   public function testRowWithBasicData() {
60     $row = new Row($this->testValues, $this->testSourceIds);
61     $this->assertSame($this->testValues, $row->getSource(), 'Row with data, simple id.');
62   }
63
64   /**
65    * Tests object creation: multiple source IDs.
66    */
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.');
72   }
73
74   /**
75    * Tests object creation: invalid values.
76    */
77   public function testRowWithInvalidData() {
78     $invalid_values = [
79       'title' => 'node X',
80     ];
81     $this->setExpectedException(\Exception::class);
82     $row = new Row($invalid_values, $this->testSourceIds);
83   }
84
85   /**
86    * Tests source immutability after freeze.
87    */
88   public function testSourceFreeze() {
89     $row = new Row($this->testValues, $this->testSourceIds);
90     $row->rehash();
91     $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
92     $row->setSourceProperty('title', 'new title');
93     $row->rehash();
94     $this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
95     $row->freezeSource();
96     $this->setExpectedException(\Exception::class);
97     $row->setSourceProperty('title', 'new title');
98   }
99
100   /**
101    * Tests setting on a frozen row.
102    */
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');
108   }
109
110   /**
111    * Tests hashing.
112    */
113   public function testHashing() {
114     $row = new Row($this->testValues, $this->testSourceIds);
115     $this->assertSame('', $row->getHash(), 'No hash at creation');
116     $row->rehash();
117     $this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
118     $row->rehash();
119     $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.');
120
121     // Set the map to needs update.
122     $test_id_map = [
123       'original_hash' => '',
124       'hash' => '',
125       'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
126     ];
127     $row->setIdMap($test_id_map);
128     $this->assertTrue($row->needsUpdate());
129
130     $row->rehash();
131     $this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.');
132     $row->setSourceProperty('title', 'new title');
133     $row->rehash();
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()));
140
141     // Set the map to successfully imported.
142     $test_id_map = [
143       'original_hash' => '',
144       'hash' => '',
145       'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
146     ];
147     $row->setIdMap($test_id_map);
148     $this->assertFalse($row->needsUpdate());
149
150     // Set the same hash value and ensure it was not changed.
151     $random = $this->randomMachineName();
152     $test_id_map = [
153       'original_hash' => $random,
154       'hash' => $random,
155       'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
156     ];
157     $row->setIdMap($test_id_map);
158     $this->assertFalse($row->changed());
159
160     // Set different has values to ensure it is marked as changed.
161     $test_id_map = [
162       'original_hash' => $this->randomMachineName(),
163       'hash' => $this->randomMachineName(),
164       'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
165     ];
166     $row->setIdMap($test_id_map);
167     $this->assertTrue($row->changed());
168   }
169
170   /**
171    * Tests getting/setting the ID Map.
172    *
173    * @covers ::setIdMap
174    * @covers ::getIdMap
175    */
176   public function testGetSetIdMap() {
177     $row = new Row($this->testValues, $this->testSourceIds);
178     $test_id_map = [
179       'original_hash' => '',
180       'hash' => '',
181       'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
182     ];
183     $row->setIdMap($test_id_map);
184     $this->assertEquals($test_id_map, $row->getIdMap());
185   }
186
187   /**
188    * Tests the source ID.
189    */
190   public function testSourceIdValues() {
191     $row = new Row($this->testValues, $this->testSourceIds);
192     $this->assertSame(['nid' => $this->testValues['nid']], $row->getSourceIdValues());
193   }
194
195   /**
196    * Tests the multiple source IDs.
197    */
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',
204       ];
205     $multi_source_ids_values = $this->testValues + [
206         'vid' => 1,
207         'type' => 'page',
208         'langcode' => 'en',
209       ];
210     $row = new Row($multi_source_ids_values, $multi_source_ids);
211     $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
212
213     // Set values in different order.
214     $multi_source_ids = $this->testSourceIds + [
215         'vid' => 'Node revision',
216         'type' => 'Node type',
217         'langcode' => 'Node language',
218       ];
219     $multi_source_ids_values = $this->testValues + [
220         'langcode' => 'en',
221         'type' => 'page',
222         'vid' => 1,
223       ];
224     $row = new Row($multi_source_ids_values, $multi_source_ids);
225     $this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
226   }
227
228   /**
229    * Tests getting the source property.
230    *
231    * @covers ::getSourceProperty
232    */
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'));
238   }
239
240   /**
241    * Tests setting and getting the destination.
242    */
243   public function testDestination() {
244     $row = new Row($this->testValues, $this->testSourceIds);
245     $this->assertEmpty($row->getDestination());
246     $this->assertFalse($row->hasDestinationProperty('nid'));
247
248     // Set a destination.
249     $row->setDestinationProperty('nid', 2);
250     $this->assertTrue($row->hasDestinationProperty('nid'));
251     $this->assertEquals(['nid' => 2], $row->getDestination());
252   }
253
254   /**
255    * Tests setting/getting multiple destination IDs.
256    */
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);
262
263     $this->assertTrue($row->hasDestinationProperty('image'));
264     $this->assertFalse($row->hasDestinationProperty('alt'));
265     $this->assertFalse($row->hasDestinationProperty('fid'));
266
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'));
272   }
273
274 }