Fix storing custom field dates in different formats [MAILPOET-2881]

This commit is contained in:
wxa
2020-05-06 18:46:25 +03:00
committed by Veljko V
parent 6fbf9e8efa
commit 637a56c92a
2 changed files with 39 additions and 9 deletions

View File

@ -65,19 +65,18 @@ class CustomField extends Model {
$dateParts = explode('_', $dateType); $dateParts = explode('_', $dateType);
switch ($dateType) { switch ($dateType) {
case 'year_month_day': case 'year_month_day':
$value = sprintf( $value = str_replace(
'%s/%s/%s', ['DD', 'MM', 'YYYY'],
$value['month'], [$value['day'], $value['month'], $value['year']],
$value['day'], $dateFormat
$value['year']
); );
break; break;
case 'year_month': case 'year_month':
$value = sprintf( $value = str_replace(
'%s/%s', ['MM', 'YYYY'],
$value['month'], [$value['month'], $value['year']],
$value['year'] $dateFormat
); );
break; break;

View File

@ -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([ $customField3 = CustomField::createOrUpdate([
'name' => 'Registered on', 'name' => 'Registered on',
'type' => 'date', '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([ $subscriberWithCustomField = Subscriber::createOrUpdate([
'email' => 'user.with.cf@mailpoet.com', 'email' => 'user.with.cf@mailpoet.com',
'cf_' . $customField->id => 'Paris', 'cf_' . $customField->id => 'Paris',
@ -354,7 +372,16 @@ class SubscriberTest extends \MailPoetTest {
'month' => 3, 'month' => 3,
'year' => 1984, 'year' => 1984,
], // date as array value ], // 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_' . $customField3->id => '2013-07', // date as string value
'cf_' . $customField5->id => [
'month' => 5,
'year' => 2020,
], // date as array value
]); ]);
$subscriber = Subscriber::findOne($subscriberWithCustomField->id) $subscriber = Subscriber::findOne($subscriberWithCustomField->id)
@ -365,8 +392,12 @@ class SubscriberTest extends \MailPoetTest {
expect($subscriber->{'cf_' . $customField->id})->equals('Paris'); expect($subscriber->{'cf_' . $customField->id})->equals('Paris');
// date specified as array gets converted to string // date specified as array gets converted to string
expect($subscriber->{'cf_' . $customField2->id})->equals('1984-03-09 00:00:00'); 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 // date specified as string is stored as is
expect($subscriber->{'cf_' . $customField3->id})->equals('2013-07'); 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() { public function testItShouldUnsubscribeFromAllSegments() {