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)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 26 Apr 2017 04:31:38 +0000 (14:31 +1000)
CRM/Activity/BAO/Activity.php
tests/phpunit/CRM/Activity/BAO/ActivityTest.php

index b0693d6f7a5d3999f556a8178d7104e67d5c6c58..1c5f2d2b516ad6c72573a6b1ce0deebe57ef0c02 100644 (file)
@@ -676,24 +676,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 21de7b820248abf14b044e03ff97a9bf279d39b2..c1705cb2a07dd98a1885d3d88c66815238274ec7 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.
    */
@@ -770,4 +745,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,
+    );
+  }
+
 }