CRM-16701 - fixed hack in commit f1d23743.
[civicrm-core.git] / tests / phpunit / api / v3 / SyntaxConformanceTest.php
index 8d01a2a0c50604865c9a7f2fcdca7a39d8f642e7..b69486a376a7a4f9b715d532451bfdd88acba26b 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -278,7 +278,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    *
    * Mailing Contact Just doesn't support id. We have always insisted on finding a way to
    * support id in API but in this case the underlying tables are crying out for a restructure
-   * & it just doesn't make sense, on the otherhand Event need id to be existant as pseudo property
+   * & it just doesn't make sense, on the otherhand Event need id to be existent as pseudo property
    * is been associated with it, so we need to bypass for get api otherwise it will through pseudo_match validation
    *
    * @param bool $sequential
@@ -599,6 +599,15 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
           'weight', //won't update as there is no 1 in the same price set
         ),
       ),
+      'SavedSearch' => array(
+        // I think the fields below are generated based on form_values.
+        'cant_update' => array(
+          'search_custom_id',
+          'where_clause',
+          'select_tables',
+          'where_tables',
+        ),
+      ),
     );
     if (empty($knownFailures[$entity]) || empty($knownFailures[$entity][$key])) {
       return array();
@@ -615,7 +624,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    */
   public function testNotImplemented_get($Entity) {
     $result = civicrm_api($Entity, 'Get', array('version' => 3));
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     // $this->assertContains("API ($Entity, Get) does not exist", $result['error_message']);
     $this->assertRegExp('/API (.*) does not exist/', $result['error_message']);
   }
@@ -657,7 +666,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       return;
     }
     $result = civicrm_api($Entity, 'Get', array());
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
   }
 
@@ -998,7 +1007,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    */
   public function testNotImplemented_create($Entity) {
     $result = civicrm_api($Entity, 'Create', array('version' => 3));
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertContains(strtolower("API ($Entity, Create) does not exist"), strtolower($result['error_message']));
   }
 
@@ -1018,7 +1027,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    * @throws \PHPUnit_Framework_IncompleteTestError
    */
   public function testEmptyParam_create($Entity) {
-    $this->markTestIncomplete("fixing this test to test the api functions fails on numberous tests
+    $this->markTestIncomplete("fixing this test to test the api functions fails on numerous tests
       which will either create a completely blank entity (batch, participant status) or
       have a damn good crack at it (e.g mailing job). Marking this as incomplete beats false success");
     return;
@@ -1053,7 +1062,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    */
   public function testCreateWrongTypeParamTag_create() {
     $result = civicrm_api("Tag", 'Create', 'this is not a string');
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
   }
 
@@ -1074,9 +1083,9 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
     $baoString = _civicrm_api3_get_BAO($entityName);
     $this->assertNotEmpty($baoString, $entityName);
     $this->assertNotEmpty($entityName, $entityName);
-    $fieldsGet = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get'));
+    $fieldsGet = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get', 'options' => array('get_options' => 'all')));
     if ($entityName != 'Pledge') {
-      $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create'));
+      $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create', 'options' => array('get_options' => 'all')));
     }
     $fields = $fields['values'];
     $return = array_keys($fieldsGet['values']);
@@ -1121,12 +1130,10 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       $this->assertArrayHasKey('type', $specs, "the _spec function for $entityName field $field does not specify the type");
       switch ($specs['type']) {
         case CRM_Utils_Type::T_DATE:
-        case CRM_Utils_Type::T_TIMESTAMP:
           $entity[$fieldName] = '2012-05-20';
           break;
 
-        //case CRM_Utils_Type::T_DATETIME:
-
+        case CRM_Utils_Type::T_TIMESTAMP:
         case 12:
           $entity[$fieldName] = '2012-05-20 03:05:20';
           break;
@@ -1137,7 +1144,19 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
         case CRM_Utils_Type::T_TEXT:
         case CRM_Utils_Type::T_LONGTEXT:
         case CRM_Utils_Type::T_EMAIL:
-          $entity[$fieldName] = substr('New String', 0, CRM_Utils_Array::Value('maxlength', $specs, 100));
+          if ($fieldName == 'form_values' && $entity_name == 'SavedSearch') {
+            // This is a hack for the SavedSearch API. It expects form_values
+            // to be a serialized array.
+            // If you want to fix this, you should definitely read this forum
+            // post.
+            // http://forum.civicrm.org/index.php/topic,33990.0.html
+            // See also my question on the CiviCRM Stack Exchange:
+            // https://civicrm.stackexchange.com/questions/3437
+            $entity[$fieldName] = array('sort_name' => "SortName2");
+          }
+          else {
+            $entity[$fieldName] = substr('New String', 0, CRM_Utils_Array::Value('maxlength', $specs, 100));
+          }
           break;
 
         case CRM_Utils_Type::T_INT:
@@ -1179,23 +1198,27 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
         case CRM_Utils_Type::T_URL:
           $entity[$field] = 'warm.beer.com';
       }
-      if (!empty($specs['pseudoconstant'])) {
-        $options = $this->callAPISuccess($entityName, 'getoptions', array('context' => 'create', 'field' => $field));
-        if (empty($options['values'])) {
+      if (empty($specs['FKClassName']) && (!empty($specs['pseudoconstant']) || !empty($specs['options']))) {
+        $options = CRM_Utils_Array::value('options', $specs, array());
+        if (!$options) {
           //eg. pdf_format id doesn't ship with any
           if (isset($specs['pseudoconstant']['optionGroupName'])) {
-            $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
-                'name' => 'pdf_format',
-                'return' => 'id',
-              ));
             $optionValue = $this->callAPISuccess('option_value', 'create', array(
-                'option_group_id' => $optionGroupID,
+                'option_group_id' => $specs['pseudoconstant']['optionGroupName'],
                 'label' => 'new option value',
+                'sequential' => 1,
               ));
-            $options['values'][] = $optionValue['id'];
+            $optionValue = $optionValue['values'];
+            $options[$optionValue[0]['value']] = 'new option value';
           }
         }
-        $entity[$field] = array_rand($options['values']);
+        $entity[$field] = array_rand($options);
+      }
+      if (!empty($specs['FKClassName']) && !empty($specs['pseudoconstant'])) {
+        // in the weird situation where a field has both an fk and pseudoconstant defined,
+        // e.g. campaign_id field, need to flush caches.
+        // FIXME: Why doesn't creating a campaign clear caches?
+        civicrm_api3($entityName, 'getfields', array('cache_clear' => 1));
       }
       $updateParams = array(
         'id' => $entity['id'],
@@ -1251,7 +1274,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
   public function testNotImplemented_delete($Entity) {
     $nonExistantID = 151416349;
     $result = civicrm_api($Entity, 'Delete', array('version' => 3, 'id' => $nonExistantID));
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertContains(strtolower("API ($Entity, Delete) does not exist"), strtolower($result['error_message']));
   }
 
@@ -1275,7 +1298,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       return;
     }
     $result = civicrm_api($Entity, 'Delete', array());
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']);
   }
 
@@ -1300,7 +1323,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
    */
   public function testDeleteWrongTypeParamTag_delete() {
     $result = civicrm_api("Tag", 'Delete', 'this is not a string');
-    $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertEquals(1, $result['is_error']);
     $this->assertEquals("Input variable `params` is not an array", $result['error_message']);
   }