3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\Plugin\migrate\process\FormatDate;
9 * Tests the format date process plugin.
13 * @coversDefaultClass Drupal\migrate\Plugin\migrate\process\FormatDate
15 class FormatDateTest extends MigrateProcessTestCase {
18 * Tests that missing configuration will throw an exception.
20 public function testMigrateExceptionMissingFromFormat() {
23 'to_format' => 'Y-m-d',
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');
32 * Tests that missing configuration will throw an exception.
34 public function testMigrateExceptionMissingToFormat() {
36 'from_format' => 'm/d/Y',
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');
46 * Tests that date format mismatches will throw an exception.
48 public function testMigrateExceptionBadFormat() {
50 'from_format' => 'm/d/Y',
51 'to_format' => 'Y-m-d',
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');
60 * Tests that "timezone" configuration key triggers deprecation error.
64 * @dataProvider providerTestDeprecatedTimezoneConfigurationKey
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
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');
73 $this->assertEquals($expected, $actual);
77 * Data provider for testDeprecatedTimezoneConfigurationKey.
79 public function providerTestDeprecatedTimezoneConfigurationKey() {
83 'from_format' => 'Y-m-d\TH:i:sO',
85 'timezone' => 'America/Managua',
87 'value' => '2004-12-19T10:19:42-0600',
88 'expected' => '2004-12-19T10:19:42-06:00 -06:00',
94 * Tests transformation.
98 * @dataProvider datesDataProvider
100 * @param $configuration
101 * The configuration of the migration process plugin.
103 * The source value for the migration process plugin.
105 * The expected value of the migration process plugin.
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');
111 $this->assertEquals($expected, $actual);
115 * Data provider of test dates.
118 * Array of date formats and actual/expected values.
120 public function datesDataProvider() {
124 'from_format' => 'm/d/Y',
125 'to_format' => 'Y-m-d',
127 'value' => '01/05/1955',
128 'expected' => '1955-01-05',
130 'datetime_datetime' => [
132 'from_format' => 'm/d/Y H:i:s',
133 'to_format' => 'Y-m-d\TH:i:s e',
135 'value' => '01/05/1955 10:43:22',
136 'expected' => '1955-01-05T10:43:22 Australia/Sydney',
140 'from_format' => 'm/d/Y',
141 'to_format' => 'Y-m-d',
146 'timezone_from_to' => [
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',
153 'value' => '2004-12-19 10:19:42',
154 'expected' => '2004-12-19 16:19:42 UTC',
158 'from_format' => 'Y-m-d h:i:s',
159 'to_format' => 'Y-m-d h:i:s e',
160 'from_timezone' => 'America/Managua',
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',
169 'from_format' => 'Y-m-d H:i:s',
170 'to_format' => 'Y-m-d H:i:s e',
171 'to_timezone' => 'America/Managua',
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',
180 'from_format' => 'U',
181 'to_format' => 'Y-m-d',
184 'expected' => '1970-01-01',
188 'from_format' => 'U',
189 'to_format' => 'Y-m-d',
192 'expected' => '1970-01-01',
196 'from_format' => 'Y-m-d H:i:s',
197 'to_format' => 'Y-m-d H:i:s e',
198 'settings' => ['validate_format' => FALSE],
200 'value' => '0000-00-00 00:00:00',
201 'expected' => '-0001-11-30 00:00:00 Australia/Sydney',
203 'zeros_same_timezone' => [
205 'from_format' => 'Y-m-d H:i:s',
206 'to_format' => 'Y-m-d H:i:s',
207 'settings' => ['validate_format' => FALSE],
208 'from_timezone' => 'UTC',
209 'to_timezone' => 'UTC',
211 'value' => '0000-00-00 00:00:00',
212 'expected' => '-0001-11-30 00:00:00',