c1e0c631bd2eb886381205986407a9326799b77e
[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 transformation.
61    *
62    * @covers ::transform
63    *
64    * @dataProvider datesDataProvider
65    *
66    * @param $configuration
67    *   The configuration of the migration process plugin.
68    * @param $value
69    *   The source value for the migration process plugin.
70    * @param $expected
71    *   The expected value of the migration process plugin.
72    */
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');
76
77     $this->assertEquals($expected, $actual);
78   }
79
80   /**
81    * Data provider of test dates.
82    *
83    * @return array
84    *   Array of date formats and actual/expected values.
85    */
86   public function datesDataProvider() {
87     return [
88       'datetime_date' => [
89         'configuration' => [
90           'from_format' => 'm/d/Y',
91           'to_format' => 'Y-m-d',
92         ],
93         'value' => '01/05/1955',
94         'expected' => '1955-01-05',
95       ],
96       'datetime_datetime' => [
97         'configuration' => [
98           'from_format' => 'm/d/Y H:i:s',
99           'to_format' => 'Y-m-d\TH:i:s',
100         ],
101         'value' => '01/05/1955 10:43:22',
102         'expected' => '1955-01-05T10:43:22',
103       ],
104       'empty_values' => [
105         'configuration' => [
106           'from_format' => 'm/d/Y',
107           'to_format' => 'Y-m-d',
108         ],
109         'value' => '',
110         'expected' => '',
111       ],
112       'timezone' => [
113         'configuration' => [
114           'from_format' => 'Y-m-d\TH:i:sO',
115           'to_format' => 'Y-m-d\TH:i:s',
116           'timezone' => 'America/Managua',
117         ],
118         'value' => '2004-12-19T10:19:42-0600',
119         'expected' => '2004-12-19T10:19:42',
120       ],
121     ];
122   }
123
124 }