APIv4 - Set default status when creating GroupContact record
authorColeman Watts <coleman@civicrm.org>
Mon, 27 Dec 2021 00:52:57 +0000 (19:52 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 27 Dec 2021 00:52:57 +0000 (19:52 -0500)
Fixes dev/core#2924

Civi/Api4/Service/Spec/Provider/GroupContactCreationSpecProvider.php [new file with mode: 0644]
tests/phpunit/api/v4/Entity/GroupContactTest.php [new file with mode: 0644]
tests/phpunit/api/v4/UnitTestCase.php

diff --git a/Civi/Api4/Service/Spec/Provider/GroupContactCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/GroupContactCreationSpecProvider.php
new file mode 100644 (file)
index 0000000..240a1a2
--- /dev/null
@@ -0,0 +1,33 @@
+<?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       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Service\Spec\Provider;
+
+use Civi\Api4\Service\Spec\RequestSpec;
+
+class GroupContactCreationSpecProvider implements Generic\SpecProviderInterface {
+
+  /**
+   * @inheritDoc
+   */
+  public function modifySpec(RequestSpec $spec) {
+    $spec->getFieldByName('status')->setDefaultValue('Added');
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function applies($entity, $action) {
+    return $entity === 'GroupContact' && $action === 'create';
+  }
+
+}
diff --git a/tests/phpunit/api/v4/Entity/GroupContactTest.php b/tests/phpunit/api/v4/Entity/GroupContactTest.php
new file mode 100644 (file)
index 0000000..0824e24
--- /dev/null
@@ -0,0 +1,41 @@
+<?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 api\v4\UnitTestCase;
+use Civi\Api4\GroupContact;
+
+/**
+ * @group headless
+ */
+class GroupContactTest extends UnitTestCase {
+
+  public function testCreate() {
+    $contact = $this->createEntity(['type' => 'Individual']);
+    $group = $this->createEntity(['type' => 'Group']);
+    $result = GroupContact::create(FALSE)
+      ->addValue('group_id', $group['id'])
+      ->addValue('contact_id', $contact['id'])
+      ->execute()
+      ->first();
+    $this->assertEquals('Added', $result['status']);
+  }
+
+}
index 5982f1b920f1bec854af4759eb63ca81be8939c1..992550b1b4dff786221ceba66791f705f239a7e0 100644 (file)
@@ -264,6 +264,9 @@ class UnitTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterf
         'subject' => 'unit testing',
         'source_contact_id' => 'dummy.Individual',
       ],
+      'Group' => [
+        'title' => 'unit testing',
+      ],
     ];
     if ($type == 'Contact') {
       $type = 'Individual';