Skip to content

Commit

Permalink
Merge pull request #31761 from colemanw/afformTestCleanup
Browse files Browse the repository at this point in the history
Afform - Test cleanup
  • Loading branch information
colemanw authored Jan 12, 2025
2 parents b1f652f + 1dd3d9e commit 726d32a
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 108 deletions.
11 changes: 11 additions & 0 deletions Civi/Test/Api4TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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__);

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOHTML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Civi\Api4\Contact;
use Civi\Api4\Relationship;
use Civi\Api4\RelationshipType;
use Civi\Test\TransactionalInterface;

/**
* Test case for Afform.prefill and Afform.submit.
*
* @group headless
*/
class AfformRelationshipUsageTest extends AfformUsageTestCase {
class AfformRelationshipUsageTest extends AfformUsageTestCase implements TransactionalInterface {

/**
* Tests creating a relationship between multiple contacts
Expand Down
14 changes: 8 additions & 6 deletions ext/afform/mock/tests/phpunit/api/v4/Afform/AfformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

use Civi\Api4\Afform;
use Civi\Api4\Dashboard;
use Civi\Test\TransactionalInterface;

/**
* Afform.Get API Test Case
* This is a generic test class implemented with PHPUnit.
* @group headless
*/
class AfformTest extends AfformTestCase {
use \Civi\Test\Api3TestTrait;
use \Civi\Test\ContactTestTrait;
class AfformTest extends AfformTestCase implements TransactionalInterface {

/**
* DOMDocument outputs some tags a little different than they were input.
Expand All @@ -33,12 +32,14 @@ private function fudgeMarkup($markup) {
}

public function getBasicDirectives() {
return [
$directives = [
['mockPage', ['title' => '', '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);
}

/**
Expand Down Expand Up @@ -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];
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
Loading

0 comments on commit 726d32a

Please sign in to comment.