CRM-16526 Added new unit tests for financial acl
authorEdsel <edsel.lopez@jmaconsulting.biz>
Mon, 29 Feb 2016 13:44:41 +0000 (19:14 +0530)
committerEdsel <edsel.lopez@jmaconsulting.biz>
Tue, 1 Mar 2016 06:49:12 +0000 (12:19 +0530)
----------------------------------------
* CRM-16526: ACLs for Financial Types
  https://issues.civicrm.org/jira/browse/CRM-16526

tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php

index 6ca21655dd37099c61cdfa5ab9972a12b86fbabe..7ae71ae9ae854ceeb61c6d61aa1dce952e70f48c 100644 (file)
@@ -225,4 +225,96 @@ class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase {
     $this->assertEquals($permissions, $checkPerms, 'Verify that permissions for each financial type have been added');
   }
 
+  /**
+   * Check method testcheckPermissionedLineItems()
+   */
+  public function testcheckPermissionedLineItems() {
+    $contactId = Contact::createIndividual();
+    $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
+    $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 priceset added in the table.
+    $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
+      'id', $paramsSet['title'], 'Check DB for created priceset'
+    );
+    $paramsField = array(
+      'label' => 'Price Field',
+      'name' => CRM_Utils_String::titleToVar('Price Field'),
+      'html_type' => 'CheckBox',
+      'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
+      'option_value' => array('1' => 100, '2' => 200),
+      'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
+      'option_weight' => array('1' => 1, '2' => 2),
+      'option_amount' => array('1' => 100, '2' => 200),
+      'is_display_amounts' => 1,
+      'weight' => 1,
+      'options_per_line' => 1,
+      'is_active' => array('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', array('price_field_id' => $priceField->id));
+    $contributionParams = array(
+      'total_amount' => 300,
+      'currency' => 'USD',
+      'contact_id' => $contactId,
+      'financial_type_id' => 1,
+      'contribution_status_id' => 1,
+    );
+
+    foreach ($priceFields['values'] as $key => $priceField) {
+      $lineItems[1][$key] = array(
+        '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 = array();
+    $this->setACL();
+    $config = &CRM_Core_Config::singleton();
+    $config->userPermissionClass->permissions = array(
+      'view contributions of type Member Dues',
+    );
+
+    try {
+      $error = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
+      $this->fail("Missed expected exception");
+    }
+    catch (Exception $e) {
+      $this->assertEquals("A fatal error was triggered: You do not have permission to access this page.", $e->getMessage());
+    }
+    $config = &CRM_Core_Config::singleton();
+    $config->userPermissionClass->permissions = array(
+      'view contributions of type Donation',
+    );
+    $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
+    $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');
+  }
+
+  /**
+   * Check method testisACLFinancialTypeStatus()
+   */
+  public function testisACLFinancialTypeStatus() {
+    $isACL = CRM_Core_BAO_Setting::getItem(NULL, 'contribution_invoice_settings');
+    $this->assertEquals(array_search('acl_financial_type', $isACL), NULL);
+    $this->setACL();
+    $isACL = CRM_Core_BAO_Setting::getItem(NULL, 'contribution_invoice_settings');
+    $this->assertEquals($isACL, array('acl_financial_type' => 1));
+  }
+
 }