<?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.
]);
// Saved search for filtering
- \Civi\Api4\SavedSearch::create(FALSE)
+ SavedSearch::create(FALSE)
->setValues([
'name' => 'the_unit_test_search',
'label' => 'the_unit_test_search',
],
];
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
// 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();
->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']],
],
];
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
// 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();
->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'],
],
];
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
// 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();
<?php
-require_once __DIR__ . '/AfformTestCase.php';
-require_once __DIR__ . '/AfformUsageTestCase.php';
+
+use Civi\Api4\Afform;
/**
* Test case for Afform.prefill and Afform.submit.
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');
['fields' => ['first_name' => 'Firsty', 'last_name' => 'Lasty']],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues(['me' => $submission])
->execute();
]);
// Get states for USA
- $result = Civi\Api4\Afform::getOptions()
+ $result = Afform::getOptions()
->setName($this->formName)
->setModelName('me')
->setFieldName('state_province_id')
$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')
],
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
try {
- Civi\Api4\Afform::prefill()
+ Afform::prefill()
->setName($this->formName)
->setArgs([])
->execute()
}
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setArgs([])
->setValues([
],
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
],
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
],
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
],
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
];
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
];
try {
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($values)
->execute();
];
// 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();
// 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();
<?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.
*
* 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'],
->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')
],
],
];
- 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')
*
* @group headless
*/
+
+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 api_v4_AfformFileUploadTest extends api_v4_AfformUsageTestCase {
*/
public function testSubmitFile(): void {
// Single-value set
- \Civi\Api4\CustomGroup::create(FALSE)
+ CustomGroup::create(FALSE)
->addValue('name', 'MyInfo')
->addValue('title', 'My Info')
->addValue('extends', 'Contact')
- ->addChain('fields', \Civi\Api4\CustomField::save()
+ ->addChain('fields', CustomField::save()
->addDefault('custom_group_id', '$id')
->setRecords([
['name' => 'single_file_field', 'label' => 'A File', 'data_type' => 'File', 'html_type' => 'File'],
->execute();
// Multi-record set
- \Civi\Api4\CustomGroup::create(FALSE)
+ 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', \Civi\Api4\CustomField::save()
+ ->addChain('fields', CustomField::save()
->addDefault('custom_group_id', '$id')
->setRecords([
['name' => 'my_file', 'label' => 'My File', 'data_type' => 'File', 'html_type' => 'File'],
],
],
];
- $submission = Civi\Api4\Afform::submit()
+ $submission = Afform::submit()
->setName($this->formName)
->setValues($values)
->execute()->first();
foreach ([0, 1] as $entityIndex) {
$this->mockUploadFile();
- Civi\Api4\Afform::submitFile()
+ Afform::submitFile()
->setName($this->formName)
->setToken($submission['token'])
->setModelName('Individual1')
foreach ([0, 1] as $joinIndex) {
$this->mockUploadFile();
- Civi\Api4\Afform::submitFile()
+ Afform::submitFile()
->setName($this->formName)
->setToken($submission['token'])
->setModelName('Individual1')
}
}
- $contacts = \Civi\Api4\Contact::get(FALSE)
+ $contacts = Contact::get(FALSE)
->addWhere('last_name', '=', $lastName)
->addJoin('Custom_MyFiles AS MyFiles', 'LEFT', ['id', '=', 'MyFiles.entity_id'])
->addSelect('first_name', 'MyInfo.single_file_field', 'MyFiles.my_file')
<?php
+use Civi\Api4\Afform;
+
/**
* Test case for Afform with autocomplete.
*
],
]);
- $prefill = Civi\Api4\Afform::prefill()
+ $prefill = Afform::prefill()
->setName($this->formName)
->setArgs(['Individual1' => $cid])
->execute()
$this->assertEmpty($prefill['Individual1']['values'][1]['joins']['Phone']);
// Prefill a specific contact for the af-repeat entity
- $prefill = Civi\Api4\Afform::prefill()
+ $prefill = Afform::prefill()
->setName($this->formName)
->setArgs(['Individual1' => [1 => $cid[3]]])
->execute()
$this->assertEquals('3-1', $prefill['Individual1']['values'][1]['joins']['Phone'][0]['phone']);
// Form entity has `max="3"` so a forth contact (index 3) is out-of-bounds
- $prefill = Civi\Api4\Afform::prefill()
+ $prefill = Afform::prefill()
->setName($this->formName)
->setArgs(['Individual1' => [3 => $cid[0]]])
->execute();
civicrm_api4('Relationship', 'create', ['values' => $values]);
}
- $prefill = Civi\Api4\Afform::prefill()
+ $prefill = Afform::prefill()
->setName($this->formName)
->execute()
->indexBy('name');
<?php
+use Civi\Api4\Afform;
+use Civi\Api4\Contact;
use Civi\Api4\Relationship;
+use Civi\Api4\RelationshipType;
/**
* Test case for Afform.prefill and Afform.submit.
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($submission)
->execute();
$typeIds = [];
foreach ($types as $type) {
- $typeIds[] = \Civi\Api4\RelationshipType::create(FALSE)
+ $typeIds[] = RelationshipType::create(FALSE)
->addValue('contact_type_a', 'Organization')
->addValue('contact_type_b', 'Individual')
->addValue('name_a_b', $type)
],
];
- Civi\Api4\Afform::submit()
+ Afform::submit()
->setName($this->formName)
->setValues($submission)
->execute();
'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
- $contact = \Civi\Api4\Contact::save(FALSE)
+ $contact = Contact::save(FALSE)
->addRecord(['first_name' => 'Child1'])
->addRecord(['first_name' => 'Child2', 'is_deleted' => TRUE])
->addRecord(['first_name' => 'Parent'])
->addRecord(['contact_id_a' => $contact[6], 'contact_id_b' => $contact[2], 'relationship_type_id:name' => 'Child of'])
->execute();
- $prefill = Civi\Api4\Afform::prefill(FALSE)
+ $prefill = Afform::prefill(FALSE)
->setName($this->formName)
->setArgs(['Organization1' => $contact[3]])
->execute()
<?php
+use Civi\Api4\Afform;
+
/**
* Ensure that the routes created by Afform are working.
* @group e2e
public function setUp(): void {
parent::setUp();
- Civi\Api4\Afform::revert()
+ Afform::revert()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->execute();
public function tearDown(): void {
parent::tearDown();
- Civi\Api4\Afform::revert()
+ Afform::revert()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->execute();
$result = $http->get($url('civicrm/mock-page'));
$this->assertNotAuthorized($result, 'mock-page');
- Civi\Api4\Afform::update()
+ Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->addValue('permission', CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
return CRM_Utils_System::url($path, $query, TRUE, NULL, FALSE);
};
- Civi\Api4\Afform::update()
+ Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->addValue('permission', CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION)
$this->assertOpensPage($http->get($url('civicrm/mock-page')), 'mock-page');
$this->assertNotAuthorized($http->get($url('civicrm/mock-page-renamed')), 'mock-page');
- Civi\Api4\Afform::update()
+ Afform::update()
->setCheckPermissions(FALSE)
->addWhere('name', '=', $this->formName)
->addValue('server_route', 'civicrm/mock-page-renamed')
<?php
+
+use Civi\Api4\Afform;
use Civi\Api4\Dashboard;
/**
}
};
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
$message = 'The initial Afform.get should return default data';
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addSelect('*', 'has_base', 'has_local', 'base_module')
->addWhere('name', '=', $formName)->execute();
$this->assertEquals($formName, $result[0]['name'], $message);
$checkDashlet($originalMetadata);
$message = 'After updating with Afform.create, the revised data should be returned';
- $result = Civi\Api4\Afform::update()
+ $result = Afform::update()
->addWhere('name', '=', $formName)
->addValue('description', 'The temporary description')
->addValue('permission', ['access foo', 'access bar'])
$this->assertEquals('The temporary description', $result[0]['description'], $message);
$message = 'After updating, the Afform.get API should return blended data';
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addSelect('*', 'has_base', 'has_local', 'base_module')
->addWhere('name', '=', $formName)->execute();
$this->assertEquals($formName, $result[0]['name'], $message);
$this->assertEquals('org.civicrm.afform-mock', $get($result[0], 'base_module'), $message);
$checkDashlet($result[0]);
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
$message = 'After reverting, the final Afform.get should return default data';
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addSelect('*', 'has_base', 'has_local', 'base_module')
->addWhere('name', '=', $formName)->execute();
$this->assertEquals($formName, $result[0]['name'], $message);
* @dataProvider getFormatExamples
*/
public function testBasicConvert($formName, $updateFormat, $updateLayout, $readFormat, $readLayout, $exampleName): void {
- $actual = Civi\Api4\Afform::convert()->setLayout($updateLayout)
+ $actual = Afform::convert()->setLayout($updateLayout)
->setFrom($updateFormat)
->setTo($readFormat)
->execute();
* @dataProvider getFormatExamples
*/
public function testUpdateAndGetFormat($formName, $updateFormat, $updateLayout, $readFormat, $readLayout, $exampleName): void {
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
- Civi\Api4\Afform::update()
+ Afform::update()
->addWhere('name', '=', $formName)
->setLayoutFormat($updateFormat)
->setValues(['layout' => $updateLayout])
->execute();
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addWhere('name', '=', $formName)
->setLayoutFormat($readFormat)
->execute();
$this->assertEquals($readLayout, $this->fudgeMarkup($result[0]['layout']), "Based on \"$exampleName\", writing content as \"$updateFormat\" and reading back as \"$readFormat\".");
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
}
public function getWhitespaceExamples() {
* @dataProvider getWhitespaceExamples
*/
public function testWhitespaceFormat($directiveName, $example, $exampleName): void {
- Civi\Api4\Afform::save()
+ Afform::save()
->addRecord(['name' => $directiveName, 'layout' => $example['html']])
->setLayoutFormat('html')
->execute();
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addWhere('name', '=', $directiveName)
->setLayoutFormat('shallow')
->setFormatWhitespace(TRUE)
$this->assertEquals($example['stripped'] ?? $example['shallow'], $this->fudgeMarkup($result['layout']));
- Civi\Api4\Afform::save()
+ Afform::save()
->addRecord(['name' => $directiveName, 'layout' => $result['layout']])
->setLayoutFormat('shallow')
->setFormatWhitespace(TRUE)
->execute();
- $result = Civi\Api4\Afform::get()
+ $result = Afform::get()
->addWhere('name', '=', $directiveName)
->setLayoutFormat('html')
->execute()
$this->createLoggedInUser();
// The default mockPage has 1 explicit requirement + 2 automatic requirements.
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
$angModule = Civi::service('angular')->getModule($formName);
$this->assertEquals(['afCore', 'mockBespoke', 'mockBareFile', 'mockFoo'], $angModule['requires']);
- $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
+ $storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals(['mockBespoke'], $storedRequires[0]['requires']);
// Knock down to 1 explicit + 1 automatic.
- Civi\Api4\Afform::update()
+ Afform::update()
->addWhere('name', '=', $formName)
->setLayoutFormat('html')
->setValues(['layout' => '<div>The bare file says "<mock-bare-file/>"</div>'])
->execute();
$angModule = Civi::service('angular')->getModule($formName);
$this->assertEquals(['afCore', 'mockBespoke', 'mockBareFile'], $angModule['requires']);
- $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
+ $storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals(['mockBespoke'], $storedRequires[0]['requires']);
// Remove the last explict and implicit requirements.
- Civi\Api4\Afform::update()
+ Afform::update()
->addWhere('name', '=', $formName)
->setLayoutFormat('html')
->setValues([
->execute();
$angModule = Civi::service('angular')->getModule($formName);
$this->assertEquals(['afCore'], $angModule['requires']);
- $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
+ $storedRequires = Afform::get()->addWhere('name', '=', $formName)->addSelect('requires')->execute();
$this->assertEquals([], $storedRequires[0]['requires']);
- Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
+ Afform::revert()->addWhere('name', '=', $formName)->execute();
$angModule = Civi::service('angular')->getModule($formName);
$this->assertEquals(['afCore', 'mockBespoke', 'mockBareFile', 'mockFoo'], $angModule['requires']);
}
<?php
+use Civi\Api4\Afform;
+
/**
* Test case for Afform.prefill and Afform.submit.
*
public function setUp(): void {
parent::setUp();
- Civi\Api4\Afform::revert(FALSE)
+ Afform::revert(FALSE)
->addWhere('type', '=', 'block')
->execute();
$this->formName = 'mock' . rand(0, 100000);
}
public function tearDown(): void {
- Civi\Api4\Afform::revert(FALSE)
+ Afform::revert(FALSE)
->addWhere('name', '=', $this->formName)
->execute();
parent::tearDown();
'name' => $this->formName,
];
$full = array_merge($defaults, $values);
- Civi\Api4\Afform::create(FALSE)
+ Afform::create(FALSE)
->setLayoutFormat('html')
->setValues($full)
->execute();