CRM-15889 - More specific condition and add test coverage
authorColeman Watts <coleman@civicrm.org>
Sat, 28 Feb 2015 20:19:43 +0000 (15:19 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 28 Feb 2015 20:19:43 +0000 (15:19 -0500)
CRM/Contact/BAO/Relationship.php
tests/phpunit/api/v3/MembershipTest.php

index 58b05f78ee40ba8014fa8552c90ad9ad5493bdbe..f093f93f5295067c026d39a426e247dd5ac76aef 100644 (file)
@@ -66,7 +66,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
     }
 
     //alter related membership if the is_active param is changed
-    if (!empty($params['id'])) {
+    if (!empty($params['id']) && array_key_exists('is_active', $params)) {
       $action = CRM_Core_Action::DISABLE;
       if (!empty($params['is_active'])) {
         $action = CRM_Core_Action::ENABLE;
index ed53da2ec1c16d79bcedd151391c67c6fad07350..468ac93190782ddf9e9c9574705ca872206f310f 100644 (file)
@@ -553,6 +553,33 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
     $result = $this->callAPISuccess('membership', 'get', $params);
     $this->assertEquals(0, $result['count']);
 
+    // Set up params for enable/disable checks
+    $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1]));
+    $params = array(
+      'contact_id' => $memberContactId[1],
+      'membership_type_id' => $membershipTypeId,
+    );
+
+    // Deactivate relationship using create and assert membership is not inherited
+    $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0));
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Re-enable relationship using create and assert membership is inherited
+    $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1));
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $result['count']);
+
+    // Deactivate relationship using setvalue and assert membership is not inherited
+    $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0));
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Re-enable relationship using setvalue and assert membership is inherited
+    $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1));
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $result['count']);
+
     // Tear down - reverse of creation to be safe
     $this->contactDelete($memberContactId[2]);
     $this->contactDelete($memberContactId[1]);