From 11f8028be0afb7f25ba9d2bf026122f79dc213d4 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 23 Oct 2020 17:28:14 +1300 Subject: [PATCH] Unit test for financial acl CRM_Member_BAO_MembershipType::getPermissionedMembershipTypes function --- .../tests/phpunit/BuildAmountHookTest.php | 79 ------------------- .../Civi/Financialacls/BaseTestClass.php | 59 ++++++++++++++ .../Financialacls/BuildAmountHookTest.php | 44 +++++++++++ .../{ => Civi/Financialacls}/LineItemTest.php | 57 +++---------- .../Financialacls/MembershipTypesTest.php | 28 +++++++ 5 files changed, 141 insertions(+), 126 deletions(-) delete mode 100644 ext/financialacls/tests/phpunit/BuildAmountHookTest.php create mode 100644 ext/financialacls/tests/phpunit/Civi/Financialacls/BaseTestClass.php create mode 100644 ext/financialacls/tests/phpunit/Civi/Financialacls/BuildAmountHookTest.php rename ext/financialacls/tests/phpunit/{ => Civi/Financialacls}/LineItemTest.php (62%) create mode 100644 ext/financialacls/tests/phpunit/Civi/Financialacls/MembershipTypesTest.php diff --git a/ext/financialacls/tests/phpunit/BuildAmountHookTest.php b/ext/financialacls/tests/phpunit/BuildAmountHookTest.php deleted file mode 100644 index b240ac3aee..0000000000 --- a/ext/financialacls/tests/phpunit/BuildAmountHookTest.php +++ /dev/null @@ -1,79 +0,0 @@ -installMe(__DIR__) - ->apply(); - } - - /** - * Test api applies permissions on line item actions (delete & get). - */ - public function testBuildAmount() { - $priceSet = PriceSet::create()->setValues(['name' => 'test', 'title' => 'test', 'extends' => 'CiviMember'])->execute()->first(); - PriceField::create()->setValues([ - 'financial_type_id:name' => 'Donation', - 'name' => 'donation', - 'label' => 'donation', - 'price_set_id' => $priceSet['id'], - 'html_type' => 'Select', - ])->addChain('field_values', PriceFieldValue::save()->setRecords([ - ['financial_type_id:name' => 'Donation', 'name' => 'a', 'label' => 'a', 'amount' => 1], - ['financial_type_id:name' => 'Member Dues', 'name' => 'b', 'label' => 'b', 'amount' => 2], - ])->setDefaults(['price_field_id' => '$id']))->execute(); - Civi::settings()->set('acl_financial_type', TRUE); - $this->setPermissions([ - 'access CiviCRM', - 'access CiviContribute', - 'view contributions of type Donation', - 'delete contributions of type Donation', - 'add contributions of type Donation', - 'edit contributions of type Donation', - ]); - $this->createLoggedInUser(); - $form = new CRM_Member_Form_Membership(); - $form->controller = new CRM_Core_Controller(); - $form->set('priceSetId', $priceSet['id']); - CRM_Price_BAO_PriceSet::buildPriceSet($form); - $priceField = reset($form->_priceSet['fields']); - $this->assertCount(1, $priceField['options']); - $this->assertEquals('a', reset($priceField['options'])['name']); - } - - /** - * Set ACL permissions, overwriting any existing ones. - * - * @param array $permissions - * Array of permissions e.g ['access CiviCRM','access CiviContribute'], - */ - protected function setPermissions(array $permissions) { - CRM_Core_Config::singleton()->userPermissionClass->permissions = $permissions; - if (isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'])) { - unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']); - } - } - -} diff --git a/ext/financialacls/tests/phpunit/Civi/Financialacls/BaseTestClass.php b/ext/financialacls/tests/phpunit/Civi/Financialacls/BaseTestClass.php new file mode 100644 index 0000000000..6eb159cd81 --- /dev/null +++ b/ext/financialacls/tests/phpunit/Civi/Financialacls/BaseTestClass.php @@ -0,0 +1,59 @@ +installMe(__DIR__) + ->apply(); + } + + /** + * Set ACL permissions, overwriting any existing ones. + * + * @param array $permissions + * Array of permissions e.g ['access CiviCRM','access CiviContribute'], + */ + protected function setPermissions(array $permissions) { + \CRM_Core_Config::singleton()->userPermissionClass->permissions = $permissions; + if (isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'])) { + unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']); + } + } + + protected function setupLoggedInUserWithLimitedFinancialTypeAccess(): void { + $this->setPermissions([ + 'access CiviCRM', + 'access CiviContribute', + 'edit contributions', + 'delete in CiviContribute', + 'view contributions of type Donation', + 'delete contributions of type Donation', + 'add contributions of type Donation', + 'edit contributions of type Donation', + ]); + \Civi::settings()->set('acl_financial_type', TRUE); + $this->createLoggedInUser(); + } + +} diff --git a/ext/financialacls/tests/phpunit/Civi/Financialacls/BuildAmountHookTest.php b/ext/financialacls/tests/phpunit/Civi/Financialacls/BuildAmountHookTest.php new file mode 100644 index 0000000000..1ef9435ff1 --- /dev/null +++ b/ext/financialacls/tests/phpunit/Civi/Financialacls/BuildAmountHookTest.php @@ -0,0 +1,44 @@ +setValues(['name' => 'test', 'title' => 'test', 'extends' => 'CiviMember'])->execute()->first(); + PriceField::create()->setValues([ + 'financial_type_id:name' => 'Donation', + 'name' => 'donation', + 'label' => 'donation', + 'price_set_id' => $priceSet['id'], + 'html_type' => 'Select', + ])->addChain('field_values', PriceFieldValue::save()->setRecords([ + ['financial_type_id:name' => 'Donation', 'name' => 'a', 'label' => 'a', 'amount' => 1], + ['financial_type_id:name' => 'Member Dues', 'name' => 'b', 'label' => 'b', 'amount' => 2], + ])->setDefaults(['price_field_id' => '$id']))->execute(); + $this->setupLoggedInUserWithLimitedFinancialTypeAccess(); + $form = new \CRM_Member_Form_Membership(); + $form->controller = new \CRM_Core_Controller(); + $form->set('priceSetId', $priceSet['id']); + \CRM_Price_BAO_PriceSet::buildPriceSet($form); + $priceField = reset($form->_priceSet['fields']); + $this->assertCount(1, $priceField['options']); + $this->assertEquals('a', reset($priceField['options'])['name']); + } + +} diff --git a/ext/financialacls/tests/phpunit/LineItemTest.php b/ext/financialacls/tests/phpunit/Civi/Financialacls/LineItemTest.php similarity index 62% rename from ext/financialacls/tests/phpunit/LineItemTest.php rename to ext/financialacls/tests/phpunit/Civi/Financialacls/LineItemTest.php index 0c4c4b9c72..33db7de342 100644 --- a/ext/financialacls/tests/phpunit/LineItemTest.php +++ b/ext/financialacls/tests/phpunit/Civi/Financialacls/LineItemTest.php @@ -1,11 +1,12 @@ installMe(__DIR__) - ->apply(); - } +class LineItemTest extends BaseTestClass { /** * Test api applies permissions on line item actions (delete & get). + * * @dataProvider versionThreeAndFour */ public function testLineItemApiPermissions($version) { @@ -51,13 +38,13 @@ class LineItemTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf [ 'line_item' => [ [ - 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), + 'financial_type_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), 'line_total' => 40, 'price_field_id' => $defaultPriceFieldID, 'qty' => 1, ], [ - 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues'), + 'financial_type_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues'), 'line_total' => 50, 'price_field_id' => $defaultPriceFieldID, 'qty' => 1, @@ -68,18 +55,7 @@ class LineItemTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf ]); $this->_apiversion = $version; - $this->setPermissions([ - 'access CiviCRM', - 'access CiviContribute', - 'edit contributions', - 'delete in CiviContribute', - 'view contributions of type Donation', - 'delete contributions of type Donation', - 'add contributions of type Donation', - 'edit contributions of type Donation', - ]); - Civi::settings()->set('acl_financial_type', TRUE); - $this->createLoggedInUser(); + $this->setupLoggedInUserWithLimitedFinancialTypeAccess(); $lineItems = $this->callAPISuccess('LineItem', 'get', ['sequential' => TRUE])['values']; $this->assertCount(2, $lineItems); @@ -105,19 +81,6 @@ class LineItemTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf $this->callAPISuccess('LineItem', 'Create', ['id' => $line['id'], 'check_permissions' => TRUE, 'financial_type_id' => 'Donation']); } - /** - * Set ACL permissions, overwriting any existing ones. - * - * @param array $permissions - * Array of permissions e.g ['access CiviCRM','access CiviContribute'], - */ - protected function setPermissions($permissions) { - CRM_Core_Config::singleton()->userPermissionClass->permissions = $permissions; - if (isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'])) { - unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']); - } - } - /** * @return mixed * @throws \API_Exception diff --git a/ext/financialacls/tests/phpunit/Civi/Financialacls/MembershipTypesTest.php b/ext/financialacls/tests/phpunit/Civi/Financialacls/MembershipTypesTest.php new file mode 100644 index 0000000000..8e63eeeaa2 --- /dev/null +++ b/ext/financialacls/tests/phpunit/Civi/Financialacls/MembershipTypesTest.php @@ -0,0 +1,28 @@ +setRecords([ + ['name' => 'Forbidden', 'financial_type_id:name' => 'Member Dues'], + ['name' => 'Go for it', 'financial_type_id:name' => 'Donation'], + ])->setDefaults(['period_type' => 'rolling', 'member_of_contact_id' => 1])->execute()->indexBy('name'); + $this->setupLoggedInUserWithLimitedFinancialTypeAccess(); + $permissionedTypes = \CRM_Member_BAO_Membership::buildMembershipTypeValues(new \CRM_Member_Form_Membership()); + $this->assertEquals([$types['Go for it']['id']], array_keys($permissionedTypes)); + } + +} -- 2.25.1