diff --git a/Civi/Test/Api4TestTrait.php b/Civi/Test/Api4TestTrait.php index 771fd90d75e..760a5422889 100644 --- a/Civi/Test/Api4TestTrait.php +++ b/Civi/Test/Api4TestTrait.php @@ -368,6 +368,17 @@ protected function deleteTestRecords(): void { } } + protected function conditionallyDeleteTestRecords(): void { + $implements = class_implements($this); + // If not created in a transaction, test records must be deleted + $needsCleanup = !in_array('Civi\Test\TransactionalInterface', $implements, TRUE) || + // Creating custom groups or custom fields breaks transactions & requires cleanup + array_intersect(['CustomField', 'CustomGroup'], array_column($this->testRecords, 0)); + if ($needsCleanup) { + $this->deleteTestRecords(); + } + } + /** * Get an ID for the appropriate entity. * diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformAutocompleteUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformAutocompleteUsageTest.php index 9076dfb3623..46496f7b6e8 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformAutocompleteUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformAutocompleteUsageTest.php @@ -3,10 +3,7 @@ use Civi\Api4\Afform; use Civi\Api4\Contact; -use Civi\Api4\Group; -use Civi\Api4\GroupContact; use Civi\Api4\OptionValue; -use Civi\Api4\SavedSearch; /** * Test case for Afform with autocomplete. @@ -37,22 +34,20 @@ public function testAutocompleteWithSavedSearchFilter(): void { ]); // Saved search for filtering - SavedSearch::create(FALSE) - ->setValues([ - 'name' => 'the_unit_test_search', - 'label' => 'the_unit_test_search', - 'api_entity' => 'Contact', - 'api_params' => [ - 'version' => 4, - 'select' => ['id', 'display_name'], - 'orderBy' => [], - 'where' => [ - ['contact_type:name', '=', 'Individual'], - ['source', '=', 'Yes'], - ], + $this->createTestRecord('SavedSearch', [ + 'name' => 'the_unit_test_search', + 'label' => 'the_unit_test_search', + 'api_entity' => 'Contact', + 'api_params' => [ + 'version' => 4, + 'select' => ['id', 'display_name'], + 'orderBy' => [], + 'where' => [ + ['contact_type:name', '=', 'Individual'], + ['source', '=', 'Yes'], ], - ]) - ->execute(); + ], + ]); $lastName = uniqid(__FUNCTION__); @@ -130,13 +125,19 @@ public function testCustomContactRefFieldWithGroupsFilter(): void { 'records' => $sampleData, ])->column('id', 'first_name'); + $group = $this->createTestRecord('Group', [ + 'name' => $lastName, + 'title' => $lastName, + ]); // Place contacts A & B in the group, but not contact C - $group = Group::create(FALSE) - ->addValue('name', $lastName) - ->addValue('title', $lastName) - ->addChain('A', GroupContact::create()->addValue('group_id', '$id')->addValue('contact_id', $contacts['A'])) - ->addChain('B', GroupContact::create()->addValue('group_id', '$id')->addValue('contact_id', $contacts['B'])) - ->execute()->single(); + $this->createTestRecord('GroupContact', [ + 'group_id' => $group['id'], + 'contact_id' => $contacts['A'], + ]); + $this->createTestRecord('GroupContact', [ + 'group_id' => $group['id'], + 'contact_id' => $contacts['B'], + ]); $this->createTestRecord('CustomGroup', [ 'extends' => 'Contact', @@ -344,12 +345,6 @@ public function testAutocompleteWithSearchJoin(): void { 'option_values' => ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue', 'y' => 'Yellow'], ]); - // Hacky workaround for transactions rolling back the autocleanup :( - // Ideally this test would NOT use TransactionalInterface - civicrm_api4('SavedSearch', 'delete', [ - 'where' => [['name', '=', 'test_activity_search']], - ]); - $this->createTestRecord('SavedSearch', [ 'name' => 'test_activity_search', 'api_entity' => 'Activity', @@ -429,6 +424,7 @@ public function testAutocompleteWithSearchJoin(): void { 'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, ]); + // Autocompleting with the letter "l" will give 2 matches: Blue & Yellow $result = OptionValue::autocomplete() ->setFormName('afform:' . $this->formName) ->setFieldName('test_activity_search_display:Activity_ActivityContact_Contact_01.test_af_autocomplete_search.select_auto') diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformConditionalUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformConditionalUsageTest.php index 62c63613236..0143baac88d 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformConditionalUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformConditionalUsageTest.php @@ -2,13 +2,14 @@ namespace api\v4\Afform; use Civi\Api4\Afform; +use Civi\Test\TransactionalInterface; /** * Test case for Afform with autocomplete. * * @group headless */ -class AfformConditionalUsageTest extends AfformUsageTestCase { +class AfformConditionalUsageTest extends AfformUsageTestCase implements TransactionalInterface { /** * Required field based on text input diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformContactUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformContactUsageTest.php index 947f5e8f254..689a6b36ea7 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformContactUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformContactUsageTest.php @@ -448,11 +448,11 @@ public function testDedupeIndividual(): void { ]); $lastName = uniqid(__FUNCTION__); - $contact = \Civi\Api4\Contact::create(FALSE) - ->addValue('first_name', 'Bob') - ->addValue('last_name', $lastName) - ->addValue('email_primary.email', '123@example.com') - ->execute()->single(); + $contact = $this->createTestRecord('Individual', [ + 'first_name' => 'Bob', + 'last_name' => $lastName, + 'email_primary.email' => '123@example.com', + ]); $locationType = \CRM_Core_BAO_LocationType::getDefault()->id; $values = [ diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformCustomFieldUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformCustomFieldUsageTest.php index c8342406087..84dbddc9605 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformCustomFieldUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformCustomFieldUsageTest.php @@ -3,8 +3,6 @@ use Civi\Api4\Afform; use Civi\Api4\Contact; -use Civi\Api4\CustomField; -use Civi\Api4\CustomGroup; /** * Test case for Afform.prefill and Afform.submit. @@ -15,8 +13,6 @@ class AfformCustomFieldUsageTest extends AfformUsageTestCase { public function tearDown(): void { parent::tearDown(); - CustomField::delete(FALSE)->addWhere('id', '>', '0')->execute(); - CustomGroup::delete(FALSE)->addWhere('id', '>', '0')->execute(); } public static function setUpBeforeClass(): void { @@ -43,21 +39,33 @@ public static function setUpBeforeClass(): void { * which can be submitted multiple times */ public function testMultiRecordCustomBlock(): void { - CustomGroup::create(FALSE) - ->addValue('name', 'MyThings') - ->addValue('title', 'My Things') - ->addValue('style', 'Tab with table') - ->addValue('extends', 'Contact') - ->addValue('is_multiple', TRUE) - ->addValue('max_multiple', 2) - ->addChain('fields', CustomField::save() - ->addDefault('custom_group_id', '$id') - ->setRecords([ - ['name' => 'my_text', 'label' => 'My Text', 'data_type' => 'String', 'html_type' => 'Text'], - ['name' => 'my_friend', 'label' => 'My Friend', 'data_type' => 'ContactReference', 'html_type' => 'Autocomplete-Select'], - ]) - ) - ->execute(); + $this->createTestRecord('CustomGroup', [ + 'name' => 'MyThings', + 'title' => 'My Things', + 'style' => 'Tab with table', + 'extends' => 'Contact', + 'is_multiple' => TRUE, + 'max_multiple' => 2, + ]); + $this->saveTestRecords('CustomField', [ + 'defaults' => [ + 'custom_group_id.name' => 'MyThings', + ], + 'records' => [ + [ + 'name' => 'my_text', + 'label' => 'My Text', + 'data_type' => 'String', + 'html_type' => 'Text', + ], + [ + 'name' => 'my_friend', + 'label' => 'My Friend', + 'data_type' => 'ContactReference', + 'html_type' => 'Autocomplete-Select', + ], + ], + ]); // Creating a custom group should automatically create an afform block $block = Afform::get() diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformFileUploadTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformFileUploadTest.php index 550eae578aa..dbf19ad1b1a 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformFileUploadTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformFileUploadTest.php @@ -9,11 +9,7 @@ use Civi\Api4\Afform; use Civi\Api4\Contact; -use Civi\Api4\CustomField; -use Civi\Api4\CustomGroup; -require_once __DIR__ . '/AfformTestCase.php'; -require_once __DIR__ . '/AfformUsageTestCase.php'; class AfformFileUploadTest extends AfformUsageTestCase { public static function setUpBeforeClass(): void { @@ -45,33 +41,35 @@ public function tearDown(): void { */ public function testSubmitFile(): void { // Single-value set - CustomGroup::create(FALSE) - ->addValue('name', 'MyInfo') - ->addValue('title', 'My Info') - ->addValue('extends', 'Contact') - ->addChain('fields', CustomField::save() - ->addDefault('custom_group_id', '$id') - ->setRecords([ - ['name' => 'single_file_field', 'label' => 'A File', 'data_type' => 'File', 'html_type' => 'File'], - ]) - ) - ->execute(); + $this->createTestRecord('CustomGroup', [ + 'name' => 'MyInfo', + 'title' => 'My Info', + 'extends' => 'Contact', + ]); + $this->createTestRecord('CustomField', [ + 'custom_group_id.name' => 'MyInfo', + 'name' => 'single_file_field', + 'label' => 'A File', + 'data_type' => 'File', + 'html_type' => 'File', + ]); // Multi-record set - CustomGroup::create(FALSE) - ->addValue('name', 'MyFiles') - ->addValue('title', 'My Files') - ->addValue('style', 'Tab with table') - ->addValue('extends', 'Contact') - ->addValue('is_multiple', TRUE) - ->addValue('max_multiple', 3) - ->addChain('fields', CustomField::save() - ->addDefault('custom_group_id', '$id') - ->setRecords([ - ['name' => 'my_file', 'label' => 'My File', 'data_type' => 'File', 'html_type' => 'File'], - ]) - ) - ->execute(); + $this->createTestRecord('CustomGroup', [ + 'name' => 'MyFiles', + 'title' => 'My Files', + 'style' => 'Tab with table', + 'extends' => 'Contact', + 'is_multiple' => TRUE, + 'max_multiple' => 3, + ]); + $this->createTestRecord('CustomField', [ + 'custom_group_id.name' => 'MyFiles', + 'name' => 'my_file', + 'label' => 'My File', + 'data_type' => 'File', + 'html_type' => 'File', + ]); $this->useValues([ 'layout' => self::$layouts['customFiles'], diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformGroupSubscriptionUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformGroupSubscriptionUsageTest.php index cfbd5fd2540..42592d08d2f 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformGroupSubscriptionUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformGroupSubscriptionUsageTest.php @@ -18,11 +18,9 @@ class AfformGroupSubscriptionUsageTest extends AfformUsageTestCase implements Tr public function testGroupSubscription(): void { $groupName = __FUNCTION__; $lastName = uniqid(__FUNCTION__); - civicrm_api4('Group', 'create', [ - 'values' => [ - 'title' => 'Test Group', - 'name' => $groupName, - ], + $this->createTestRecord('Group', [ + 'title' => 'Test Group', + 'name' => $groupName, ]); $layout = << '', 'description' => '', 'server_route' => 'civicrm/mock-page', 'permission' => ['access Foobar'], 'placement' => ['dashboard_dashlet'], 'submit_enabled' => TRUE]], ['mockBareFile', ['title' => '', 'description' => '', 'permission' => ['access CiviCRM'], 'placement' => [], 'submit_enabled' => TRUE]], ['mockFoo', ['title' => '', 'description' => '', 'permission' => ['access CiviCRM']], 'submit_enabled' => TRUE], ['mock-weird-name', ['title' => 'Weird Name', 'description' => '', 'permission' => ['access CiviCRM']], 'submit_enabled' => TRUE], ]; + // Provide a meaningful index for test data set + return array_column($directives, NULL, 0); } /** @@ -137,7 +138,8 @@ public function getFormatExamples() { if (isset($example['deep'])) { foreach ($formats as $updateFormat) { foreach ($formats as $readFormat) { - $ex[] = ['mockBareFile', $updateFormat, $example[$updateFormat], $readFormat, $example[$readFormat], $exampleFile]; + $key = basename($exampleFile, '.php') . '-' . $updateFormat . '-' . $readFormat; + $ex[$key] = ['mockBareFile', $updateFormat, $example[$updateFormat], $readFormat, $example[$readFormat], $exampleFile]; } } } @@ -231,7 +233,7 @@ public function getWhitespaceExamples() { foreach (glob(__DIR__ . '/../formatExamples/*.php') as $exampleFile) { $example = require $exampleFile; if (isset($example['pretty'])) { - $ex[] = ['mockBareFile', $example, $exampleFile]; + $ex[basename($exampleFile, '.php')] = ['mockBareFile', $example, $exampleFile]; } } return $ex; diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformTestCase.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformTestCase.php index 8951f5e05cd..df2428f893b 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformTestCase.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformTestCase.php @@ -2,12 +2,12 @@ namespace api\v4\Afform; use Civi\Test\HeadlessInterface; -use Civi\Test\TransactionalInterface; /** * Base class for Afform API tests. */ -abstract class AfformTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface { +abstract class AfformTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterface { + use \Civi\Test\Api4TestTrait; /** * Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). diff --git a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformUsageTestCase.php b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformUsageTestCase.php index aefb066b9e1..82b255be10f 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformUsageTestCase.php +++ b/ext/afform/mock/tests/phpunit/api/v4/Afform/AfformUsageTestCase.php @@ -10,7 +10,6 @@ * @group headless */ abstract class AfformUsageTestCase extends AfformTestCase { - use \Civi\Test\Api4TestTrait; protected static $layouts = []; @@ -31,7 +30,7 @@ public function tearDown(): void { CustomGroup::delete(FALSE) ->addWhere('id', '>', 0) ->execute(); - $this->deleteTestRecords(); + $this->conditionallyDeleteTestRecords(); parent::tearDown(); } diff --git a/tests/phpunit/api/v4/Api4TestBase.php b/tests/phpunit/api/v4/Api4TestBase.php index 88eecd102bc..78fc4986451 100644 --- a/tests/phpunit/api/v4/Api4TestBase.php +++ b/tests/phpunit/api/v4/Api4TestBase.php @@ -52,14 +52,7 @@ public function setUpHeadless(): CiviEnvBuilder { * Post test cleanup. */ public function tearDown(): void { - $implements = class_implements($this); - // If not created in a transaction, test records must be deleted - $needsCleanup = !in_array('Civi\Test\TransactionalInterface', $implements, TRUE) || - // Creating custom groups or custom fields breaks transactions & requires cleanup - array_intersect(['CustomField', 'CustomGroup'], array_column($this->testRecords, 0)); - if ($needsCleanup) { - $this->deleteTestRecords(); - } + $this->conditionallyDeleteTestRecords(); } /**