CIVICRM-1128: Added Unit Tests to validate the Case counts when case role is active...
authorAlok Patel <alok@agileware.com.au>
Wed, 9 Jan 2019 07:24:08 +0000 (12:54 +0530)
committerAlok Patel <alok@agileware.com.au>
Wed, 9 Jan 2019 07:24:08 +0000 (12:54 +0530)
CRM/Case/Form/Case.php
tests/phpunit/CRM/Case/BAO/CaseTest.php

index 5f59008df463f5ee90802b168b04d661c1384a68..8ceb6791c72ec1c2810597aa86800f240b36ffaf 100644 (file)
@@ -302,34 +302,30 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
   }
 
   /**
-   * Process the form submission.
+   * Wrapper for unit testing the post process submit function.
+   *
+   * @param $params
+   * @param $activityTypeFile
+   * @param $contactId
+   * @param $context
+   * @return CRM_Case_BAO_Case
    */
-  public function postProcess() {
-    $transaction = new CRM_Core_Transaction();
+  public function testSubmit($params, $activityTypeFile, $contactId, $context = "case") {
+    $this->controller = new CRM_Core_Controller();
 
-    // check if dedupe button, if so return.
-    $buttonName = $this->controller->getButtonName();
-    if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
-      return;
-    }
+    $this->_activityTypeFile = $activityTypeFile;
+    $this->_currentUserId = $contactId;
+    $this->_context = $context;
 
-    if ($this->_action & CRM_Core_Action::DELETE) {
-      $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
-      if ($caseDelete) {
-        CRM_Core_Session::setStatus(ts('You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.'), ts('Case Deleted'), 'success');
-      }
-      return;
-    }
+    return $this->submit($params);
+  }
 
-    if ($this->_action & CRM_Core_Action::RENEW) {
-      $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
-      if ($caseRestore) {
-        CRM_Core_Session::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
-      }
-      return;
-    }
-    // store the submitted values in an array
-    $params = $this->controller->exportValues($this->_name);
+  /**
+   * Submit the form with given params.
+   *
+   * @param $params
+   */
+  public function submit(&$params) {
     $params['now'] = date("Ymd");
 
     // 1. call begin post process
@@ -401,7 +397,42 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
       $className::endPostProcess($this, $params);
     }
 
+    return $caseObj;
+  }
+
+  /**
+   * Process the form submission.
+   */
+  public function postProcess() {
+    $transaction = new CRM_Core_Transaction();
+
+    // check if dedupe button, if so return.
+    $buttonName = $this->controller->getButtonName();
+    if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
+      return;
+    }
+
+    if ($this->_action & CRM_Core_Action::DELETE) {
+      $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
+      if ($caseDelete) {
+        CRM_Core_Session::setStatus(ts('You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.'), ts('Case Deleted'), 'success');
+      }
+      return;
+    }
+
+    if ($this->_action & CRM_Core_Action::RENEW) {
+      $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
+      if ($caseRestore) {
+        CRM_Core_Session::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
+      }
+      return;
+    }
+    // store the submitted values in an array
+    $params = $this->controller->exportValues($this->_name);
+    $this->submit($params);
+
     CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success');
+
   }
 
 }
index 16d052beb30a7a518e3278c3eb98955b93ac6db8..7c8f1ed47f8ba97b734e660d07bef8e30c7a9662 100644 (file)
@@ -47,6 +47,92 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
     $this->assertEquals('Test Contact - Housing Support', $recent[0]['title']);
   }
 
+  /**
+   * Create and return case object of given Client ID.
+   * @param $clientId
+   * @return CRM_Case_BAO_Case
+   */
+  private function createCase($clientId) {
+    $caseParams = array(
+      'activity_subject' => 'Case Subject',
+      'client_id'        => $clientId,
+      'case_type_id'     => 1,
+      'status_id'        => 1,
+      'case_type'        => 'housing_support',
+      'subject'          => 'Case Subject',
+      'start_date'       => date("Y-m-d"),
+      'start_date_time'  => date("YmdHis"),
+      'medium_id'        => 2,
+      'activity_details' => '',
+    );
+    $form = new CRM_Case_Form_Case();
+    $caseObj = $form->testSubmit($caseParams, "OpenCase", $clientId, "standalone");
+    return $caseObj;
+  }
+
+  /**
+   * Create case role relationship between given contacts for provided case ID.
+   *
+   * @param $contactIdA
+   * @param $contactIdB
+   * @param $caseId
+   * @param bool $isActive
+   */
+  private function createCaseRoleRelationship($contactIdA, $contactIdB, $caseId, $isActive = TRUE) {
+    $relationshipType = $this->relationshipTypeCreate([
+      'contact_type_b' => 'Individual',
+    ]);
+
+    $this->callAPISuccess('Relationship', 'create', array(
+      'contact_id_a'         => $contactIdA,
+      'contact_id_b'         => $contactIdB,
+      'relationship_type_id' => $relationshipType,
+      'case_id'              => $caseId,
+      'is_active'            => $isActive,
+    ));
+  }
+
+  /**
+   * Asserts number of cases for given logged in user.
+   *
+   * @param $loggedInUser
+   * @param $caseId
+   * @param $caseCount
+   */
+  private function assertCasesOfUser($loggedInUser, $caseId, $caseCount) {
+    $summary = CRM_Case_BAO_Case::getCasesSummary(FALSE);
+    $upcomingCases = CRM_Case_BAO_Case::getCases(FALSE, array(), 'dashboard', TRUE);
+    $caseRoles = CRM_Case_BAO_Case::getCaseRoles($loggedInUser, $caseId);
+
+    $this->assertEquals($caseCount, $upcomingCases, 'Upcoming case count must be ' . $caseCount);
+    $this->assertEquals($caseCount, $summary['rows']['Housing Support']['Ongoing']['count'], 'Housing Support Ongoing case summary must be ' . $caseCount);
+    $this->assertEquals($caseCount, count($caseRoles), 'Total case roles for logged in users must be ' . $caseCount);
+  }
+
+  /**
+   * Test that Case count is exactly one for logged in user for user's active role.
+   */
+  public function testActiveCaseRole() {
+    $individual = $this->individualCreate();
+    $caseObj = $this->createCase($individual);
+    $caseId = $caseObj->id;
+    $loggedInUser = $this->createLoggedInUser();
+    $this->createCaseRoleRelationship($individual, $loggedInUser, $caseId);
+    $this->assertCasesOfUser($loggedInUser, $caseId, 1);
+  }
+
+  /**
+   * Test that case count is zero for logged in user for user's inactive role.
+   */
+  public function testInactiveCaseRole() {
+    $individual = $this->individualCreate();
+    $caseObj = $this->createCase($individual);
+    $caseId = $caseObj->id;
+    $loggedInUser = $this->createLoggedInUser();
+    $this->createCaseRoleRelationship($individual, $loggedInUser, $caseId, FALSE);
+    $this->assertCasesOfUser($loggedInUser, $caseId, 0);
+  }
+
   public function testGetCaseType() {
     $caseTypeLabel = CRM_Case_BAO_Case::getCaseType(1);
     $this->assertEquals('Housing Support', $caseTypeLabel);