Trashes/restores/deletes (+ same bulk actions) children newsletters and
associations as per discussion on Slack: https://mailpoet.slack.com/archives/C02MTKAJL/p1496427873491785
This commit is contained in:
@@ -94,62 +94,111 @@ class Newsletter extends Model {
|
|||||||
|
|
||||||
function trash() {
|
function trash() {
|
||||||
// trash queue associations
|
// trash queue associations
|
||||||
SendingQueue::rawExecute(
|
$children = $this->children()->select('id')->findArray();
|
||||||
'UPDATE `' . SendingQueue::$_table . '` ' .
|
if($children) {
|
||||||
'SET `deleted_at` = NOW() ' .
|
$this->children()->rawExecute(
|
||||||
'WHERE `newsletter_id` = ' . $this->id
|
'UPDATE `' . self::$_table . '` ' .
|
||||||
);
|
'SET `deleted_at` = NOW() ' .
|
||||||
|
'WHERE `parent_id` = ' . $this->id
|
||||||
|
);
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = NOW() ' .
|
||||||
|
'WHERE `newsletter_id` IN (' . join(',', array_merge(Helpers::flattenArray($children), array($this->id))) . ')'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = NOW() ' .
|
||||||
|
'WHERE `newsletter_id` = ' . $this->id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return parent::trash();
|
return parent::trash();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkTrash($orm) {
|
static function bulkTrash($orm) {
|
||||||
// bulk trash queue associations
|
// bulk trash queue and notification history associations
|
||||||
parent::bulkAction($orm, function($ids) {
|
parent::bulkAction($orm, function($ids) {
|
||||||
SendingQueue::rawExecute(join(' ', array(
|
$children = Newsletter::whereIn('parent_id', $ids)->select('id')->findArray();
|
||||||
'UPDATE `' . SendingQueue::$_table . '`',
|
if($children) {
|
||||||
'SET `deleted_at` = NOW()',
|
Newsletter::rawExecute(
|
||||||
'WHERE `newsletter_id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
|
'UPDATE `' . Newsletter::$_table . '` ' .
|
||||||
)),
|
'SET `deleted_at` = NOW() ' .
|
||||||
$ids
|
'WHERE `parent_id` IN (' . join(',', Helpers::flattenArray($ids)) . ')'
|
||||||
);
|
);
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = NOW() ' .
|
||||||
|
'WHERE `newsletter_id` IN (' . join(',', array_merge(Helpers::flattenArray($children), $ids)) . ')'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = NOW() ' .
|
||||||
|
'WHERE `newsletter_id` IN (' . join(',', Helpers::flattenArray($ids)) . ')'
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return parent::bulkTrash($orm);
|
return parent::bulkTrash($orm);
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete() {
|
function delete() {
|
||||||
// delete segment associations
|
// delete queue, notification history and segment associations
|
||||||
$this->segmentRelations()->deleteMany();
|
$children = $this->children()->select('id')->findArray();
|
||||||
// delete queue associations
|
if($children) {
|
||||||
$this->queue()->deleteMany();
|
$children = Helpers::flattenArray($children);
|
||||||
|
$this->children()->deleteMany();
|
||||||
|
SendingQueue::whereIn('newsletter_id', array_merge($children, array($this->id)))->deleteMany();
|
||||||
|
NewsletterSegment::whereIn('newsletter_id', array_merge($children, array($this->id)))->deleteMany();
|
||||||
|
} else {
|
||||||
|
$this->queue()->deleteMany();
|
||||||
|
$this->segmentRelations()->deleteMany();
|
||||||
|
}
|
||||||
|
|
||||||
return parent::delete();
|
return parent::delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkDelete($orm) {
|
static function bulkDelete($orm) {
|
||||||
// bulk delete segment associations
|
// bulk delete queue, notification history and segment associations
|
||||||
parent::bulkAction($orm, function($ids) {
|
parent::bulkAction($orm, function($ids) {
|
||||||
NewsletterSegment::whereIn('newsletter_id', $ids)
|
$children = Newsletter::whereIn('parent_id', $ids)->select('id')->findArray();
|
||||||
->deleteMany();
|
if($children) {
|
||||||
});
|
$children = Helpers::flattenArray($children);
|
||||||
|
Newsletter::whereIn('parent_id', $ids)->deleteMany();
|
||||||
// bulk delete queue associations
|
SendingQueue::whereIn('newsletter_id', array_merge($children, $ids))->deleteMany();
|
||||||
parent::bulkAction($orm, function($ids) {
|
NewsletterSegment::whereIn('newsletter_id', array_merge($children, $ids))->deleteMany();
|
||||||
SendingQueue::whereIn('newsletter_id', $ids)
|
} else {
|
||||||
->deleteMany();
|
SendingQueue::whereIn('newsletter_id', $ids)->deleteMany();
|
||||||
|
NewsletterSegment::whereIn('newsletter_id', $ids)->deleteMany();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return parent::bulkDelete($orm);
|
return parent::bulkDelete($orm);
|
||||||
}
|
}
|
||||||
|
|
||||||
function restore() {
|
function restore() {
|
||||||
// restore trashed queue associations
|
// restore trashed queue and notification history associations
|
||||||
SendingQueue::rawExecute(
|
$children = $this->children()->select('id')->findArray();
|
||||||
'UPDATE `' . SendingQueue::$_table . '` ' .
|
if($children) {
|
||||||
'SET `deleted_at` = null ' .
|
$this->children()->rawExecute(
|
||||||
'WHERE `newsletter_id` = ' . $this->id
|
'UPDATE `' . self::$_table . '` ' .
|
||||||
);
|
'SET `deleted_at` = null ' .
|
||||||
|
'WHERE `parent_id` = ' . $this->id
|
||||||
|
);
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = null ' .
|
||||||
|
'WHERE `newsletter_id` IN (' . join(',', array_merge(Helpers::flattenArray($children), array($this->id))) . ')'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
SendingQueue::rawExecute(
|
||||||
|
'UPDATE `' . SendingQueue::$_table . '` ' .
|
||||||
|
'SET `deleted_at` = null ' .
|
||||||
|
'WHERE `newsletter_id` = ' . $this->id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if($this->status == self::STATUS_SENDING) {
|
if($this->status == self::STATUS_SENDING) {
|
||||||
$this->set('status', self::STATUS_DRAFT);
|
$this->set('status', self::STATUS_DRAFT);
|
||||||
@@ -159,13 +208,27 @@ class Newsletter extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function bulkRestore($orm) {
|
static function bulkRestore($orm) {
|
||||||
// bulk restore trashed queue associations
|
// bulk restore trashed queue and notification history associations
|
||||||
parent::bulkAction($orm, function($ids) {
|
parent::bulkAction($orm, function($ids) {
|
||||||
SendingQueue::whereIn('newsletter_id', $ids)
|
$children = Newsletter::whereIn('parent_id', $ids)->select('id')->findArray();
|
||||||
->whereNotNull('deleted_at')
|
if($children) {
|
||||||
->findResultSet()
|
Newsletter::whereIn('parent_id', $ids)
|
||||||
->set('deleted_at', null)
|
->whereNotNull('deleted_at')
|
||||||
->save();
|
->findResultSet()
|
||||||
|
->set('deleted_at', null)
|
||||||
|
->save();
|
||||||
|
SendingQueue::whereIn('newsletter_id', Helpers::flattenArray($children))
|
||||||
|
->whereNotNull('deleted_at')
|
||||||
|
->findResultSet()
|
||||||
|
->set('deleted_at', null)
|
||||||
|
->save();
|
||||||
|
} else {
|
||||||
|
SendingQueue::whereIn('newsletter_id', $ids)
|
||||||
|
->whereNotNull('deleted_at')
|
||||||
|
->findResultSet()
|
||||||
|
->set('deleted_at', null)
|
||||||
|
->save();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
parent::bulkAction($orm, function($ids) {
|
parent::bulkAction($orm, function($ids) {
|
||||||
|
@@ -459,6 +459,39 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect($newsletter_segments)->isEmpty();
|
expect($newsletter_segments)->isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItDeletesChildrenSegmentAndQueueAssociationsWhenParentNewsletterIsDeleted() {
|
||||||
|
$parent_newsletter = $this->newsletter;
|
||||||
|
// create multiple children (post notification history) newsletters and sending queues
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $parent_newsletter->id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->save();
|
||||||
|
$newsletter_segment = NewsletterSegment::create();
|
||||||
|
$newsletter_segment->newsletter_id = $newsletter->id;
|
||||||
|
$newsletter_segment->segment_id = 1;
|
||||||
|
$newsletter_segment->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure relations exist
|
||||||
|
// 1 parent newsletter/queues, 2 parent segments and 5 children queues/newsletters/segments
|
||||||
|
expect(Newsletter::findArray())->count(6);
|
||||||
|
expect(SendingQueue::findArray())->count(6);
|
||||||
|
expect(NewsletterSegment::findArray())->count(7);
|
||||||
|
|
||||||
|
// delete parent newsletter and check that relations no longer exist
|
||||||
|
$parent_newsletter->delete();
|
||||||
|
expect(Newsletter::findArray())->count(0);
|
||||||
|
expect(SendingQueue::findArray())->count(0);
|
||||||
|
expect(NewsletterSegment::findArray())->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
function testItTrashesQueueAssociationsWhenNewsletterIsTrashed() {
|
function testItTrashesQueueAssociationsWhenNewsletterIsTrashed() {
|
||||||
// create multiple sending queues
|
// create multiple sending queues
|
||||||
$newsletter = $this->newsletter;
|
$newsletter = $this->newsletter;
|
||||||
@@ -475,6 +508,32 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItTrashesChildrenQueueAssociationsWhenParentNewsletterIsTrashed() {
|
||||||
|
$parent_newsletter = $this->newsletter;
|
||||||
|
// create multiple children (post notification history) newsletters and sending queues
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $parent_newsletter->id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->save();
|
||||||
|
}
|
||||||
|
// 1 parent and 5 children queues/newsletters
|
||||||
|
expect(Newsletter::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
|
||||||
|
// trash parent newsletter and check that relations are trashed
|
||||||
|
$parent_newsletter->trash();
|
||||||
|
// 1 parent and 5 children queues/newsletters
|
||||||
|
expect(Newsletter::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
}
|
||||||
|
|
||||||
function testItRestoresTrashedQueueAssociationsWhenNewsletterIsRestored() {
|
function testItRestoresTrashedQueueAssociationsWhenNewsletterIsRestored() {
|
||||||
// create multiple sending queues
|
// create multiple sending queues
|
||||||
$newsletter = $this->newsletter;
|
$newsletter = $this->newsletter;
|
||||||
@@ -492,6 +551,41 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItRestoresTrashedChildrenQueueAssociationsWhenParentNewsletterIsRestored() {
|
||||||
|
// delete parent newsletter and sending queue
|
||||||
|
$parent_newsletter = $this->newsletter;
|
||||||
|
$parent_newsletter->deleted_at = date('Y-m-d H:i:s');
|
||||||
|
$parent_newsletter->save();
|
||||||
|
$parent_sending_queue = $this->sending_queue;
|
||||||
|
$parent_sending_queue->deleted_at = date('Y-m-d H:i:s');
|
||||||
|
$parent_sending_queue->save();
|
||||||
|
|
||||||
|
// create multiple children (post notification history) newsletters and sending queues
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $parent_newsletter->id,
|
||||||
|
'deleted_at' => date('Y-m-d H:i:s')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->deleted_at = date('Y-m-d H:i:s');
|
||||||
|
$sending_queue->save();
|
||||||
|
}
|
||||||
|
// 1 parent and 5 children queues/newsletters
|
||||||
|
expect(Newsletter::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
|
||||||
|
// restore parent newsletter and check that relations are restored
|
||||||
|
$parent_newsletter->restore();
|
||||||
|
// 1 parent and 5 children queues/newsletters
|
||||||
|
expect(Newsletter::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
}
|
||||||
|
|
||||||
function testItTrashesAllQueueAssociationsWhenNewslettersAreBulkTrashed() {
|
function testItTrashesAllQueueAssociationsWhenNewslettersAreBulkTrashed() {
|
||||||
// create multiple newsletters and sending queues
|
// create multiple newsletters and sending queues
|
||||||
for($i = 1; $i <= 5; $i++) {
|
for($i = 1; $i <= 5; $i++) {
|
||||||
@@ -515,6 +609,30 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItTrashesAllChildrenQueueAssociationsWhenParentNewslettersAreBulkTrashed() {
|
||||||
|
// create multiple children (post notification history) newsletters and sending queues
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $this->newsletter->id,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->save();
|
||||||
|
}
|
||||||
|
// 5 queues/newsletters + 1 of each created in _before() method
|
||||||
|
expect(Newsletter::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
|
||||||
|
// bulk trash parent newsletters and check that relations are trashed
|
||||||
|
Newsletter::bulkTrash(ORM::forTable(Newsletter::$_table));
|
||||||
|
expect(Newsletter::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(6);
|
||||||
|
}
|
||||||
|
|
||||||
function testItBulkRestoresTrashedQueueAssociationsWhenNewslettersAreBulkRestored() {
|
function testItBulkRestoresTrashedQueueAssociationsWhenNewslettersAreBulkRestored() {
|
||||||
// create multiple newsletters and sending queues
|
// create multiple newsletters and sending queues
|
||||||
for($i = 1; $i <= 5; $i++) {
|
for($i = 1; $i <= 5; $i++) {
|
||||||
@@ -540,6 +658,32 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItBulkRestoresTrashedChildrenQueueAssociationsWhenParentNewslettersAreBulkRestored() {
|
||||||
|
// create multiple children (post notification history) newsletters and sending queues
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $this->newsletter->id,
|
||||||
|
'deleted_at' => date('Y-m-d H:i:s')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->deleted_at = date('Y-m-d H:i:s');
|
||||||
|
$sending_queue->save();
|
||||||
|
}
|
||||||
|
expect(Newsletter::whereNotNull('deleted_at')->findArray())->count(5);
|
||||||
|
expect(SendingQueue::whereNotNull('deleted_at')->findArray())->count(5);
|
||||||
|
|
||||||
|
// bulk restore parent newsletters and check that relations are restored
|
||||||
|
Newsletter::bulkRestore(ORM::forTable(Newsletter::$_table));
|
||||||
|
// 1 parent and 5 queues/newsletters
|
||||||
|
expect(Newsletter::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
expect(SendingQueue::whereNull('deleted_at')->findArray())->count(6);
|
||||||
|
}
|
||||||
|
|
||||||
function testItBulkDeletesSegmentAndQueueAssociationsWhenNewslettersAreBulkDeleted() {
|
function testItBulkDeletesSegmentAndQueueAssociationsWhenNewslettersAreBulkDeleted() {
|
||||||
// create multiple newsletters, sending queues and newsletter segments
|
// create multiple newsletters, sending queues and newsletter segments
|
||||||
for($i = 1; $i <= 5; $i++) {
|
for($i = 1; $i <= 5; $i++) {
|
||||||
@@ -570,6 +714,37 @@ class NewsletterTest extends MailPoetTest {
|
|||||||
expect(NewsletterSegment::findArray())->count(0);
|
expect(NewsletterSegment::findArray())->count(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItBulkDeletesChildrenSegmentAndQueueAssociationsWhenParentNewslettersAreBulkDeleted() {
|
||||||
|
$this->_after();
|
||||||
|
// create multiple children (post notification history) newsletters, sending queues and newsletter segments
|
||||||
|
for($i = 1; $i <= 5; $i++) {
|
||||||
|
$newsletter = Newsletter::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'subject' => 'test',
|
||||||
|
'type' => Newsletter::TYPE_NOTIFICATION_HISTORY,
|
||||||
|
'parent_id' => $this->newsletter->id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$sending_queue = SendingQueue::create();
|
||||||
|
$sending_queue->newsletter_id = $newsletter->id;
|
||||||
|
$sending_queue->save();
|
||||||
|
$newsletter_segment = NewsletterSegment::create();
|
||||||
|
$newsletter_segment->newsletter_id = $newsletter->id;
|
||||||
|
$newsletter_segment->segment_id = 1;
|
||||||
|
$newsletter_segment->save();
|
||||||
|
}
|
||||||
|
// 5 queues/newsletters/segment associations
|
||||||
|
expect(Newsletter::findArray())->count(5);
|
||||||
|
expect(SendingQueue::findArray())->count(5);
|
||||||
|
expect(NewsletterSegment::findArray())->count(5);
|
||||||
|
|
||||||
|
// bulk delete newsletters and check that relations are deleted
|
||||||
|
Newsletter::bulkDelete(ORM::forTable(Newsletter::$_table));
|
||||||
|
expect(Newsletter::findArray())->count(0);
|
||||||
|
expect(SendingQueue::findArray())->count(0);
|
||||||
|
expect(NewsletterSegment::findArray())->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . NewsletterOption::$_table);
|
ORM::raw_execute('TRUNCATE ' . NewsletterOption::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
||||||
|
Reference in New Issue
Block a user