diff --git a/lib/Models/CustomField.php b/lib/Models/CustomField.php index 91acc36c26..1279b5065f 100644 --- a/lib/Models/CustomField.php +++ b/lib/Models/CustomField.php @@ -148,4 +148,15 @@ class CustomField extends Model { return $custom_field->save(); } + + static function extractCustomFieldsFromFromObject($data) { + $custom_fields = array(); + foreach($data as $key => $value) { + if(strpos($key, 'cf_') === 0) { + $custom_fields[(int)substr($key, 3)] = $value; + unset($data[$key]); + } + } + return array($data, $custom_fields); + } } diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 0957ee650b..1ed1b058e2 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -491,27 +491,11 @@ class Subscriber extends Model { unset($data['segments']); } - if($subscriber === false) { - // fields that must exist - $not_null_fields = array( - 'first_name' => '', - 'last_name' => '' - ); - foreach($not_null_fields as $field => $value) { - if(!isset($data[$field])) { - $data[$field] = $value; - } - } - } + // set required fields' default values + $data = self::setRequiredFieldsDefaultValues($data); - // custom fields - $custom_fields = array(); - foreach($data as $key => $value) { - if(strpos($key, 'cf_') === 0) { - $custom_fields[(int)substr($key, 3)] = $value; - unset($data[$key]); - } - } + // get custom fields + list($data, $custom_fields) = CustomField::extractCustomFieldsFromFromObject($data); // wipe any unconfirmed data at this point $data['unconfirmed_data'] = null; @@ -884,4 +868,17 @@ class Subscriber extends Model { }, $subscribers) ); } + + static function setRequiredFieldsDefaultValues($data) { + $required_field_default_values = array( + 'first_name' => '', + 'last_name' => '' + ); + foreach($required_field_default_values as $field => $value) { + if(!isset($data[$field])) { + $data[$field] = $value; + } + } + return $data; + } } \ No newline at end of file diff --git a/tests/unit/Models/CustomFieldTest.php b/tests/unit/Models/CustomFieldTest.php index 7e8387949f..432dfb3ac7 100644 --- a/tests/unit/Models/CustomFieldTest.php +++ b/tests/unit/Models/CustomFieldTest.php @@ -131,6 +131,30 @@ class CustomFieldTest extends MailPoetTest { expect($subscriber->value)->equals($association->value); } + function testItExtractsCustomFieldsFromObject() { + $data = array( + 'email' => 'test@example.com', + 'cf_1' => 'Paris', + 'first_name' => 'John', + 'cf_2' => 'France', + 'last_name' => 'Doe' + ); + list($data, $custom_values) = CustomField::extractCustomFieldsFromFromObject($data); + expect($data)->equals( + array( + 'email' => 'test@example.com', + 'first_name' => 'John', + 'last_name' => 'Doe' + ) + ); + expect($custom_values)->equals( + array( + '1' => 'Paris', + '2' => 'France' + ) + ); + } + function _after() { CustomField::deleteMany(); Subscriber::deleteMany(); diff --git a/tests/unit/Models/SubscriberTest.php b/tests/unit/Models/SubscriberTest.php index 719e02398a..ac4e6c6f42 100644 --- a/tests/unit/Models/SubscriberTest.php +++ b/tests/unit/Models/SubscriberTest.php @@ -903,6 +903,16 @@ class SubscriberTest extends MailPoetTest { ); } + function testItSetsDefaultValuesForRequiredFields() { + // MySQL running in strict mode requires a value to be set for certain fields + expect(Subscriber::setRequiredFieldsDefaultValues(array()))->equals( + array( + 'first_name' => '', + 'last_name' => '' + ) + ); + } + function _after() { ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); ORM::raw_execute('TRUNCATE ' . Segment::$_table);