Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / SubstrTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\Substr;
7
8 /**
9  * Tests the substr plugin.
10  *
11  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Substr
12  *
13  * @group migrate
14  */
15 class SubstrTest extends MigrateProcessTestCase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setUp() {
21     parent::setUp();
22   }
23
24   /**
25    * Tests Substr plugin based on providerTestSubstr() values.
26    *
27    * @dataProvider providerTestSubstr
28    */
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);
35   }
36
37   /**
38    * Data provider for testSubstr().
39    */
40   public function providerTestSubstr() {
41     return [
42       // Tests with valid start and length values.
43       [0, 7, 'Captain'],
44       // Tests with valid start > 0 and valid length.
45       [6, 3, 'n J'],
46       // Tests with valid start < 0 and valid length.
47       [-7, 4, 'Jane'],
48       // Tests without start value and valid length value.
49       [NULL, 7, 'Captain'],
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'],
54     ];
55   }
56
57   /**
58    * Tests invalid input type.
59    */
60   public function testSubstrFail() {
61     $configuration = [];
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');
65   }
66
67   /**
68    * Tests that the start parameter is an integer.
69    */
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');
75   }
76
77   /**
78    * Tests that the length parameter is an integer.
79    */
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');
85   }
86
87 }