20bf3a0311d1b9f9b124cb1c00f683a6c2a343a2
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / ExplodeTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\Explode;
7 use Drupal\migrate\Plugin\migrate\process\Concat;
8
9 /**
10  * Tests the Explode process plugin.
11  *
12  * @group migrate
13  */
14 class ExplodeTest extends MigrateProcessTestCase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     $configuration = [
21       'delimiter' => ',',
22     ];
23     $this->plugin = new Explode($configuration, 'map', []);
24     parent::setUp();
25   }
26
27   /**
28    * Test explode transform process works.
29    */
30   public function testTransform() {
31     $value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
32     $this->assertSame($value, ['foo', 'bar', 'tik']);
33   }
34
35   /**
36    * Test explode transform process works with a limit.
37    */
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']);
42   }
43
44   /**
45    * Test if the explode process can be chained with a handles_multiple process.
46    */
47   public function testChainedTransform() {
48     $exploded = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
49
50     $concat = new Concat([], 'map', []);
51     $concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destinationproperty');
52     $this->assertSame($concatenated, 'foobartik');
53   }
54
55   /**
56    * Test explode fails properly on non-strings.
57    */
58   public function testExplodeWithNonString() {
59     $this->setExpectedException(MigrateException::class, 'is not a string');
60     $this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
61   }
62
63   /**
64    * Tests that explode works on non-strings but with strict set to FALSE.
65    *
66    * @dataProvider providerExplodeWithNonStrictAndEmptySource
67    */
68   public function testExplodeWithNonStrictAndEmptySource($value, $expected) {
69     $plugin = new Explode(['delimiter' => '|', 'strict' => FALSE], 'map', []);
70
71     $processed = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
72     $this->assertSame($expected, $processed);
73   }
74
75   /**
76    * Data provider for ::testExplodeWithNonStrictAndEmptySource().
77    */
78   public function providerExplodeWithNonStrictAndEmptySource() {
79     return [
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' => ['', []],
87     ];
88   }
89
90   /**
91    * Tests that explode raises an exception when the value cannot be casted to
92    * string.
93    */
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);
99   }
100
101   /**
102    * Tests that explode with an empty string and strict check returns a
103    * non-empty array.
104    */
105   public function testExplodeWithStrictAndEmptyString() {
106     $plugin = new Explode(['delimiter' => '|'], 'map', []);
107     $processed = $plugin->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
108     $this->assertSame([''], $processed);
109   }
110
111   /**
112    * Test explode fails with empty delimiter.
113    */
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');
118   }
119
120 }