Schema - Add controlField tags where needed + test
authorcolemanw <coleman@civicrm.org>
Fri, 11 Aug 2023 18:17:26 +0000 (14:17 -0400)
committercolemanw <coleman@civicrm.org>
Tue, 22 Aug 2023 16:17:58 +0000 (12:17 -0400)
CRM/Case/DAO/Case.php
CRM/Contact/DAO/Contact.php
CRM/Core/DAO/EntityTag.php
tests/phpunit/api/v4/Action/GetFieldsTest.php
tests/phpunit/api/v4/Custom/CoreUtilTest.php
xml/schema/Case/Case.xml
xml/schema/Contact/Contact.xml
xml/schema/Core/EntityTag.xml

index 70afd42baef1019c1e04b3f3d40a251f03c03e51..08d048818b67526a1d01a8dcb961be7e1adb1fcb 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Case/Case.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:64f4fa789f0b49013001698acf6c7463)
+ * (GenCodeChecksum:83be6db09d4f5220f74ccc72149dcab6)
  */
 
 /**
@@ -357,6 +357,7 @@ class CRM_Case_DAO_Case extends CRM_Core_DAO {
           'localizable' => 0,
           'html' => [
             'type' => 'Select',
+            'controlField' => 'case_type_id',
           ],
           'pseudoconstant' => [
             'optionGroupName' => 'case_status',
index e4794d7b5fa003611b24f0ed86691b079a2d6fc7..edae63298bead15a245862c0f8e020cc701684af 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Contact/Contact.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:7cbc554ead9281e6c0ce8b7865056777)
+ * (GenCodeChecksum:4066902548c1bc5ba4358b9e0195a3fa)
  */
 
 /**
@@ -728,6 +728,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO {
           'serialize' => self::SERIALIZE_SEPARATOR_BOOKEND,
           'html' => [
             'type' => 'Select',
+            'controlField' => 'contact_type',
           ],
           'pseudoconstant' => [
             'table' => 'civicrm_contact_type',
index c099f7b3832e358a131ffed2772de0f85ac57b45..3f57d572bdecb9825380674732ec5df88f4ad08b 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/EntityTag.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:abace77d8add308d67ba45fa73c2f249)
+ * (GenCodeChecksum:2fccc138a26fe391325fd641c891cd91)
  */
 
 /**
@@ -195,6 +195,7 @@ class CRM_Core_DAO_EntityTag extends CRM_Core_DAO {
           'html' => [
             'type' => 'Select',
             'label' => ts("Tag"),
+            'controlField' => 'entity_table',
           ],
           'pseudoconstant' => [
             'table' => 'civicrm_tag',
index 3de060d975d42decdc1ec1856db0901577b9a0b9..884edf489f8e78c7dfe847800032919066ce8601 100644 (file)
@@ -25,6 +25,7 @@ use Civi\Api4\Campaign;
 use Civi\Api4\Contact;
 use Civi\Api4\Contribution;
 use Civi\Api4\EntityTag;
+use Civi\Api4\Utils\CoreUtil;
 use Civi\Test\TransactionalInterface;
 
 /**
@@ -32,6 +33,11 @@ use Civi\Test\TransactionalInterface;
  */
 class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
 
+  public function setUp(): void {
+    \CRM_Core_BAO_ConfigSetting::enableAllComponents();
+    parent::setUp();
+  }
+
   public function testOptionsAreReturned() {
     $fields = Contact::getFields(FALSE)
       ->execute()
@@ -88,11 +94,11 @@ class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
     }
   }
 
-  public function testPreloadFalse() {
+  public function testPrefetchDisabled() {
     \CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
     \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
     Campaign::create()->setValues(['name' => 'Big Campaign', 'title' => 'Biggie'])->execute();
-    // The campaign_id field has preload = false in the schema,
+    // The campaign_id field has prefetch = disabled in the schema,
     // Which means the options will NOT load but suffixes are still available
     $fields = Contribution::getFields(FALSE)
       ->setLoadOptions(['name', 'label'])
@@ -148,4 +154,63 @@ class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
     $this->assertEquals(['contact_type' => 'Organization'], $field['input_attrs']['filter']);
   }
 
+  public function testTopSortFields(): void {
+    $sampleFields = [
+      [
+        'name' => 'd',
+        'title' => 'Fourth',
+        'input_attrs' => [
+          'control_field' => 'a',
+        ],
+      ],
+      [
+        'name' => 'a',
+        'title' => 'Third',
+        'input_attrs' => [
+          'control_field' => 'c',
+        ],
+      ],
+      [
+        'name' => 'b',
+        'title' => 'First',
+      ],
+      [
+        'name' => 'c',
+        'title' => 'Second',
+        'input_attrs' => [
+          'control_field' => 'b',
+        ],
+      ],
+    ];
+    CoreUtil::topSortFields($sampleFields);
+    $this->assertEquals(['First', 'Second', 'Third', 'Fourth'], array_column($sampleFields, 'title'));
+  }
+
+  public function entityFieldsWithDependencies(): array {
+    return [
+      ['Contact', ['contact_type', 'contact_sub_type']],
+      ['Case', ['case_type_id', 'status_id']],
+      ['EntityTag', ['entity_table', 'tag_id']],
+      ['Address', ['country_id', 'state_province_id', 'county_id']],
+      ['ActionSchedule', ['mapping_id', 'entity_value', 'entity_status']],
+      ['CustomGroup', ['extends', 'extends_entity_column_id', 'extends_entity_column_value']],
+    ];
+  }
+
+  /**
+   * @dataProvider entityFieldsWithDependencies
+   */
+  public function testTopSortEntityFields(string $entityName, array $orderedFieldNames): void {
+    $entityFields = (array) civicrm_api4($entityName, 'getFields', [
+      'checkPermissions' => FALSE,
+      'where' => [['name', 'IN', $orderedFieldNames]],
+    ]);
+    // Try sorting with different starting orders; the outcome should always be the same
+    $entityFields2 = array_reverse($entityFields);
+    CoreUtil::topSortFields($entityFields);
+    CoreUtil::topSortFields($entityFields2);
+    $this->assertEquals($orderedFieldNames, array_column($entityFields, 'name'));
+    $this->assertEquals($orderedFieldNames, array_column($entityFields2, 'name'));
+  }
+
 }
index 8fe46d05ff17659b424a21132bfb7030135a6cc6..fbd329f5dc239920e7f869e94909e80d1ecfe13c 100644 (file)
@@ -30,7 +30,7 @@ class CoreUtilTest extends CustomTestBase {
 
   /**
    */
-  public function testGetApiNameFromTableName() {
+  public function testGetApiNameFromTableName(): void {
     $this->assertEquals('Contact', CoreUtil::getApiNameFromTableName('civicrm_contact'));
     $this->assertNull(CoreUtil::getApiNameFromTableName('civicrm_nothing'));
 
@@ -56,7 +56,7 @@ class CoreUtilTest extends CustomTestBase {
     $this->assertEquals($multiGroup['table_name'], CoreUtil::getTableName('Custom_' . $multiGroup['name']));
   }
 
-  public function testGetApiClass() {
+  public function testGetApiClass(): void {
     $this->assertEquals('Civi\Api4\Contact', CoreUtil::getApiClass('Contact'));
     $this->assertEquals('Civi\Api4\CiviCase', CoreUtil::getApiClass('Case'));
     $this->assertNull(CoreUtil::getApiClass('NothingAtAll'));
@@ -77,36 +77,4 @@ class CoreUtilTest extends CustomTestBase {
     $this->assertEquals('Civi\Api4\CustomValue', CoreUtil::getApiClass('Custom_' . $multiGroup['name']));
   }
 
-  public function testTopSortFields() {
-    $sampleFields = [
-      [
-        'name' => 'd',
-        'title' => 'Fourth',
-        'input_attrs' => [
-          'control_field' => 'a',
-        ],
-      ],
-      [
-        'name' => 'a',
-        'title' => 'Third',
-        'input_attrs' => [
-          'control_field' => 'c',
-        ],
-      ],
-      [
-        'name' => 'b',
-        'title' => 'First',
-      ],
-      [
-        'name' => 'c',
-        'title' => 'Second',
-        'input_attrs' => [
-          'control_field' => 'b',
-        ],
-      ],
-    ];
-    CoreUtil::topSortFields($sampleFields);
-    $this->assertEquals(['First', 'Second', 'Third', 'Fourth'], array_column($sampleFields, 'title'));
-  }
-
 }
index 7065db62c88f2c04a0536032d22a94f5d2f4d305..d78de6fc224dbbb553f02317707e8ae355b0e171 100644 (file)
     </pseudoconstant>
     <html>
       <type>Select</type>
+      <controlField>case_type_id</controlField>
     </html>
     <add>1.8</add>
   </field>
index 4d25e0cd87c947ef04b72650dbe67088997347df..c1d50300f69342e5a5bbca52c6df47f5d374bc45 100644 (file)
     <html>
       <type>Select</type>
       <multiple>1</multiple>
+      <controlField>contact_type</controlField>
     </html>
     <serialize>SEPARATOR_BOOKEND</serialize>
     <add>1.5</add>
index cccfd8314dadc9df5507d1ab3937be58891953b4..ab063e8a7d8faf5eeddd07e7f3f4162bdf672944 100644 (file)
@@ -62,6 +62,7 @@
     <html>
       <type>Select</type>
       <label>Tag</label>
+      <controlField>entity_table</controlField>
     </html>
   </field>
   <foreignKey>