Skip to content

Commit

Permalink
Cleanup Apiv3-UfField test
Browse files Browse the repository at this point in the history
Stop using xml for set up, other minor fixes, remove tests that are very well covered elsewhere
(basic api wrapper & crud)
  • Loading branch information
eileenmcnaughton committed Jun 24, 2023
1 parent d0df23a commit f30fd18
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 146 deletions.
5 changes: 3 additions & 2 deletions Civi/Test/ContactTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ public function contactDelete($contactID): void {
* Add a Group.
*
* @param array $params
* @param string $identifier
*
* @return int
* groupId of created group
*/
public function groupCreate(array $params = []): int {
public function groupCreate(array $params = [], string $identifier = 'group'): int {
$params = array_merge([
'name' => 'Test Group 1',
'domain_id' => 1,
Expand All @@ -219,7 +220,7 @@ public function groupCreate(array $params = []): int {
],
], $params);

$result = $this->callAPISuccess('Group', 'create', $params);
$result = $this->createTestEntity('Group', $params, $identifier);
return $result['id'];
}

Expand Down
28 changes: 17 additions & 11 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2130,10 +2130,14 @@ public function financialAccountDelete($name) {
*
* You need to have pre-created these groups & created the user e.g
* $this->createLoggedInUser();
* $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
* $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
* $this->_permissionedDisabledGroup = $this->groupCreate(array('title' =>
* 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
* $this->_permissionedGroup = $this->groupCreate(array('title' =>
* 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
*
* @param bool $isProfile
*
* @throws \Civi\Core\Exception\DBQueryException
*/
public function setupACL($isProfile = FALSE) {
global $_REQUEST;
Expand All @@ -2147,24 +2151,26 @@ public function setupACL($isProfile = FALSE) {
if ($ov->find(TRUE)) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
}
$optionValue = $this->callAPISuccess('option_value', 'create', [
$this->callAPISuccess('option_value', 'create', [
'option_group_id' => $optionGroupID,
'label' => 'pick me',
'value' => 55,
]);

CRM_Core_DAO::executeQuery("
CRM_Core_DAO::executeQuery('
TRUNCATE civicrm_acl_cache
");
');

CRM_Core_DAO::executeQuery("
CRM_Core_DAO::executeQuery('
TRUNCATE civicrm_acl_contact_cache
");
');

// Setting ids is preferred.
$permissionedGroup = $this->ids['Group']['permissioned_group'] ?? $this->_permissionedGroup;
CRM_Core_DAO::executeQuery("
INSERT INTO civicrm_acl_entity_role (
`acl_role_id`, `entity_table`, `entity_id`, `is_active`
) VALUES (55, 'civicrm_group', {$this->_permissionedGroup}, 1);
) VALUES (55, 'civicrm_group', $permissionedGroup, 1);
");

if ($isProfile) {
Expand All @@ -2183,7 +2189,7 @@ public function setupACL($isProfile = FALSE) {
`name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
)
VALUES (
'view picked', 'civicrm_group', $this->_permissionedGroup , 'Edit', 'civicrm_group', {$this->_permissionedGroup}, 1
'view picked', 'civicrm_group', $permissionedGroup , 'Edit', 'civicrm_group', {$this->_permissionedGroup}, 1
);
");

Expand All @@ -2192,14 +2198,14 @@ public function setupACL($isProfile = FALSE) {
`name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
)
VALUES (
'view picked', 'civicrm_group', $this->_permissionedGroup, 'Edit', 'civicrm_group', {$this->_permissionedDisabledGroup}, 1
'view picked', 'civicrm_group', $permissionedGroup, 'Edit', 'civicrm_group', {$this->_permissionedDisabledGroup}, 1
);
");
}

$this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
$this->callAPISuccess('group_contact', 'create', [
'group_id' => $this->_permissionedGroup,
'group_id' => $permissionedGroup,
'contact_id' => $this->_loggedInUser,
]);

Expand Down
174 changes: 41 additions & 133 deletions tests/phpunit/api/v3/UFFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@
*/
class api_v3_UFFieldTest extends CiviUnitTestCase {

/**
* ids from the uf_group_test.xml fixture
*
* @var int
*/
protected $_ufGroupId = 11;

protected $_ufFieldId;

protected $_contactId = 69;

protected $_params;

protected $_entity = 'uf_field';

/**
* Set up for test.
*
* @throws \Exception
*/
protected function setUp(): void {
parent::setUp();
public function tearDown(): void {
$this->quickCleanup(
[
'civicrm_group',
Expand All @@ -49,12 +33,21 @@ protected function setUp(): void {
'civicrm_uf_match',
]
);
parent::tearDown();
}

$this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');

$this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);

$this->_params = [
/**
* Create a field with 'weight=1' and then a second with 'weight=1'.
*
* The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testCreateUFFieldWithDefaultAutoWeight(int $version): void {
$this->_apiversion = $version;
$params = [
'field_name' => 'phone',
'field_type' => 'Contact',
'visibility' => 'Public Pages and Listings',
Expand All @@ -64,86 +57,20 @@ protected function setUp(): void {
'is_active' => 1,
'location_type_id' => 1,
'phone_type_id' => 1,
'uf_group_id' => $this->_ufGroupId,
'uf_group_id' => $this->createTestEntity('UFGroup', [
'group_type' => 'Contact',
'title' => 'Test Profile',
])['id'],
];
}

/**
* Tear down function.
*
* @throws \Exception
*/
public function tearDown(): void {
$this->quickCleanup(
[
'civicrm_group',
'civicrm_contact',
'civicrm_uf_group',
'civicrm_uf_join',
'civicrm_uf_match',
]
);
}

/**
* Create / updating field.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testCreateUFField($version) {
$this->_apiversion = $version;
$params = $this->_params;
$ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
unset($params['uf_group_id']);
$this->_ufFieldId = $ufField['id'];
foreach ($params as $key => $value) {
$this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
}
}

/**
* Failure test for field_name.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testCreateUFFieldWithBadFieldName($version) {
$this->_apiversion = $version;
$params = $this->_params;
$params['field_name'] = 'custom_98789';
$this->callAPIFailure('uf_field', 'create', $params);
}

/**
* Failure test for bad parameters.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testCreateUFFieldWithWrongParams($version) {
$this->_apiversion = $version;
$this->callAPIFailure('uf_field', 'create', ['field_name' => 'test field']);
$this->callAPIFailure('uf_field', 'create', ['label' => 'name-less field']);
}

/**
* Create a field with 'weight=1' and then a second with 'weight=1'.
*
* The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testCreateUFFieldWithDefaultAutoWeight($version) {
$this->_apiversion = $version;
$params1 = $this->_params;
$ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
$ufField1 = $this->callAPISuccess('uf_field', 'create', $params);
$this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
$this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
1 => [$ufField1['id'], 'Int'],
]);

$params2 = $this->_params;
// needs to be a different field
$params2['location_type_id'] = 2;
$ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
$params['location_type_id'] = 2;
$ufField2 = $this->callAPISuccess('UFField', 'create', $params);
$this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
$this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
1 => [$ufField2['id'], 'Int'],
Expand All @@ -153,36 +80,10 @@ public function testCreateUFFieldWithDefaultAutoWeight($version) {
]);
}

/**
* Deleting field.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testDeleteUFField($version) {
$this->_apiversion = $version;
$ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
$params = [
'field_id' => $ufField['id'],
];
$this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
}

/**
* Test getting ufField.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testGetUFFieldSuccess($version) {
$this->_apiversion = $version;
$this->callAPISuccess($this->_entity, 'create', $this->_params);
$result = $this->callAPIAndDocument($this->_entity, 'get', [], __FUNCTION__, __FILE__);
$this->getAndCheck($this->_params, $result['id'], $this->_entity);
}

/**
* Create / updating field.
*/
public function testReplaceUFFields() {
public function testReplaceUFFields(): void {
$baseFields = [];
$baseFields[] = [
'field_name' => 'first_name',
Expand Down Expand Up @@ -216,17 +117,20 @@ public function testReplaceUFFields() {
];

$params = [
'uf_group_id' => $this->_ufGroupId,
'uf_group_id' => $this->createTestEntity('UFGroup', [
'group_type' => 'Contact',
'title' => 'Test Profile',
])['id'],
'option.autoweight' => FALSE,
'values' => $baseFields,
'check_permissions' => TRUE,
];

$result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__);
$result = $this->callAPISuccess('UFField', 'replace', $params);
$inputsByName = CRM_Utils_Array::index(['field_name'], $params['values']);
$this->assertEquals(count($params['values']), count($result['values']));
$this->assertSameSize($params['values'], $result['values']);
foreach ($result['values'] as $outUfField) {
$this->assertTrue(is_string($outUfField['field_name']));
$this->assertIsString($outUfField['field_name']);
$inUfField = $inputsByName[$outUfField['field_name']];
foreach ($inUfField as $key => $inValue) {
$this->assertEquals($inValue, $outUfField[$key],
Expand All @@ -246,7 +150,7 @@ public function testReplaceUFFields() {
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testProfilesWithoutACL($version) {
public function testProfilesWithoutACL(int $version): void {
$this->_apiversion = $version;
$this->createLoggedInUser();
$baseFields[] = [
Expand All @@ -260,25 +164,29 @@ public function testProfilesWithoutACL($version) {
];
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
$params = [
'uf_group_id' => $this->_ufGroupId,
'uf_group_id' => $this->createTestEntity('UFGroup', [
'group_type' => 'Contact',
'title' => 'Test Profile',
])['id'],
'option.autoweight' => FALSE,
'values' => $baseFields,
'check_permissions' => TRUE,
];
$this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
$this->callAPIFailure('uf_field', 'replace', $params);
$this->callAPIFailure('UFField', 'replace', $params);
}

/**
* Check Profile ACL for API permission.
*
* @throws \Civi\Core\Exception\DBQueryException
*/
public function testACLPermissionforProfiles() {
public function testACLPermissionForProfiles(): void {
$this->createLoggedInUser();
$this->_permissionedGroup = $this->groupCreate([
$this->groupCreate([
'title' => 'Edit Profiles',
'is_active' => 1,
'name' => 'edit-profiles',
]);
], 'permissioned_group');
$this->setupACL(TRUE);
$this->testReplaceUFFields();
}
Expand Down

0 comments on commit f30fd18

Please sign in to comment.