CRM-15168 fix test to cope with problems with getactions
[civicrm-core.git] / tests / phpunit / api / v3 / SyntaxConformanceTest.php
index 0f33d48d39aa2d3fe7a4ef0d93d1293b4ff0464d..b79acae7ff86b9452fe1251ea25e8cea87f45021 100644 (file)
@@ -48,6 +48,25 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
 
   protected $_entity;
 
+  /** Map custom group entities to civicrm components */
+  static $componentMap = array(
+    'Contact' => NULL,
+    'Individual' => NULL,
+    'Household' => NULL,
+    'Organization' => NULL,
+    'Contribution' => 'CiviContribute',
+    'Membership' => 'CiviMember',
+    'Participant' => 'CiviEvent',
+    'Group' => NULL,
+    'Relationship' => NULL,
+    'Event' => 'CiviEvent',
+    'Case' => 'CiviCase',
+    'Activity' => NULL,
+    'Pledge' => 'CiviPledge',
+    'Grant' => 'CiviGrant',
+    'Address' => NULL,
+  );
+
   /* they are two types of missing APIs:
        - Those that are to be implemented
          (in some future version when someone steps in -hint hint-). List the entities in toBeImplemented[ {$action} ]
@@ -74,6 +93,11 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * @param null $skip
+   *
+   * @return array
+   */
   public static function entities($skip = NULL) {
     // To only test specific entities, call phpunit with SYNTAX_CONFORMANCE_ENTITIES="TheEntityName"
     // or uncomment this line:
@@ -99,45 +123,80 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     return $entities;
   }
 
+  /**
+   * @return array
+   */
   public static function entities_get() {
     // all the entities, beside the ones flagged
     return static::entities(static::toBeSkipped_get(TRUE));
   }
 
+  /**
+   * @return array
+   */
   public static function entities_create() {
     return static::entities(static::toBeSkipped_create(TRUE));
   }
 
+  /**
+   * @return array
+   */
   public static function entities_updatesingle() {
     return static::entities(static::toBeSkipped_updatesingle(TRUE));
   }
 
+  /**
+   * @return array
+   */
   public static function entities_getlimit() {
     return static::entities(static::toBeSkipped_getlimit());
   }
 
+  /**
+   * @return array
+   */
   public static function entities_delete() {
     return static::entities(static::toBeSkipped_delete(TRUE));
   }
 
+  /**
+   * @return array
+   */
+  public static function entities_getfields() {
+    return static::entities(static::toBeSkipped_getfields(TRUE));
+  }
+  /**
+   * @return array
+   */
   public static function custom_data_entities_get() {
     return static::custom_data_entities();
   }
 
+  /**
+   * @return array
+   */
   public static function custom_data_entities() {
-   $entities = CRM_Core_BAO_CustomQuery::$extendsMap;
-   $customDataEntities = array();
+    $enableComponents = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
+    $entities = CRM_Core_BAO_CustomQuery::$extendsMap;
+    $components = self::$componentMap;
+    $customDataEntities = array();
     $invalidEntities = array('Individual', 'Organization', 'Household');
     $entitiesToFix = array('Case', 'Relationship');
-   foreach ($entities as $entityName => $entity ) {
-     if(!in_array($entityName, $invalidEntities)
-       && !in_array($entityName, $entitiesToFix)) {
-       $customDataEntities[] = array($entityName );
-     }
-   }
+    foreach ($entities as $entityName => $entity ) {
+      if(!in_array($entityName, $invalidEntities)
+        && !in_array($entityName, $entitiesToFix)
+        && (!empty($components[$entityName]) && in_array($components[$entityName], $enableComponents) || $components[$entityName] == NULL)) {
+        $customDataEntities[] = array($entityName );
+      }
+    }
     return $customDataEntities;
   }
 
+  /**
+   * @param bool $sequential
+   *
+   * @return array
+   */
   public static function toBeSkipped_get($sequential = FALSE) {
     $entitiesWithoutGet = array('MailingEventSubscribe', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'Location');
     if ($sequential === TRUE) {
@@ -163,6 +222,11 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     return array('MailingContact');
   }
 
+  /**
+   * @param bool $sequential
+   *
+   * @return array
+   */
   public static function toBeSkipped_create($sequential = FALSE) {
     $entitiesWithoutCreate = array('MailingGroup', 'Constant', 'Entity', 'Location', 'Profile', 'MailingRecipients');
     if ($sequential === TRUE) {
@@ -175,6 +239,11 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     return $entities;
   }
 
+  /**
+   * @param bool $sequential
+   *
+   * @return array
+   */
   public static function toBeSkipped_delete($sequential = FALSE) {
     $entitiesWithout = array('MailingContact', 'MailingEventConfirm', 'MailingEventResubscribe', 'MailingEventSubscribe', 'MailingEventUnsubscribe', 'MailingGroup', 'MailingRecipients', 'Constant', 'Entity', 'Location', 'Domain', 'Profile', 'CustomValue', 'Setting');
     if ($sequential === TRUE) {
@@ -187,6 +256,23 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     return $entities;
   }
 
+  /**
+   * @param bool $sequential
+   *
+   * @return array
+   * @todo add metadata for ALL these entities
+   */
+  public static function toBeSkipped_getfields($sequential = FALSE) {
+    $entitiesWithMetadataNotYetFixed = array('ReportTemplate', 'CustomSearch');
+    if ($sequential === TRUE) {
+      return $entitiesWithMetadataNotYetFixed ;
+    }
+    $entities = array();
+    foreach ($entitiesWithMetadataNotYetFixed as $e) {
+      $entities[] = array($e);
+    }
+    return $entities;
+  }
 /**
  * Generate list of entities to test for get by id functions
  * @param boolean $sequential
@@ -368,6 +454,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
           'pledge_contribution_page_id',
           'pledge_status_id',
           'pledge_campaign_id',
+          'pledge_financial_type_id',
         )
       ),
       'PaymentProcessorType' => array(
@@ -912,6 +999,10 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
         'id' => $entity['id'],
         $field => isset($entity[$field]) ? $entity[$field] : NULL,
       );
+      if(isset($updateParams['financial_type_id']) && $entityName != 'Product') {
+        //api has special handling on these 2 fields for backward compatibility reasons
+        $entity['contribution_type_id'] = $updateParams['financial_type_id'];
+      }
 
       $update = $this->callAPISuccess($entityName, 'create', $updateParams);
       $checkParams = array(
@@ -1032,6 +1123,50 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * Create two entities and make sure delete action only deletes one!
+   *
+   * @dataProvider entities_getfields
+   *
+   */
+  public function testGetfieldsHasTitle($entity) {
+    $entities = $this->getEntitiesSupportingCustomFields();
+    if (in_array($entity, $entities)) {
+      $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, $entity . 'Test.php');
+    }
+    $actions =  $this->callAPISuccess($entity, 'getactions', array());
+    foreach ($actions['values'] as $action) {
+      if (substr($action, -7) == '_create'  || substr($action, -4) == '_get' || substr($action, -7) == '_delete') {
+        //getactions can't distinguish between contribution_page.create & contribution_page.create
+        continue;
+      }
+      $fields = $this->callAPISuccess($entity, 'getfields', array('action' => $action));
+      if (!empty($ids) && in_array($action, array('create', 'get'))) {
+        $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $fields['values']);
+      }
+
+      foreach ($fields['values'] as $fieldName => $fieldSpec) {
+        $this->assertArrayHasKey('title', $fieldSpec, "no title for $entity - $fieldName on action $action");
+        $this->assertNotEmpty($fieldSpec['title'], "empty title for $entity - $fieldName");
+      }
+    }
+    if (!empty($ids)) {
+      $this->customFieldDelete($ids['custom_field_id']);
+      $this->customGroupDelete($ids['custom_group_id']);
+    }
+  }
+
+  /**
+   * @return array
+   */
+  public function getEntitiesSupportingCustomFields() {
+    $entities = self::custom_data_entities_get();
+    $returnEntities = array();
+    foreach ($entities as $entityArray) {
+      $returnEntities[] = $entityArray[0];
+    }
+    return $returnEntities;
+  }
   /**
    * @param $entityName
    * @param int $count
@@ -1206,5 +1341,4 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       1 => array($eventId, 'Integer')
     ));
   }
-
 }