Add serialization metadata for MembershipType api
authoreileen <emcnaughton@wikimedia.org>
Fri, 2 Aug 2019 23:03:32 +0000 (11:03 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 2 Aug 2019 23:30:43 +0000 (11:30 +1200)
CRM/Member/DAO/MembershipType.php
tests/phpunit/CRM/Contact/BAO/RelationshipTest.php
xml/schema/Member/MembershipType.xml

index 7ad60e02cce9a6a5c9335914abff68064bd61310..01c5b69e662937e3dd711bd7744fe2048e97a27f 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Member/MembershipType.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:b166299a8c5c846f5cb4f34fd6532f55)
+ * (GenCodeChecksum:f0aee394cff9d0c7eee0245021a4a3e1)
  */
 
 /**
@@ -405,6 +405,7 @@ class CRM_Member_DAO_MembershipType extends CRM_Core_DAO {
           'entity' => 'MembershipType',
           'bao' => 'CRM_Member_BAO_MembershipType',
           'localizable' => 0,
+          'serialize' => self::SERIALIZE_SEPARATOR_TRIMMED,
         ],
         'relationship_direction' => [
           'name' => 'relationship_direction',
@@ -417,6 +418,7 @@ class CRM_Member_DAO_MembershipType extends CRM_Core_DAO {
           'entity' => 'MembershipType',
           'bao' => 'CRM_Member_BAO_MembershipType',
           'localizable' => 0,
+          'serialize' => self::SERIALIZE_SEPARATOR_TRIMMED,
         ],
         'max_related' => [
           'name' => 'max_related',
index 382f404f96ad1d80f20a1bbeb5ae7c447486da97..7b3ed5bcecefa3364e4c8a2209496a6a94681c0a 100644 (file)
@@ -206,4 +206,69 @@ class CRM_Contact_BAO_RelationshipTest extends CiviUnitTestCase {
     return $data;
   }
 
+  /**
+   * Test that two similar memberships are not created for two relationships
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSingleMembershipForTwoRelationships() {
+    $individualID = $this->individualCreate(['display_name' => 'Individual A']);
+    $organisationID = $this->organizationCreate(['organization_name' => 'Organization B']);
+    $membershipOrganisationID = $this->organizationCreate(['organization_name' => 'Membership Organization']);
+    $orgToPersonTypeId1 = $this->relationshipTypeCreate(['name_a_b' => 'Inherited_Relationship_1_A_B', 'name_b_a' => 'Inherited_Relationship_1_B_A']);
+    $orgToPersonTypeId2 = $this->relationshipTypeCreate(['name_a_b' => 'Inherited_Relationship_2_A_B', 'name_b_a' => 'Inherited_Relationship_2_B_A']);
+
+    $membershipType = $this->callAPISuccess('MembershipType', 'create', [
+      'member_of_contact_id' => $membershipOrganisationID,
+      'financial_type_id' => 'Member Dues',
+      'duration_unit' => 'year',
+      'duration_interval' => 1,
+      'period_type' => 'rolling',
+      'name' => 'Inherited Membership',
+      'relationship_type_id' => [$orgToPersonTypeId1, $orgToPersonTypeId2],
+      'relationship_direction' => ['b_a', 'a_b'],
+    ]);
+    $membershipType = $this->callAPISuccessGetSingle('MembershipType', ['id' => $membershipType['id']]);
+    // Check the metadata worked....
+    $this->assertEquals([$orgToPersonTypeId1, $orgToPersonTypeId2], $membershipType['relationship_type_id']);
+    $this->assertEquals(['b_a', 'a_b'], $membershipType['relationship_direction']);
+
+    $this->callAPISuccess('Membership', 'create', [
+      'membership_type_id' => $membershipType['id'],
+      'contact_id' => $organisationID,
+    ]);
+
+    $relationshipOne = $this->callAPISuccess('Relationship', 'create', [
+      'contact_id_a' => $individualID,
+      'contact_id_b' => $organisationID,
+      'relationship_type_id' => $orgToPersonTypeId1,
+    ]);
+    $relationshipTwo = $this->callAPISuccess('Relationship', 'create', [
+      'contact_id_a' => $individualID,
+      'contact_id_b' => $organisationID,
+      'relationship_type_id' => $orgToPersonTypeId2,
+    ]);
+
+    /*
+     * @todo this section not yet working due to bug in would-be-tested code.
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
+    $relationshipTwo['is_active'] = 0;
+    $this->callAPISuccess('Relationship', 'create', $relationshipTwo);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
+    $relationshipOne['is_active'] = 0;
+    $this->callAPISuccess('Relationship', 'create', $relationshipOne);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
+    $relationshipOne['is_active'] = 1;
+    $this->callAPISuccess('Relationship', 'create', $relationshipOne);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
+    $relationshipTwo['is_active'] = 1;
+    $this->callAPISuccess('Relationship', 'create', $relationshipTwo);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
+    $this->callAPISuccess('Relationship', 'delete', ['id' => $relationshipTwo['id']]);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
+    $this->callAPISuccess('Relationship', 'delete', ['id' => $relationshipOne['id']]);
+    $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
+     */
+  }
+
 }
index a71afd51a7fa530b8eb6d29bcb535b690fe503d4..47a623054846f76c078f8c5ebaac8dd1522e1c87 100644 (file)
     <length>64</length>
     <comment>FK to Relationship Type ID</comment>
     <add>1.5</add>
+    <serialize>SEPARATOR_TRIMMED</serialize>
   </field>
   <foreignKey>
     <name>relationship_type_id</name>
     <type>varchar</type>
     <length>128</length>
     <add>1.7</add>
+    <serialize>SEPARATOR_TRIMMED</serialize>
   </field>
   <field>
     <name>max_related</name>