From b2d3d2d3e36ee7b1e8bf9562a4855a983e4b0987 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 3 Jun 2023 12:06:00 +1200 Subject: [PATCH] More test UFGroup cleanup Fixes a couple more tests to appropriate UFGroup cleanup --- tests/phpunit/CRM/Contact/SelectorTest.php | 6 +- tests/phpunit/CRM/Core/BAO/UFFieldTest.php | 69 +++++++++------------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index 24475b7694..7d4c33846a 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -123,7 +123,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase { */ public function testSearchByProfile(): void { //Create search profile for contacts. - $ufGroup = $this->callAPISuccess('UFGroup', 'create', [ + $this->setTestEntityID('UFGroup', $this->callAPISuccess('UFGroup', 'create', [ 'group_type' => 'Contact', 'name' => 'test_search_profile', 'title' => 'Test Search Profile', @@ -136,7 +136,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase { 'in_selector' => 1, ], ], - ]); + ])['id'], 'test_profile'); $contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']); //Put the email on hold. $email = $this->callAPISuccess('Email', 'get', [ @@ -152,7 +152,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase { 'description' => 'Normal default behaviour', 'class' => 'CRM_Contact_Selector', 'settings' => [], - 'form_values' => ['email' => 'mickey@mouseville.com', 'uf_group_id' => $ufGroup['id']], + 'form_values' => ['email' => 'mickey@mouseville.com', 'uf_group_id' => $this->ids['UFGroup']['test_profile']], 'params' => [], 'return_properties' => NULL, 'context' => 'advanced', diff --git a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php index 1b6fd1d122..e4eb850e5f 100644 --- a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php @@ -1,22 +1,18 @@ quickCleanup(['civicrm_uf_group', 'civicrm_uf_field']); - } - /** - * When passing in a GID, fields should be omitted if they already appear in the group. + * When passing in an id, fields should be omitted if they already appear in the group. */ - public function testGetAvailable_byGid() { - $ufGroupId = $this->createUFGroup([ + public function testGetByID(): void { + $fields = CRM_Core_BAO_UFField::getAvailableFields($this->createUFGroup([ [ 'field_name' => 'do_not_sms', 'field_type' => 'Contact', @@ -41,8 +37,7 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { 'field_name' => 'activity_date_time', 'field_type' => 'Activity', ], - ]); - $fields = CRM_Core_BAO_UFField::getAvailableFields($ufGroupId); + ])); // Make sure that each entity has 1+ present field and 1+ missing (already-used) field // already used @@ -85,8 +80,8 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { * When passing in $defaults, the currently selected field should still be included -- even if * it's already part of the profile. */ - public function testGetAvailable_byGidDefaults() { - $ufGroupId = $this->createUFGroup([ + public function testGetAvailableByIDDefaults(): void { + $this->createUFGroup([ [ 'field_name' => 'do_not_sms', 'field_type' => 'Contact', @@ -97,7 +92,7 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { ], ]); $defaults = ['field_name' => ['Individual', 'first_name']]; - $fields = CRM_Core_BAO_UFField::getAvailableFields($ufGroupId, $defaults); + $fields = CRM_Core_BAO_UFField::getAvailableFields($this->ids['UFGroup']['test'], $defaults); // already used $this->assertFalse(isset($fields['Contact']['do_not_sms'])); @@ -109,9 +104,9 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { } /** - * When omitting a GID, return a list of all fields. + * When omitting the id, return a list of all fields. */ - public function testGetAvailable_full() { + public function testGetAvailableFull(): void { $fields = CRM_Core_BAO_UFField::getAvailableFields(); // Make sure that each entity appears with at least one field @@ -146,9 +141,9 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { } /** - * When omitting a GID, return a list of all fields. + * When omitting the id, return a list of all fields. */ - public function testGetAvailableFlat() { + public function testGetAvailableFlat(): void { $fields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); // Make sure that each entity appears with at least one field @@ -179,30 +174,26 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { $this->assertFalse(isset($fields['membership_type_id'])); } - /** - * Make sure that the existence of a profile doesn't break listing all fields - * - * public function testGetAvailable_mixed() { - * // FIXME - * $this->testGetAvailable_full(); - * // $this->testGetAvailable_byGid(); - * $this->testGetAvailable_full(); - * // $this->testGetAvailable_byGid(); - * } // */ - /** * @param array $fields * List of fields to include in the profile. + * * @return int * field id */ - protected function createUFGroup($fields) { - $ufGroup = CRM_Core_DAO::createTestObject('CRM_Core_DAO_UFGroup'); - $this->assertTrue(is_numeric($ufGroup->id)); - + protected function createUFGroup(array $fields): int { + try { + $this->ids['UFGroup']['test'] = UFGroup::create() + ->setValues(['name' => 'uf_field_test', 'title' => 'group']) + ->execute() + ->first()['id']; + } + catch (CRM_Core_Exception $e) { + $this->fail('Failed to create UFGroup'); + } foreach ($fields as $field) { $defaults = [ - 'uf_group_id' => $ufGroup->id, + 'uf_group_id' => $this->ids['UFGroup']['test'], 'visibility' => 'Public Pages and Listings', 'weight' => 1, 'label' => 'Label for ' . $field['field_name'], @@ -210,18 +201,16 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { 'is_active' => 1, 'location_type_id' => NULL, ]; - $params = array_merge($field, $defaults); - $ufField = $this->callAPISuccess('UFField', 'create', $params); - $this->assertAPISuccess($ufField); + $this->callAPISuccess('UFField', 'create', array_merge($field, $defaults)); } - return $ufGroup->id; + return $this->ids['UFGroup']['test']; } /** * Test ability to modify the acceptable fields for use in a profile via hook */ - public function testGetFieldsFlatModifiedByHook() { + public function testGetFieldsFlatModifiedByHook(): void { unset(Civi::$statics['UFFieldsFlat']); $this->hookClass->setHook('civicrm_alterUFFields', [$this, 'modifyUFFields']); $fields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); @@ -230,7 +219,7 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { $this->assertEquals('contact_id', $fields['foo_contact_id']['name']); } - public function modifyUFFields(&$fields) { + public function modifyUFFields(&$fields): void { $fields['Foo'] = [ 'foo_id' => [ 'name' => 'id', -- 2.25.1