From b033e09a6d935282fa9cfb79b4d6501fa5825600 Mon Sep 17 00:00:00 2001
From: Eileen McNaughton <emcnaughton@wikimedia.org>
Date: Fri, 23 Jun 2023 20:18:50 -0700
Subject: [PATCH] Cleanup Apiv3-UfField test

Stop using xml for set up, other minor fixes, remove tests that are very well covered elsewhere
(basic api wrapper & crud)
---
 Civi/Test/ContactTestTrait.php              |   5 +-
 tests/phpunit/CiviTest/CiviUnitTestCase.php |  28 ++--
 tests/phpunit/api/v3/GroupTest.php          |  12 +-
 tests/phpunit/api/v3/UFFieldTest.php        | 174 +++++---------------
 4 files changed, 68 insertions(+), 151 deletions(-)

diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php
index 39ae9acb8a..b15fdd9333 100644
--- a/Civi/Test/ContactTestTrait.php
+++ b/Civi/Test/ContactTestTrait.php
@@ -201,11 +201,12 @@ trait ContactTestTrait {
    * Add a Group.
    *
    * @param array $params
+   * @param string $identifier
    *
    * @return int
    *   groupId of created group
    */
-  public function groupCreate(array $params = []): int {
+  public function groupCreate(array $params = [], string $identifier = 'group'): int {
     $params = array_merge([
       'name' => 'Test Group 1',
       'domain_id' => 1,
@@ -219,7 +220,7 @@ trait ContactTestTrait {
       ],
     ], $params);
 
-    $result = $this->callAPISuccess('Group', 'create', $params);
+    $result = $this->createTestEntity('Group', $params, $identifier);
     return $result['id'];
   }
 
diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php
index 7e924805a7..5ac2c77d66 100644
--- a/tests/phpunit/CiviTest/CiviUnitTestCase.php
+++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php
@@ -2130,10 +2130,14 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    *
    *  You need to have pre-created these groups & created the user e.g
    *  $this->createLoggedInUser();
-   *   $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
-   *   $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
+   *   $this->_permissionedDisabledGroup = $this->groupCreate(array('title' =>
+   * 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
+   *   $this->_permissionedGroup = $this->groupCreate(array('title' =>
+   * 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
    *
    * @param bool $isProfile
+   *
+   * @throws \Civi\Core\Exception\DBQueryException
    */
   public function setupACL($isProfile = FALSE) {
     global $_REQUEST;
@@ -2147,24 +2151,26 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
     if ($ov->find(TRUE)) {
       CRM_Core_DAO::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
     }
-    $optionValue = $this->callAPISuccess('option_value', 'create', [
+    $this->callAPISuccess('option_value', 'create', [
       'option_group_id' => $optionGroupID,
       'label' => 'pick me',
       'value' => 55,
     ]);
 
-    CRM_Core_DAO::executeQuery("
+    CRM_Core_DAO::executeQuery('
       TRUNCATE civicrm_acl_cache
-    ");
+    ');
 
-    CRM_Core_DAO::executeQuery("
+    CRM_Core_DAO::executeQuery('
       TRUNCATE civicrm_acl_contact_cache
-    ");
+    ');
 
+    // Setting ids is preferred.
+    $permissionedGroup = $this->ids['Group']['permissioned_group'] ?? $this->_permissionedGroup;
     CRM_Core_DAO::executeQuery("
     INSERT INTO civicrm_acl_entity_role (
     `acl_role_id`, `entity_table`, `entity_id`, `is_active`
-    ) VALUES (55, 'civicrm_group', {$this->_permissionedGroup}, 1);
+    ) VALUES (55, 'civicrm_group', $permissionedGroup, 1);
     ");
 
     if ($isProfile) {
@@ -2183,7 +2189,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
       `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
       )
       VALUES (
-      'view picked', 'civicrm_group', $this->_permissionedGroup , 'Edit', 'civicrm_group', {$this->_permissionedGroup}, 1
+      'view picked', 'civicrm_group', $permissionedGroup , 'Edit', 'civicrm_group', {$this->_permissionedGroup}, 1
       );
       ");
 
@@ -2192,14 +2198,14 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
       `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
       )
       VALUES (
-      'view picked', 'civicrm_group',  $this->_permissionedGroup, 'Edit', 'civicrm_group', {$this->_permissionedDisabledGroup}, 1
+      'view picked', 'civicrm_group',  $permissionedGroup, 'Edit', 'civicrm_group', {$this->_permissionedDisabledGroup}, 1
       );
       ");
     }
 
     $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
     $this->callAPISuccess('group_contact', 'create', [
-      'group_id' => $this->_permissionedGroup,
+      'group_id' => $permissionedGroup,
       'contact_id' => $this->_loggedInUser,
     ]);
 
diff --git a/tests/phpunit/api/v3/GroupTest.php b/tests/phpunit/api/v3/GroupTest.php
index 26f721a6c1..fa54cced5d 100644
--- a/tests/phpunit/api/v3/GroupTest.php
+++ b/tests/phpunit/api/v3/GroupTest.php
@@ -266,12 +266,14 @@ class api_v3_GroupTest extends CiviUnitTestCase {
    * Per https://lab.civicrm.org/dev/core/issues/1321 the group_type filter is deceptive
    * - it only filters on exact match not 'is one of'.
    *
-   * @throws \CRM_Core_Exception
    */
-  public function testGroupWithGroupTypeFilter() {
-    $this->groupCreate(['group_type' => ['Access Control'], 'name' => 'access_list', 'title' => 'access list']);
-    $this->groupCreate(['group_type' => ['Mailing List'], 'name' => 'mailing_list', 'title' => 'mailing list']);
-    $this->groupCreate(['group_type' => ['Access Control', 'Mailing List'], 'name' => 'group', 'title' => 'group']);
+  public function testGroupWithGroupTypeFilter(): void {
+    // Note that calling the api v3 actually creates groups incorrectly - it the
+    // separator is SEPARATOR_BOOKEND but api v3 just saves the integer for only one.
+    // If you fix that this test fails.... make of that what you will...
+    $this->callAPISuccess('Group', 'create', ['group_type' => ['Access Control'], 'name' => 'access_list', 'title' => 'access list']);
+    $this->callAPISuccess('Group', 'create', ['group_type' => ['Mailing List'], 'name' => 'mailing_list', 'title' => 'mailing list']);
+    $this->callAPISuccess('Group', 'create', ['group_type' => ['Access Control', 'Mailing List'], 'name' => 'group', 'title' => 'group']);
     $group = $this->callAPISuccessGetSingle('Group', ['return' => 'id,title,group_type', 'group_type' => 'Mailing List']);
     $this->assertEquals('mailing list', $group['title']);
   }
diff --git a/tests/phpunit/api/v3/UFFieldTest.php b/tests/phpunit/api/v3/UFFieldTest.php
index c52aab89fa..2e956e1021 100644
--- a/tests/phpunit/api/v3/UFFieldTest.php
+++ b/tests/phpunit/api/v3/UFFieldTest.php
@@ -17,28 +17,12 @@
  */
 class api_v3_UFFieldTest extends CiviUnitTestCase {
 
-  /**
-   * ids from the uf_group_test.xml fixture
-   *
-   * @var int
-   */
-  protected $_ufGroupId = 11;
-
-  protected $_ufFieldId;
-
-  protected $_contactId = 69;
-
-  protected $_params;
-
-  protected $_entity = 'uf_field';
-
   /**
    * Set up for test.
    *
    * @throws \Exception
    */
-  protected function setUp(): void {
-    parent::setUp();
+  public function tearDown(): void {
     $this->quickCleanup(
       [
         'civicrm_group',
@@ -49,12 +33,21 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
         'civicrm_uf_match',
       ]
     );
+    parent::tearDown();
+  }
 
-    $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
-
-    $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
-
-    $this->_params = [
+  /**
+   * Create a field with 'weight=1' and then a second with 'weight=1'.
+   *
+   * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
+   *
+   * @param int $version
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreateUFFieldWithDefaultAutoWeight(int $version): void {
+    $this->_apiversion = $version;
+    $params = [
       'field_name' => 'phone',
       'field_type' => 'Contact',
       'visibility' => 'Public Pages and Listings',
@@ -64,86 +57,20 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
       'is_active' => 1,
       'location_type_id' => 1,
       'phone_type_id' => 1,
-      'uf_group_id' => $this->_ufGroupId,
+      'uf_group_id' => $this->createTestEntity('UFGroup', [
+        'group_type' => 'Contact',
+        'title' => 'Test Profile',
+      ])['id'],
     ];
-  }
-
-  /**
-   * Tear down function.
-   *
-   * @throws \Exception
-   */
-  public function tearDown(): void {
-    $this->quickCleanup(
-      [
-        'civicrm_group',
-        'civicrm_contact',
-        'civicrm_uf_group',
-        'civicrm_uf_join',
-        'civicrm_uf_match',
-      ]
-    );
-  }
-
-  /**
-   * Create / updating field.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testCreateUFField($version) {
-    $this->_apiversion = $version;
-    $params = $this->_params;
-    $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
-    unset($params['uf_group_id']);
-    $this->_ufFieldId = $ufField['id'];
-    foreach ($params as $key => $value) {
-      $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
-    }
-  }
-
-  /**
-   * Failure test for field_name.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testCreateUFFieldWithBadFieldName($version) {
-    $this->_apiversion = $version;
-    $params = $this->_params;
-    $params['field_name'] = 'custom_98789';
-    $this->callAPIFailure('uf_field', 'create', $params);
-  }
-
-  /**
-   * Failure test for bad parameters.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testCreateUFFieldWithWrongParams($version) {
-    $this->_apiversion = $version;
-    $this->callAPIFailure('uf_field', 'create', ['field_name' => 'test field']);
-    $this->callAPIFailure('uf_field', 'create', ['label' => 'name-less field']);
-  }
-
-  /**
-   * Create a field with 'weight=1' and then a second with 'weight=1'.
-   *
-   * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testCreateUFFieldWithDefaultAutoWeight($version) {
-    $this->_apiversion = $version;
-    $params1 = $this->_params;
-    $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
+    $ufField1 = $this->callAPISuccess('uf_field', 'create', $params);
     $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
     $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
       1 => [$ufField1['id'], 'Int'],
     ]);
 
-    $params2 = $this->_params;
     // needs to be a different field
-    $params2['location_type_id'] = 2;
-    $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
+    $params['location_type_id'] = 2;
+    $ufField2 = $this->callAPISuccess('UFField', 'create', $params);
     $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
     $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
       1 => [$ufField2['id'], 'Int'],
@@ -153,36 +80,10 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
     ]);
   }
 
-  /**
-   * Deleting field.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testDeleteUFField($version) {
-    $this->_apiversion = $version;
-    $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
-    $params = [
-      'field_id' => $ufField['id'],
-    ];
-    $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
-  }
-
-  /**
-   * Test getting ufField.
-   * @param int $version
-   * @dataProvider versionThreeAndFour
-   */
-  public function testGetUFFieldSuccess($version) {
-    $this->_apiversion = $version;
-    $this->callAPISuccess($this->_entity, 'create', $this->_params);
-    $result = $this->callAPIAndDocument($this->_entity, 'get', [], __FUNCTION__, __FILE__);
-    $this->getAndCheck($this->_params, $result['id'], $this->_entity);
-  }
-
   /**
    * Create / updating field.
    */
-  public function testReplaceUFFields() {
+  public function testReplaceUFFields(): void {
     $baseFields = [];
     $baseFields[] = [
       'field_name' => 'first_name',
@@ -216,17 +117,20 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
     ];
 
     $params = [
-      'uf_group_id' => $this->_ufGroupId,
+      'uf_group_id' => $this->createTestEntity('UFGroup', [
+        'group_type' => 'Contact',
+        'title' => 'Test Profile',
+      ])['id'],
       'option.autoweight' => FALSE,
       'values' => $baseFields,
       'check_permissions' => TRUE,
     ];
 
-    $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__);
+    $result = $this->callAPISuccess('UFField', 'replace', $params);
     $inputsByName = CRM_Utils_Array::index(['field_name'], $params['values']);
-    $this->assertEquals(count($params['values']), count($result['values']));
+    $this->assertSameSize($params['values'], $result['values']);
     foreach ($result['values'] as $outUfField) {
-      $this->assertTrue(is_string($outUfField['field_name']));
+      $this->assertIsString($outUfField['field_name']);
       $inUfField = $inputsByName[$outUfField['field_name']];
       foreach ($inUfField as $key => $inValue) {
         $this->assertEquals($inValue, $outUfField[$key],
@@ -246,7 +150,7 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
    * @param int $version
    * @dataProvider versionThreeAndFour
    */
-  public function testProfilesWithoutACL($version) {
+  public function testProfilesWithoutACL(int $version): void {
     $this->_apiversion = $version;
     $this->createLoggedInUser();
     $baseFields[] = [
@@ -260,25 +164,29 @@ class api_v3_UFFieldTest extends CiviUnitTestCase {
     ];
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
     $params = [
-      'uf_group_id' => $this->_ufGroupId,
+      'uf_group_id' => $this->createTestEntity('UFGroup', [
+        'group_type' => 'Contact',
+        'title' => 'Test Profile',
+      ])['id'],
       'option.autoweight' => FALSE,
       'values' => $baseFields,
       'check_permissions' => TRUE,
     ];
-    $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
-    $this->callAPIFailure('uf_field', 'replace', $params);
+    $this->callAPIFailure('UFField', 'replace', $params);
   }
 
   /**
    * Check Profile ACL for API permission.
+   *
+   * @throws \Civi\Core\Exception\DBQueryException
    */
-  public function testACLPermissionforProfiles() {
+  public function testACLPermissionForProfiles(): void {
     $this->createLoggedInUser();
-    $this->_permissionedGroup = $this->groupCreate([
+    $this->groupCreate([
       'title' => 'Edit Profiles',
       'is_active' => 1,
       'name' => 'edit-profiles',
-    ]);
+    ], 'permissioned_group');
     $this->setupACL(TRUE);
     $this->testReplaceUFFields();
   }
-- 
2.25.1