8c747abadf5cba52307870adb346540f67c10342
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / process / ExtractTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel\process;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate\MigrateExecutable;
7 use Drupal\migrate\MigrateMessage;
8 use Drupal\migrate\Plugin\MigrationInterface;
9
10 /**
11  * Tests the extract process plugin.
12  *
13  * @group migrate
14  */
15 class ExtractTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['migrate'];
21
22   /**
23    * Returns test migration definition.
24    *
25    * @return array
26    */
27   public function getDefinition() {
28     return [
29       'source' => [
30         'plugin' => 'embedded_data',
31         'data_rows' => [],
32         'ids' => [
33           'id' => ['type' => 'string'],
34         ],
35       ],
36       'process' => [
37         'first' => [
38           'plugin' => 'extract',
39           'index' => [0],
40           'source' => 'simple_array',
41         ],
42         'second' => [
43           'plugin' => 'extract',
44           'index' => [1],
45           'source' => 'complex_array',
46         ],
47       ],
48       'destination' => [
49         'plugin' => 'config',
50         'config_name' => 'migrate_test.settings',
51       ],
52     ];
53   }
54
55   /**
56    * Tests multiple value handling.
57    *
58    * @dataProvider multipleValueProviderSource
59    *
60    * @param array $source_data
61    * @param array $expected_data
62    */
63   public function testMultipleValueExplode(array $source_data, array $expected_data) {
64     $definition = $this->getDefinition();
65     $definition['source']['data_rows'] = [$source_data];
66
67     $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
68
69     $executable = new MigrateExecutable($migration, new MigrateMessage());
70     $result = $executable->import();
71
72     // Migration needs to succeed before further assertions are made.
73     $this->assertSame(MigrationInterface::RESULT_COMPLETED, $result);
74
75     // Compare with expected data.
76     $this->assertEquals($expected_data, \Drupal::config('migrate_test.settings')->get());
77   }
78
79   /**
80    * Provides multiple source data for "extract" process plugin test.
81    */
82   public function multipleValueProviderSource() {
83     $tests = [
84       [
85         'source_data' => [
86           'id' => '1',
87           'simple_array' => ['alpha', 'beta'],
88           'complex_array' => [['alpha', 'beta'], ['psi', 'omega']],
89         ],
90         'expected_data' => [
91           'first' => 'alpha',
92           'second' => ['psi', 'omega'],
93         ],
94       ],
95       [
96         'source_data' => [
97           'id' => '2',
98           'simple_array' => ['one'],
99           'complex_array' => [0, 1],
100         ],
101         'expected_data' => [
102           'first' => 'one',
103           'second' => 1,
104         ],
105       ],
106     ];
107
108     return $tests;
109   }
110
111 }