CRM-20441 add test for no activities viewable and add test fix
authoreileen <emcnaughton@wikimedia.org>
Tue, 25 Apr 2017 22:25:37 +0000 (10:25 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 25 Apr 2017 22:56:30 +0000 (10:56 +1200)
CRM/Activity/BAO/Activity.php
tests/phpunit/CRM/Activity/BAO/ActivityTest.php

index 885a47be9197f618f70b2dd8d17bcb7a394ac2b0..f21a06988c58ed14ac4c1dab0ae904794ca89747 100644 (file)
@@ -677,24 +677,23 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
         return $getCount ? count($activities) : $activities;
       }
       $activityIDs = explode(',', $activityIDs);
-    }
-
-    // CRM-20441 Check if user has access to the activities.
-    // This is a temporary fix we need to figure out the rules around
-    // the right permissions to access Activities.
-    // This attpemts to reduce fatal errors in 4.7.19 RC.
-    if (!empty($activityIDs)) {
-      foreach ($activityIDs as $key => $activityId) {
-        try {
-          civicrm_api3('Activity', 'get', array('id' => $activityId, 'check_permissions' => 1));
-        }
-        catch (Exception $e) {
-          unset($activityIDs[$key]);
+      // CRM-20441 Check if user has access to the activities.
+      // This is a temporary fix we need to figure out the rules around
+      // the right permissions to access Activities.
+      // This attempts to reduce fatal errors in 4.7.19 RC.
+      if (!empty($activityIDs)) {
+        foreach ($activityIDs as $key => $activityId) {
+          try {
+            civicrm_api3('Activity', 'get', array('id' => $activityId, 'check_permissions' => 1));
+          }
+          catch (Exception $e) {
+            unset($activityIDs[$key]);
+          }
         }
       }
-    }
-    if (empty($activityIDs)) {
-      return $getCount ? count($activities) : $activities;
+      if (empty($activityIDs)) {
+        return $getCount ? count($activities) : $activities;
+      }
     }
 
     // fetch all active activity types
index ea42c1f0517f4f94cce9f73bb0de9adce070831a..cfae9c815945bdbd977af4689c3ada312293fbae 100644 (file)
@@ -280,29 +280,9 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
    * Test getActivities BAO method for getting count.
    */
   public function testGetActivitiesCountForAdminDashboard() {
-    $op = new PHPUnit_Extensions_Database_Operation_Insert();
-    $op->execute($this->_dbconn,
-      $this->createFlatXMLDataSet(
-        dirname(__FILE__) . '/activities_for_dashboard_count.xml'
-      )
-    );
-
-    $params = array(
-      'contact_id' => NULL,
-      'admin' => TRUE,
-      'caseId' => NULL,
-      'context' => 'home',
-      'activity_type_id' => NULL,
-      'offset' => 0,
-      'rowCount' => 0,
-      'sort' => NULL,
-    );
-    $activityCount = CRM_Activity_BAO_Activity::getActivities($params, TRUE);
-
-    //since we are loading activities from dataset, we know total number of activities
-    // 8 schedule activities that should be shown on dashboard
-    $count = 8;
-    $this->assertEquals($count, $activityCount);
+    $this->setUpForActivityDashboardTests();
+    $activityCount = CRM_Activity_BAO_Activity::getActivities($this->_params, TRUE);
+    $this->assertEquals(8, $activityCount);
   }
 
   /**
@@ -431,24 +411,8 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
    * Test getActivities BAO method.
    */
   public function testGetActivitiesForAdminDashboard() {
-    $op = new PHPUnit_Extensions_Database_Operation_Insert();
-    $op->execute($this->_dbconn,
-      $this->createFlatXMLDataSet(
-        dirname(__FILE__) . '/activities_for_dashboard_count.xml'
-      )
-    );
-
-    $params = array(
-      'contact_id' => NULL,
-      'admin' => TRUE,
-      'caseId' => NULL,
-      'context' => 'home',
-      'activity_type_id' => NULL,
-      'offset' => 0,
-      'rowCount' => 0,
-      'sort' => NULL,
-    );
-    $activities = CRM_Activity_BAO_Activity::getActivities($params);
+    $this->setUpForActivityDashboardTests();
+    $activities = CRM_Activity_BAO_Activity::getActivities($this->_params);
 
     //since we are loading activities from dataset, we know total number of activities
     // with no contact ID and there should be 8 schedule activities shown on dashboard
@@ -462,6 +426,17 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test getActivities BAO method.
+   */
+  public function testGetActivitiesForAdminDashboardNoViewContacts() {
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
+    $this->setUpForActivityDashboardTests();
+    $activities = CRM_Activity_BAO_Activity::getActivities($this->_params);
+    $this->assertEquals(0, count($activities));
+
+  }
+
   /**
    * Test getActivities BAO method.
    */
@@ -772,4 +747,27 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
     //  to change the domain setting, which isn't straight forward in test environment
   }
 
+  /**
+   * Set up for testing activity queries.
+   */
+  protected function setUpForActivityDashboardTests() {
+    $op = new PHPUnit_Extensions_Database_Operation_Insert();
+    $op->execute($this->_dbconn,
+      $this->createFlatXMLDataSet(
+        dirname(__FILE__) . '/activities_for_dashboard_count.xml'
+      )
+    );
+
+    $this->_params = array(
+      'contact_id' => NULL,
+      'admin' => TRUE,
+      'caseId' => NULL,
+      'context' => 'home',
+      'activity_type_id' => NULL,
+      'offset' => 0,
+      'rowCount' => 0,
+      'sort' => NULL,
+    );
+  }
+
 }