3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\Substr;
9 * Tests the substr plugin.
11 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Substr
15 class SubstrTest extends MigrateProcessTestCase {
20 protected function setUp() {
25 * Tests Substr plugin based on providerTestSubstr() values.
27 * @dataProvider providerTestSubstr
29 public function testSubstr($start = NULL, $length = NULL, $expected = NULL) {
30 $configuration['start'] = $start;
31 $configuration['length'] = $length;
32 $this->plugin = new Substr($configuration, 'map', []);
33 $value = $this->plugin->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destinationproperty');
34 $this->assertSame($expected, $value);
38 * Data provider for testSubstr().
40 public function providerTestSubstr() {
42 // Tests with valid start and length values.
44 // Tests with valid start > 0 and valid length.
46 // Tests with valid start < 0 and valid length.
48 // Tests without start value and valid length value.
50 // Tests with valid start value and no length value.
51 [1, NULL, 'aptain Janeway'],
52 // Tests without both start and length values.
53 [NULL, NULL, 'Captain Janeway'],
58 * Tests invalid input type.
60 public function testSubstrFail() {
62 $this->plugin = new Substr($configuration, 'map', []);
63 $this->setExpectedException(MigrateException::class, 'The input value must be a string.');
64 $this->plugin->transform(['Captain Janeway'], $this->migrateExecutable, $this->row, 'destinationproperty');
68 * Tests that the start parameter is an integer.
70 public function testStartIsString() {
71 $configuration['start'] = '2';
72 $this->plugin = new Substr($configuration, 'map', []);
73 $this->setExpectedException(MigrateException::class, 'The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
74 $this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
78 * Tests that the length parameter is an integer.
80 public function testLengthIsString() {
81 $configuration['length'] = '1';
82 $this->plugin = new Substr($configuration, 'map', []);
83 $this->setExpectedException(MigrateException::class, 'The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
84 $this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');