dev/core#4530 - Change email fields to text in search mode
authorcolemanw <coleman@civicrm.org>
Wed, 23 Aug 2023 20:39:27 +0000 (16:39 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 25 Aug 2023 00:04:17 +0000 (20:04 -0400)
Civi/Api4/Service/Spec/Provider/EmailGetSpecProvider.php [new file with mode: 0644]
tests/phpunit/api/v4/Action/GetFieldsTest.php

diff --git a/Civi/Api4/Service/Spec/Provider/EmailGetSpecProvider.php b/Civi/Api4/Service/Spec/Provider/EmailGetSpecProvider.php
new file mode 100644 (file)
index 0000000..42ccdd4
--- /dev/null
@@ -0,0 +1,45 @@
+<?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;
+
+/**
+ * This applies to all entities with an email field.
+ *
+ * In the contact of "get", email validation doesn't make sense so change it to text.
+ *
+ * @service
+ * @internal
+ */
+class EmailGetSpecProvider extends \Civi\Core\Service\AutoService implements Generic\SpecProviderInterface {
+
+  /**
+   * @inheritDoc
+   */
+  public function modifySpec(RequestSpec $spec) {
+    foreach ($spec->getFields() as $field) {
+      if ($field->getInputType() === 'Email') {
+        $field->setInputType('Text');
+      }
+    }
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function applies($entity, $action) {
+    return $action === 'get';
+  }
+
+}
index 3de060d975d42decdc1ec1856db0901577b9a0b9..cd3d6c53198ac52d5443bc02be31eda527b1d2da 100644 (file)
@@ -24,6 +24,7 @@ use Civi\Api4\Activity;
 use Civi\Api4\Campaign;
 use Civi\Api4\Contact;
 use Civi\Api4\Contribution;
+use Civi\Api4\Email;
 use Civi\Api4\EntityTag;
 use Civi\Test\TransactionalInterface;
 
@@ -73,6 +74,20 @@ class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
     $this->assertCount(1, $fields);
   }
 
+  public function testEmailFields() {
+    $getFields = Email::getFields(FALSE)
+      ->setAction('get')
+      ->execute()->indexBy('name');
+
+    $this->assertEquals('Text', $getFields['email']['input_type']);
+
+    $createFields = Email::getFields(FALSE)
+      ->setAction('create')
+      ->execute()->indexBy('name');
+
+    $this->assertEquals('Email', $createFields['email']['input_type']);
+  }
+
   public function testInternalPropsAreHidden() {
     // Public getFields should not contain @internal props
     $fields = Contact::getFields(FALSE)