From: deb.monish Date: Wed, 27 Jul 2016 14:23:10 +0000 (+0530) Subject: CRM-19133: added unit test [follow-up PR] X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=877b164ae45262e3b5ccde4acfe374a64f0542ce;p=civicrm-core.git CRM-19133: added unit test [follow-up PR] --- diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php index bc636ba709..802119b96b 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php @@ -286,4 +286,61 @@ DELETE FROM civicrm_contact_type CRM_Contact_BAO_Contact::deleteContact($contact->id); } + /** + * Unit test to ensure that removing any subtype from CustomGroup's + * extend_for setting, won't delete any custom data of contact + * + * Success expected + */ + public function testCRM19133() { + $customGroupName = md5(microtime()); + $subtypesToPreserve = array($this->student, $this->parent); + + // Create custom group that extends student and parent subtype + $apiParams = array( + 'title' => $customGroupName, + 'extends' => array('Individual', $subtypesToPreserve), + 'is_active' => TRUE, + ); + $result = civicrm_api3('customGroup', 'create', $apiParams); + $customGroupId = $result['id']; + + // Create desired custom field + $apiParams = array( + 'debug' => 1, + 'custom_group_id' => $result['id'], + 'label' => $customGroupName, + 'html_type' => 'Text', + 'data_type' => 'String', + 'is_active' => TRUE, + ); + $result = civicrm_api3('custom_field', 'create', $apiParams); + $customFieldId = $result['id']; + + // Create contact of subtype parent and student + $params = array( + 'first_name' => 'Anne', + 'last_name' => 'Grant', + 'contact_type' => 'Individual', + 'contact_sub_type' => array($this->student, $this->parent), + ); + $contact = CRM_Contact_BAO_Contact::add($params); + + // Record custom value for desired customGroup + $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contact->id, 'custom_' . $customFieldId => 'value 1')); + + // Subtype to be removed from customGroup setting + $subtypesToBeRemoved = array($this->student); + CRM_Contact_BAO_ContactType::deleteCustomRowsOfSubtype($customGroupId, $subtypesToBeRemoved, $subtypesToPreserve); + + // Check with correct value to assert that custom data is not deleted + $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customFieldId => 'value 1')); + $this->assertEquals(1, $result['count']); + $this->assertEquals($contact->id, $result['id']); + + //Check with incorrect custom value that our previous assertion was correct + $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customFieldId => 'wrong value')); + $this->assertEquals(0, $result['count']); + } + }