replace setStatus with statusBounce
authorJitendra Purohit <jitendra@fuzion.co.nz>
Mon, 14 Aug 2017 16:03:33 +0000 (21:33 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Mon, 14 Aug 2017 16:03:33 +0000 (21:33 +0530)
CRM/Financial/BAO/FinancialTypeAccount.php
CRM/Financial/Form/FinancialTypeAccount.php
tests/phpunit/CRM/Financial/BAO/FinancialTypeAccountTest.php

index c0febdc9e03e4464703ebd5622c3e2573ecfc982..0ea4abfd71f1954783c82ca95ff47fcbed614906 100644 (file)
@@ -91,10 +91,7 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
       $financialTypeAccount->find(TRUE);
     }
     $financialTypeAccount->copyValues($params);
-    $valid = self::validateRelationship($financialTypeAccount);
-    if (!$valid) {
-      return FALSE;
-    }
+    self::validateRelationship($financialTypeAccount);
     $financialTypeAccount->save();
     return $financialTypeAccount;
   }
@@ -105,7 +102,6 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
    * @param int $financialTypeAccountId
    * @param int $accountId
    *
-   * @return bool
    */
   public static function del($financialTypeAccountId, $accountId = NULL) {
     // check if financial type is present
@@ -277,6 +273,7 @@ WHERE cog.name = 'payment_instrument' ";
    *
    * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount
    *
+  * @throws CRM_Core_Exception
    */
   public static function validateRelationship($financialTypeAccount) {
     $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
@@ -286,10 +283,8 @@ WHERE cog.name = 'payment_instrument' ";
       $params = array(
         1 => $accountRelationships[$financialTypeAccount->account_relationship],
       );
-      CRM_Core_Session::setStatus(ts('This financial account cannot have \'%1\' relationship.', $params), ts('Error'), 'error');
-      return FALSE;
+      throw new CRM_Core_Exception(ts("This financial account cannot have '%1' relationship.", $params));
     }
-    return TRUE;
   }
 
 }
index d1f8cb50ffeee4e4bc49d2360454608157484ad7..0b380935f11138c374742a4c5f914fb3e825b25d 100644 (file)
@@ -299,10 +299,13 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form {
       if ($this->_action & CRM_Core_Action::ADD) {
         $params['entity_id'] = $this->_aid;
       }
-      $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids);
-      if ($financialTypeAccount) {
-        CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'));
+      try {
+        $financialTypeAccount = CRM_Financial_BAO_FinancialTypeAccount::add($params, $ids);
       }
+      catch (CRM_Core_Exception $e) {
+        CRM_Core_Error::statusBounce($e->getMessage());
+      }
+      CRM_Core_Session::setStatus(ts('The financial type Account has been saved.'));
     }
 
     $buttonName = $this->controller->getButtonName();
index c29b51875dd697cc7a613d49394d1a5bfce6d4b3..88172853d05d21f4af7d1cd58d3df536c20261cb 100644 (file)
@@ -131,8 +131,14 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
     $financialAccountType->entity_id = array_search('Member Dues', $financialType);
     $financialAccountType->account_relationship = array_search('Credit/Contra Revenue Account is', $accountRelationships);
     $financialAccountType->financial_account_id = array_search('Liability', $financialAccount);
-    $valid = CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
-    $this->assertFalse($valid);
+    try {
+      CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
+      $this->fail("Missed expected exception");
+    }
+    catch (Exception $e) {
+      $this->assertTrue(TRUE, 'Received expected exception');
+      $this->assertEquals($e->getMessage(), "This financial account cannot have 'Credit/Contra Revenue Account is' relationship.");
+    }
   }
 
   /**