Merge pull request #9187 from sqweets/ExportHeadersRelationships
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
index b8396cb74b84a43d60597eccf6fb14ec2655f8c1..a0afa3eb9f50f1fb003e9b6170e8eb092383816f 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -25,9 +25,6 @@
  +--------------------------------------------------------------------+
  */
 
-require_once 'CiviTest/CiviUnitTestCase.php';
-
-
 /**
  *  Test APIv3 civicrm_entity_tag_* functions
  *
@@ -35,13 +32,15 @@ require_once 'CiviTest/CiviUnitTestCase.php';
  * @subpackage API_Core
  */
 
-require_once 'CiviTest/CiviUnitTestCase.php';
-
 /**
- * Class api_v3_EntityTagTest
+ * Class api_v3_EntityTagTest.
+ * @group headless
  */
 class api_v3_EntityTagTest extends CiviUnitTestCase {
 
+  /**
+   * @var int
+   */
   protected $_individualID;
   protected $_householdID;
   protected $_organizationID;
@@ -50,68 +49,67 @@ class api_v3_EntityTagTest extends CiviUnitTestCase {
   protected $_tag;
   protected $_entity = 'entity_tag';
 
+  /**
+   * Basic parameters for create.
+   *
+   * @var array
+   */
+  protected $_params = array();
 
+  /**
+   * Set up for test.
+   */
   public function setUp() {
     parent::setUp();
     $this->useTransaction(TRUE);
 
     $this->_individualID = $this->individualCreate();
-    $this->_tag = $this->tagCreate();
+    $this->_tag = $this->tagCreate(array('name' => 'EntityTagTest'));
     $this->_tagID = $this->_tag['id'];
     $this->_householdID = $this->houseHoldCreate();
     $this->_organizationID = $this->organizationCreate();
-  }
-
-  public function testAddEmptyParams() {
-    $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params = array(),
-      'contact_id is a required field'
+    $this->_params = array(
+      'contact_id' => $this->_individualID,
+      'tag_id' => $this->_tagID,
     );
   }
 
-  public function testAddWithoutTagID() {
-    $params = array(
-      'contact_id' => $this->_individualID,
-    );
-    $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
+  /**
+   * Test required parameters.
+   *
+   * These failure tests are low value and may not be worth putting in v4.
+   */
+  public function testFailureTests() {
+    $this->callAPIFailure('entity_tag', 'create', array('contact_id' => $this->_individualID),
       'tag_id is a required field'
     );
-  }
-
-  public function testAddWithoutContactID() {
-    $params = array(
-      'tag_id' => $this->_tagID,
+    $this->callAPIFailure('entity_tag', 'create', array('tag_id' => $this->_tagID),
+      'contact_id is a required field'
     );
-    $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
-      'contact_id is a required field');
   }
 
+  /**
+   * Test basic create.
+   */
   public function testContactEntityTagCreate() {
-    $params = array(
-      'contact_id' => $this->_individualID,
-      'tag_id' => $this->_tagID,
-    );
-
-    $result = $this->callAPISuccess('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
+    $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
     $this->assertEquals($result['added'], 1);
   }
 
+  /**
+   * Test multiple add functionality.
+   *
+   * This needs review for api v4 as it makes for a very non standard api.
+   */
   public function testAddDouble() {
-    $individualId = $this->_individualID;
-    $organizationId = $this->_organizationID;
-    $tagID = $this->_tagID;
-    $params = array(
-      'contact_id' => $individualId,
-      'tag_id' => $tagID,
-    );
-
-    $result = $this->callAPISuccess('entity_tag', 'create', $params);
 
+    $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
     $this->assertEquals($result['added'], 1);
 
     $params = array(
-      'contact_id_i' => $individualId,
-      'contact_id_o' => $organizationId,
-      'tag_id' => $tagID,
+      'contact_id_i' => $this->_individualID,
+      'contact_id_o' => $this->_organizationID,
+      'tag_id' => $this->_tagID,
     );
 
     $result = $this->callAPISuccess('entity_tag', 'create', $params);
@@ -120,68 +118,58 @@ class api_v3_EntityTagTest extends CiviUnitTestCase {
   }
 
   /**
-   * civicrm_entity_tag_get methods.
+   * Test that get works without an entity.
    */
   public function testGetNoEntityID() {
-    $ContactId = $this->_individualID;
-    $tagID = $this->_tagID;
-    $params = array(
-      'contact_id' => $ContactId,
-      'tag_id' => $tagID,
-    );
-
-    $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
-    $this->assertEquals($individualEntity['added'], 1);
-    $result = $this->callAPISuccess($this->_entity, 'get', array('sequential' => 1, 'tag_id' => $tagID));
-    $this->assertEquals($ContactId, $result['values'][0]['entity_id']);
+    $this->callAPISuccess('entity_tag', 'create', $this->_params);
+    $result = $this->callAPISuccess($this->_entity, 'get', array('sequential' => 1, 'tag_id' => $this->_tagID));
+    $this->assertEquals($this->_individualID, $result['values'][0]['entity_id']);
   }
 
+  /**
+   * Basic get functionality test.
+   */
   public function testIndividualEntityTagGet() {
-    $contactId = $this->_individualID;
-    $tagID = $this->_tagID;
-    $params = array(
-      'contact_id' => $contactId,
-      'tag_id' => $tagID,
-    );
-
-    $individualEntity = $this->callAPIAndDocument('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
+    $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params);
     $this->assertEquals($individualEntity['added'], 1);
 
     $paramsEntity = array(
-      'contact_id' => $contactId,
+      'contact_id' => $this->_individualID,
     );
-    $entity = $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
+    $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
   }
 
-  public function testHouseholdEntityGet() {
-    $ContactId = $this->_householdID;
-    $tagID = $this->_tagID;
+  /**
+   * Test tag can be added to a household.
+   */
+  public function testHouseholdEntityCreate() {
     $params = array(
-      'contact_id' => $ContactId,
-      'tag_id' => $tagID,
+      'contact_id' => $this->_householdID,
+      'tag_id' => $this->_tagID,
     );
 
     $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
     $this->assertEquals($householdEntity['added'], 1);
   }
 
+  /**
+   * Test tag can be added to an organization.
+   */
   public function testOrganizationEntityGet() {
-    $ContactId = $this->_organizationID;
-    $tagID = $this->_tagID;
+
     $params = array(
-      'contact_id' => $ContactId,
-      'tag_id' => $tagID,
+      'contact_id' => $this->_organizationID,
+      'tag_id' => $this->_tagID,
     );
 
     $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
     $this->assertEquals($organizationEntity['added'], 1);
 
-    $paramsEntity = array('contact_id' => $ContactId);
-    $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
+    $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID));
   }
 
   /**
-   * civicrm_entity_tag_Delete methods.
+   * Civicrm_entity_tag_Delete methods.
    */
   public function testEntityTagDeleteNoTagId() {
     $entityTagParams = array(
@@ -258,20 +246,6 @@ class api_v3_EntityTagTest extends CiviUnitTestCase {
     $this->assertEquals($result['not_removed'], 1);
   }
 
-  /**
-   * civicrm_tag_entities_get methods.
-   */
-  public function testCommonContactEntityTagAdd() {
-    $params = array(
-      'contact_id' => $this->_individualID,
-      'tag_id' => $this->_tagID,
-    );
-
-    $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
-    $this->assertEquals($individualEntity['added'], 1);
-  }
-
-
   public function testEntityTagCommonDeleteINDHH() {
     $entityTagParams = array(
       'contact_id_i' => $this->_individualID,
@@ -326,4 +300,30 @@ class api_v3_EntityTagTest extends CiviUnitTestCase {
     $this->assertEquals($result['not_removed'], 1);
   }
 
+  public function testEntityTagJoin() {
+    $org = $this->callAPISuccess('Contact', 'create', array(
+      'contact_type' => 'Organization',
+      'organization_name' => 'Org123',
+      'api.EntityTag.create' => array(
+        'tag_id' => $this->_tagID,
+      ),
+    ));
+    // Fetch contact info via join
+    $result = $this->callAPISuccessGetSingle('EntityTag', array(
+      'return' => array("entity_id.organization_name", "tag_id.name"),
+      'entity_id' => $org['id'],
+      'entity_table' => "civicrm_contact",
+    ));
+    $this->assertEquals('Org123', $result['entity_id.organization_name']);
+    $this->assertEquals('EntityTagTest', $result['tag_id.name']);
+    // This should return no results by restricting contact_type
+    $result = $this->callAPISuccess('EntityTag', 'get', array(
+      'return' => array("entity_id.organization_name"),
+      'entity_id' => $org['id'],
+      'entity_table' => "civicrm_contact",
+      'entity_id.contact_type' => "Individual",
+    ));
+    $this->assertEquals(0, $result['count']);
+  }
+
 }