Unverified Commit 4768d66d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3133516 by quietone, huzooka, phenaproxima, mikelutz: Make every...

Issue #3133516 by quietone, huzooka, phenaproxima, mikelutz: Make every migrate process plugin that provides 'default_value' be able to correctly handle 'NULL' default values

(cherry picked from commit ea0df4e8)
parent 6b971cef
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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 {
+77 −0
Original line number Diff line number Diff line
@@ -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,
        ],
        '',
      ],
    ];
  }

}
+38 −6
Original line number Diff line number Diff line
@@ -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,
      ],
    ];
  }

}