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 transformation.
64 * @dataProvider datesDataProvider
66 * @param $configuration
67 * The configuration of the migration process plugin.
69 * The source value for the migration process plugin.
71 * The expected value of the migration process plugin.
73 public function testTransform($configuration, $value, $expected) {
74 $this->plugin = new FormatDate($configuration, 'test_format_date', []);
75 $actual = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'field_date');
77 $this->assertEquals($expected, $actual);
81 * Data provider of test dates.
84 * Array of date formats and actual/expected values.
86 public function datesDataProvider() {
90 'from_format' => 'm/d/Y',
91 'to_format' => 'Y-m-d',
93 'value' => '01/05/1955',
94 'expected' => '1955-01-05',
96 'datetime_datetime' => [
98 'from_format' => 'm/d/Y H:i:s',
99 'to_format' => 'Y-m-d\TH:i:s',
101 'value' => '01/05/1955 10:43:22',
102 'expected' => '1955-01-05T10:43:22',
106 'from_format' => 'm/d/Y',
107 'to_format' => 'Y-m-d',
114 'from_format' => 'Y-m-d\TH:i:sO',
115 'to_format' => 'Y-m-d\TH:i:s',
116 'timezone' => 'America/Managua',
118 'value' => '2004-12-19T10:19:42-0600',
119 'expected' => '2004-12-19T10:19:42',