Skip to content

Commit

Permalink
APIv4 Unit test fixes - ensure test records are cleaned up in non-tra…
Browse files Browse the repository at this point in the history
…nsaction tests

Use the new `createTestRecord` and `saveTestRecords` in non-transaction test classes
so the created records are automatically cleaned-up.
  • Loading branch information
colemanw committed May 11, 2022
1 parent 5376bfc commit c77cad5
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 342 deletions.
3 changes: 1 addition & 2 deletions tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/api/v4/Action/GetExtraFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/api/v4/Action/GetFromArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/api/v4/Action/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions tests/phpunit/api/v4/Api4TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
}
}
}

Expand Down Expand Up @@ -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();
}

/**
Expand Down
175 changes: 77 additions & 98 deletions tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,14 @@
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;

/**
* @group headless
*/
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
*/
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand All @@ -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'])
Expand Down
50 changes: 25 additions & 25 deletions tests/phpunit/api/v4/Custom/CreateWithOptionGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
Loading

0 comments on commit c77cad5

Please sign in to comment.