APIv4 - Add activity contacts to APIv4 field spec
authorColeman Watts <coleman@civicrm.org>
Mon, 6 Jul 2020 21:59:31 +0000 (17:59 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 14 Jul 2020 02:59:31 +0000 (22:59 -0400)
The BAO supports these pseudo-fields during save operations, this exposes them to the api explorer.

Civi/Api4/Service/Spec/Provider/ActivityCreationSpecProvider.php [deleted file]
Civi/Api4/Service/Spec/Provider/ActivitySpecProvider.php [new file with mode: 0644]
tests/phpunit/api/v3/ActivityTest.php

diff --git a/Civi/Api4/Service/Spec/Provider/ActivityCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/ActivityCreationSpecProvider.php
deleted file mode 100644 (file)
index 028dbcf..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-<?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 Civi\Api4\Service\Spec\Provider;
-
-use Civi\Api4\Service\Spec\FieldSpec;
-use Civi\Api4\Service\Spec\RequestSpec;
-
-class ActivityCreationSpecProvider implements Generic\SpecProviderInterface {
-
-  /**
-   * @inheritDoc
-   */
-  public function modifySpec(RequestSpec $spec) {
-    $sourceContactField = new FieldSpec('source_contact_id', 'Activity', 'Integer');
-    $sourceContactField->setRequired(TRUE);
-    $sourceContactField->setFkEntity('Contact');
-
-    $spec->addFieldSpec($sourceContactField);
-  }
-
-  /**
-   * @inheritDoc
-   */
-  public function applies($entity, $action) {
-    return $entity === 'Activity' && $action === 'create';
-  }
-
-}
diff --git a/Civi/Api4/Service/Spec/Provider/ActivitySpecProvider.php b/Civi/Api4/Service/Spec/Provider/ActivitySpecProvider.php
new file mode 100644 (file)
index 0000000..9550495
--- /dev/null
@@ -0,0 +1,60 @@
+<?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 Civi\Api4\Service\Spec\Provider;
+
+use Civi\Api4\Service\Spec\FieldSpec;
+use Civi\Api4\Service\Spec\RequestSpec;
+
+class ActivitySpecProvider implements Generic\SpecProviderInterface {
+
+  /**
+   * @inheritDoc
+   */
+  public function modifySpec(RequestSpec $spec) {
+    $action = $spec->getAction();
+
+    $field = new FieldSpec('source_contact_id', 'Activity', 'Integer');
+    $field->setTitle(ts('Source Contact'));
+    $field->setDescription(ts('Contact who created this activity.'));
+    $field->setRequired($action === 'create');
+    $field->setFkEntity('Contact');
+    $spec->addFieldSpec($field);
+
+    $field = new FieldSpec('target_contact_id', 'Activity', 'Array');
+    $field->setTitle(ts('Target Contacts'));
+    $field->setDescription(ts('Contact(s) involved in this activity.'));
+    $field->setFkEntity('Contact');
+    $spec->addFieldSpec($field);
+
+    $field = new FieldSpec('assignee_contact_id', 'Activity', 'Array');
+    $field->setTitle(ts('Assignee Contacts'));
+    $field->setDescription(ts('Contact(s) assigned to this activity.'));
+    $field->setFkEntity('Contact');
+    $spec->addFieldSpec($field);
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function applies($entity, $action) {
+    return $entity === 'Activity' && in_array($action, ['create', 'update'], TRUE);
+  }
+
+}
index 7db390114fbc6c8e22bc0c56b4552de1414314d5..86e7d23062f850852c8ebb3de7a9b35c1db49273 100644 (file)
@@ -370,9 +370,11 @@ class api_v3_ActivityTest extends CiviUnitTestCase {
 
   /**
    * Test get returns target and assignee contacts.
+   * @dataProvider versionThreeAndFour
+   * @var int $version
    */
-  public function testActivityReturnTargetAssignee() {
-
+  public function testActivityReturnTargetAssignee($version) {
+    $this->_apiversion = $version;
     $description = "Demonstrates setting & retrieving activity target & source.";
     $subfile = "GetTargetandAssignee";
     $params = [
@@ -390,9 +392,18 @@ class api_v3_ActivityTest extends CiviUnitTestCase {
     ];
 
     $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
+
+    $actContacts = $this->callAPISuccess('ActivityContact', 'get', [
+      'activity_id' => $result['id'],
+    ]);
+    $this->assertCount(3, $actContacts['values']);
+
+    if ($version === 4) {
+      $this->markTestIncomplete("APIv4 doesn't retrieve activity contacts directly.");
+    }
+
     $result = $this->callAPISuccess('activity', 'get', [
       'id' => $result['id'],
-      'version' => $this->_apiversion,
       'return.assignee_contact_id' => 1,
       'return.target_contact_id' => 1,
     ]);