Loading core/modules/migrate/src/Plugin/migrate/process/Extract.php +1 −1 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable } $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists); if (!$key_exists) { if (isset($this->configuration['default'])) { if (array_key_exists('default', $this->configuration)) { $new_value = $this->configuration['default']; } else { Loading core/modules/migrate/tests/src/Unit/process/ExtractTest.php +77 −0 Original line number Diff line number Diff line Loading @@ -55,4 +55,81 @@ public function testExtractFailDefault() { $this->assertSame('test', $value, ''); } /** * Test the extract plugin with default values. * * @param array $value * The process plugin input value. * @param array $configuration * The plugin configuration. * @param string|null $expected * The expected transformed value. * * @throws \Drupal\migrate\MigrateException * * @dataProvider providerExtractDefault */ public function testExtractDefault(array $value, array $configuration, $expected) { $this->plugin = new Extract($configuration, 'map', []); $value = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame($expected, $value); } /** * Data provider for testExtractDefault. */ public function providerExtractDefault() { return [ [ ['foo' => 'bar'], [ 'index' => ['foo'], 'default' => 'one', ], 'bar', ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => 'two', ], 'two', ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => NULL, ], NULL, ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => TRUE, ], TRUE, ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => FALSE, ], FALSE, ], [ ['foo' => ''], [ 'index' => ['foo'], 'default' => NULL, ], '', ], ]; } } core/modules/migrate/tests/src/Unit/process/NullCoalesceTest.php +38 −6 Original line number Diff line number Diff line Loading @@ -77,16 +77,48 @@ public function transformDataProvider() { } /** * Tests null_coalesce with default value. * Tests null_coalesce. * * @param array $source * The source value. * @param string $default_value * The default value. * @param mixed $expected_result * The expected result. * * @covers ::transform * * @dataProvider transformWithDefaultProvider * * @throws \Drupal\migrate\MigrateException */ public function testTransformWithDefault() { $plugin = new NullCoalesce(['default_value' => 'default'], 'null_coalesce', []); $result = $plugin->transform([NULL, NULL, 'Test', 'Test 2'], $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame('Test', $result); public function testTransformWithDefault(array $source, $default_value, $expected_result) { $plugin = new NullCoalesce(['default_value' => $default_value], 'null_coalesce', []); $result = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame($expected_result, $result); } $this->assertSame('default', $plugin->transform([NULL, NULL], $this->migrateExecutable, $this->row, 'destinationproperty')); /** * Provides Data for ::testTransformWithDefault. */ public function transformWithDefaultProvider() { return [ 'default not used' => [ 'source' => [NULL, NULL, 'Test', 'Test 2'], 'default_value' => 'default', 'expected_result' => 'Test', ], 'default string' => [ 'source' => [NULL, NULL], 'default_value' => 'default', 'expected_result' => 'default', ], 'default NULL' => [ 'source' => [NULL, NULL], 'default_value' => NULL, 'expected_result' => NULL, ], ]; } } Loading
core/modules/migrate/src/Plugin/migrate/process/Extract.php +1 −1 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable } $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists); if (!$key_exists) { if (isset($this->configuration['default'])) { if (array_key_exists('default', $this->configuration)) { $new_value = $this->configuration['default']; } else { Loading
core/modules/migrate/tests/src/Unit/process/ExtractTest.php +77 −0 Original line number Diff line number Diff line Loading @@ -55,4 +55,81 @@ public function testExtractFailDefault() { $this->assertSame('test', $value, ''); } /** * Test the extract plugin with default values. * * @param array $value * The process plugin input value. * @param array $configuration * The plugin configuration. * @param string|null $expected * The expected transformed value. * * @throws \Drupal\migrate\MigrateException * * @dataProvider providerExtractDefault */ public function testExtractDefault(array $value, array $configuration, $expected) { $this->plugin = new Extract($configuration, 'map', []); $value = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame($expected, $value); } /** * Data provider for testExtractDefault. */ public function providerExtractDefault() { return [ [ ['foo' => 'bar'], [ 'index' => ['foo'], 'default' => 'one', ], 'bar', ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => 'two', ], 'two', ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => NULL, ], NULL, ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => TRUE, ], TRUE, ], [ ['foo' => 'bar'], [ 'index' => ['not_key'], 'default' => FALSE, ], FALSE, ], [ ['foo' => ''], [ 'index' => ['foo'], 'default' => NULL, ], '', ], ]; } }
core/modules/migrate/tests/src/Unit/process/NullCoalesceTest.php +38 −6 Original line number Diff line number Diff line Loading @@ -77,16 +77,48 @@ public function transformDataProvider() { } /** * Tests null_coalesce with default value. * Tests null_coalesce. * * @param array $source * The source value. * @param string $default_value * The default value. * @param mixed $expected_result * The expected result. * * @covers ::transform * * @dataProvider transformWithDefaultProvider * * @throws \Drupal\migrate\MigrateException */ public function testTransformWithDefault() { $plugin = new NullCoalesce(['default_value' => 'default'], 'null_coalesce', []); $result = $plugin->transform([NULL, NULL, 'Test', 'Test 2'], $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame('Test', $result); public function testTransformWithDefault(array $source, $default_value, $expected_result) { $plugin = new NullCoalesce(['default_value' => $default_value], 'null_coalesce', []); $result = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame($expected_result, $result); } $this->assertSame('default', $plugin->transform([NULL, NULL], $this->migrateExecutable, $this->row, 'destinationproperty')); /** * Provides Data for ::testTransformWithDefault. */ public function transformWithDefaultProvider() { return [ 'default not used' => [ 'source' => [NULL, NULL, 'Test', 'Test 2'], 'default_value' => 'default', 'expected_result' => 'Test', ], 'default string' => [ 'source' => [NULL, NULL], 'default_value' => 'default', 'expected_result' => 'default', ], 'default NULL' => [ 'source' => [NULL, NULL], 'default_value' => NULL, 'expected_result' => NULL, ], ]; } }