CRM-20316 the mailing should still create without created_id (not mandatory).
authoreileen <emcnaughton@wikimedia.org>
Tue, 18 Apr 2017 23:00:06 +0000 (11:00 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 19 Apr 2017 03:18:49 +0000 (15:18 +1200)
api/v3/Activity.php
api/v3/utils.php
tests/phpunit/api/v3/ActivityTest.php
tests/phpunit/api/v3/MailingTest.php

index 5815edc9e4b1eacef861ebe56aa45b520d1343ad..a501b66ab5f4651c183028fcefb6258a5b2ec981 100644 (file)
@@ -180,9 +180,6 @@ function civicrm_api3_activity_create($params) {
  */
 function _civicrm_api3_activity_create_spec(&$params) {
 
-  // Default for source_contact_id = currently logged in user.
-  $params['source_contact_id']['api.default'] = 'user_contact_id';
-
   $params['status_id']['api.aliases'] = array('activity_status');
 
   $params['assignee_contact_id'] = array(
@@ -210,6 +207,7 @@ function _civicrm_api3_activity_create_spec(&$params) {
     'FKClassName' => 'CRM_Contact_DAO_Contact',
     'api.default' => 'user_contact_id',
     'FKApiName' => 'Contact',
+    'api.required' => TRUE,
   );
 
   $params['case_id'] = array(
index 1e78f7b80ee39c53b87c933e21ce0932841c7ca0..8164a31ef2b49989876e304c91999738c4f366a7 100644 (file)
@@ -2083,6 +2083,10 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti
       elseif (is_numeric($realContactId)) {
         $fieldValue = $realContactId;
       }
+      elseif (is_null($realContactId) && empty($fieldInfo['api.required'])) {
+        // If not mandatory this will be OK. If mandatory it should fail.
+        $fieldValue = NULL;
+      }
     }
     if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
       _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op);
@@ -2090,7 +2094,7 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti
 
     // After swapping options, ensure we have an integer(s)
     foreach ((array) ($fieldValue) as $value) {
-      if ($value && !is_numeric($value) && $value !== 'null' && !is_array($value)) {
+      if ($value && !is_numeric($value) && $value !== 'null' && $value !== NULL && !is_array($value)) {
         throw new API_Exception("$fieldName is not a valid integer", 2001, array('error_field' => $fieldName, "type" => "integer"));
       }
     }
index 414a6643d9b1767da48af1a6c72fa44c1498aaac..c9f3ae9f7802b3ed74bafdd51c5471a35629f648 100644 (file)
@@ -169,6 +169,26 @@ class api_v3_ActivityTest extends CiviUnitTestCase {
     $this->callAPISuccess('activity', 'create', $params);
   }
 
+  /**
+   * CRM-20316 this should fail based on validation with no logged in user.
+   *
+   * Since the field is required the validation should reject the default.
+   */
+  public function testActivityCreateWithMissingContactIdNoLoggedInUser() {
+    CRM_Core_Session::singleton()->set('userID', NULL);
+    $params = array(
+      'subject' => 'Make-it-Happen Meeting',
+      'activity_date_time' => date('Ymd'),
+      'duration' => 120,
+      'location' => 'Pennsylvania',
+      'details' => 'a test activity',
+      'status_id' => 1,
+      'activity_name' => 'Test activity type',
+    );
+
+    $this->callAPIFailure('activity', 'create', $params, 'source_contact_id is not a valid integer');
+  }
+
   /**
    * Test civicrm_activity_id() with non-numeric source_contact_id.
    */
index 04a1b73c6c73898b34d6e586497adbac7e24a206..f875a580d42e17bf5e6e3234cd3d8311b308663d 100644 (file)
@@ -85,6 +85,15 @@ class api_v3_MailingTest extends CiviUnitTestCase {
     $this->getAndCheck($this->_params, $result['id'], 'mailing');
   }
 
+  /**
+   * Per CRM-20316 the mailing should still create without created_id (not mandatory).
+   */
+  public function testMailerCreateSuccessNoCreatedID() {
+    unset($this->_params['created_id']);
+    $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
+    $this->getAndCheck($this->_params, $result['id'], 'mailing');
+  }
+
   /**
    *
    */