Skip to content

Commit

Permalink
Merge pull request #27803 from colemanw/afformTest
Browse files Browse the repository at this point in the history
[NFC] Afform - Test cleanup
  • Loading branch information
aydun authored Oct 13, 2023
2 parents c324e6c + 69939ea commit 69d12f1
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use Civi\Api4\Afform;
use Civi\Api4\Contact;
use Civi\Api4\CustomField;
use Civi\Api4\CustomGroup;
use Civi\Api4\Group;
use Civi\Api4\GroupContact;
use Civi\Api4\SavedSearch;

/**
* Test case for Afform with autocomplete.
Expand Down Expand Up @@ -32,7 +37,7 @@ public function testAutocompleteWithSavedSearchFilter(): void {
]);

// Saved search for filtering
\Civi\Api4\SavedSearch::create(FALSE)
SavedSearch::create(FALSE)
->setValues([
'name' => 'the_unit_test_search',
'label' => 'the_unit_test_search',
Expand Down Expand Up @@ -84,7 +89,7 @@ public function testAutocompleteWithSavedSearchFilter(): void {
],
];
try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand All @@ -96,7 +101,7 @@ public function testAutocompleteWithSavedSearchFilter(): void {

// Submit with a valid ID, it should work
$values['Individual1'][0]['fields']['id'] = $contacts['B'];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -126,17 +131,17 @@ public function testCustomContactRefFieldWithGroupsFilter(): void {
->execute()->indexBy('first_name')->column('id');

// Place contacts A & B in the group, but not contact C
$group = \Civi\Api4\Group::create(FALSE)
$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();

\Civi\Api4\CustomGroup::create(FALSE)
CustomGroup::create(FALSE)
->addValue('title', 'test_af_fields')
->addValue('extends', 'Contact')
->addChain('fields', \Civi\Api4\CustomField::save()
->addChain('fields', CustomField::save()
->addDefault('custom_group_id', '$id')
->setRecords([
['label' => 'contact_ref', 'data_type' => 'ContactReference', 'html_type' => 'Autocomplete', 'filter' => 'action=get&group=' . $group['id']],
Expand Down Expand Up @@ -184,7 +189,7 @@ public function testCustomContactRefFieldWithGroupsFilter(): void {
],
];
try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand All @@ -196,7 +201,7 @@ public function testCustomContactRefFieldWithGroupsFilter(): void {

// Submit with a valid ID, it should work
$values['Individual1'][0]['fields']['test_af_fields.contact_ref'] = $contacts['B'];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -224,10 +229,10 @@ public function testCustomContactRefFieldOnJoinEntity(): void {
->setRecords($sampleData)
->execute()->indexBy('first_name')->column('id');

\Civi\Api4\CustomGroup::create(FALSE)
CustomGroup::create(FALSE)
->addValue('title', 'test_address_fields')
->addValue('extends', 'Address')
->addChain('fields', \Civi\Api4\CustomField::save()
->addChain('fields', CustomField::save()
->addDefault('custom_group_id', '$id')
->setRecords([
['label' => 'contact_ref', 'data_type' => 'ContactReference', 'html_type' => 'Autocomplete', 'filter' => 'action=get&source=in'],
Expand Down Expand Up @@ -282,7 +287,7 @@ public function testCustomContactRefFieldOnJoinEntity(): void {
],
];
try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand All @@ -294,7 +299,7 @@ public function testCustomContactRefFieldOnJoinEntity(): void {

// Submit with a valid ID, it should work
$values['Individual1'][0]['joins']['Address'][0]['test_address_fields.contact_ref'] = $contacts['A'];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down
44 changes: 22 additions & 22 deletions ext/afform/mock/tests/phpunit/api/v4/AfformContactUsageTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
require_once __DIR__ . '/AfformTestCase.php';
require_once __DIR__ . '/AfformUsageTestCase.php';

use Civi\Api4\Afform;

/**
* Test case for Afform.prefill and Afform.submit.
Expand Down Expand Up @@ -87,7 +87,7 @@ public function testAboutMeAllowed(): void {
CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();

// Autofill form with current user. See `Civi\Afform\Behavior\ContactAutofill`
$prefill = Civi\Api4\Afform::prefill()
$prefill = Afform::prefill()
->setName($this->formName)
->execute()
->indexBy('name');
Expand All @@ -98,7 +98,7 @@ public function testAboutMeAllowed(): void {
['fields' => ['first_name' => 'Firsty', 'last_name' => 'Lasty']],
];

Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues(['me' => $submission])
->execute();
Expand All @@ -115,7 +115,7 @@ public function testChainSelect(): void {
]);

// Get states for USA
$result = Civi\Api4\Afform::getOptions()
$result = Afform::getOptions()
->setName($this->formName)
->setModelName('me')
->setFieldName('state_province_id')
Expand All @@ -125,7 +125,7 @@ public function testChainSelect(): void {
$this->assertEquals('Alabama', $result[0]['label']);

// Get states for UK
$result = Civi\Api4\Afform::getOptions()
$result = Afform::getOptions()
->setName($this->formName)
->setModelName('me')
->setFieldName('state_province_id')
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testCheckEntityReferenceFieldsReplacement(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -208,7 +208,7 @@ public function testAboutMeForbidden(): void {
CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();

try {
Civi\Api4\Afform::prefill()
Afform::prefill()
->setName($this->formName)
->setArgs([])
->execute()
Expand All @@ -220,7 +220,7 @@ public function testAboutMeForbidden(): void {
}

try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setArgs([])
->setValues([
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testEmployerReference(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -301,7 +301,7 @@ public function testEmptyEmployerReference(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -347,7 +347,7 @@ public function testCreatingContactsWithOnlyEmail(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -415,7 +415,7 @@ public function testDedupeIndividual(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -448,7 +448,7 @@ public function testFormValidationEntityFields(): void {
];

try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -484,7 +484,7 @@ public function testFormValidationEntityJoinFields(): void {
];

try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
Expand Down Expand Up @@ -514,26 +514,26 @@ public function testSubmissionLimit() {
];

// Submit twice
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues(['me' => $submitValues])
->execute();
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues(['me' => $submitValues])
->execute();

// Autofilling form works because limit hasn't been reached
Civi\Api4\Afform::prefill()->setName($this->formName)->execute();
Afform::prefill()->setName($this->formName)->execute();

// Last time
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues(['me' => $submitValues])
->execute();

// Stats should report that we've reached the submission limit
$stats = \Civi\Api4\Afform::get(FALSE)
$stats = Afform::get(FALSE)
->addSelect('submit_enabled', 'submission_count', 'submit_currently_open')
->addWhere('name', '=', $this->formName)
->execute()->single();
Expand All @@ -543,13 +543,13 @@ public function testSubmissionLimit() {

// Prefilling and submitting are no longer allowed.
try {
Civi\Api4\Afform::prefill()->setName($this->formName)->execute();
Afform::prefill()->setName($this->formName)->execute();
$this->fail();
}
catch (\Civi\API\Exception\UnauthorizedException $e) {
}
try {
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues(['me' => $submitValues])
->execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

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 Down Expand Up @@ -31,14 +36,14 @@ public static function setUpBeforeClass(): void {
* which can be submitted multiple times
*/
public function testMultiRecordCustomBlock(): void {
\Civi\Api4\CustomGroup::create(FALSE)
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', \Civi\Api4\CustomField::save()
->addChain('fields', CustomField::save()
->addDefault('custom_group_id', '$id')
->setRecords([
['name' => 'my_text', 'label' => 'My Text', 'data_type' => 'String', 'html_type' => 'Text'],
Expand All @@ -48,7 +53,7 @@ public function testMultiRecordCustomBlock(): void {
->execute();

// Creating a custom group should automatically create an afform block
$block = \Civi\Api4\Afform::get()
$block = Afform::get()
->addWhere('name', '=', 'afblockCustom_MyThings')
->addSelect('layout', 'directive_name')
->setLayoutFormat('shallow')
Expand Down Expand Up @@ -83,11 +88,11 @@ public function testMultiRecordCustomBlock(): void {
],
],
];
Civi\Api4\Afform::submit()
Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
$contact = \Civi\Api4\Contact::get(FALSE)
$contact = Contact::get(FALSE)
->addWhere('first_name', '=', $firstName)
->addJoin('Custom_MyThings AS Custom_MyThings', 'LEFT', ['id', '=', 'Custom_MyThings.entity_id'])
->addSelect('Custom_MyThings.my_text', 'Custom_MyThings.my_friend')
Expand Down
Loading

0 comments on commit 69d12f1

Please sign in to comment.