- Deletes related custom fields when deleting subscriber
- Updates unit tests
This commit is contained in:
@ -51,7 +51,8 @@ class Subscriber extends Model {
|
||||
} else {
|
||||
// delete all relations to segments
|
||||
SubscriberSegment::deleteSubscriptions($this);
|
||||
|
||||
// delete all relations to custom fields
|
||||
SubscriberCustomField::deleteSubscriberRelations($this);
|
||||
return parent::delete();
|
||||
}
|
||||
}
|
||||
|
@ -86,4 +86,14 @@ class SubscriberCustomField extends Model {
|
||||
Helpers::flattenArray($values)
|
||||
);
|
||||
}
|
||||
|
||||
static function deleteSubscriberRelations($subscriber) {
|
||||
if($subscriber === false) return false;
|
||||
$relations = self::where('subscriber_id', $subscriber->id);
|
||||
return $relations->deleteMany();
|
||||
}
|
||||
|
||||
static function subscribed($orm) {
|
||||
return $orm->where('status', Subscriber::STATUS_SUBSCRIBED);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Codeception\Util\Fixtures;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
@ -482,6 +483,23 @@ class SubscriberTest extends MailPoetTest {
|
||||
expect($subscriber)->notEquals(false);
|
||||
}
|
||||
|
||||
function testItCanDeleteCustomFieldRelations() {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||
$subscriber->save();
|
||||
foreach(range(1, 5) as $custom_field) {
|
||||
$subscriber_custom_field = SubscriberCustomField::create();
|
||||
$subscriber_custom_field->custom_field_id = $custom_field;
|
||||
$subscriber_custom_field->subscriber_id = ($custom_field !== 5) ?
|
||||
$subscriber->id :
|
||||
100; // create one record with a nonexistent subscriber id
|
||||
$subscriber_custom_field->save();
|
||||
}
|
||||
expect(SubscriberCustomField::findMany())->count(5);
|
||||
$subscriber->delete();
|
||||
expect(SubscriberCustomField::findMany())->count(1);
|
||||
}
|
||||
|
||||
function testItCanGetTheTotalNumberOfSubscribers() {
|
||||
// remove all subscribers
|
||||
Subscriber::deleteMany();
|
||||
|
Reference in New Issue
Block a user