APIv4 - Ensure getFields always returns table_name and column_name
authorColeman Watts <coleman@civicrm.org>
Mon, 31 May 2021 15:53:56 +0000 (11:53 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 31 May 2021 17:46:52 +0000 (13:46 -0400)
Civi/Api4/Service/Spec/CustomFieldSpec.php
Civi/Api4/Service/Spec/FieldSpec.php
Civi/Api4/Service/Spec/RequestSpec.php
Civi/Api4/Service/Spec/SpecFormatter.php
tests/phpunit/api/v4/Action/CustomValueTest.php
tests/phpunit/api/v4/Action/GetFieldsTest.php

index d64dff2e0be3bf572c8c78e41a0f8d167e190f30..11fca39c52268aca249f4de8ddebc9d121fe870b 100644 (file)
@@ -30,11 +30,6 @@ class CustomFieldSpec extends FieldSpec {
    */
   public $customGroup;
 
-  /**
-   * @var string
-   */
-  public $tableName;
-
   /**
    * @inheritDoc
    */
@@ -91,22 +86,4 @@ class CustomFieldSpec extends FieldSpec {
     return $this;
   }
 
-  /**
-   * @return string
-   */
-  public function getCustomTableName() {
-    return $this->tableName;
-  }
-
-  /**
-   * @param string $customFieldColumnName
-   *
-   * @return $this
-   */
-  public function setCustomTableName($customFieldColumnName) {
-    $this->tableName = $customFieldColumnName;
-
-    return $this;
-  }
-
 }
index 5bba209081bb8a94ca05eb8e8df85d6c025e8aa0..54f7b948f868c01c66acd8ea8fd44dc904915b5e 100644 (file)
@@ -65,6 +65,11 @@ class FieldSpec {
    */
   public $options;
 
+  /**
+   * @var string
+   */
+  public $tableName;
+
   /**
    * @var callable
    */
@@ -219,6 +224,17 @@ class FieldSpec {
     return $this;
   }
 
+  /**
+   * @param string $entity
+   *
+   * @return $this
+   */
+  public function setEntity($entity) {
+    $this->entity = $entity;
+
+    return $this;
+  }
+
   /**
    * @return string
    */
@@ -446,6 +462,24 @@ class FieldSpec {
     $this->helpPost = is_string($helpPost) && strlen($helpPost) ? $helpPost : NULL;
   }
 
+  /**
+   * @param string $customFieldColumnName
+   *
+   * @return CustomFieldSpec
+   */
+  public function setTableName($customFieldColumnName) {
+    $this->tableName = $customFieldColumnName;
+
+    return $this;
+  }
+
+  /**
+   * @return string
+   */
+  public function getTableName() {
+    return $this->tableName;
+  }
+
   /**
    * Add valid types that are not not part of \CRM_Utils_Type::dataTypes
    *
index dbf460a3ba71bd2888ce0d435b0a5608cdded360..d9e2b5bf135c1b412ec3ee3a9e2e1cd93773e281 100644 (file)
@@ -19,6 +19,8 @@
 
 namespace Civi\Api4\Service\Spec;
 
+use Civi\Api4\Utils\CoreUtil;
+
 class RequestSpec {
 
   /**
@@ -31,6 +33,11 @@ class RequestSpec {
    */
   protected $action;
 
+  /**
+   * @var string
+   */
+  protected $entityTableName;
+
   /**
    * @var FieldSpec[]
    */
@@ -43,9 +50,16 @@ class RequestSpec {
   public function __construct($entity, $action) {
     $this->entity = $entity;
     $this->action = $action;
+    $this->entityTableName = CoreUtil::getTableName($entity);
   }
 
   public function addFieldSpec(FieldSpec $field) {
+    if (!$field->getEntity()) {
+      $field->setEntity($this->entity);
+    }
+    if (!$field->getTableName()) {
+      $field->setTableName($this->entityTableName);
+    }
     $this->fields[] = $field;
   }
 
index bc1451f6fb45f7b1ba45a1911d621239b974d143..4a4fc9838d82b17f8de230c3040f48aa3efe222a 100644 (file)
@@ -39,7 +39,7 @@ class SpecFormatter {
         $field->setName($data['custom_group.name'] . '.' . $data['name']);
       }
       else {
-        $field->setCustomTableName($data['custom_group.table_name']);
+        $field->setTableName($data['custom_group.table_name']);
       }
       $field->setColumnName($data['column_name']);
       $field->setCustomFieldId($data['id'] ?? NULL);
index 0b39e261731413989b2c651431632f9f46b54ada..7a666f8a3444711d041a7f90dcf0520adf5b999f 100644 (file)
@@ -38,9 +38,9 @@ class CustomValueTest extends BaseCustomValueTest {
     $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
 
     $group = uniqid('groupc');
-    $colorField = uniqid('colorc');
-    $multiField = uniqid('chkbx');
-    $textField = uniqid('txt');
+    $colorFieldName = uniqid('colorc');
+    $multiFieldName = uniqid('chkbx');
+    $textFieldName = uniqid('txt');
 
     $customGroup = CustomGroup::create(FALSE)
       ->addValue('name', $group)
@@ -49,28 +49,28 @@ class CustomValueTest extends BaseCustomValueTest {
       ->execute()
       ->first();
 
-    CustomField::create(FALSE)
-      ->addValue('label', $colorField)
+    $colorField = CustomField::create(FALSE)
+      ->addValue('label', $colorFieldName)
       ->addValue('option_values', $optionValues)
       ->addValue('custom_group_id', $customGroup['id'])
       ->addValue('html_type', 'Select')
       ->addValue('data_type', 'String')
-      ->execute();
+      ->execute()->first();
 
-    CustomField::create(FALSE)
-      ->addValue('label', $multiField)
+    $multiField = CustomField::create(FALSE)
+      ->addValue('label', $multiFieldName)
       ->addValue('option_values', $optionValues)
       ->addValue('custom_group_id', $customGroup['id'])
       ->addValue('html_type', 'CheckBox')
       ->addValue('data_type', 'String')
-      ->execute();
+      ->execute()->first();
 
-    CustomField::create(FALSE)
-      ->addValue('label', $textField)
+    $textField = CustomField::create(FALSE)
+      ->addValue('label', $textFieldName)
       ->addValue('custom_group_id', $customGroup['id'])
       ->addValue('html_type', 'Text')
       ->addValue('data_type', 'String')
-      ->execute();
+      ->execute()->first();
 
     $this->contactID = Contact::create(FALSE)
       ->addValue('first_name', 'Johann')
@@ -84,9 +84,11 @@ class CustomValueTest extends BaseCustomValueTest {
     $expectedResult = [
       [
         'custom_group' => $group,
-        'name' => $colorField,
-        'title' => $colorField,
+        'name' => $colorFieldName,
+        'title' => $colorFieldName,
         'entity' => "Custom_$group",
+        'table_name' => $customGroup['table_name'],
+        'column_name' => $colorField['column_name'],
         'data_type' => 'String',
         'fk_entity' => NULL,
         'serialize' => 0,
@@ -94,9 +96,11 @@ class CustomValueTest extends BaseCustomValueTest {
       ],
       [
         'custom_group' => $group,
-        'name' => $multiField,
-        'title' => $multiField,
+        'name' => $multiFieldName,
+        'title' => $multiFieldName,
         'entity' => "Custom_$group",
+        'table_name' => $customGroup['table_name'],
+        'column_name' => $multiField['column_name'],
         'data_type' => 'String',
         'fk_entity' => NULL,
         'serialize' => 1,
@@ -104,9 +108,11 @@ class CustomValueTest extends BaseCustomValueTest {
       ],
       [
         'custom_group' => $group,
-        'name' => $textField,
-        'title' => $textField,
+        'name' => $textFieldName,
+        'title' => $textFieldName,
         'entity' => "Custom_$group",
+        'table_name' => $customGroup['table_name'],
+        'column_name' => $textField['column_name'],
         'data_type' => 'String',
         'fk_entity' => NULL,
         'serialize' => 0,
@@ -115,12 +121,16 @@ class CustomValueTest extends BaseCustomValueTest {
         'name' => 'id',
         'title' => ts('Custom Value ID'),
         'entity' => "Custom_$group",
+        'table_name' => $customGroup['table_name'],
+        'column_name' => 'id',
         'data_type' => 'Integer',
         'fk_entity' => NULL,
       ],
       [
         'name' => 'entity_id',
         'title' => ts('Entity ID'),
+        'table_name' => $customGroup['table_name'],
+        'column_name' => 'entity_id',
         'entity' => "Custom_$group",
         'data_type' => 'Integer',
         'fk_entity' => 'Contact',
@@ -137,18 +147,18 @@ class CustomValueTest extends BaseCustomValueTest {
     // Create two records for a single contact and using CustomValue::get ensure that two records are created
     $created = [
       CustomValue::create($group)
-        ->addValue($colorField, 'g')
+        ->addValue($colorFieldName, 'g')
         ->addValue("entity_id", $this->contactID)
         ->execute()->first(),
       CustomValue::create($group)
-        ->addValue($colorField . ':label', 'Red')
+        ->addValue($colorFieldName . ':label', 'Red')
         ->addValue("entity_id", $this->contactID)
         ->execute()->first(),
     ];
     // fetch custom values using API4 CustomValue::get
     $result = CustomValue::get($group)
-      ->addSelect('id', 'entity_id', $colorField, $colorField . ':label')
-      ->addOrderBy($colorField, 'ASC')
+      ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label')
+      ->addOrderBy($colorFieldName, 'ASC')
       ->execute();
 
     // check if two custom values are created
@@ -156,14 +166,14 @@ class CustomValueTest extends BaseCustomValueTest {
     $expectedResult = [
       [
         'id' => 1,
-        $colorField => 'g',
-        $colorField . ':label' => 'Green',
+        $colorFieldName => 'g',
+        $colorFieldName . ':label' => 'Green',
         'entity_id' => $this->contactID,
       ],
       [
         'id' => 2,
-        $colorField => 'r',
-        $colorField . ':label' => 'Red',
+        $colorFieldName => 'r',
+        $colorFieldName . ':label' => 'Red',
         'entity_id' => $this->contactID,
       ],
     ];
@@ -181,14 +191,14 @@ class CustomValueTest extends BaseCustomValueTest {
     // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
     CustomValue::update($group)
       ->addWhere("id", "=", 1)
-      ->addValue($colorField . ':label', 'Blue')
+      ->addValue($colorFieldName . ':label', 'Blue')
       ->execute();
 
     // ensure that the value is changed for id = 1
     $color = CustomValue::get($group)
       ->addWhere("id", "=", 1)
       ->execute()
-      ->first()[$colorField];
+      ->first()[$colorFieldName];
     $this->assertEquals('b', $color);
 
     // CASE 3: Test CustomValue::replace
@@ -202,23 +212,23 @@ class CustomValueTest extends BaseCustomValueTest {
     // Replace all the records which was created earlier with entity_id = first contact
     //  with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
     CustomValue::replace($group)
-      ->setRecords([[$colorField => 'g', $multiField . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
+      ->setRecords([[$colorFieldName => 'g', $multiFieldName . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
       ->addWhere('entity_id', '=', $this->contactID)
       ->execute();
 
     // Check the two records created earlier is replaced by new contact
     $result = CustomValue::get($group)
-      ->addSelect('id', 'entity_id', $colorField, $colorField . ':label', $multiField, $multiField . ':label')
+      ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label', $multiFieldName, $multiFieldName . ':label')
       ->execute();
     $this->assertEquals(1, count($result));
 
     $expectedResult = [
       [
         'id' => 3,
-        $colorField => 'g',
-        $colorField . ':label' => 'Green',
-        $multiField => ['r', 'g'],
-        $multiField . ':label' => ['Red', 'Green'],
+        $colorFieldName => 'g',
+        $colorFieldName . ':label' => 'Green',
+        $multiFieldName => ['r', 'g'],
+        $multiFieldName . ':label' => ['Red', 'Green'],
         'entity_id' => $secondContactID,
       ],
     ];
index 5c442941b025a97435a09fa90bba0906bef60c3a..f7a735277fbb177953a8cbb0b7f363f00d28ea55 100644 (file)
@@ -42,6 +42,14 @@ class GetFieldsTest extends UnitTestCase {
     $this->assertFalse($fields['first_name']['options']);
   }
 
+  public function testTableAndColumnReturned() {
+    $fields = Contact::getFields(FALSE)
+      ->execute()
+      ->indexBy('name');
+    $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']);
+    $this->assertEquals('display_name', $fields['display_name']['column_name']);
+  }
+
   public function testComponentFields() {
     \CRM_Core_BAO_ConfigSetting::disableComponent('CiviCampaign');
     $fields = \Civi\Api4\Event::getFields()