Test fixes
authormonishdeb <monish.deb@webaccessglobal.com>
Wed, 9 Jul 2014 09:54:36 +0000 (15:24 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Wed, 9 Jul 2014 09:54:36 +0000 (15:24 +0530)
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/MembershipTest.php

index bbbd598235d68cf526b1159171b64afa07f3cc39..b36c7f8c99d5aa520064b82e9ae2bf40bf33a64f 100644 (file)
@@ -58,7 +58,7 @@ define('API_LATEST_VERSION', 3);
 class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
   /**
-   * api version - easier to override than just a defin
+   * api version - easier to override than just a define
    */
   protected $_apiversion = API_LATEST_VERSION;
   /**
@@ -851,38 +851,28 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Organisation, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple organizations
    *
    * @return int    id of Organisation created
    */
-  function organizationCreate($params = array()) {
+  function organizationCreate($params = array(), $seq = 0) {
     if (!$params) {
       $params = array();
     }
-    $orgParams = array(
-      'organization_name' => 'Unit Test Organization',
-      'contact_type' => 'Organization',
-    );
-    return $this->_contactCreate(array_merge($orgParams, $params));
+    $params = array_merge($this->sampleContact('Organization', $seq), $params);
+    return $this->_contactCreate($params);
   }
 
   /**
    * Generic function to create Individual, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple individuals
    *
    * @return int    id of Individual created
    */
-  function individualCreate($params = array()) {
-    $params = array_merge(array(
-        'first_name' => 'Anthony',
-        'middle_name' => 'J.',
-        'last_name' => 'Anderson',
-        'prefix_id' => 3,
-        'suffix_id' => 3,
-        'email' => 'anthony_anderson@civicrm.org',
-        'contact_type' => 'Individual',
-      ), $params);
-
+  function individualCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Individual', $seq), $params);
     return $this->_contactCreate($params);
   }
 
@@ -890,17 +880,53 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Household, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple households
    *
    * @return int    id of Household created
    */
-  function householdCreate($params =  array()) {
-    $params = array_merge(array(
-        'household_name' => 'Unit Test household',
-        'contact_type' => 'Household',
-      ), $params);
+  function householdCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Organization', $seq), $params);
     return $this->_contactCreate($params);
   }
 
+  /**
+   * Helper function for getting sample contact properties
+   *
+   * @param enum     contact type: Individual, Organization
+   * @param int      sequence number for the values of this type
+   *
+   * @return array   properties of sample contact (ie. $params for API call)
+   */
+  function sampleContact($contact_type, $seq = 0) {
+    $samples = array(
+      'Individual' => array(
+        // The number of values in each list need to be coprime numbers to not have duplicates
+        'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
+        'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
+        'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
+      ),
+      'Organization' => array(
+        'organization_name' => array('Unit Test Organization', 'Acme', 'Roberts and Sons', 'Cryo Space Labs', 'Sharper Pens'),
+      ),
+      'Household' => array(
+        'household_name' => array('Unit Test household'),
+      ),
+    );
+    $params = array('contact_type' => $contact_type);
+    foreach ($samples[$contact_type] as $key => $values) {
+      $params[$key] = $values[$seq % sizeof($values)];
+    }
+    if ($contact_type == 'Individual' ) {
+        $employer = $this->sampleContact('Organization', $seq);
+        $params['email'] = strtolower(
+          $params['first_name'] . '.' . $params['last_name'] . '@civicrm.org'
+        );
+        $params['prefix_id'] = 3;
+        $params['suffix_id'] = 3;
+    }
+    return $params;
+  }
+
   /**
    * Private helper function for calling civicrm_contact_add
    *
index 84747021b31f23a272f537d474d3c32977cc11a9..06f7b2cef1f70ce03a39e5f82b674dfdb92f92e7 100644 (file)
@@ -437,6 +437,132 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($memberContactId);
   }
 
+  /**
+   * Test civicrm_membership_create with relationships.
+   * create/get Memberships.
+   *
+   * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
+   * and max_related property for Membership_Type and Membership entities
+   */
+  function testCreateWithRelationship() {
+    // Create membership type: inherited through employment, max_related = 2
+    $params = array(
+      'name_a_b' => 'Employee of',
+    );
+    $result = $this->callAPISuccess('relationship_type', 'get', $params);
+    $relationshipTypeId = $result['id'];
+    $membershipOrgId = $this->organizationCreate();
+    $params = array(
+      'name' => 'Corporate Membership',
+      'duration_unit' => 'year',
+      'duration_interval' => 1,
+      'period_type' => 'rolling',
+      'member_of_contact_id' => $membershipOrgId,
+      'domain_id' => 1,
+      'financial_type_id' => 1,
+      'relationship_type_id' => $relationshipTypeId,
+      'relationship_direction' => 'b_a',
+      'max_related' => 2,
+      'is_active' => 1,
+    );
+    $result = $this->callAPISuccess('membership_type', 'create', $params);
+    $membershipTypeId = $result['id'];
+
+    // Create employer and first employee
+    $employerId[0] = $this->organizationCreate(array(), 1);
+    $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
+
+    // Create organization's membership
+    $params = array(
+      'contact_id' => $employerId[0],
+      'membership_type_id' => $membershipTypeId,
+      'source' => 'Test suite',
+      'start_date' => date('Y-m-d'),
+      'end_date' => "+1 year",
+    );
+    $OrganizationMembershipID = $this->contactMembershipCreate($params);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[0],
+      'membership_type_id' => $membershipTypeId,
+    );
+
+    $result = $this->callAPISuccess('membership', 'get', $params);
+
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // Create second employee
+    $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[1],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    //exit;
+    // If it fails here CRM-14758 is not fixed
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // Create third employee
+    $memberContactId[2] = $this->individualCreate(array('current_employer' => $employerId[0]), 2);
+
+    // Check that employee does NOT inherit the membership (max_related = 2)
+    $params = array(
+      'contact_id' => $memberContactId[2],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Increase max_related for the employer's membership
+    $params = array(
+      'id' => $OrganizationMembershipID,
+      'max_related' => 3,
+    );
+    $this->contactMembershipCreate($params);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[2],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // First employee moves to a new job
+    $employerId[1] = $this->organizationCreate(array(), 2);
+    $params = array(
+      'id' => $memberContactId[0],
+      'employer_id' => $employerId[1],
+    );
+    $this->callAPISuccess('contact', 'create', $params);
+
+    // Check that employee does NO LONGER inherit the membership
+    $params = array(
+      'contact_id' => $memberContactId[0],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Tear down - reverse of creation to be safe
+    $this->contactDelete($memberContactId[2]);
+    $this->contactDelete($memberContactId[1]);
+    $this->contactDelete($memberContactId[0]);
+    $this->contactDelete($employerId[1]);
+    $this->contactDelete($employerId[0]);
+    $this->membershipTypeDelete(array('id' => $membershipTypeId));
+    $this->contactDelete($membershipOrgId);
+  }
+
   /**
    * We are checking for no enotices + only id & end_date returned
    */
@@ -866,4 +992,3 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
 
    }
 }
-