Enable api_v3_syntaxConformaceTest::testInvalidID_delete (#9068)
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 27 Sep 2016 21:47:30 +0000 (07:47 +1000)
committercolemanw <coleman@civicrm.org>
Tue, 27 Sep 2016 21:47:30 +0000 (17:47 -0400)
* Enable api_v3_SyntaxConformanceTest delete with wrong id

* Remove unneded if and remove debugging

* Remove debugging

* Tidy up fix for CustomValue tear down

23 files changed:
CRM/Contribute/BAO/ContributionSoft.php
CRM/Core/BAO/Dashboard.php
CRM/Core/BAO/OptionValue.php
CRM/Core/ManagedEntities.php
CRM/Event/BAO/Participant.php
CRM/Event/BAO/ParticipantStatusType.php
CRM/Member/BAO/MembershipStatus.php
api/v3/ActivityType.php
api/v3/ContributionSoft.php
api/v3/Dashboard.php
api/v3/Group.php
api/v3/GroupContact.php
api/v3/MembershipStatus.php
api/v3/OptionValue.php
api/v3/ParticipantStatusType.php
api/v3/SystemLog.php
api/v3/utils.php
tests/phpunit/CRM/Batch/Form/EntryTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ActivityTest.php
tests/phpunit/api/v3/CustomSearchTest.php
tests/phpunit/api/v3/CustomValueTest.php
tests/phpunit/api/v3/SyntaxConformanceTest.php

index 756c22a37c03129f18e6922b98c33faf583d4c33..5d835e957009dfa58aab365a06af6d381b864cdf 100644 (file)
@@ -230,10 +230,16 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio
   public static function del($params) {
     //delete from contribution soft table
     $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
+    $contributionSoft->id = $params['id'];
+    if (!$contributionSoft->find()) {
+      return FALSE;
+    }
+    unset($params['id']);
     foreach ($params as $column => $value) {
       $contributionSoft->$column = $value;
     }
     $contributionSoft->delete();
+    return TRUE;
   }
 
   /**
index ccc82d105c5fd80320df00a4fd5592079b80184d..d64620e3f66e81adb472e6432a7c86e6390d7ad8 100644 (file)
@@ -493,6 +493,9 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
   public static function deleteDashlet($dashletID) {
     $dashlet = new CRM_Core_DAO_Dashboard();
     $dashlet->id = $dashletID;
+    if (!$dashlet->find(TRUE)) {
+      return FALSE;
+    }
     $dashlet->delete();
     return TRUE;
   }
index a529d1b18dcf01fa95b64f214a6372c247f301bd..0fc76b3121303967383f80f41c7982cbaef70875 100644 (file)
@@ -240,9 +240,13 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
   public static function del($optionValueId) {
     $optionValue = new CRM_Core_DAO_OptionValue();
     $optionValue->id = $optionValueId;
+    if (!$optionValue->find()) {
+      return FALSE;
+    }
     if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) {
       CRM_Core_PseudoConstant::flush();
-      return $optionValue->delete();
+      $optionValue->delete();
+      return TRUE;
     }
     return FALSE;
   }
index 4de184a28c1b1d11092c1f98481327da4c5e87f4..5021859b01c0ef9a2cbabd1fa397aa7f369d5a34 100644 (file)
@@ -334,14 +334,17 @@ class CRM_Core_ManagedEntities {
         'version' => 3,
         'id' => $dao->entity_id,
       );
-      $result = civicrm_api($dao->entity_type, 'delete', $params);
-      if ($result['is_error']) {
-        $this->onApiError($dao->entity_type, 'delete', $params, $result);
-      }
+      $check = civicrm_api3($dao->entity_type, 'get', $params);
+      if ((bool) $check['count']) {
+        $result = civicrm_api($dao->entity_type, 'delete', $params);
+        if ($result['is_error']) {
+          $this->onApiError($dao->entity_type, 'delete', $params, $result);
+        }
 
-      CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
-        1 => array($dao->id, 'Integer'),
-      ));
+        CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
+          1 => array($dao->id, 'Integer'),
+        ));
+      }
     }
   }
 
index 49921a411e938aed2d9d0df83efae17642b79100..b2a608f4664a92d1c42d72f3db0e0c6d8c3342b5 100644 (file)
@@ -870,6 +870,11 @@ WHERE  civicrm_participant.id = {$participantId}
    * @return \CRM_Event_DAO_Participant
    */
   public static function deleteParticipant($id) {
+    $participant = new CRM_Event_DAO_Participant();
+    $participant->id = $id;
+    if (!$participant->find()) {
+      return FALSE;
+    }
     CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray);
 
     $transaction = new CRM_Core_Transaction();
@@ -902,8 +907,6 @@ WHERE  civicrm_participant.id = {$participantId}
       CRM_Core_BAO_Note::del($noteId, FALSE);
     }
 
-    $participant = new CRM_Event_DAO_Participant();
-    $participant->id = $id;
     $participant->delete();
 
     $transaction->commit();
index 802c7363d35d198c0e2604d184187ae89efd407f..05308d69b81cc4497a7f199c29a1f6636e09a7b0 100644 (file)
@@ -80,7 +80,9 @@ class CRM_Event_BAO_ParticipantStatusType extends CRM_Event_DAO_ParticipantStatu
 
     $dao = new CRM_Event_DAO_ParticipantStatusType();
     $dao->id = $id;
-    $dao->find(TRUE);
+    if (!$dao->find()) {
+      return FALSE;
+    }
     $dao->delete();
     return TRUE;
   }
index 3e9033b14acbdc4ecfe451ea02e94fda320b161f..5cdd7b9810107f33e7817aebdc7f92e64be2fa82 100644 (file)
@@ -207,6 +207,9 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus {
     //delete from membership Type table
     $membershipStatus = new CRM_Member_DAO_MembershipStatus();
     $membershipStatus->id = $membershipStatusId;
+    if (!$membershipStatus->find()) {
+      throw new CRM_Core_Exception(ts('Cannot delete membership status ' . $membershipStatusId));
+    }
     $membershipStatus->delete();
     CRM_Member_PseudoConstant::flush('membershipStatus');
     $membershipStatus->free();
index f54c8bf9beb86e146a4acd0fd7f06cc0f0179025..ad15847d1dd9adad1e79ec595faf95c8f3085aff 100644 (file)
@@ -117,5 +117,9 @@ function _civicrm_api3_activity_type_create_spec(&$params) {
  * @deprecated use OptionValue api
  */
 function civicrm_api3_activity_type_delete($params) {
-  return civicrm_api3_create_success(CRM_Core_BAO_OptionValue::del($params['id']), $params);
+  $result = CRM_Core_BAO_OptionValue::del($params['id']);
+  if ($result) {
+    return civicrm_api3_create_success(TRUE, $params);
+  }
+  throw new API_Exception("Failure to delete activity type id {$params['id']}");
 }
index 3bfe307f2f31146b4a1e4400df542eba840bd5f9..30b7a46e68205d6f00a0626336f0b1140938365d 100644 (file)
@@ -65,8 +65,11 @@ function _civicrm_api3_contribution_soft_create_spec(&$params) {
  */
 function civicrm_api3_contribution_soft_delete($params) {
   // Non standard BAO - we have to write custom code to cope.
-  CRM_Contribute_BAO_ContributionSoft::del(array('id' => $params['id']));
-
+  $result = CRM_Contribute_BAO_ContributionSoft::del(array('id' => $params['id']));
+  if (!$result) {
+    throw new API_Exception('Cannot delete contributionSoft ' . $params['id']);
+  }
+  civicrm_api3_create_success(TRUE);
 }
 
 /**
index 904fc837cfe9e3c1883f69923e1ce49947fec301..77f6d30bd6141e0d46e3c67f2b858f32f1101fa3 100644 (file)
@@ -93,6 +93,6 @@ function civicrm_api3_dashboard_delete($params) {
     return civicrm_api3_create_success(1, $params, 'Dashboard', 'delete');
   }
   else {
-    return civicrm_api3_create_error('Could not delete dashlet');
+    throw new API_Exception('Could not delete dashlet');
   }
 }
index c0e1188ea7d118dfe446d88a6532b7c86b3d5e3a..f53172961eaf1d0ed1c80e8881ff38c45f679a27 100644 (file)
@@ -97,7 +97,10 @@ function civicrm_api3_group_get($params) {
  *   API result array
  */
 function civicrm_api3_group_delete($params) {
-
+  $group = civicrm_api3_group_get(array('id' => $params['id']));
+  if ($group['count'] == 0) {
+    throw new API_Exception('Could not delete group ' . $params['id']);
+  }
   CRM_Contact_BAO_Group::discard($params['id']);
   return civicrm_api3_create_success(TRUE);
 }
index 1546b692a9c52f6d27f5093445f05b90519832f0..e296e18297ad20c8cf3c66468f35954fbdb0353d 100644 (file)
@@ -146,6 +146,10 @@ function civicrm_api3_group_contact_create($params) {
  * @deprecated
  */
 function civicrm_api3_group_contact_delete($params) {
+  $groupContact = civicrm_api3('GroupContact', 'get', $params);
+  if ($groupContact['count'] == 0) {
+    throw new API_Exception('Cannot Delete GroupContact');
+  }
   $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted');
   // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
   return civicrm_api3_group_contact_create($params);
index 499c823fe35736d25460b3c97561462e9aabb7ed..696de0ad4a331d422a6b8350c147fad090c2de88 100644 (file)
@@ -126,7 +126,10 @@ function civicrm_api3_membership_status_update($params) {
 function civicrm_api3_membership_status_delete($params) {
 
   $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del($params['id'], TRUE);
-  return $memberStatusDelete ? civicrm_api3_create_error($memberStatusDelete['error_message']) : civicrm_api3_create_success();
+  if ($memberStatusDelete) {
+    throw new API_Exception($memberStatusDelete['error_message']);
+  }
+  return civicrm_api3_create_success();
 }
 
 /**
index e34f3ec28b44bd0cf8a7ab6f506046a288437960..2e5cd6d4a14030c2888f8d802064c847b553e327 100644 (file)
@@ -106,11 +106,12 @@ function _civicrm_api3_option_value_create_spec(&$params) {
 function civicrm_api3_option_value_delete($params) {
   // We will get the option group id before deleting so we can flush pseudoconstants.
   $optionGroupID = civicrm_api('option_value', 'getvalue', array('version' => 3, 'id' => $params['id'], 'return' => 'option_group_id'));
-  if (CRM_Core_BAO_OptionValue::del((int) $params['id'])) {
+  $result = CRM_Core_BAO_OptionValue::del($params['id']);
+  if ($result) {
     civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID));
     return civicrm_api3_create_success();
   }
   else {
-    civicrm_api3_create_error('Could not delete OptionValue ' . $params['id']);
+    throw new API_Exception('Could not delete OptionValue ' . $params['id']);
   }
 }
index fc3549f995fe68667f90382152d59e79ee1ba6a6..c433fc5b9aee790c30bee5b5b5c7f20b295fda10 100644 (file)
@@ -77,5 +77,5 @@ function civicrm_api3_participant_status_type_delete($params) {
     return civicrm_api3_create_success(TRUE);
   }
 
-  return civicrm_api3_create_error(TRUE);
+  throw new API_Exception('Could not delete participant status type id ' . $params['id']);
 }
index 9b624639e44ba2d436c5fb771c21fa20be3abb21..b1248f6c2b8913333d180b9f3c141b4b08abf92a 100644 (file)
@@ -39,7 +39,7 @@
  * @return array
  */
 function civicrm_api3_system_log_delete($params) {
-  return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'SystemLog');
+  return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
 }
 
 /**
index 078d9717e26993ac76724a5127b1ba309b607a73..96f616b32e60236b1a41c4e78ce17eb6cfbb0b30 100644 (file)
@@ -1457,9 +1457,14 @@ function _civicrm_api3_basic_delete($bao_name, &$params) {
   _civicrm_api3_check_edit_permissions($bao_name, array('id' => $params['id']));
   $args = array(&$params['id']);
   if (method_exists($bao_name, 'del')) {
-    $bao = call_user_func_array(array($bao_name, 'del'), $args);
-    if ($bao !== FALSE) {
-      return civicrm_api3_create_success(TRUE);
+    $dao = new $bao_name();
+    $dao->id = $params['id'];
+    if ($dao->find()) {
+      $bao = call_user_func_array(array($bao_name, 'del'), $args);
+      if ($bao !== FALSE) {
+        return civicrm_api3_create_success();
+      }
+      throw new API_Exception('Could not delete entity id ' . $params['id']);
     }
     throw new API_Exception('Could not delete entity id ' . $params['id']);
   }
index ad5e4c696ea591121bbf8850b521b97b440f8598..ef40bb28e6135a4f66e5f2d9dd57e1fb82274006 100644 (file)
@@ -146,7 +146,9 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     if ($this->callAPISuccessGetCount('membership', array('id' => $this->_membershipTypeID))) {
       $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
     }
-    $this->membershipStatusDelete($this->_membershipStatusID);
+    if ($this->callAPISuccessGetCount('MembershipStatus', array('id' => $this->_membershipStatusID))) {
+      $this->membershipStatusDelete($this->_membershipStatusID);
+    }
     $this->contactDelete($this->_contactID);
     $this->contactDelete($this->_contactID2);
     $this->contactDelete($this->_orgContactID);
index 1d952657b92634345cd269ef53dec5f253606855..0aab1a3d55c53307fa3034bd383012205d447115 100644 (file)
@@ -1266,7 +1266,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    */
   public function relationshipTypeDelete($relationshipTypeID) {
     $params['id'] = $relationshipTypeID;
-    $this->callAPISuccess('relationship_type', 'delete', $params);
+    $check = $this->callAPISuccess('relationship_type', 'get', $params);
+    if (!empty($check['count'])) {
+      $this->callAPISuccess('relationship_type', 'delete', $params);
+    }
   }
 
   /**
@@ -1621,7 +1624,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $params = array(
       'id' => $participantID,
     );
-    return $this->callAPISuccess('Participant', 'delete', $params);
+    $check = $this->callAPISuccess('Participant', 'get', $params);
+    if ($check['count'] > 0) {
+      return $this->callAPISuccess('Participant', 'delete', $params);
+    }
   }
 
   /**
index 27611c362dc9ad9cd8cd3bd5e48867581333182d..21e7f236cf77e30c54841a20be86eda4deb996e4 100644 (file)
@@ -106,7 +106,10 @@ class api_v3_ActivityTest extends CiviUnitTestCase {
       'civicrm_uf_match',
     );
     $this->quickCleanup($tablesToTruncate, TRUE);
-    $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id));
+    $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id));
+    if (!empty($type['count'])) {
+      $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id));
+    }
   }
 
   /**
index e6c38ea2be326a087a3245513e88adb718403b0a..ebf7cc31134d86c524eaf7cc678a84d2f998ce85 100644 (file)
@@ -57,10 +57,12 @@ class api_v3_CustomSearchTest extends CiviUnitTestCase {
       AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "custom_search") ');
     $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
       WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"');
-
-    $result = $this->callAPISuccess('CustomSearch', 'delete', array(
-      'id' => $entityId,
-    ));
+    $check = $this->callAPISuccess('CustomSearch', 'get', array('id' => $entityId));
+    if (!empty($check['count'])) {
+      $result = $this->callAPISuccess('CustomSearch', 'delete', array(
+        'id' => $entityId,
+      ));
+    }
     $this->assertEquals(1, $result['count']);
     $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
index 6e63b6e7983c20de3dea10f0ecad5df031c8c20a..7bfd07f05241dc5492f7593e566d2641c2476a9e 100644 (file)
@@ -94,7 +94,10 @@ class api_v3_CustomValueTest extends CiviUnitTestCase {
     if (!empty($this->optionGroup)) {
       foreach ($this->optionGroup as $type => $value) {
         if (!empty($value['id'])) {
-          $this->callAPISuccess('OptionGroup', 'delete', array('id' => $value['id']));
+          $count = $this->callAPISuccess('OptionGroup', 'get', array('id' => $value['id']));
+          if ((bool) $count['count']) {
+            $this->callAPISuccess('OptionGroup', 'delete', array('id' => $value['id']));
+          }
         }
       }
     }
index 78cfdfe20f3fed33a652ecd9987ed5bbbc79f306..c32b983005d1676b863fd80a1cfa7238f15b61a2 100644 (file)
@@ -1363,11 +1363,8 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    * @throws \PHPUnit_Framework_IncompleteTestError
    */
   public function testInvalidID_delete($Entity) {
-    // turn test off for now
-    $this->markTestIncomplete("Entity [ $Entity ] cannot be mocked - no known DAO");
-    return;
     if (in_array($Entity, $this->toBeImplemented['delete'])) {
-      // $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
+      $this->markTestIncomplete("civicrm_api3_{$Entity}_delete to be implemented");
       return;
     }
     $result = $this->callAPIFailure($Entity, 'Delete', array('id' => 999));