Clean up buildPermissionedWhereClause
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 13 Jan 2022 00:27:44 +0000 (13:27 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 13 Jan 2022 06:01:52 +0000 (19:01 +1300)
CRM/Contact/BAO/Query.php
CRM/Financial/BAO/FinancialType.php
ext/financialacls/tests/phpunit/Civi/Financialacls/FinancialTypeTest.php

index a487b37af4d06490b8d908cfec7bab242f142599..27b5de1bfc27611304f55515081b98673d8b6103 100644 (file)
@@ -581,7 +581,11 @@ class CRM_Contact_BAO_Query {
     }
     if (isset($component) && !$this->_skipPermission) {
       // Unit test coverage in api_v3_FinancialTypeACLTest::testGetACLContribution.
-      CRM_Financial_BAO_FinancialType::buildPermissionedClause($this->_whereClause, $component);
+      $clauses = CRM_Financial_BAO_FinancialType::buildPermissionedClause($component);
+      if (!empty($this->_whereClause) && !empty($clauses)) {
+        $this->_whereClause .= ' AND ';
+      }
+      $this->_whereClause .= $clauses;
     }
 
     $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity);
index d5e8618961b5b4f5a07a05d8d4c27e33fe3f2419..e5489e8ff923d6b9d80c500b5e25ee9ba7f0671a 100644 (file)
@@ -343,35 +343,34 @@ class CRM_Financial_BAO_FinancialType extends CRM_Financial_DAO_FinancialType im
   /**
    * Function to build a permissioned sql where clause based on available financial types.
    *
-   * @param array $whereClauses
-   *   (reference ) an array of clauses
    * @param string $component
    *   the type of component
-   * @param string $alias
-   *   the alias to use
    *
+   * @return string $clauses
    */
-  public static function buildPermissionedClause(&$whereClauses, $component = NULL, $alias = NULL) {
+  public static function buildPermissionedClause(string $component): string {
+    $clauses = [];
     // @todo the relevant addSelectWhere clause should be called.
     if (!self::isACLFinancialTypeStatus()) {
-      return FALSE;
+      return '';
     }
-    if ($component == 'contribution') {
-      $types = self::getAllEnabledAvailableFinancialTypes();
-      $column = "financial_type_id";
+    if ($component === 'contribution') {
+      $types = array_keys(self::getAllEnabledAvailableFinancialTypes());
+      if (empty($types)) {
+        $types = [0];
+      }
+      $clauses[] = ' civicrm_contribution.financial_type_id IN (' . implode(',', $types) . ')';
     }
-    if ($component == 'membership') {
+    if ($component === 'membership') {
       self::getAvailableMembershipTypes($types, CRM_Core_Action::VIEW);
-      $column = "membership_type_id";
-    }
-    if (!empty($whereClauses)) {
-      $whereClauses .= ' AND ';
-    }
-    if (empty($types)) {
-      $whereClauses .= " civicrm_{$component}.{$column} IN (0)";
-      return;
+      $types = array_keys($types);
+      if (empty($types)) {
+        $types = [0];
+      }
+      $clauses[] = ' civicrm_membership.membership_type_id IN (' . implode(',', $types) . ')';
+
     }
-    $whereClauses .= " civicrm_{$component}.{$column} IN (" . implode(',', array_keys($types)) . ")";
+    return implode(' AND ', $clauses);
   }
 
   /**
index bb68b08eabbc3c7e3ad81914c4fe3dbcb2148733..4c07331e88a4936a824b55b621cd9d7a68db6a15 100644 (file)
@@ -16,8 +16,6 @@ class FinancialTypeTest extends BaseTestClass {
   /**
    * Test that a message is put in session when changing the name of a
    * financial type.
-   *
-   * @throws \CRM_Core_Exception
    */
   public function testChangeFinancialTypeName(): void {
     Civi::settings()->set('acl_financial_type', TRUE);
@@ -73,4 +71,25 @@ class FinancialTypeTest extends BaseTestClass {
     $this->assertEquals([1 => 'Donation'], $type);
   }
 
+  /**
+   * Check method test buildPermissionedClause()
+   */
+  public function testBuildPermissionedClause(): void {
+    Civi::settings()->set('acl_financial_type', 1);
+    $this->setPermissions([
+      'view contributions of type Donation',
+      'view contributions of type Member Dues',
+    ]);
+    $whereClause = \CRM_Financial_BAO_FinancialType::buildPermissionedClause('contribution');
+    $this->assertEquals(' civicrm_contribution.financial_type_id IN (1,2)', $whereClause);
+    $this->setPermissions([
+      'view contributions of type Donation',
+      'view contributions of type Member Dues',
+      'view contributions of type Event Fee',
+    ]);
+
+    $whereClause = \CRM_Financial_BAO_FinancialType::buildPermissionedClause('contribution');
+    $this->assertEquals(' civicrm_contribution.financial_type_id IN (1,4,2)', $whereClause);
+  }
+
 }