From c77cad5872b2ff1982817333b190673e5f97d51e Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 11 May 2022 19:39:17 -0400 Subject: [PATCH] APIv4 Unit test fixes - ensure test records are cleaned up in non-transaction tests Use the new `createTestRecord` and `saveTestRecords` in non-transaction test classes so the created records are automatically cleaned-up. --- .../v4/Action/AbstractActionFunctionTest.php | 3 +- .../api/v4/Action/GetExtraFieldsTest.php | 3 +- .../api/v4/Action/GetFromArrayTest.php | 3 +- tests/phpunit/api/v4/Action/IndexTest.php | 3 +- tests/phpunit/api/v4/Api4TestBase.php | 12 +- .../api/v4/Custom/BasicCustomFieldTest.php | 175 ++++++++---------- .../v4/Custom/CreateWithOptionGroupTest.php | 50 ++--- .../api/v4/Custom/CustomContactRefTest.php | 82 ++++---- .../api/v4/Custom/CustomFieldAlterTest.php | 17 +- .../v4/Custom/CustomValuePerformanceTest.php | 20 +- .../phpunit/api/v4/Custom/CustomValueTest.php | 23 +-- .../v4/Custom/ExtendFromIndividualTest.php | 13 +- .../api/v4/Custom/PseudoconstantTest.php | 136 +++++++------- .../api/v4/Custom/UpdateCustomValueTest.php | 13 +- tests/phpunit/api/v4/Entity/EntityTest.php | 3 +- .../phpunit/api/v4/Entity/ExampleDataTest.php | 3 +- .../v4/Service/Schema/SchemaMapperTest.php | 3 +- .../phpunit/api/v4/Spec/SpecGathererTest.php | 48 +---- 18 files changed, 268 insertions(+), 342 deletions(-) diff --git a/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php b/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php index 84fd299c73d4..2c7bea1c2641 100644 --- a/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php +++ b/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php @@ -19,12 +19,11 @@ namespace api\v4\Action; use api\v4\Api4TestBase; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class AbstractActionFunctionTest extends Api4TestBase implements TransactionalInterface { +class AbstractActionFunctionTest extends Api4TestBase { public function testUndefinedParamException(): void { $this->expectException('API_Exception'); diff --git a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php index 192cf0f88f81..32b680946d00 100644 --- a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php @@ -24,12 +24,11 @@ use Civi\Api4\Address; use Civi\Api4\Contact; use Civi\Api4\Tag; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class GetExtraFieldsTest extends Api4TestBase implements TransactionalInterface { +class GetExtraFieldsTest extends Api4TestBase { public function testGetFieldsByContactType() { $getFields = Contact::getFields(FALSE)->addSelect('name')->addWhere('type', '=', 'Field'); diff --git a/tests/phpunit/api/v4/Action/GetFromArrayTest.php b/tests/phpunit/api/v4/Action/GetFromArrayTest.php index 85e20bda0255..90facdd3d35c 100644 --- a/tests/phpunit/api/v4/Action/GetFromArrayTest.php +++ b/tests/phpunit/api/v4/Action/GetFromArrayTest.php @@ -21,12 +21,11 @@ use api\v4\Api4TestBase; use Civi\Api4\MockArrayEntity; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class GetFromArrayTest extends Api4TestBase implements TransactionalInterface { +class GetFromArrayTest extends Api4TestBase { public function testArrayGetWithLimit() { $result = MockArrayEntity::get() diff --git a/tests/phpunit/api/v4/Action/IndexTest.php b/tests/phpunit/api/v4/Action/IndexTest.php index 26fcd90ce4cd..aa9c507f1061 100644 --- a/tests/phpunit/api/v4/Action/IndexTest.php +++ b/tests/phpunit/api/v4/Action/IndexTest.php @@ -20,12 +20,11 @@ namespace api\v4\Action; use api\v4\Api4TestBase; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class IndexTest extends Api4TestBase implements TransactionalInterface { +class IndexTest extends Api4TestBase { public function testIndex() { // Results indexed by name diff --git a/tests/phpunit/api/v4/Api4TestBase.php b/tests/phpunit/api/v4/Api4TestBase.php index 07a1e6f22e8e..ff0c5282dc5c 100644 --- a/tests/phpunit/api/v4/Api4TestBase.php +++ b/tests/phpunit/api/v4/Api4TestBase.php @@ -56,9 +56,13 @@ public function setUpHeadless() { /** */ public function tearDown(): void { - // Delete all test records in reverse order to prevent fk constraints - foreach (array_reverse($this->testRecords) as $record) { - civicrm_api4($record[0], 'delete', ['checkPermissions' => FALSE, 'where' => $record[1]]); + $impliments = class_implements($this); + // If not created in a transaction, test records must be deleted + if (!in_array('Civi\Test\TransactionalInterface', $impliments, TRUE)) { + // Delete all test records in reverse order to prevent fk constraints + foreach (array_reverse($this->testRecords) as $record) { + civicrm_api4($record[0], 'delete', ['checkPermissions' => FALSE, 'where' => $record[1]]); + } } } @@ -114,7 +118,7 @@ public function createLoggedInUser() { * @throws \Civi\API\Exception\NotImplementedException */ public function createTestRecord(string $entityName, array $values = []) { - return $this->saveTestRecords($entityName, ['records' => [$values]])->first(); + return $this->saveTestRecords($entityName, ['records' => [$values]])->single(); } /** diff --git a/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php b/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php index 33c53e9a5de5..b69940e2e0d9 100644 --- a/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php +++ b/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php @@ -22,10 +22,7 @@ use Civi\Api4\Contact; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; -use Civi\Api4\Event; -use Civi\Api4\FinancialType; use Civi\Api4\OptionGroup; -use Civi\Api4\Relationship; use Civi\Api4\RelationshipCache; /** @@ -33,16 +30,6 @@ */ class BasicCustomFieldTest extends CustomTestBase { - public function tearDown(): void { - FinancialType::delete(FALSE) - ->addWhere('name', '=', 'Test_Type') - ->execute(); - Event::delete(FALSE) - ->addWhere('id', '>', 0) - ->execute(); - parent::tearDown(); - } - /** * @throws \API_Exception */ @@ -66,13 +53,12 @@ public function testWithSingleField(): void { $this->assertContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name')); $this->assertNotContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type:name' => 'Household'])->execute()->column('name')); - $contactId = Contact::create(FALSE) - ->addValue('first_name', 'Johann') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyIndividualFields.FavColor', 'Red') - ->execute() - ->first()['id']; + $contactId = $this->createTestRecord('Contact', [ + 'first_name' => 'Johann', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyIndividualFields.FavColor' => 'Red', + ])['id']; $contact = Contact::get(FALSE) ->addSelect('first_name') @@ -158,21 +144,19 @@ public function testWithTwoFields() { // But the api will report is_required as not nullable $this->assertFalse($fields['MyContactFields2.FavFood']['nullable']); - $contactId1 = Contact::create(FALSE) - ->addValue('first_name', 'Johann') - ->addValue('last_name', 'Tester') - ->addValue('MyContactFields.FavColor', 'Red') - ->addValue('MyContactFields.FavFood', 'Cherry') - ->execute() - ->first()['id']; + $contactId1 = $this->createTestRecord('Contact', [ + 'first_name' => 'Johann', + 'last_name' => 'Tester', + 'MyContactFields.FavColor' => 'Red', + 'MyContactFields.FavFood' => 'Cherry', + ])['id']; - $contactId2 = Contact::create(FALSE) - ->addValue('first_name', 'MaryLou') - ->addValue('last_name', 'Tester') - ->addValue('MyContactFields.FavColor', 'Purple') - ->addValue('MyContactFields.FavFood', 'Grapes') - ->execute() - ->first()['id']; + $contactId2 = $this->createTestRecord('Contact', [ + 'first_name' => 'MaryLou', + 'last_name' => 'Tester', + 'MyContactFields.FavColor' => 'Purple', + 'MyContactFields.FavFood' => 'Grapes', + ])['id']; $contact = Contact::get(FALSE) ->addSelect('first_name') @@ -287,26 +271,24 @@ public function testRelationshipCacheCustomFields() { ->execute() ); - $parent = Contact::create(FALSE) - ->addValue('first_name', 'Parent') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; - - $child = Contact::create(FALSE) - ->addValue('first_name', 'Child') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; - - Relationship::create(FALSE) - ->addValue('contact_id_a', $parent) - ->addValue('contact_id_b', $child) - ->addValue('relationship_type_id', 1) - ->addValue("$cgName.PetName", 'Buddy') - ->execute(); + $parent = $this->createTestRecord('Contact', [ + 'first_name' => 'Parent', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + ])['id']; + + $child = $this->createTestRecord('Contact', [ + 'first_name' => 'Child', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + ])['id']; + + $this->createTestRecord('Relationship', [ + 'contact_id_a' => $parent, + 'contact_id_b' => $child, + 'relationship_type_id' => 1, + "$cgName.PetName" => 'Buddy', + ]); // Test get directly from relationshipCache entity $results = RelationshipCache::get(FALSE) @@ -351,25 +333,23 @@ public function testMultipleJoinsToCustomTable() { ->addValue('data_type', 'String')) ->execute(); - $parent = Contact::create(FALSE) - ->addValue('first_name', 'Parent') - ->addValue('last_name', 'Tester') - ->addValue("$cgName.FavColor", 'Purple') - ->execute() - ->first()['id']; + $parent = $this->createTestRecord('Contact', [ + 'first_name' => 'Parent', + 'last_name' => 'Tester', + "$cgName.FavColor" => 'Purple', + ])['id']; - $child = Contact::create(FALSE) - ->addValue('first_name', 'Child') - ->addValue('last_name', 'Tester') - ->addValue("$cgName.FavColor", 'Cyan') - ->execute() - ->first()['id']; + $child = $this->createTestRecord('Contact', [ + 'first_name' => 'Child', + 'last_name' => 'Tester', + "$cgName.FavColor" => 'Cyan', + ])['id']; - Relationship::create(FALSE) - ->addValue('contact_id_a', $parent) - ->addValue('contact_id_b', $child) - ->addValue('relationship_type_id', 1) - ->execute(); + $this->createTestRecord('Relationship', [ + 'contact_id_a' => $parent, + 'contact_id_b' => $child, + 'relationship_type_id' => 1, + ]); $results = Contact::get(FALSE) ->addSelect('first_name', 'child.first_name', "$cgName.FavColor", "child.$cgName.FavColor") @@ -514,13 +494,12 @@ public function testCustomDateFields(): void { ->addValue('time_format', '1')) ->execute(); - $cid = Contact::create(FALSE) - ->addValue('first_name', 'Parent') - ->addValue('last_name', 'Tester') - ->addValue("$cgName.DateOnly", '2025-05-10') - ->addValue("$cgName.DateTime", '2025-06-11 12:15:30') - ->execute() - ->first()['id']; + $cid = $this->createTestRecord('Contact', [ + 'first_name' => 'Parent', + 'last_name' => 'Tester', + "$cgName.DateOnly" => '2025-05-10', + "$cgName.DateTime" => '2025-06-11 12:15:30', + ])['id']; $contact = Contact::get(FALSE) ->addSelect('custom.*') ->addWhere('id', '=', $cid) @@ -570,11 +549,11 @@ public function testExtendsMetadata() { $this->assertEquals('case_type_id', $options['Case']); // Test contribution type - $financialType = FinancialType::create(FALSE) - ->addValue('name', 'Test_Type') - ->addValue('is_deductible', TRUE) - ->addValue('is_reserved', FALSE) - ->execute()->single(); + $financialType = $this->createTestRecord('FinancialType', [ + 'name' => 'Test_Type', + 'is_deductible' => TRUE, + 'is_reserved' => FALSE, + ]); $contributionGroup = CustomGroup::create(FALSE) ->addValue('extends', 'Contribution') ->addValue('title', 'Contribution Fields') @@ -584,21 +563,21 @@ public function testExtendsMetadata() { } public function testExtendsParticipantMetadata() { - $event1 = Event::create(FALSE) - ->addValue('event_type_id:name', 'Fundraiser') - ->addValue('title', 'Test Fun Event') - ->addValue('start_date', '2022-05-02 18:24:00') - ->execute()->first(); - $event2 = Event::create(FALSE) - ->addValue('event_type_id:name', 'Fundraiser') - ->addValue('title', 'Test Fun Event2') - ->addValue('start_date', '2022-05-02 18:24:00') - ->execute()->first(); - $event3 = Event::create(FALSE) - ->addValue('event_type_id:name', 'Meeting') - ->addValue('title', 'Test Me Event') - ->addValue('start_date', '2022-05-02 18:24:00') - ->execute()->first(); + $event1 = $this->createTestRecord('Event', [ + 'event_type_id:name' => 'Fundraiser', + 'title' => 'Test Fun Event', + 'start_date' => '2022-05-02 18:24:00', + ]); + $event2 = $this->createTestRecord('Event', [ + 'event_type_id:name' => 'Fundraiser', + 'title' => 'Test Fun Event2', + 'start_date' => '2022-05-02 18:24:00', + ]); + $event3 = $this->createTestRecord('Event', [ + 'event_type_id:name' => 'Meeting', + 'title' => 'Test Me Event', + 'start_date' => '2022-05-02 18:24:00', + ]); $field = \Civi\Api4\CustomGroup::getFields(FALSE) ->setLoadOptions(['id', 'name', 'label']) diff --git a/tests/phpunit/api/v4/Custom/CreateWithOptionGroupTest.php b/tests/phpunit/api/v4/Custom/CreateWithOptionGroupTest.php index cee1b375a551..c217f3f62fc1 100644 --- a/tests/phpunit/api/v4/Custom/CreateWithOptionGroupTest.php +++ b/tests/phpunit/api/v4/Custom/CreateWithOptionGroupTest.php @@ -70,14 +70,14 @@ public function testGetWithCustomData() { ->addValue('data_type', 'Money') ->execute(); - Contact::create(FALSE) - ->addValue('first_name', 'Jerome') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue("$group.$colorField", 'r') - ->addValue("$group.$foodField", '1') - ->addValue('FinancialStuff.Salary', 50000) - ->execute(); + $this->createTestRecord('Contact', [ + 'first_name' => 'Jerome', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + "$group.$colorField" => 'r', + "$group.$foodField" => '1', + 'FinancialStuff.Salary' => 50000, + ]); $result = Contact::get(FALSE) ->addSelect('first_name') @@ -136,23 +136,23 @@ public function testWithCustomDataForMultipleContacts() { ->addValue('data_type', 'Money') ->execute(); - Contact::create(FALSE) - ->addValue('first_name', 'Red') - ->addValue('last_name', 'Corn') - ->addValue('contact_type', 'Individual') - ->addValue("$group.$colorField", 'r') - ->addValue("$group.$foodField", '1') - ->addValue('FinancialStuff.Salary', 10000) - ->execute(); - - Contact::create(FALSE) - ->addValue('first_name', 'Blue') - ->addValue('last_name', 'Cheese') - ->addValue('contact_type', 'Individual') - ->addValue("$group.$colorField", 'b') - ->addValue("$group.$foodField", '3') - ->addValue('FinancialStuff.Salary', 500000) - ->execute(); + $this->createTestRecord('Contact', [ + 'first_name' => 'Red', + 'last_name' => 'Corn', + 'contact_type' => 'Individual', + "$group.$colorField" => 'r', + "$group.$foodField" => '1', + 'FinancialStuff.Salary' => 10000, + ]); + + $this->createTestRecord('Contact', [ + 'first_name' => 'Blue', + 'last_name' => 'Cheese', + 'contact_type' => 'Individual', + "$group.$colorField" => 'b', + "$group.$foodField" => '3', + 'FinancialStuff.Salary' => 500000, + ]); $result = Contact::get(FALSE) ->addSelect('first_name') diff --git a/tests/phpunit/api/v4/Custom/CustomContactRefTest.php b/tests/phpunit/api/v4/Custom/CustomContactRefTest.php index 4b42a84c004d..1e9473ee79a6 100644 --- a/tests/phpunit/api/v4/Custom/CustomContactRefTest.php +++ b/tests/phpunit/api/v4/Custom/CustomContactRefTest.php @@ -52,43 +52,38 @@ public function testGetWithJoin() { ->addValue('serialize', 1) ->execute(); - $favPersonId = Contact::create(FALSE) - ->addValue('first_name', $firstName) - ->addValue('last_name', 'Person') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; - - $favPeopleId1 = Contact::create(FALSE) - ->addValue('first_name', 'FirstFav') - ->addValue('last_name', 'People1') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; - - $favPeopleId2 = Contact::create(FALSE) - ->addValue('first_name', 'SecondFav') - ->addValue('last_name', 'People2') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; - - $contactId1 = Contact::create(FALSE) - ->addValue('first_name', 'Mya') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactRef.FavPerson', $favPersonId) - ->addValue('MyContactRef.FavPeople', [$favPeopleId2, $favPeopleId1]) - ->execute() - ->first()['id']; - - $contactId2 = Contact::create(FALSE) - ->addValue('first_name', 'Bea') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactRef.FavPeople', [$favPeopleId2]) - ->execute() - ->first()['id']; + $favPersonId = $this->createTestRecord('Contact', [ + 'first_name' => $firstName, + 'last_name' => 'Person', + 'contact_type' => 'Individual', + ])['id']; + + $favPeopleId1 = $this->createTestRecord('Contact', [ + 'first_name' => 'FirstFav', + 'last_name' => 'People1', + 'contact_type' => 'Individual', + ])['id']; + + $favPeopleId2 = $this->createTestRecord('Contact', [ + 'first_name' => 'SecondFav', + 'last_name' => 'People2', + 'contact_type' => 'Individual', + ])['id']; + + $contactId1 = $this->createTestRecord('Contact', [ + 'first_name' => 'Mya', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactRef.FavPerson' => $favPersonId, + 'MyContactRef.FavPeople' => [$favPeopleId2, $favPeopleId1], + ])['id']; + + $contactId2 = $this->createTestRecord('Contact', [ + 'first_name' => 'Bea', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactRef.FavPeople' => [$favPeopleId2], + ])['id']; $result = Contact::get(FALSE) ->addSelect('display_name') @@ -139,13 +134,12 @@ public function testCurrentUser() { ->addValue('data_type', 'ContactReference') ->execute(); - $contactId = Contact::create(FALSE) - ->addValue('first_name', 'Mya') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactRef.FavPerson', 'user_contact_id') - ->execute() - ->first()['id']; + $contactId = $this->createTestRecord('Contact', [ + 'first_name' => 'Mya', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactRef.FavPerson' => 'user_contact_id', + ])['id']; $contact = Contact::get(FALSE) ->addSelect('display_name') diff --git a/tests/phpunit/api/v4/Custom/CustomFieldAlterTest.php b/tests/phpunit/api/v4/Custom/CustomFieldAlterTest.php index 7d681b3b406a..96da07110a28 100644 --- a/tests/phpunit/api/v4/Custom/CustomFieldAlterTest.php +++ b/tests/phpunit/api/v4/Custom/CustomFieldAlterTest.php @@ -68,13 +68,16 @@ public function testChangeSerialize() { ->execute() ->first(); - Activity::save(FALSE) - ->setDefaults(['activity_type_id' => 1, 'source_contact_id' => $contact['id']]) - ->addRecord(['subject' => 'A1', 'MyFieldsToAlter.TestText' => 'A1', 'MyFieldsToAlter.TestOptions' => '1']) - ->addRecord(['subject' => 'A2', 'MyFieldsToAlter.TestText' => 'A2', 'MyFieldsToAlter.TestOptions' => '2']) - ->addRecord(['subject' => 'A3', 'MyFieldsToAlter.TestText' => 'A3', 'MyFieldsToAlter.TestOptions' => '']) - ->addRecord(['subject' => 'A4', 'MyFieldsToAlter.TestText' => 'A4', 'MyFieldsToAlter.TestCountry' => [1228, 1039]]) - ->execute(); + $sampeData = [ + ['subject' => 'A1', 'MyFieldsToAlter.TestText' => 'A1', 'MyFieldsToAlter.TestOptions' => '1'], + ['subject' => 'A2', 'MyFieldsToAlter.TestText' => 'A2', 'MyFieldsToAlter.TestOptions' => '2'], + ['subject' => 'A3', 'MyFieldsToAlter.TestText' => 'A3', 'MyFieldsToAlter.TestOptions' => ''], + ['subject' => 'A4', 'MyFieldsToAlter.TestText' => 'A4', 'MyFieldsToAlter.TestCountry' => [1228, 1039]], + ]; + $this->saveTestRecords('Activity', [ + 'defaults' => ['activity_type_id' => 1, 'source_contact_id' => $contact['id']], + 'records' => $sampeData, + ]); $result = Activity::get(FALSE) ->addWhere('MyFieldsToAlter.TestText', 'IS NOT NULL') diff --git a/tests/phpunit/api/v4/Custom/CustomValuePerformanceTest.php b/tests/phpunit/api/v4/Custom/CustomValuePerformanceTest.php index 18420db03805..5d0a47348a68 100644 --- a/tests/phpunit/api/v4/Custom/CustomValuePerformanceTest.php +++ b/tests/phpunit/api/v4/Custom/CustomValuePerformanceTest.php @@ -73,15 +73,15 @@ public function testQueryCount() { $this->beginQueryCount(); - Contact::create(FALSE) - ->addValue('first_name', 'Red') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactFields.FavColor', 'r') - ->addValue('MyContactFields.FavAnimal', 'Sheep') - ->addValue('MyContactFields.FavLetter', 'z') - ->addValue('MyContactFields.FavFood', 'Coconuts') - ->execute(); + $this->createTestRecord('Contact', [ + 'first_name' => 'Red', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactFields.FavColor' => 'r', + 'MyContactFields.FavAnimal' => 'Sheep', + 'MyContactFields.FavLetter' => 'z', + 'MyContactFields.FavFood' => 'Coconuts', + ]); Contact::get(FALSE) ->addSelect('display_name') @@ -100,6 +100,8 @@ public function testQueryCount() { // FIXME: This count is artificially high due to the line // $this->entity = Tables::getBriefName(Tables::getClassForTable($targetTable)); // In class Joinable. TODO: Investigate why. + // $this->assertLessThan(10, $this->getQueryCount()); + } } diff --git a/tests/phpunit/api/v4/Custom/CustomValueTest.php b/tests/phpunit/api/v4/Custom/CustomValueTest.php index a0931cce9438..dd632c2f6f0d 100644 --- a/tests/phpunit/api/v4/Custom/CustomValueTest.php +++ b/tests/phpunit/api/v4/Custom/CustomValueTest.php @@ -22,7 +22,6 @@ use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; use Civi\Api4\CustomValue; -use Civi\Api4\Contact; use Civi\Api4\Entity; /** @@ -73,12 +72,11 @@ public function testCRUD() { ->addValue('data_type', 'String') ->execute()->first(); - $this->contactID = Contact::create(FALSE) - ->addValue('first_name', 'Johann') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; + $this->contactID = $this->createTestRecord('Contact', [ + 'first_name' => 'Johann', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + ])['id']; // Ensure virtual api entity has been created $entity = Entity::get(FALSE) @@ -220,12 +218,11 @@ public function testCRUD() { // CASE 3: Test CustomValue::replace // create a second contact which will be used to replace the custom values, created earlier - $secondContactID = Contact::create(FALSE) - ->addValue('first_name', 'Adam') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->execute() - ->first()['id']; + $secondContactID = $this->createTestRecord('Contact', [ + 'first_name' => 'Adam', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + ])['id']; // Replace all the records which was created earlier with entity_id = first contact // with custom record [$colorField => 'g', 'entity_id' => $secondContactID] CustomValue::replace($group) diff --git a/tests/phpunit/api/v4/Custom/ExtendFromIndividualTest.php b/tests/phpunit/api/v4/Custom/ExtendFromIndividualTest.php index 2f7440cce6c9..61ccb13e24be 100644 --- a/tests/phpunit/api/v4/Custom/ExtendFromIndividualTest.php +++ b/tests/phpunit/api/v4/Custom/ExtendFromIndividualTest.php @@ -44,13 +44,12 @@ public function testGetWithNonStandardExtends() { ->addValue('data_type', 'String') ->execute(); - $contactId = Contact::create(FALSE) - ->addValue('first_name', 'Johann') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactFields.FavColor', 'Red') - ->execute() - ->first()['id']; + $contactId = $this->createTestRecord('Contact', [ + 'first_name' => 'Johann', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactFields.FavColor' => 'Red', + ])['id']; $contact = Contact::get(FALSE) ->addSelect('display_name') diff --git a/tests/phpunit/api/v4/Custom/PseudoconstantTest.php b/tests/phpunit/api/v4/Custom/PseudoconstantTest.php index 358d8192b3b0..bf3a90bb641b 100644 --- a/tests/phpunit/api/v4/Custom/PseudoconstantTest.php +++ b/tests/phpunit/api/v4/Custom/PseudoconstantTest.php @@ -20,7 +20,6 @@ namespace api\v4\Custom; use Civi\Api4\Address; -use Civi\Api4\Campaign; use Civi\Api4\Contact; use Civi\Api4\Activity; use Civi\Api4\Contribution; @@ -28,9 +27,7 @@ use Civi\Api4\CustomGroup; use Civi\Api4\Email; use Civi\Api4\EntityTag; -use Civi\Api4\OptionValue; use Civi\Api4\Participant; -use Civi\Api4\Tag; /** * @group headless @@ -38,12 +35,12 @@ class PseudoconstantTest extends CustomTestBase { public function testOptionValue() { - $cid = Contact::create(FALSE)->addValue('first_name', 'bill')->execute()->first()['id']; + $cid = $this->createTestRecord('Contact', ['first_name', 'bill'])['id']; $subject = uniqid('subject'); - OptionValue::create() - ->addValue('option_group_id:name', 'activity_type') - ->addValue('label', 'Fake Type') - ->execute(); + $this->createTestRecord('OptionValue', [ + 'option_group_id:name' => 'activity_type', + 'label' => 'Fake Type', + ]); $options = Activity::getFields() ->addWhere('name', '=', 'activity_type_id') @@ -52,17 +49,17 @@ public function testOptionValue() { $options = array_column($options, NULL, 'name'); $this->assertEquals('Fake Type', $options['Fake_Type']['label']); - Activity::create() - ->addValue('activity_type_id:name', 'Meeting') - ->addValue('source_contact_id', $cid) - ->addValue('subject', $subject) - ->execute(); + $this->createTestRecord('Activity', [ + 'activity_type_id:name' => 'Meeting', + 'source_contact_id' => $cid, + 'subject' => $subject, + ]); - Activity::create() - ->addValue('activity_type_id:name', 'Fake_Type') - ->addValue('source_contact_id', $cid) - ->addValue('subject', $subject) - ->execute(); + $this->createTestRecord('Activity', [ + 'activity_type_id:name' => 'Fake_Type', + 'source_contact_id' => $cid, + 'subject' => $subject, + ]); $act = Activity::get() ->addWhere('activity_type_id:label', '=', 'Fake Type') @@ -101,27 +98,28 @@ public function testOptionValue() { } public function testAddressOptions() { - $cid = Contact::create(FALSE)->addValue('first_name', 'addr')->execute()->first()['id']; - Address::save() - ->addRecord([ + $cid = $this->createTestRecord('Contact', ['first_name', 'addr'])['id']; + $addressData = [ + [ 'contact_id' => $cid, 'state_province_id:abbr' => 'CA', 'country_id:label' => 'United States', 'street_address' => '1', - ]) - ->addRecord([ + ], + [ 'contact_id' => $cid, 'state_province_id:abbr' => 'CA', 'country_id:label' => 'Uruguay', 'street_address' => '2', - ]) - ->addRecord([ + ], + [ 'contact_id' => $cid, 'state_province_id:abbr' => 'CA', 'country_id:abbr' => 'ES', 'street_address' => '3', - ]) - ->execute(); + ], + ]; + $this->saveTestRecords('Address', ['records' => $addressData]); $addr = Address::get() ->addWhere('contact_id', '=', $cid) @@ -181,10 +179,10 @@ public function testCustomOptions() { } } - $cid = Contact::create(FALSE) - ->addValue('first_name', 'col') - ->addValue('myPseudoconstantTest.Color:label', 'blü') - ->execute()->first()['id']; + $cid = $this->createTestRecord('Contact', [ + 'first_name' => 'col', + 'myPseudoconstantTest.Color:label' => 'blü', + ])['id']; $result = Contact::get(FALSE) ->addWhere('id', '=', $cid) @@ -195,14 +193,14 @@ public function testCustomOptions() { $this->assertEquals('bl_', $result['myPseudoconstantTest.Color:name']); $this->assertEquals('b', $result['myPseudoconstantTest.Color']); - $cid1 = Contact::create(FALSE) - ->addValue('first_name', 'two') - ->addValue('myPseudoconstantTest.Technicolor:label', 'RED') - ->execute()->first()['id']; - $cid2 = Contact::create(FALSE) - ->addValue('first_name', 'two') - ->addValue('myPseudoconstantTest.Technicolor:label', 'GREEN') - ->execute()->first()['id']; + $cid1 = $this->createTestRecord('Contact', [ + 'first_name' => 'two', + 'myPseudoconstantTest.Technicolor:label' => 'RED', + ])['id']; + $cid2 = $this->createTestRecord('Contact', [ + 'first_name' => 'two', + 'myPseudoconstantTest.Technicolor:label' => 'GREEN', + ])['id']; // Test ordering by label $result = Contact::get(FALSE) @@ -220,20 +218,22 @@ public function testCustomOptions() { } public function testJoinOptions() { - $cid1 = Contact::create(FALSE) - ->addValue('first_name', 'Tom') - ->addValue('gender_id:label', 'Male') - ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'tom@example.com', 'location_type_id:name' => 'Work'])) - ->execute()->first()['id']; - $cid2 = Contact::create(FALSE) - ->addValue('first_name', 'Sue') - ->addValue('gender_id:name', 'Female') - ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'sue@example.com', 'location_type_id:name' => 'Home'])) - ->execute()->first()['id']; - $cid3 = Contact::create(FALSE) - ->addValue('first_name', 'Pat') - ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'pat@example.com', 'location_type_id:name' => 'Home'])) - ->execute()->first()['id']; + $cid1 = $this->createTestRecord('Contact', [ + 'first_name' => 'Tom', + 'gender_id:label' => 'Male', + ])['id']; + $this->createTestRecord('Email', ['contact_id' => $cid1, 'email' => 'tom@example.com', 'location_type_id:name' => 'Work']); + + $cid2 = $this->createTestRecord('Contact', [ + 'first_name' => 'Sue', + 'gender_id:name' => 'Female', + ])['id']; + $this->createTestRecord('Email', ['contact_id' => $cid2, 'email' => 'sue@example.com', 'location_type_id:name' => 'Home']); + + $cid3 = $this->createTestRecord('Contact', [ + 'first_name' => 'Pat', + ])['id']; + $this->createTestRecord('Email', ['contact_id' => $cid3, 'email' => 'pat@example.com', 'location_type_id:name' => 'Home']); $emails = Email::get() ->addSelect('location_type_id:name', 'contact_id.gender_id:label', 'email', 'contact_id') @@ -260,11 +260,11 @@ public function testJoinOptions() { public function testTagOptions() { $tag = uniqid('tag'); - Tag::create(FALSE) - ->addValue('name', $tag) - ->addValue('description', 'colorful') - ->addValue('color', '#aabbcc') - ->execute(); + $this->createTestRecord('Tag', [ + 'name' => $tag, + 'description' => 'colorful', + 'color' => '#aabbcc', + ]); $options = EntityTag::getFields() ->setLoadOptions(['id', 'name', 'color', 'description', 'label']) ->addWhere('name', '=', 'tag_id') @@ -308,17 +308,17 @@ public function testPreloadFalse() { $campaignTitle = uniqid('Test '); - $campaignId = Campaign::create(FALSE) - ->addValue('title', $campaignTitle) - ->addValue('campaign_type_id', 1) - ->execute()->first()['id']; + $campaignId = $this->createTestRecord('Campaign', [ + 'title' => $campaignTitle, + 'campaign_type_id' => 1, + ])['id']; - $contributionId = Contribution::create(FALSE) - ->addValue('campaign_id', $campaignId) - ->addValue('contact_id', $contact['id']) - ->addValue('financial_type_id', 1) - ->addValue('total_amount', .01) - ->execute()->first()['id']; + $contributionId = $this->createTestRecord('Contribution', [ + 'campaign_id' => $campaignId, + 'contact_id' => $contact['id'], + 'financial_type_id' => 1, + 'total_amount' => .01, + ])['id']; // Even though the option list of campaigns is not available (prefetch = false) // We should still be able to get the title of the campaign as :label diff --git a/tests/phpunit/api/v4/Custom/UpdateCustomValueTest.php b/tests/phpunit/api/v4/Custom/UpdateCustomValueTest.php index 34c5b3dcdc7e..84208ff92ef6 100644 --- a/tests/phpunit/api/v4/Custom/UpdateCustomValueTest.php +++ b/tests/phpunit/api/v4/Custom/UpdateCustomValueTest.php @@ -44,13 +44,12 @@ public function testGetWithCustomData() { ->addValue('data_type', 'String') ->execute(); - $contactId = Contact::create(FALSE) - ->addValue('first_name', 'Red') - ->addValue('last_name', 'Tester') - ->addValue('contact_type', 'Individual') - ->addValue('MyContactFields.FavColor', 'Red') - ->execute() - ->first()['id']; + $contactId = $this->createTestRecord('Contact', [ + 'first_name' => 'Red', + 'last_name' => 'Tester', + 'contact_type' => 'Individual', + 'MyContactFields.FavColor' => 'Red', + ])['id']; Contact::update(FALSE) ->addWhere('id', '=', $contactId) diff --git a/tests/phpunit/api/v4/Entity/EntityTest.php b/tests/phpunit/api/v4/Entity/EntityTest.php index fa93ddbeba16..1bd6f6e91611 100644 --- a/tests/phpunit/api/v4/Entity/EntityTest.php +++ b/tests/phpunit/api/v4/Entity/EntityTest.php @@ -22,12 +22,11 @@ use Civi\API\Exception\NotImplementedException; use Civi\Api4\Entity; use api\v4\Api4TestBase; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class EntityTest extends Api4TestBase implements TransactionalInterface { +class EntityTest extends Api4TestBase { public function testEntityGet() { \CRM_Core_BAO_ConfigSetting::enableComponent('CiviEvent'); diff --git a/tests/phpunit/api/v4/Entity/ExampleDataTest.php b/tests/phpunit/api/v4/Entity/ExampleDataTest.php index 682a25352b02..82839674cbef 100644 --- a/tests/phpunit/api/v4/Entity/ExampleDataTest.php +++ b/tests/phpunit/api/v4/Entity/ExampleDataTest.php @@ -20,12 +20,11 @@ namespace api\v4\Entity; use api\v4\Api4TestBase; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class ExampleDataTest extends Api4TestBase implements TransactionalInterface { +class ExampleDataTest extends Api4TestBase { /** * Basic canary test fetching a specific example. diff --git a/tests/phpunit/api/v4/Service/Schema/SchemaMapperTest.php b/tests/phpunit/api/v4/Service/Schema/SchemaMapperTest.php index 84406ebe2d0c..46851ad20e4b 100644 --- a/tests/phpunit/api/v4/Service/Schema/SchemaMapperTest.php +++ b/tests/phpunit/api/v4/Service/Schema/SchemaMapperTest.php @@ -23,12 +23,11 @@ use Civi\Api4\Service\Schema\SchemaMap; use Civi\Api4\Service\Schema\Table; use api\v4\Api4TestBase; -use Civi\Test\TransactionalInterface; /** * @group headless */ -class SchemaMapperTest extends Api4TestBase implements TransactionalInterface { +class SchemaMapperTest extends Api4TestBase { public function testWillHaveNoPathWithNoTables() { $map = new SchemaMap(); diff --git a/tests/phpunit/api/v4/Spec/SpecGathererTest.php b/tests/phpunit/api/v4/Spec/SpecGathererTest.php index 9544b081c09b..5d06562fd37f 100644 --- a/tests/phpunit/api/v4/Spec/SpecGathererTest.php +++ b/tests/phpunit/api/v4/Spec/SpecGathererTest.php @@ -24,10 +24,9 @@ use Civi\Api4\Service\Spec\SpecGatherer; use api\v4\Traits\OptionCleanupTrait; use api\v4\Api4TestBase; -use Civi\Api4\CustomField; -use Civi\Api4\CustomGroup; use api\v4\Traits\TableDropperTrait; use Prophecy\Argument; +use function Civi\Api4\Utils\CoreUtil; /** * @group headless @@ -37,21 +36,10 @@ class SpecGathererTest extends Api4TestBase { use TableDropperTrait; use OptionCleanupTrait; - public function setUpHeadless() { - $this->dropByPrefix('civicrm_value_favorite'); - $this->cleanup([ - 'tablesToTruncate' => [ - 'civicrm_custom_group', - 'civicrm_custom_field', - ], - ]); - return parent::setUpHeadless(); - } - public function testBasicFieldsGathering() { $gatherer = new SpecGatherer(); $specs = $gatherer->getSpec('Contact', 'get', FALSE); - $contactDAO = _civicrm_api3_get_DAO('Contact'); + $contactDAO = CoreUtil::getBAOFromApiName('Contact'); $contactFields = $contactDAO::fields(); $specFieldNames = $specs->getFieldNames(); $contactFieldNames = array_column($contactFields, 'name'); @@ -77,36 +65,4 @@ public function testWithSpecProvider() { $this->assertContains('foo', $fieldNames); } - public function testPseudoConstantOptionsWillBeAdded() { - $customGroupId = CustomGroup::create(FALSE) - ->addValue('title', 'FavoriteThings') - ->addValue('extends', 'Contact') - ->execute() - ->first()['id']; - - $options = ['r' => 'Red', 'g' => 'Green', 'p' => 'Pink']; - - CustomField::save(FALSE) - ->addRecord([ - 'label' => 'FavColor', - 'custom_group_id' => $customGroupId, - 'option_values' => $options, - 'html_type' => 'Select', - 'data_type' => 'String', - ]) - ->execute(); - - $gatherer = new SpecGatherer(); - $spec = $gatherer->getSpec('Contact', 'get', TRUE); - - $regularField = $spec->getFieldByName('contact_type'); - $this->assertNotEmpty($regularField->getOptions()); - $this->assertContains('Individual', array_column($regularField->getOptions([], ['id', 'label']), 'label', 'id')); - - $customField = $spec->getFieldByName('FavoriteThings.FavColor'); - $options = array_column($customField->getOptions([], ['id', 'name', 'label']), NULL, 'id'); - $this->assertEquals('Green', $options['g']['name']); - $this->assertEquals('Pink', $options['p']['label']); - } - }