Skip to content

Commit

Permalink
APIv4 - CiviCase API: Fix openening a case with current user as creator
Browse files Browse the repository at this point in the history
The DAOActionTrait::writeObjects function was formatting values but not by reference,
so the CiviCase writeObjects function was using unformatted values to open the case,
which would contain the raw string `user_contact_id` instead of the processed value.
  • Loading branch information
colemanw committed May 6, 2021
1 parent 15acadd commit baf63a6
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Civi/Api4/Action/Address/AddressSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ trait AddressSaveTrait {
/**
* @inheritDoc
*/
protected function writeObjects($items) {
protected function writeObjects(&$items) {
foreach ($items as &$item) {
if ($this->streetParsing && !empty($item['street_address'])) {
$item = array_merge($item, \CRM_Core_BAO_Address::parseStreetAddress($item['street_address']));
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Action/CiviCase/CiviCaseSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait CiviCaseSaveTrait {
* @param array $cases
* @return array
*/
protected function writeObjects($cases) {
protected function writeObjects(&$cases) {
$cases = array_values($cases);
$result = parent::writeObjects($cases);

Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Action/GroupContact/GroupContactSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait GroupContactSaveTrait {
/**
* @inheritDoc
*/
protected function writeObjects($items) {
protected function writeObjects(&$items) {
foreach ($items as &$item) {
$item['method'] = $this->method;
$item['tracking'] = $this->tracking;
Expand Down
5 changes: 2 additions & 3 deletions Civi/Api4/Generic/DAOCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public function _run(Result $result) {
$this->validateValues();
$params = $this->values;
$this->fillDefaults($params);
$items = [$params];

$resultArray = $this->writeObjects([$params]);

$result->exchangeArray($resultArray);
$result->exchangeArray($this->writeObjects($items));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Civi/Api4/Generic/DAOUpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function _run(Result $result) {
// Update a single record by ID unless select requires more than id
if ($this->getSelect() === ['id'] && count($this->where) === 1 && $this->where[0][0] === 'id' && $this->where[0][1] === '=' && !empty($this->where[0][2])) {
$this->values['id'] = $this->where[0][2];
$result->exchangeArray($this->writeObjects([$this->values]));
$items = [$this->values];
$result->exchangeArray($this->writeObjects($items));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Generic/Traits/CustomValueActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getEntityName() {
/**
* @inheritDoc
*/
protected function writeObjects($items) {
protected function writeObjects(&$items) {
$fields = $this->entityFields();
foreach ($items as $idx => $item) {
FormattingUtil::formatWriteParams($item, $fields);
Expand Down
4 changes: 2 additions & 2 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function fillDefaults(&$params) {
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function writeObjects($items) {
protected function writeObjects(&$items) {
$baoName = $this->getBaoName();

// Some BAOs are weird and don't support a straightforward "create" method.
Expand All @@ -118,7 +118,7 @@ protected function writeObjects($items) {

$result = [];

foreach ($items as $item) {
foreach ($items as &$item) {
$entityId = $item['id'] ?? NULL;
FormattingUtil::formatWriteParams($item, $this->entityFields());
$this->formatCustomParams($item, $entityId);
Expand Down
10 changes: 10 additions & 0 deletions Civi/Api4/Service/Spec/Provider/CaseCreationSpecProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public function modifySpec(RequestSpec $spec) {
$duration->setInputType('Number');
$duration->setDescription('Open Case activity duration (minutes).');
$spec->addFieldSpec($duration);

$defaultStatus = \CRM_Core_DAO::singleValueQuery('SELECT value FROM civicrm_option_value
WHERE is_default
AND domain_id = ' . \CRM_Core_BAO_Domain::getDomain()->id . '
AND option_group_id = (SELECT id FROM civicrm_option_group WHERE name = "case_status")
LIMIT 1');
if ($defaultStatus) {
$status = $spec->getFieldByName('status_id');
$status->setDefaultValue((int) $defaultStatus);
}
}

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/api/v4/DataSets/CaseType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"CaseType": [
{
"name": "test_case_type",
"title": "Test Case Type",
"definition": {
"activityTypes": [
{
"name": "Open Case",
"max_instances": "1"
},
{
"name": "Follow up"
}
],
"activitySets": [
{
"name": "standard_timeline",
"label": "Standard Timeline",
"timeline": 1,
"activityTypes": [
{
"name": "Open Case",
"status": "Completed"
},
{
"name": "Follow up",
"reference_activity": "Open Case",
"reference_offset": "3",
"reference_select": "newest"
}
]
}
],
"timelineActivityTypes": [
{
"name": "Open Case",
"status": "Completed"
},
{
"name": "Follow up",
"reference_activity": "Open Case",
"reference_offset": "3",
"reference_select": "newest"
}
],
"caseRoles": [
{
"name": "Parent of",
"creator": "1",
"manager": "1"
}
]
},
"@ref": "test_case_type_1"
}
]
}
56 changes: 0 additions & 56 deletions tests/phpunit/api/v4/DataSets/ConformanceTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,6 @@
"@ref": "test_contact_2"
}
],
"CaseType": [
{
"name": "test_case_type",
"title": "Test Case Type",
"definition": {
"activityTypes": [
{
"name": "Open Case",
"max_instances": "1"
},
{
"name": "Follow up"
}
],
"activitySets": [
{
"name": "standard_timeline",
"label": "Standard Timeline",
"timeline": 1,
"activityTypes": [
{
"name": "Open Case",
"status": "Completed"
},
{
"name": "Follow up",
"reference_activity": "Open Case",
"reference_offset": "3",
"reference_select": "newest"
}
]
}
],
"timelineActivityTypes": [
{
"name": "Open Case",
"status": "Completed"
},
{
"name": "Follow up",
"reference_activity": "Open Case",
"reference_offset": "3",
"reference_select": "newest"
}
],
"caseRoles": [
{
"name": "Parent of",
"creator": "1",
"manager": "1"
}
]
},
"@ref": "test_case_type_1"
}
],
"Case": [
{
"case_type_id": "@ref test_case_type_1.id",
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/api/v4/Entity/CaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


namespace api\v4\Entity;

use Civi\Api4\CiviCase;
use api\v4\UnitTestCase;

/**
* @group headless
*/
class CaseTest extends UnitTestCase {

public function setUp() {
parent::setUp();
\CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
$this->loadDataSet('CaseType');
}

public function testCreateUsingLoggedInUser() {
$this->createLoggedInUser();

$contactID = $this->createEntity(['type' => 'Individual'])['id'];

$result = CiviCase::create(FALSE)
->addValue('case_type_id', $this->getReference('test_case_type_1')['id'])
->addValue('creator_id', 'user_contact_id')
->addValue('status_id', 1)
->addValue('contact_id', $contactID)
->execute()
->first();

}

}
1 change: 1 addition & 0 deletions tests/phpunit/api/v4/Entity/ConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function setUp(): void {
$this->dropByPrefix('civicrm_value_myfavorite');
$this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
$this->setUpOptionCleanup();
$this->loadDataSet('CaseType');
$this->loadDataSet('ConformanceTest');
$this->creationParamProvider = \Civi::container()->get('test.param_provider');
parent::setUp();
Expand Down

0 comments on commit baf63a6

Please sign in to comment.