Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeACLTest.php
index abe128b1f577d4a759f0c263fea4e1c3048f25e0..d1ecb25cccbd0a4882ef3cd6b0538fee44e177ea 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -33,6 +33,8 @@
  */
 class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
 
+  use CRMTraits_Financial_FinancialACLTrait;
+
   /**
    * Assume empty database with just civicrm_data.
    */
@@ -109,29 +111,14 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
   public function tearDown() {
     $this->quickCleanUpFinancialEntities();
     $this->quickCleanup(array('civicrm_uf_match'));
-    CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array();
-    CRM_Financial_BAO_FinancialType::$_statusACLFt = array();
-    $params = array(
-      'domain_id' => 1,
-      'contribution_invoice_settings' => array('acl_financial_type' => 0),
-    );
-  }
-
-  public function setACL() {
-    CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array();
-    CRM_Financial_BAO_FinancialType::$_statusACLFt = array();
-    $params = array(
-      'domain_id' => 1,
-      'contribution_invoice_settings' => array('acl_financial_type' => 1),
-    );
-    $this->callAPISuccess('setting', 'create', $params);
+    $this->disableFinancialACLs();
   }
 
   /**
    * Test Get.
    */
   public function testCreateACLContribution() {
-    $this->setACL();
+    $this->enableFinancialACLs();
     $p = array(
       'contact_id' => $this->_individualId,
       'receive_date' => '2010-01-20',
@@ -146,28 +133,29 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
       'contribution_status_id' => 1,
       'check_permissions' => TRUE,
     );
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array(
+
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'edit contributions',
-    );
+    ]);
     $result = $this->callAPIFailure('contribution', 'create', $p);
     $this->assertEquals('You do not have permission to create this contribution', $result['error_message']);
-    $config->userPermissionClass->permissions[] = 'add contributions of type Donation';
+    $this->addFinancialAclPermissions([['add', 'Donation']]);
+
     $contribution = $this->callAPISuccess('contribution', 'create', $p);
 
     $params = array(
       'contribution_id' => $contribution['id'],
     );
 
-    $config->userPermissionClass->permissions = array(
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'edit contributions',
       'view contributions of type Donation',
       'delete contributions of type Donation',
-    );
+    ]);
 
     $contribution = $this->callAPISuccess('contribution', 'get', $params);
 
@@ -191,14 +179,14 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
    * Test that acl contributions can be retrieved.
    */
   public function testGetACLContribution() {
-    $this->setACL();
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array(
+    $this->enableFinancialACLs();
+
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'view all contacts',
       'add contributions of type Donation',
-    );
+    ]);
     $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
 
     $params = array(
@@ -208,9 +196,7 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
     $contribution = $this->callAPISuccess('contribution', 'get', $params);
     $this->assertEquals($contribution['count'], 0);
 
-    CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
-
-    $config->userPermissionClass->permissions[3] = 'view contributions of type Donation';
+    $this->addFinancialAclPermissions([['view', 'Donation']]);
     $contribution = $this->callAPISuccess('contribution', 'get', $params);
 
     $this->assertEquals($contribution['count'], 1);
@@ -220,7 +206,7 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
    * Test checks that passing in line items suppresses the create mechanism.
    */
   public function testCreateACLContributionChainedLineItems() {
-    $this->setACL();
+    $this->enableFinancialACLs();
     $params = array(
       'contact_id' => $this->_individualId,
       'receive_date' => '20120511',
@@ -251,33 +237,27 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
       ),
     );
 
-    $description = "Create Contribution with Nested Line Items.";
-    $subfile = "CreateWithNestedLineItems";
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array(
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'edit contributions',
       'delete in CiviContribute',
       'add contributions of type Donation',
       'delete contributions of type Donation',
-    );
-    $contribution = $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
+    ]);
+    $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
 
     // Check that the entire contribution has rolled back.
     $contribution = $this->callAPISuccess('contribution', 'get', array());
     $this->assertEquals(0, $contribution['count']);
 
-    CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
-
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array_merge($config->userPermissionClass->permissions, array(
-      'add contributions of type Member Dues',
-      'view contributions of type Donation',
-      'view contributions of type Member Dues',
-      'delete contributions of type Member Dues',
-    ));
-    $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
+    $this->addFinancialAclPermissions([
+      ['add', 'Member Dues'],
+      ['view', 'Donation'],
+      ['view', 'Member Dues'],
+      ['delete', 'Member Dues'],
+    ]);
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
 
     $lineItemParams = array(
       'contribution_id' => $contribution['id'],
@@ -301,7 +281,7 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
    * Test that acl contributions can be edited.
    */
   public function testEditACLContribution() {
-    $this->setACL();
+    $this->enableFinancialACLs();
     $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
 
     $params = array(
@@ -309,16 +289,16 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
       'check_permissions' => TRUE,
       'total_amount' => 200.00,
     );
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array(
+
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'edit contributions',
       'view contributions of type Donation',
-    );
-    $contribution = $this->callAPIFailure('Contribution', 'create', $params);
+    ]);
+    $this->callAPIFailure('Contribution', 'create', $params);
 
-    $config->userPermissionClass->permissions[] = 'edit contributions of type Donation';
+    $this->addFinancialAclPermissions([['edit', 'Donation']]);
     $contribution = $this->callAPISuccess('Contribution', 'create', $params);
 
     $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
@@ -328,24 +308,24 @@ class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
    * Test that acl contributions can be deleted.
    */
   public function testDeleteACLContribution() {
-    $this->setACL();
-    $config = &CRM_Core_Config::singleton();
-    $config->userPermissionClass->permissions = array(
+    $this->enableFinancialACLs();
+
+    $this->setPermissions([
       'access CiviCRM',
       'access CiviContribute',
       'view all contacts',
       'add contributions of type Donation',
-    );
+    ]);
     $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
 
     $params = array(
       'contribution_id' => $contribution['id'],
       'check_permissions' => TRUE,
     );
-    $config->userPermissionClass->permissions[3] = 'delete in CiviContribute';
-    $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
+    $this->addPermissions(['delete in CiviContribute']);
+    $this->callAPIFailure('Contribution', 'delete', $params);
 
-    $config->userPermissionClass->permissions[] = 'delete contributions of type Donation';
+    $this->addFinancialAclPermissions([['delete', 'Donation']]);
     $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
 
     $this->assertEquals($contribution['count'], 1);