Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
index a6535a84f944d4449c8e0b9e253d0c64b36cee23..0b8a8d2115485f17941e625339240de997752136 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -34,6 +34,11 @@ require_once 'CiviTest/CiviUnitTestCase.php';
 class api_v3_RelationshipTest extends CiviUnitTestCase {
   protected $_apiversion = 3;
   protected $_cId_a;
+  /**
+   * second individual
+   * @var integer
+   */
+  protected $_cId_a_2;
   protected $_cId_b;
   protected $_cId_b2;// second org
   protected $_relTypeID;
@@ -53,10 +58,11 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
 
   function setUp() {
     parent::setUp();
-    $this->_cId_a      = $this->individualCreate(NULL);
-    $this->_cId_b      = $this->organizationCreate();
-    $this->_cId_b2      = $this->organizationCreate(array('organization_name' => ' Org 2'));
-    $this->_entity     = 'relationship';
+    $this->_cId_a = $this->individualCreate();
+    $this->_cId_a_2 = $this->individualCreate(array('last_name' => 'c2', 'email' => 'c@w.com', 'contact_type' => 'Individual'));
+    $this->_cId_b = $this->organizationCreate();
+    $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2'));
+    $this->_entity = 'relationship';
     //Create a relationship type
     $relTypeParams = array(
       'name_a_b' => 'Relation 1 for delete',
@@ -80,10 +86,12 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
   }
 
   function tearDown() {
-    $this->quickCleanup(array('civicrm_relationship'), TRUE);
-    $this->relationshipTypeDelete($this->_relTypeID);
     $this->contactDelete($this->_cId_a);
+    $this->contactDelete($this->_cId_a_2);
     $this->contactDelete($this->_cId_b);
+    $this->contactDelete($this->_cId_b2);
+    $this->quickCleanup(array('civicrm_relationship'), TRUE);
+    $this->relationshipTypeDelete($this->_relTypeID);
   }
 
   ///////////////// civicrm_relationship_create methods
@@ -214,7 +222,6 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'debug' => 1,
     );
     $result = $this->callAPISuccess('relationship', 'create', $params);
-    $this->assertAPISuccess($result, 'in line ' . __LINE__);
     $result = $this->callAPISuccess('relationship', 'get', $params);
     $params['id'] = $relationship['id'];
     $result = $this->callAPISuccess('relationship', 'delete', $params);
@@ -263,7 +270,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
     );
 
     $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__);
-    $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+    $this->assertNotNull($result['id']);
     $relationParams = array(
       'id' => $result['id'],
     );
@@ -276,12 +283,27 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       if ($key == 'note') {
         continue;
       }
-      $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
+      $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
     }
     $params['id'] = $result['id'];
     $this->callAPISuccess('relationship', 'delete', $params);
   }
-
+  /**
+   * ensure disabling works
+   */
+  function testRelationshipUpdate() {
+    $result = $this->callAPISuccess('relationship', 'create', $this->_params);
+    $relID = $result['id'];
+    $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
+    $this->assertEquals($relID, $result['id']);
+    $this->assertEquals('blah', $result['values'][$result['id']]['description']);
+    $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
+    $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
+    $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
+    $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
+    $this->assertEquals('blah', $result['values'][$result['id']]['description']);
+    $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
+  }
   /**
    * check relationship creation
    */
@@ -297,7 +319,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
     );
 
     $result = $this->callAPISuccess('relationship', 'create', $params);
-    $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+    $this->assertNotNull($result['id']);
     $relationParams = array(
       'id' => $result['id'],
     );
@@ -500,8 +522,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'is_active' => 1,
     );
 
-    $result = $this->callAPIFailure('relationship', 'delete', $params);
-    $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
+    $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
   }
 
   /**
@@ -514,10 +535,10 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'relationship_type_id' => 'Breaking Relationship',
     );
 
-    $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__);
+    $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
 
     $params['id'] = "Invalid";
-    $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID', 'in line ' . __LINE__);
+    $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
   }
 
   /**
@@ -548,8 +569,8 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
    * check with empty array
    */
   function testRelationshipUpdateEmpty() {
-    $result = $this->callAPIFailure('relationship', 'create', array());
-    $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__);
+    $result = $this->callAPIFailure('relationship', 'create', array(),
+      'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
   }
 
   /**
@@ -571,7 +592,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
 
     $result = $this->callAPISuccess('relationship', 'create', $relParams);
 
-    $this->assertNotNull($result['id'], 'In line ' . __LINE__);
+    $this->assertNotNull($result['id']);
     $this->_relationID = $result['id'];
 
     $params = array(
@@ -583,7 +604,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'is_active' => 0,
     );
 
-    $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists', 'In line ' . __LINE__);
+    $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
 
     //delete created relationship
     $params = array(
@@ -616,18 +637,18 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'contact_id' => $this->_cId_b,
     );
     $result = $this->callAPISuccess('relationship', 'get', $params);
-    $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
+    $this->assertEquals($result['count'], 1);
     $params = array(
       'contact_id_a' => $this->_cId_a,
     );
     $result = $this->callAPISuccess('relationship', 'get', $params);
-    $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
+    $this->assertEquals($result['count'], 1);
     // contact_id_a is wrong so should be no matches
     $params = array(
       'contact_id_a' => $this->_cId_b,
     );
     $result = $this->callAPISuccess('relationship', 'get', $params);
-    $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
+    $this->assertEquals($result['count'], 0);
   }
 
   /**
@@ -782,8 +803,8 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
       'contact_type_a' => 'Individual',
       'contact_type_b' => 'Organization',
     );
-    $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
-    $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__);
+    $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
+      'id is not a valid integer');
   }
 
   /**
@@ -806,7 +827,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
     );
 
     $result = $this->callAPISuccess('relationship', 'get', $contacts);
-    $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__);
+    $this->assertGreaterThan(0, $result['count']);
     $params = array(
       'id' => $relationship['id'],
     );
@@ -840,5 +861,232 @@ class api_v3_RelationshipTest extends CiviUnitTestCase {
     $result = $this->callAPISuccess('relationship', 'delete', $params);
     $this->relationshipTypeDelete($this->_relTypeID);
   }
-}
 
+  /**
+   * Checks that passing in 'contact_id' + a relationship type
+   * will filter by relationship type (relationships go in both directions)
+   * as relationship api does a reciprocal check if contact_id provided
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByTypeReciprocal() {
+    $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id' => $this->_cId_a,
+      'relationship_type_id' => $this->_relTypeID,
+    ));
+    $this->assertEquals(1, $result['count']);
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id' => $this->_cId_a,
+      'relationship_type_id' => $this->_relTypeID + 1,
+    ));
+    $this->assertEquals(0, $result['count']);
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
+  }
+
+  /**
+   * Checks that passing in 'contact_id_b' + a relationship type
+   * will filter by relationship type for contact b
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByTypeDAO() {
+    $this->ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE,)  + $this->_params);
+    $result = $this->callAPISuccess($this->_entity, 'getcount', array(
+      'contact_id_a' => $this->_cId_a,),
+    1);
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id_a' => $this->_cId_a,
+      'relationship_type_id' => $this->_relTypeID,
+    ));
+    $this->assertEquals(1, $result['count']);
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id_a' => $this->_cId_a,
+      'relationship_type_id' => $this->_relTypeID + 1,
+    ));
+    $this->assertEquals(0, $result['count']);
+  }
+
+  /**
+   * Checks that passing in 'contact_id_b' + a relationship type
+   * will filter by relationship type for contact b
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByTypeArrayDAO() {
+    $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $org3 = $this->organizationCreate();
+    $relType2 = 5; // lets just assume built in ones aren't being messed with!
+    $relType3 = 6; // lets just assume built in ones aren't being messed with!
+
+    //relationshp 2
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType2,
+        'contact_id_b' => $this->_cId_b2))
+    );
+
+    //relationshp 3
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType3,
+        'contact_id_b' => $org3))
+    );
+
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id_a' => $this->_cId_a,
+      'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
+    ));
+
+    $this->assertEquals(2, $result['count']);
+    foreach ($result['values'] as $key => $value) {
+      $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
+    }
+  }
+
+  /**
+   * Checks that passing in 'contact_id_b' + a relationship type
+   * will filter by relationship type for contact b
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByTypeArrayReciprocal() {
+    $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $org3 = $this->organizationCreate();
+    $relType2 = 5; // lets just assume built in ones aren't being messed with!
+    $relType3 = 6; // lets just assume built in ones aren't being messed with!
+
+    //relationshp 2
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType2,
+        'contact_id_b' => $this->_cId_b2))
+    );
+
+    //relationshp 3
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType3,
+        'contact_id_b' => $org3))
+    );
+
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id' => $this->_cId_a,
+      'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
+    ));
+
+    $this->assertEquals(2, $result['count']);
+    foreach ($result['values'] as $key => $value) {
+      $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
+    }
+  }
+
+  /**
+   * Checks that passing in 'contact_id_b' + a relationship type
+   * will filter by relationship type for contact b
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByMembershipTypeDAO() {
+    $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $org3 = $this->organizationCreate();
+
+    $relType2 = 5; // lets just assume built in ones aren't being messed with!
+    $relType3 = 6; // lets just assume built in ones aren't being messed with!
+    $relType1 = 1;
+    $memberType = $this->membershipTypeCreate(array(
+      'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
+      'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
+    ));
+
+    //relationshp 2
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType2,
+        'contact_id_b' => $this->_cId_b2))
+    );
+
+    //relationshp 3
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType3,
+        'contact_id_b' => $org3))
+    );
+
+    //relationshp 4 with reveral
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType1,
+        'contact_id_a' => $this->_cId_a,
+        'contact_id_b' => $this->_cId_a_2))
+    );
+
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id_a' => $this->_cId_a,
+      'membership_type_id' => $memberType,
+    ));
+    // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
+    $this->assertEquals(1, $result['count']);
+    foreach ($result['values'] as $key => $value) {
+      $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
+    }
+  }
+
+  /**
+   * Checks that passing in 'contact_id_b' + a relationship type
+   * will filter by relationship type for contact b
+   *
+   * We should get 1 result without or with correct relationship type id & 0 with
+   * an incorrect one
+   */
+  function testGetRelationshipByMembershipTypeReciprocal() {
+      $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $org3 = $this->organizationCreate();
+
+    $relType2 = 5; // lets just assume built in ones aren't being messed with!
+    $relType3 = 6; // lets just assume built in ones aren't being messed with!
+    $relType1 = 1;
+    $memberType = $this->membershipTypeCreate(array(
+      'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
+      'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
+    ));
+
+    //relationshp 2
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType2,
+        'contact_id_b' => $this->_cId_b2))
+    );
+
+    //relationshp 3
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType3,
+        'contact_id_b' => $org3))
+    );
+
+    //relationshp 4 with reveral
+    $this->callAPISuccess($this->_entity, 'create',
+      array_merge($this->_params, array(
+        'relationship_type_id' => $relType1,
+        'contact_id_a' => $this->_cId_a,
+        'contact_id_b' => $this->_cId_a_2))
+    );
+
+    $result = $this->callAPISuccess($this->_entity, 'get', array(
+      'contact_id' => $this->_cId_a,
+      'membership_type_id' => $memberType,
+    ));
+    // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
+    $this->assertEquals(2, $result['count']);
+
+    foreach ($result['values'] as $key => $value) {
+      $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
+    }
+  }
+}