CRM-15578 - MailingAB API - Propagate deletion to any included mailings.
authorTim Otten <totten@civicrm.org>
Wed, 17 Dec 2014 01:21:16 +0000 (17:21 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 17 Dec 2014 03:06:26 +0000 (19:06 -0800)
CRM/Mailing/BAO/MailingAB.php
tests/phpunit/api/v3/MailingABTest.php

index c78a60c44530d3beee398cbb5d1e234806c1b398..5e36f3f50c1e55dbdaa53245c064f7f615502f30 100644 (file)
@@ -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);
+    });
   }
 }
index 71ec78ecc6394a73a2d893c7a7201d99a5ccad79..1f2c20fb469d179a8d06bccd74ac0d339c79117e 100755 (executable)
@@ -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() {