Skip to content

Commit

Permalink
Fix financial acls test to be valid
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jul 27, 2021
1 parent 683840a commit 4657f33
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Civi\Financialacls;

use Civi\Api4\PriceField;
use Civi\Api4\PriceFieldValue;
use Civi\Api4\PriceSet;
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
Expand All @@ -16,6 +19,13 @@ class BaseTestClass extends \PHPUnit\Framework\TestCase implements HeadlessInter
use ContactTestTrait;
use Api3TestTrait;

/**
* IDs set up for test.
*
* @var array
*/
protected $ids = [];

/**
* @return \Civi\Test\CiviEnvBuilder
* @throws \CRM_Extension_Exception_ParseException
Expand Down Expand Up @@ -57,4 +67,39 @@ protected function setupLoggedInUserWithLimitedFinancialTypeAccess(): void {
$this->createLoggedInUser();
}

/**
* Create price set.
*
* @throws \API_Exception
*/
protected function createPriceSet(): void {
$priceSet = PriceSet::create(FALSE)->setValues([
'title' => 'Price Set',
'name' => 'price_set',
'financial_type_id.name' => 'Event Fee',
'extends' => 1,
])->execute()->first();
$this->ids['PriceSet'][0] = $priceSet['id'];
$this->ids['PriceField'][0] = PriceField::create(FALSE)->setValues([
'label' => 'Price Field',
'name' => '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.name' => 'Event Fee',
])->execute()->first()['id'];
$this->ids['PriceFieldValue'] = array_keys((array) PriceFieldValue::get()
->addWhere('price_field_id', '=', $this->ids['PriceField'][0])
->execute()->indexBy('id'));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Civi\Financialacls;

use Civi\Api4\PriceField;
use Civi\Api4\PriceFieldValue;
use Civi\Api4\PriceSet;

// I fought the Autoloader and the autoloader won.
require_once 'BaseTestClass.php';
Expand All @@ -27,10 +29,11 @@ class LineItemTest extends BaseTestClass {
* Test api applies permissions on line item actions (delete & get).
*
* @dataProvider versionThreeAndFour
* @throws \API_Exception
*/
public function testLineItemApiPermissions($version): void {
$contact1 = $this->individualCreate();
$defaultPriceFieldID = $this->getDefaultPriceFieldID();
$this->createPriceSet();
$order = $this->callAPISuccess('Order', 'create', [
'financial_type_id' => 'Donation',
'contact_id' => $contact1,
Expand All @@ -40,13 +43,15 @@ public function testLineItemApiPermissions($version): void {
[
'financial_type_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
'line_total' => 40,
'price_field_id' => $defaultPriceFieldID,
'price_field_id' => $this->ids['PriceField'][0],
'price_field_value_id' => $this->ids['PriceFieldValue'][0],
'qty' => 1,
],
[
'financial_type_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues'),
'line_total' => 50,
'price_field_id' => $defaultPriceFieldID,
'price_field_id' => $this->ids['PriceField'][0],
'price_field_value_id' => $this->ids['PriceFieldValue'][1],
'qty' => 1,
],
],
Expand All @@ -68,7 +73,7 @@ public function testLineItemApiPermissions($version): void {
'entity_table' => 'civicrm_contribution',
'line_total' => 20,
'unit_price' => 20,
'price_field_id' => $defaultPriceFieldID,
'price_field_id' => $this->ids['PriceField'][0],
'qty' => 1,
'financial_type_id' => 'Donation',
'check_permissions' => ($version == 3),
Expand All @@ -82,17 +87,4 @@ public function testLineItemApiPermissions($version): void {
$this->callAPISuccess('LineItem', 'Create', ['id' => $line['id'], 'check_permissions' => ($version == 3), 'financial_type_id' => 'Donation']);
}

/**
* @return mixed
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
protected function getDefaultPriceFieldID(): int {
return PriceField::get()
->addWhere('price_set_id:name', '=', 'default_contribution_amount')
->addWhere('name', '=', 'contribution_amount')
->addWhere('html_type', '=', 'Text')
->addSelect('id')->execute()->first()['id'];
}

}

0 comments on commit 4657f33

Please sign in to comment.