From bf0dd3d8d953b5c75c3fae18241cad0129b56a8c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 16 Dec 2014 17:21:16 -0800 Subject: [PATCH] CRM-15578 - MailingAB API - Propagate deletion to any included mailings. --- CRM/Mailing/BAO/MailingAB.php | 29 +++++++++++++++++--------- tests/phpunit/api/v3/MailingABTest.php | 27 +++++++++++++++++------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/CRM/Mailing/BAO/MailingAB.php b/CRM/Mailing/BAO/MailingAB.php index c78a60c445..5e36f3f50c 100644 --- a/CRM/Mailing/BAO/MailingAB.php +++ b/CRM/Mailing/BAO/MailingAB.php @@ -125,15 +125,24 @@ class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB { if (empty($id)) { CRM_Core_Error::fatal(); } - - CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray); - - $dao = new CRM_Mailing_DAO_MailingAB(); - $dao->id = $id; - $dao->delete(); - - CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success'); - - CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao); + CRM_Core_Transaction::create()->run(function() use ($id) { + CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray); + + $dao = new CRM_Mailing_DAO_MailingAB(); + $dao->id = $id; + if ($dao->find(TRUE)) { + $mailing_ids = array($dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c); + $dao->delete(); + foreach ($mailing_ids as $mailing_id) { + if ($mailing_id) { + CRM_Mailing_BAO_Mailing::del($mailing_id); + } + } + } + + CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success'); + + CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao); + }); } } diff --git a/tests/phpunit/api/v3/MailingABTest.php b/tests/phpunit/api/v3/MailingABTest.php index 71ec78ecc6..1f2c20fb46 100755 --- a/tests/phpunit/api/v3/MailingABTest.php +++ b/tests/phpunit/api/v3/MailingABTest.php @@ -53,6 +53,7 @@ class api_v3_MailingABTest extends CiviUnitTestCase { function setUp() { parent::setUp(); + $this->useTransaction(TRUE); $this->_mailingID_A = $this->createMailing(); $this->_mailingID_B = $this->createMailing(); $this->_mailingID_C = $this->createMailing(); @@ -69,13 +70,6 @@ class api_v3_MailingABTest extends CiviUnitTestCase { ); } - function tearDown() { - $this->deleteMailing($this->_mailingID_A); - $this->deleteMailing($this->_mailingID_B); - $this->deleteMailing($this->_mailingID_C); - $this->groupDelete($this->_groupID); - } - /** * Test civicrm_mailing_create */ @@ -90,7 +84,26 @@ class api_v3_MailingABTest extends CiviUnitTestCase { */ public function testMailerDeleteSuccess() { $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + + $this->assertDBQuery(1, "SELECT count(*) FROM civicrm_mailing_abtesting WHERE id = %1", array( + 1 => array($result['id'], 'Integer'), + )); + $this->assertDBQuery(3, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array( + 1 => array($this->_mailingID_A, 'Integer'), + 2 => array($this->_mailingID_B, 'Integer'), + 3 => array($this->_mailingID_C, 'Integer'), + )); + $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id'])); + + $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing_abtesting WHERE id = %1", array( + 1 => array($result['id'], 'Integer'), + )); + $this->assertDBQuery(0, "SELECT count(*) FROM civicrm_mailing WHERE id IN (%1,%2,%3)", array( + 1 => array($this->_mailingID_A, 'Integer'), + 2 => array($this->_mailingID_B, 'Integer'), + 3 => array($this->_mailingID_C, 'Integer'), + )); } public function testMailingABRecipientsUpdate() { -- 2.25.1