ExplodeTest.php 4.06 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\migrate\Unit\process;

5
use Drupal\migrate\MigrateException;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Drupal\migrate\Plugin\migrate\process\Explode;
use Drupal\migrate\Plugin\migrate\process\Concat;

/**
 * Tests the Explode process plugin.
 *
 * @group migrate
 */
class ExplodeTest extends MigrateProcessTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $configuration = [
      'delimiter' => ',',
    ];
    $this->plugin = new Explode($configuration, 'map', []);
    parent::setUp();
  }

  /**
   * Test explode transform process works.
   */
  public function testTransform() {
31
    $value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destination_property');
32
    $this->assertSame(['foo', 'bar', 'tik'], $value);
33 34 35 36 37 38 39
  }

  /**
   * Test explode transform process works with a limit.
   */
  public function testTransformLimit() {
    $plugin = new Explode(['delimiter' => '_', 'limit' => 2], 'map', []);
40
    $value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destination_property');
41
    $this->assertSame(['foo', 'bar_tik'], $value);
42 43 44 45 46 47
  }

  /**
   * Test if the explode process can be chained with a handles_multiple process.
   */
  public function testChainedTransform() {
48
    $exploded = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destination_property');
49 50

    $concat = new Concat([], 'map', []);
51
    $concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destination_property');
52
    $this->assertSame('foobartik', $concatenated);
53 54 55 56 57 58
  }

  /**
   * Test explode fails properly on non-strings.
   */
  public function testExplodeWithNonString() {
59 60
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('is not a string');
61
    $this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destination_property');
62 63 64
  }

  /**
65
   * Tests that explode works on non-strings but with strict set to FALSE.
66
   *
67 68 69 70 71
   * @dataProvider providerExplodeWithNonStrictAndEmptySource
   */
  public function testExplodeWithNonStrictAndEmptySource($value, $expected) {
    $plugin = new Explode(['delimiter' => '|', 'strict' => FALSE], 'map', []);

72
    $processed = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destination_property');
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    $this->assertSame($expected, $processed);
  }

  /**
   * Data provider for ::testExplodeWithNonStrictAndEmptySource().
   */
  public function providerExplodeWithNonStrictAndEmptySource() {
    return [
      'normal_string' => ['a|b|c', ['a', 'b', 'c']],
      'integer_cast_to_string' => [123, ['123']],
      'zero_integer_cast_to_string' => [0, ['0']],
      'true_cast_to_string' => [TRUE, ['1']],
      'null_empty_array' => [NULL, []],
      'false_empty_array' => [FALSE, []],
      'empty_string_empty_array' => ['', []],
    ];
  }

  /**
   * Tests that explode raises an exception when the value cannot be casted to
   * string.
   */
  public function testExplodeWithNonStrictAndNonCastable() {
    $plugin = new Explode(['delimiter' => '|', 'strict' => FALSE], 'map', []);
97 98
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('cannot be casted to a string');
99
    $processed = $plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destination_property');
100 101 102 103 104 105 106 107 108
    $this->assertSame(['foo'], $processed);
  }

  /**
   * Tests that explode with an empty string and strict check returns a
   * non-empty array.
   */
  public function testExplodeWithStrictAndEmptyString() {
    $plugin = new Explode(['delimiter' => '|'], 'map', []);
109
    $processed = $plugin->transform('', $this->migrateExecutable, $this->row, 'destination_property');
110 111 112 113 114
    $this->assertSame([''], $processed);
  }

  /**
   * Test explode fails with empty delimiter.
115 116
   */
  public function testExplodeWithEmptyDelimiter() {
117 118
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('delimiter is empty');
119
    $plugin = new Explode(['delimiter' => ''], 'map', []);
120
    $plugin->transform('foo,bar', $this->migrateExecutable, $this->row, 'destination_property');
121 122 123
  }

}