f2124a6db3693919505cd0306839a27400eb8693
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / IteratorTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\migrate\Plugin\migrate\process\Get;
7 use Drupal\migrate\Plugin\migrate\process\Iterator;
8 use Drupal\migrate\Row;
9 use Drupal\Tests\migrate\Unit\MigrateTestCase;
10
11 /**
12  * Tests the iterator process plugin.
13  *
14  * @group migrate
15  */
16 class IteratorTest extends MigrateTestCase {
17
18   /**
19    * The iterator plugin being tested.
20    *
21    * @var \Drupal\migrate\Plugin\migrate\process\Iterator
22    */
23   protected $plugin;
24
25   /**
26    * @var array
27    */
28   protected $migrationConfiguration = [
29     'id' => 'test',
30   ];
31
32   /**
33    * Tests the iterator process plugin.
34    *
35    * @group legacy
36    * @expectedDeprecation The Drupal\migrate\Plugin\migrate\process\Iterator is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.0. Instead, use Drupal\migrate\Plugin\migrate\process\SubProcess
37    */
38   public function testIterator() {
39     $migration = $this->getMigration();
40     // Set up the properties for the iterator.
41     $configuration = [
42       'process' => [
43         'foo' => 'source_foo',
44         'id' => 'source_id',
45       ],
46       'key' => '@id',
47     ];
48     $plugin = new Iterator($configuration, 'iterator', []);
49     // Manually create the plugins. Migration::getProcessPlugins does this
50     // normally but the plugin system is not available.
51     foreach ($configuration['process'] as $destination => $source) {
52       $iterator_plugins[$destination][] = new Get(['source' => $source], 'get', []);
53     }
54     $migration->expects($this->at(1))
55       ->method('getProcessPlugins')
56       ->will($this->returnValue($iterator_plugins));
57     // Set up the key plugins.
58     $key_plugin['key'][] = new Get(['source' => '@id'], 'get', []);
59     $migration->expects($this->at(2))
60       ->method('getProcessPlugins')
61       ->will($this->returnValue($key_plugin));
62     $event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
63     $migrate_executable = new MigrateExecutable($migration, $this->getMock('Drupal\migrate\MigrateMessageInterface'), $event_dispatcher);
64
65     // The current value of the pipeline.
66     $current_value = [
67       [
68         'source_foo' => 'test',
69         'source_id' => 42,
70       ],
71     ];
72     // This is not used but the interface requires it, so create an empty row.
73     $row = new Row();
74
75     // After transformation, check to make sure that source_foo and source_id's
76     // values ended up in the proper destinations, and that the value of the
77     // key (@id) is the same as the destination ID (42).
78     $new_value = $plugin->transform($current_value, $migrate_executable, $row, 'test');
79     $this->assertSame(1, count($new_value));
80     $this->assertSame(2, count($new_value[42]));
81     $this->assertSame('test', $new_value[42]['foo']);
82     $this->assertSame(42, $new_value[42]['id']);
83   }
84
85 }