From 637a56c92a060d3da5ea42961ba6d03ba2ca708a Mon Sep 17 00:00:00 2001 From: wxa Date: Wed, 6 May 2020 18:46:25 +0300 Subject: [PATCH] Fix storing custom field dates in different formats [MAILPOET-2881] --- lib/Models/CustomField.php | 17 ++++++----- tests/integration/Models/SubscriberTest.php | 31 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/Models/CustomField.php b/lib/Models/CustomField.php index 552f237927..f8bc223446 100644 --- a/lib/Models/CustomField.php +++ b/lib/Models/CustomField.php @@ -65,19 +65,18 @@ class CustomField extends Model { $dateParts = explode('_', $dateType); switch ($dateType) { case 'year_month_day': - $value = sprintf( - '%s/%s/%s', - $value['month'], - $value['day'], - $value['year'] + $value = str_replace( + ['DD', 'MM', 'YYYY'], + [$value['day'], $value['month'], $value['year']], + $dateFormat ); break; case 'year_month': - $value = sprintf( - '%s/%s', - $value['month'], - $value['year'] + $value = str_replace( + ['MM', 'YYYY'], + [$value['month'], $value['year']], + $dateFormat ); break; diff --git a/tests/integration/Models/SubscriberTest.php b/tests/integration/Models/SubscriberTest.php index 25783fd019..461636f881 100644 --- a/tests/integration/Models/SubscriberTest.php +++ b/tests/integration/Models/SubscriberTest.php @@ -337,6 +337,15 @@ class SubscriberTest extends \MailPoetTest { ], ]); + $customField4 = CustomField::createOrUpdate([ + 'name' => 'Date in different format', + 'type' => 'date', + 'params' => [ + 'date_type' => 'year_month_day', + 'date_format' => 'DD/MM/YYYY', + ], + ]); + $customField3 = CustomField::createOrUpdate([ 'name' => 'Registered on', 'type' => 'date', @@ -346,6 +355,15 @@ class SubscriberTest extends \MailPoetTest { ], ]); + $customField5 = CustomField::createOrUpdate([ + 'name' => 'Year-month in different format', + 'type' => 'date', + 'params' => [ + 'date_type' => 'year_month', + 'date_format' => 'YYYY/MM', + ], + ]); + $subscriberWithCustomField = Subscriber::createOrUpdate([ 'email' => 'user.with.cf@mailpoet.com', 'cf_' . $customField->id => 'Paris', @@ -354,7 +372,16 @@ class SubscriberTest extends \MailPoetTest { 'month' => 3, 'year' => 1984, ], // date as array value + 'cf_' . $customField4->id => [ + 'day' => 25, + 'month' => 4, + 'year' => 2020, + ], // date as array value 'cf_' . $customField3->id => '2013-07', // date as string value + 'cf_' . $customField5->id => [ + 'month' => 5, + 'year' => 2020, + ], // date as array value ]); $subscriber = Subscriber::findOne($subscriberWithCustomField->id) @@ -365,8 +392,12 @@ class SubscriberTest extends \MailPoetTest { expect($subscriber->{'cf_' . $customField->id})->equals('Paris'); // date specified as array gets converted to string expect($subscriber->{'cf_' . $customField2->id})->equals('1984-03-09 00:00:00'); + // date in different format specified as array is stored correctly + expect($subscriber->{'cf_' . $customField4->id})->equals('2020-04-25 00:00:00'); // date specified as string is stored as is expect($subscriber->{'cf_' . $customField3->id})->equals('2013-07'); + // year-month date in different format specified as array is stored correctly + expect($subscriber->{'cf_' . $customField5->id})->equals('2020-05-01 00:00:00'); } public function testItShouldUnsubscribeFromAllSegments() {