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 / FormatDateTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\FormatDate;
7
8 /**
9  * Tests the format date process plugin.
10  *
11  * @group migrate
12  *
13  * @coversDefaultClass Drupal\migrate\Plugin\migrate\process\FormatDate
14  */
15 class FormatDateTest extends MigrateProcessTestCase {
16
17   /**
18    * Tests that missing configuration will throw an exception.
19    */
20   public function testMigrateExceptionMissingFromFormat() {
21     $configuration = [
22       'from_format' => '',
23       'to_format' => 'Y-m-d',
24     ];
25
26     $this->setExpectedException(MigrateException::class, 'Format date plugin is missing from_format configuration.');
27     $this->plugin = new FormatDate($configuration, 'test_format_date', []);
28     $this->plugin->transform('01/05/1955', $this->migrateExecutable, $this->row, 'field_date');
29   }
30
31   /**
32    * Tests that missing configuration will throw an exception.
33    */
34   public function testMigrateExceptionMissingToFormat() {
35     $configuration = [
36       'from_format' => 'm/d/Y',
37       'to_format' => '',
38     ];
39
40     $this->setExpectedException(MigrateException::class, 'Format date plugin is missing to_format configuration.');
41     $this->plugin = new FormatDate($configuration, 'test_format_date', []);
42     $this->plugin->transform('01/05/1955', $this->migrateExecutable, $this->row, 'field_date');
43   }
44
45   /**
46    * Tests that date format mismatches will throw an exception.
47    */
48   public function testMigrateExceptionBadFormat() {
49     $configuration = [
50       'from_format' => 'm/d/Y',
51       'to_format' => 'Y-m-d',
52     ];
53
54     $this->setExpectedException(MigrateException::class, 'Format date plugin could not transform "January 5, 1955" using the format "m/d/Y". Error: The date cannot be created from a format.');
55     $this->plugin = new FormatDate($configuration, 'test_format_date', []);
56     $this->plugin->transform('January 5, 1955', $this->migrateExecutable, $this->row, 'field_date');
57   }
58
59   /**
60    * Tests that "timezone" configuration key triggers deprecation error.
61    *
62    * @covers ::transform
63    *
64    * @dataProvider providerTestDeprecatedTimezoneConfigurationKey
65    *
66    * @group legacy
67    * @expectedDeprecation Configuration key "timezone" is deprecated in 8.4.x and will be removed before Drupal 9.0.0, use "from_timezone" and "to_timezone" instead. See https://www.drupal.org/node/2885746
68    */
69   public function testDeprecatedTimezoneConfigurationKey($configuration, $value, $expected) {
70     $this->plugin = new FormatDate($configuration, 'test_format_date', []);
71     $actual = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'field_date');
72
73     $this->assertEquals($expected, $actual);
74   }
75
76   /**
77    * Data provider for testDeprecatedTimezoneConfigurationKey.
78    */
79   public function providerTestDeprecatedTimezoneConfigurationKey() {
80     return [
81       [
82         'configuration' => [
83           'from_format' => 'Y-m-d\TH:i:sO',
84           'to_format' => 'c e',
85           'timezone' => 'America/Managua',
86         ],
87         'value' => '2004-12-19T10:19:42-0600',
88         'expected' => '2004-12-19T10:19:42-06:00 -06:00',
89       ],
90     ];
91   }
92
93   /**
94    * Tests transformation.
95    *
96    * @covers ::transform
97    *
98    * @dataProvider datesDataProvider
99    *
100    * @param $configuration
101    *   The configuration of the migration process plugin.
102    * @param $value
103    *   The source value for the migration process plugin.
104    * @param $expected
105    *   The expected value of the migration process plugin.
106    */
107   public function testTransform($configuration, $value, $expected) {
108     $this->plugin = new FormatDate($configuration, 'test_format_date', []);
109     $actual = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'field_date');
110
111     $this->assertEquals($expected, $actual);
112   }
113
114   /**
115    * Data provider of test dates.
116    *
117    * @return array
118    *   Array of date formats and actual/expected values.
119    */
120   public function datesDataProvider() {
121     return [
122       'datetime_date' => [
123         'configuration' => [
124           'from_format' => 'm/d/Y',
125           'to_format' => 'Y-m-d',
126         ],
127         'value' => '01/05/1955',
128         'expected' => '1955-01-05',
129       ],
130       'datetime_datetime' => [
131         'configuration' => [
132           'from_format' => 'm/d/Y H:i:s',
133           'to_format' => 'Y-m-d\TH:i:s e',
134         ],
135         'value' => '01/05/1955 10:43:22',
136         'expected' => '1955-01-05T10:43:22 Australia/Sydney',
137       ],
138       'empty_values' => [
139         'configuration' => [
140           'from_format' => 'm/d/Y',
141           'to_format' => 'Y-m-d',
142         ],
143         'value' => '',
144         'expected' => '',
145       ],
146       'timezone_from_to' => [
147         'configuration' => [
148           'from_format' => 'Y-m-d H:i:s',
149           'to_format' => 'Y-m-d H:i:s e',
150           'from_timezone' => 'America/Managua',
151           'to_timezone' => 'UTC',
152         ],
153         'value' => '2004-12-19 10:19:42',
154         'expected' => '2004-12-19 16:19:42 UTC',
155       ],
156       'timezone_from' => [
157         'configuration' => [
158           'from_format' => 'Y-m-d h:i:s',
159           'to_format' => 'Y-m-d h:i:s e',
160           'from_timezone' => 'America/Managua',
161         ],
162         'value' => '2004-11-19 10:25:33',
163         // Unit tests use Australia/Sydney timezone, so date value will be
164         // converted from America/Managua to Australia/Sydney timezone.
165         'expected' => '2004-11-20 03:25:33 Australia/Sydney',
166       ],
167       'timezone_to' => [
168         'configuration' => [
169           'from_format' => 'Y-m-d H:i:s',
170           'to_format' => 'Y-m-d H:i:s e',
171           'to_timezone' => 'America/Managua',
172         ],
173         'value' => '2004-12-19 10:19:42',
174         // Unit tests use Australia/Sydney timezone, so date value will be
175         // converted from Australia/Sydney to America/Managua timezone.
176         'expected' => '2004-12-18 17:19:42 America/Managua',
177       ],
178     ];
179   }
180
181 }