Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / migrate_plus / tests / src / Unit / process / StrReplaceTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_plus\Unit\process;
4
5 use Drupal\migrate_plus\Plugin\migrate\process\StrReplace;
6 use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
7
8 /**
9  * Tests the str replace process plugin.
10  *
11  * @group migrate
12  * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\StrReplace
13  */
14 class StrReplaceTest extends MigrateProcessTestCase {
15
16   /**
17    * Test for a simple str_replace string.
18    */
19   public function testStrReplace() {
20     $value = 'vero eos et accusam et justo vero';
21     $configuration['search'] = 'et';
22     $configuration['replace'] = 'that';
23     $plugin = new StrReplace($configuration, 'str_replace', []);
24     $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
25     $this->assertSame('vero eos that accusam that justo vero', $actual);
26
27   }
28
29   /**
30    * Test for case insensitive searches.
31    */
32   public function testStrIreplace() {
33     $value = 'VERO eos et accusam et justo vero';
34     $configuration['search'] = 'vero';
35     $configuration['replace'] = 'that';
36     $configuration['case_insensitive'] = TRUE;
37     $plugin = new StrReplace($configuration, 'str_replace', []);
38     $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
39     $this->assertSame('that eos et accusam et justo that', $actual);
40
41   }
42
43   /**
44    * Test for regular expressions.
45    */
46   public function testPregReplace() {
47     $value = 'vero eos et 123 accusam et justo 123 duo';
48     $configuration['search'] = '/[0-9]{3}/';
49     $configuration['replace'] = 'the';
50     $configuration['regex'] = TRUE;
51     $plugin = new StrReplace($configuration, 'str_replace', []);
52     $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
53     $this->assertSame('vero eos et the accusam et justo the duo', $actual);
54   }
55
56   /**
57    * Test for MigrateException for "search" configuration.
58    */
59   public function testSearchMigrateException() {
60     $value = 'vero eos et accusam et justo vero';
61     $configuration['replace'] = 'that';
62     $plugin = new StrReplace($configuration, 'str_replace', []);
63     $this->setExpectedException('\Drupal\migrate\MigrateException', '"search" must be configured.');
64     $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
65   }
66
67   /**
68    * Test for MigrateException for "replace" configuration.
69    */
70   public function testReplaceMigrateException() {
71     $value = 'vero eos et accusam et justo vero';
72     $configuration['search'] = 'et';
73     $plugin = new StrReplace($configuration, 'str_replace', []);
74     $this->setExpectedException('\Drupal\migrate\MigrateException', '"replace" must be configured.');
75     $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
76   }
77
78   /**
79    * Test for multiple.
80    */
81   public function testIsMultiple() {
82     $value = [
83       'vero eos et accusam et justo vero',
84       'et eos vero accusam vero justo et',
85     ];
86
87     $expected = [
88       'vero eos that accusam that justo vero',
89       'that eos vero accusam vero justo that',
90     ];
91     $configuration['search'] = 'et';
92     $configuration['replace'] = 'that';
93     $plugin = new StrReplace($configuration, 'str_replace', []);
94     $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
95     $this->assertArrayEquals($expected, $actual);
96
97     $this->assertTrue($plugin->multiple());
98   }
99
100 }