From 92a0b41fa2c2b6f2b46a8460ee13824dac1ec150 Mon Sep 17 00:00:00 2001 From: webchick <drupal@webchick.net> Date: Fri, 20 Dec 2019 17:24:07 -0800 Subject: [PATCH] Issue #3095195 by jofitz, Wim Leers, heddn: Drupal 7 date fields configured to not collect the hour/minute/second granularities can have "00" MM or DD attributes --- .../src/Plugin/migrate/process/FormatDate.php | 7 +++++++ .../tests/src/Unit/process/FormatDateTest.php | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php index 505306f05002..4c5c6eda37df 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php +++ b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php @@ -127,6 +127,13 @@ public function transform($value, MigrateExecutableInterface $migrate_executable } $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : []; + // Older versions of Drupal where omitting certain granularities (also known + // as "collected date attributes") resulted in invalid timestamps getting + // stored. + if ($fromFormat === 'Y-m-d\TH:i:s') { + $value = str_replace(['-00-00T', '-00T'], ['-01-01T', '-01T'], $value); + } + // Attempts to transform the supplied date using the defined input format. // DateTimePlus::createFromFormat can throw exceptions, so we need to // explicitly check for problems. diff --git a/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php index 629243c84cff..f3485f171452 100644 --- a/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php +++ b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php @@ -229,6 +229,22 @@ public function datesDataProvider() { 'value' => '0000-00-00 00:00:00', 'expected' => '-0001-11-30 00:00:00', ], + 'collected_date_attributes_day' => [ + 'configuration' => [ + 'from_format' => 'Y-m-d\TH:i:s', + 'to_format' => 'Y-m-d\TH:i:s', + ], + 'value' => '2012-01-00T00:00:00', + 'expected' => '2012-01-01T00:00:00', + ], + 'collected_date_attributes_month' => [ + 'configuration' => [ + 'from_format' => 'Y-m-d\TH:i:s', + 'to_format' => 'Y-m-d\TH:i:s', + ], + 'value' => '2012-00-00T00:00:00', + 'expected' => '2012-01-01T00:00:00', + ], ]; } -- GitLab