From c442f1b663a93277d043e805d74761f09beaad09 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 19 Apr 2017 11:00:06 +1200 Subject: [PATCH] CRM-20316 the mailing should still create without created_id (not mandatory). --- api/v3/Activity.php | 4 +--- api/v3/utils.php | 6 +++++- tests/phpunit/api/v3/ActivityTest.php | 20 ++++++++++++++++++++ tests/phpunit/api/v3/MailingTest.php | 9 +++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 5815edc9e4..a501b66ab5 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -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( diff --git a/api/v3/utils.php b/api/v3/utils.php index 1e78f7b80e..8164a31ef2 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -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")); } } diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index 414a6643d9..c9f3ae9f78 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -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. */ diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 04a1b73c6c..f875a580d4 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -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'); + } + /** * */ -- 2.25.1