namespace Civi\Financialacls;
use Civi;
+use Civi\Api4\PriceField;
+use Civi\Api4\PriceFieldValue;
+use Civi\Api4\PriceSet;
use CRM_Core_Session;
// I fought the Autoloader and the autoloader won.
foreach ($actions as $action => $action_ts) {
$this->assertEquals(
[
- ts('CiviCRM: %1 contributions of type %2', [1 => $action_ts, 2 => $type]),
+ ts('CiviCRM: %1 contributions of type %2', [
+ 1 => $action_ts,
+ 2 => $type,
+ ]),
ts('%1 contributions of type %2', [1 => $action_ts, 2 => $type]),
],
$permissions[$action . ' contributions of type ' . $type]
$this->assertEquals([1 => 'Donation'], $type);
}
+ /**
+ * Check method testCheckPermissionedLineItems()
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testCheckPermissionedLineItems(): void {
+ $priceSetID = PriceSet::create()->setValues([
+ 'title' => 'Price Set Financial ACLS',
+ 'name' => 'test_price_set',
+ 'extends' => 1,
+ 'financial_type_id:name' => 'Donation',
+ ])->execute()->first()['id'];
+
+ $paramsField = [
+ 'label' => 'Price Field',
+ 'name' => 'test_price_field',
+ 'html_type' => 'CheckBox',
+ 'is_display_amounts' => 1,
+ 'weight' => 1,
+ 'options_per_line' => 1,
+ 'price_set_id' => $priceSetID,
+ 'is_enter_qty' => 1,
+ 'financial_type_id:name' => 'Donation',
+ ];
+ $priceFieldID = PriceField::create()
+ ->setValues($paramsField)
+ ->execute()
+ ->first()['id'];
+ $priceFieldValueID = PriceFieldValue::create()->setValues([
+ 'price_field_id' => $priceFieldID,
+ 'amount' => 100,
+ 'name' => 'price_field_value',
+ 'label' => 'Price Field 1',
+ 'financial_type_id:name' => 'Donation',
+ 'weight' => 1,
+ ])->execute()->first()['id'];
+ $contributionParams = [
+ 'total_amount' => 300,
+ 'currency' => 'USD',
+ 'contact_id' => $this->individualCreate(),
+ 'financial_type_id' => 'Donation',
+ 'line_items' => [
+ [
+ 'line_item' => [
+ [
+ 'price_field_id' => $priceFieldID,
+ 'price_field_value_id' => $priceFieldValueID,
+ 'qty' => 3,
+ ],
+ ],
+ ],
+ ],
+ ];
+
+ $contribution = $this->callAPISuccess('Order', 'create', $contributionParams);
+ Civi::settings()->set('acl_financial_type', TRUE);
+
+ $this->setPermissions([
+ 'view contributions of type Member Dues',
+ ]);
+
+ try {
+ \CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contribution['id'], 'view');
+ $this->fail('Missed expected exception');
+ }
+ catch (\CRM_Core_Exception $e) {
+ $this->assertEquals('You do not have permission to access this page.', $e->getMessage());
+ }
+
+ $this->setPermissions([
+ 'view contributions of type Donation',
+ ]);
+ try {
+ \CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contribution['id'], 'view');
+ }
+ catch (\CRM_Core_Exception $e) {
+ $this->fail('permissions should be established');
+ }
+ }
+
}
$this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available membership types');
}
- /**
- * Check method testCheckPermissionedLineItems()
- *
- * @throws \CRM_Core_Exception
- */
- public function testCheckPermissionedLineItems(): void {
- $contactId = $this->individualCreate();
- $paramsSet['title'] = 'Price Set_test';
- $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
- $paramsSet['is_active'] = TRUE;
- $paramsSet['financial_type_id'] = 1;
- $paramsSet['extends'] = 1;
-
- $priceSet = CRM_Price_BAO_PriceSet::create($paramsSet);
- $priceSetId = $priceSet->id;
-
- //Checking for price set added in the table.
- $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
- 'id', $paramsSet['title'], 'Check DB for created price set'
- );
- $paramsField = [
- 'label' => 'Price Field',
- 'name' => CRM_Utils_String::titleToVar('Price Field'),
- 'html_type' => 'CheckBox',
- 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
- 'option_value' => ['1' => 100, '2' => 200],
- 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
- 'option_weight' => ['1' => 1, '2' => 2],
- 'option_amount' => ['1' => 100, '2' => 200],
- 'is_display_amounts' => 1,
- 'weight' => 1,
- 'options_per_line' => 1,
- 'is_active' => ['1' => 1, '2' => 1],
- 'price_set_id' => $priceSet->id,
- 'is_enter_qty' => 1,
- 'financial_type_id' => 1,
- ];
- $priceField = CRM_Price_BAO_PriceField::create($paramsField);
- $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]);
- $contributionParams = [
- 'total_amount' => 300,
- 'currency' => 'USD',
- 'contact_id' => $contactId,
- 'financial_type_id' => 1,
- 'contribution_status_id' => 1,
- 'skipCleanMoney' => TRUE,
- ];
-
- foreach ($priceFields['values'] as $key => $priceField) {
- $lineItems[1][$key] = [
- 'price_field_id' => $priceField['price_field_id'],
- 'price_field_value_id' => $priceField['id'],
- 'label' => $priceField['label'],
- 'field_title' => $priceField['label'],
- 'qty' => 1,
- 'unit_price' => $priceField['amount'],
- 'line_total' => $priceField['amount'],
- 'financial_type_id' => $priceField['financial_type_id'],
- ];
- }
- $contributionParams['line_item'] = $lineItems;
- $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
- CRM_Financial_BAO_FinancialType::$_statusACLFt = [];
- $this->setACL();
-
- $this->setPermissions([
- 'view contributions of type Member Dues',
- ]);
-
- try {
- CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
- $this->fail('Missed expected exception');
- }
- catch (CRM_Core_Exception $e) {
- $this->assertEquals('You do not have permission to access this page.', $e->getMessage());
- }
-
- $this->setPermissions([
- 'view contributions of type Donation',
- ]);
- $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
- $this->assertEquals(TRUE, $perm, 'Verify that line items now have permission.');
- }
-
}