3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\Explode;
7 use Drupal\migrate\Plugin\migrate\process\Concat;
10 * Tests the Explode process plugin.
14 class ExplodeTest extends MigrateProcessTestCase {
19 protected function setUp() {
23 $this->plugin = new Explode($configuration, 'map', []);
28 * Test explode transform process works.
30 public function testTransform() {
31 $value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
32 $this->assertSame($value, ['foo', 'bar', 'tik']);
36 * Test explode transform process works with a limit.
38 public function testTransformLimit() {
39 $plugin = new Explode(['delimiter' => '_', 'limit' => 2], 'map', []);
40 $value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destinationproperty');
41 $this->assertSame($value, ['foo', 'bar_tik']);
45 * Test if the explode process can be chained with a handles_multiple process.
47 public function testChainedTransform() {
48 $exploded = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
50 $concat = new Concat([], 'map', []);
51 $concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destinationproperty');
52 $this->assertSame($concatenated, 'foobartik');
56 * Test explode fails properly on non-strings.
58 public function testExplodeWithNonString() {
59 $this->setExpectedException(MigrateException::class, 'is not a string');
60 $this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
64 * Tests that explode works on non-strings but with strict set to FALSE.
66 * @dataProvider providerExplodeWithNonStrictAndEmptySource
68 public function testExplodeWithNonStrictAndEmptySource($value, $expected) {
69 $plugin = new Explode(['delimiter' => '|', 'strict' => FALSE], 'map', []);
71 $processed = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
72 $this->assertSame($expected, $processed);
76 * Data provider for ::testExplodeWithNonStrictAndEmptySource().
78 public function providerExplodeWithNonStrictAndEmptySource() {
80 'normal_string' => ['a|b|c', ['a', 'b', 'c']],
81 'integer_cast_to_string' => [123, ['123']],
82 'zero_integer_cast_to_string' => [0, ['0']],
83 'true_cast_to_string' => [TRUE, ['1']],
84 'null_empty_array' => [NULL, []],
85 'false_empty_array' => [FALSE, []],
86 'empty_string_empty_array' => ['', []],
91 * Tests that explode raises an exception when the value cannot be casted to
94 public function testExplodeWithNonStrictAndNonCastable() {
95 $plugin = new Explode(['delimiter' => '|', 'strict' => FALSE], 'map', []);
96 $this->setExpectedException(MigrateException::class, 'cannot be casted to a string');
97 $processed = $plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
98 $this->assertSame(['foo'], $processed);
102 * Tests that explode with an empty string and strict check returns a
105 public function testExplodeWithStrictAndEmptyString() {
106 $plugin = new Explode(['delimiter' => '|'], 'map', []);
107 $processed = $plugin->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
108 $this->assertSame([''], $processed);
112 * Test explode fails with empty delimiter.
114 public function testExplodeWithEmptyDelimiter() {
115 $this->setExpectedException(MigrateException::class, 'delimiter is empty');
116 $plugin = new Explode(['delimiter' => ''], 'map', []);
117 $plugin->transform('foo,bar', $this->migrateExecutable, $this->row, 'destinationproperty');