-- CRM-13889 APIs for Dashboard, DashboardContact and new hook hook_civicrm_dashboard...
authorNileema <nileema@nileema.(none)>
Tue, 31 Dec 2013 06:52:30 +0000 (12:22 +0530)
committerNileema <nileema@nileema.(none)>
Tue, 31 Dec 2013 06:52:30 +0000 (12:22 +0530)
----------------------------------------
* CRM-13889: Add APIs for Dashboard and DashboardContact
  http://issues.civicrm.org/jira/browse/CRM-13889

CRM/Core/BAO/Dashboard.php
CRM/Utils/Hook.php
api/v3/Dashboard.php [new file with mode: 0644]
api/v3/DashboardContact.php [new file with mode: 0644]
sql/civicrm_generated.mysql
tests/phpunit/api/v3/DashboardContactTest.php [new file with mode: 0644]
tests/phpunit/api/v3/DashboardTest.php [new file with mode: 0644]
tests/phpunit/api/v3/SyntaxConformanceTest.php

index c421f9f810e8eaee498e63faa93bf621fedb2b57..3e6f4b11c849c9f453442158bba784b9f8d8450a 100644 (file)
@@ -82,10 +82,12 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
    * @access public
    * @static
    */
-  static function getContactDashlets($flatFormat = FALSE) {
+  static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
     $dashlets = array();
 
-    $contactID = CRM_Core_Session::singleton()->get('userID');
+    if (!$contactID) {
+      $contactID = CRM_Core_Session::singleton()->get('userID');
+    }
 
     // get contact dashboard dashlets
     $hasDashlets = FALSE;
@@ -111,23 +113,42 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     }
 
     // If empty then initialize contact dashboard for this user
-    if (!$hasDashlets) {
-      $defaultDashlets = self::getDashlets();
-      if ($defaultDashlets) {
-        // Add dashlet entries for logged in contact
-        // TODO: need to optimize this sql
-        $items = '';
-        foreach ($defaultDashlets as $key => $values) {
-          // Set civicrm blog as default enabled
-          $default = $values['url'] == 'civicrm/dashlet/blog&reset=1&snippet=5' ? 1 : 0;
-          $items .= ($items ? ', ' : '') . "($key, $contactID, $default, $default)";
+    $defaultDashlet = self::initializeDashlets($hasDashlets);
+    return $defaultDashlet ? $defaultDashlet : $dashlets;
+  }
+
+  static function initializeDashlets($hasDashlets) {
+    $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config::domainID()));
+    $contactID = CRM_Core_Session::singleton()->get('userID');
+
+    $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']);
+    $defaultDashlets = array();
+    if (!$hasDashlets && CRM_Utils_Array::value('blog', $allDashlets)) {
+      $defaultDashlets['blog'] = array(
+        'dashboard_id' => $allDashlets['blog']['id'],
+        'is_active' => 1,
+        'column_no' => 1,
+        'contact_id' => $contactID,
+        'domain_id' => CRM_Core_Config::domainID(),
+      );
+    }
+    CRM_Utils_Hook::dashboard_defaults($allDashlets, $defaultDashlets);
+    if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
+      foreach ($defaultDashlets as $defaultDashlet) {
+        if (!self::checkPermission($getDashlets['values'][$defaultDashlet['dashboard_id']]['permission'],
+                                   $getDashlets['values'][$defaultDashlet['dashboard_id']]['permission_operator'])) {
+          unset($defaultDashlets[$defaultDashlet]);
+          continue;
+        }
+        else {
+          $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
+          $dashlets[$defaultDashlet['dashboard_id']] = $defaultDashlet['dashboard_id'];
         }
-        $query = "INSERT INTO civicrm_dashboard_contact (dashboard_id, contact_id, column_no, is_active) VALUES $items";
-        CRM_Core_DAO::executeQuery($query);
       }
+      return $dashlets;
     }
 
-    return $dashlets;
+    return FALSE;
   }
 
   /**
@@ -216,7 +237,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     $dashletInfo = array();
 
     $params = array(1 => array($dashletID, 'Integer'));
-    $query = "SELECT label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
+    $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
     $dashboadDAO = CRM_Core_DAO::executeQuery($query, $params);
     $dashboadDAO->fetch();
 
@@ -257,6 +278,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
 
     $dashletInfo = array(
       'title' => $dashboadDAO->label,
+      'name' => $dashboadDAO->name,
       'content' => $dao->content,
     );
 
@@ -280,12 +302,17 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
    * @access public
    * @static
    */
-  static function saveDashletChanges($columns) {
+  static function saveDashletChanges($columns, $contactID=NULL) {
+    $session = CRM_Core_Session::singleton();
+    if (!$contactID) {
+      $contactID = $session->get('userID');
+    }
+
     $session = CRM_Core_Session::singleton();
     $contactID = $session->get('userID');
 
     //we need to get existing dashletes, so we know when to update or insert
-    $contactDashlets = self::getContactDashlets(TRUE);
+    $contactDashlets = self::getContactDashlets(TRUE, $contactID);
 
     $dashletIDs = array();
     if (is_array($columns)) {
@@ -343,7 +370,8 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
   static function addDashlet(&$params) {
 
     // special case to handle duplicate entires for report instances
-    $dashboardID = NULL;
+    $dashboardID = CRM_Utils_Array::value('id', $params);
+
     if (CRM_Utils_Array::value('instanceURL', $params)) {
       $query = "SELECT id
                         FROM `civicrm_dashboard`
@@ -355,9 +383,14 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
 
     if (!$dashboardID) {
       // check url is same as exiting entries, if yes just update existing
-      $dashlet->url = CRM_Utils_Array::value('url', $params);
-      $dashlet->find(TRUE);
-      $dashlet->name = self::getDashletName(CRM_Utils_Array::value('instanceURL', $params));
+      if (CRM_Utils_Array::value('name', $params)) {
+        $dashlet->name = CRM_Utils_Array::value('name', $params);
+        $dashlet->find(TRUE);
+      }
+      else {
+        $dashlet->url = CRM_Utils_Array::value('url', $params);
+        $dashlet->find(TRUE);
+      }
     }
     else {
       $dashlet->id = $dashboardID;
@@ -435,6 +468,19 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     }
   }
 
+  static function addContactDashletToDashboard(&$params) {
+    $valuesString = NULL;
+    $columns = array();
+    foreach ($params as $dashboardIDs) {
+      $contactID = CRM_Utils_Array::value('contact_id', $dashboardIDs);
+      $dashboardID = CRM_Utils_Array::value('dashboard_id', $dashboardIDs);
+      $column = CRM_Utils_Array::value('column_no', $dashboardIDs, 0);
+      $columns[$column][$dashboardID] = 0;
+    }
+    self::saveDashletChanges($columns, $contactID);
+    return TRUE;
+  }
+
   /**
    * Function to reset dashlet cache
    *
@@ -464,6 +510,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
     $dashlet = new CRM_Core_DAO_Dashboard();
     $dashlet->id = $dashletID;
     $dashlet->delete();
+    return TRUE;
   }
 }
 
index fbb789ce3661dda7d695fd7014744dc988c0229b..bed5f0dfcdf5a2240b7396dc31d751759bd0a00c 100644 (file)
@@ -1388,4 +1388,14 @@ abstract class CRM_Utils_Hook {
   static function queryObjects(&$queryObjects, $type = 'Contact') {
     return self::singleton()->invoke(2, $queryObjects, $type, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_queryObjects');
   }
+
+  /**
+   * This hook is called while viewing contact dashboard
+   *
+   * @param array $availableDashlets list of dashlets; each is formatted per api/v3/Dashboard
+   * @param array $activeDashlets list of dashlets; each is formatted per api/v3/DashboardContact
+   */
+  static function dashboard_defaults($availableDashlets, &$defaultDashlets) {
+    return self::singleton()->invoke(2, $availableDashlets, $defaultDashlets, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_dashboard_defaults');
+  }
 }
diff --git a/api/v3/Dashboard.php b/api/v3/Dashboard.php
new file mode 100644 (file)
index 0000000..ae42779
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+// $Id$
+
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * File for the CiviCRM APIv3 Dashboard functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Activity
+ * @copyright CiviCRM LLC (c) 2004-2013
+ * @version $Id: Activity.php 30486 2010-11-02 16:12:09Z shot $
+ *
+ */
+
+
+/**
+ * Creates or updates an Dashlet.
+ *
+ * @param array  $params       Associative array of property name/value
+ *                             pairs for the Dashlet.
+ *
+ * @return array Array containing 'is_error' to denote success or failure and details of the created activity
+ *
+ */
+function civicrm_api3_dashboard_create($params) {
+  if (!CRM_Utils_Array::value('id', $params)) {
+    civicrm_api3_verify_one_mandatory($params,
+      NULL,
+      array(
+        'name', 'label', 'url', 'fullscreen_url',
+      )
+    );
+  }
+  // create dashboard element
+  $dashboardBAO = CRM_Core_BAO_Dashboard::addDashlet($params);
+  if (isset($dashboardBAO->id)) {
+    _civicrm_api3_object_to_array($dashboardBAO, $dashboardArray[$dashboardBAO->id]);
+    return civicrm_api3_create_success($dashboardArray, $params, 'dashboard', 'create', $dashboardBAO);
+  }
+}
+
+/**
+ * Specify Meta data for create. Note that this data is retrievable via the getfields function
+ * and is used for pre-filling defaults and ensuring mandatory requirements are met.
+ * @param array $params (reference) array of parameters determined by getfields
+ */
+function _civicrm_api3_dashboard_create_spec(&$params) {
+  unset($params['version']);
+}
+
+/**
+ * Gets a CiviCRM Dashlets according to parameters
+ *
+ * @param array  $params       Associative array of property name/value
+ *                             pairs for the activity.
+ *
+ * @return array
+ *
+ */
+function civicrm_api3_dashboard_get($params) {
+  $bao = new CRM_Core_BAO_Dashboard();
+  _civicrm_api3_dao_set_filter($bao, $params, true, 'Dashboard');
+  $dashlets = _civicrm_api3_dao_to_array($bao, $params, true,'Dashboard');
+  return civicrm_api3_create_success($dashlets, $params, 'dashboard', 'get', $bao);
+}
+
+/**
+ * Delete a specified Dashlet.
+ *
+ * @param array $params array holding 'id' OR 'name' of dashlet to be deleted
+ *
+ * @return void|CRM_Core_Error  An error if 'name or ID' is invalid,
+ *
+ */
+function civicrm_api3_dashboard_delete($params) {
+  if (CRM_Core_BAO_Dashboard::deleteDashlet($params['id'])) {
+    return civicrm_api3_create_success(1, $params, 'dashboard', 'delete');
+  }
+  else {
+    return civicrm_api3_create_error('Could not delete dashlet');
+  }
+}
\ No newline at end of file
diff --git a/api/v3/DashboardContact.php b/api/v3/DashboardContact.php
new file mode 100644 (file)
index 0000000..280420f
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ * File for the CiviCRM APIv3 for Dashboard Contact
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_ActionSchedule
+ *
+ * @copyright CiviCRM LLC (c) 2004-2013
+ *
+ */
+
+/**
+ * Creates/Updates a new Dashboard Contact Entry
+ *
+ * @param array $params
+ *
+ * @return array
+ * 
+ */
+function civicrm_api3_dashboard_contact_create($params) {
+  civicrm_api3_verify_one_mandatory($params,
+    NULL,
+    array(
+      'dashboard_id',
+    )
+  );
+  $errors = _civicrm_api3_dashboard_contact_check_params($params);
+  $dashboard[] = $params;
+  $dashbordContact = CRM_Core_BAO_Dashboard::addContactDashletToDashboard($dashboard);
+  return civicrm_api3_create_success(TRUE);
+}
+
+/**
+ * Gets a CiviCRM Dashlets of Contacts according to parameters
+ *
+ * @param array  $params       Associative array of property name/value
+ *                             pairs for the activity.
+ *
+ * @return array
+ *
+ */
+function civicrm_api3_dashboard_contact_get($params) {
+  civicrm_api3_verify_one_mandatory($params,
+    NULL,
+    array(
+      'contact_id',
+    )
+  );
+  $bao = new CRM_Core_BAO_Dashboard();
+  $dashboardContact = CRM_Core_BAO_Dashboard::getContactDashlets(TRUE, CRM_Utils_Array::value('contact_id',$params));
+  return civicrm_api3_create_success($dashboardContact, $params, 'dashboard_contact', 'get');
+}
+
+/**
+ * Adjust Metadata for Create action
+ *
+ * The metadata is used for setting defaults, documentation & validation
+ * @param array $params array or parameters determined by getfields
+ */
+function _civicrm_api3_dashboard_contact_create_spec(&$params) {
+  unset($params['version']);
+}
+
+function _civicrm_api3_dashboard_contact_check_params(&$params) {
+  $dashboard_id = CRM_Utils_Array::value('dashboard_id', $params);
+  $allDashlets = CRM_Core_BAO_Dashboard::getDashlets();
+  if (!in_array($dashboard_id, $allDashlets)) {
+    return civicrm_api3_create_error('Invalid Dashboard ID');
+  }
+  return null;
+}
\ No newline at end of file
index 2084e31f1f763c341f64f1cf5f3c057a90d7ff96..0bcb37b2c3fc78669921c6c5d564b11c0418adee 100644 (file)
@@ -325,7 +325,7 @@ UNLOCK TABLES;
 
 LOCK TABLES `civicrm_dashboard` WRITE;
 /*!40000 ALTER TABLE `civicrm_dashboard` DISABLE KEYS */;
-INSERT INTO `civicrm_dashboard` (`id`, `domain_id`, `label`, `url`, `permission`, `permission_operator`, `column_no`, `is_minimized`, `fullscreen_url`, `is_fullscreen`, `is_active`, `is_reserved`, `weight`) VALUES (1,1,'CiviCRM News','civicrm/dashlet/blog&reset=1&snippet=5','access CiviCRM',NULL,0,0,'civicrm/dashlet/blog&reset=1&snippet=5&context=dashletFullscreen',1,1,1,0),(2,1,'Activities','civicrm/dashlet/activity&reset=1&snippet=5','access CiviCRM',NULL,0,0,'civicrm/dashlet/activity&reset=1&snippet=5&context=dashletFullscreen',1,1,1,1),(3,1,'My Cases','civicrm/dashlet/myCases&reset=1&snippet=5','access my cases and activities',NULL,0,0,'civicrm/dashlet/myCases&reset=1&snippet=5&context=dashletFullscreen',1,1,1,2),(4,1,'All Cases','civicrm/dashlet/allCases&reset=1&snippet=5','access all cases and activities',NULL,0,0,'civicrm/dashlet/allCases&reset=1&snippet=5&context=dashletFullscreen',1,1,1,3),(5,1,'Case Dashboard Dashlet','civicrm/dashlet/casedashboard&reset=1&snippet=5','access CiviCase',NULL,0,0,'civicrm/dashlet/casedashboard&reset=1&snippet=5&context=dashletFullscreen',1,1,1,4),(6,1,'Donor Summary','civicrm/report/instance/6&reset=1&section=1&snippet=5&charts=barChart','access CiviContribute','AND',0,0,'civicrm/report/instance/6&reset=1&section=1&snippet=5&charts=barChart&context=dashletFullscreen',1,1,0,4),(7,1,'Top Donors','civicrm/report/instance/13&reset=1&section=2&snippet=5','access CiviContribute','AND',0,0,'civicrm/report/instance/13&reset=1&section=2&snippet=5&context=dashletFullscreen',1,1,0,5),(8,1,'Event Income Summary','civicrm/report/instance/25&reset=1&section=1&snippet=5&charts=pieChart','access CiviEvent','AND',0,0,'civicrm/report/instance/25&reset=1&section=1&snippet=5&charts=pieChart&context=dashletFullscreen',1,1,0,6),(9,1,'Membership Summary','civicrm/report/instance/20&reset=1&section=2&snippet=5','access CiviMember','AND',0,0,'civicrm/report/instance/20&reset=1&section=2&snippet=5&context=dashletFullscreen',1,1,0,7);
+INSERT INTO `civicrm_dashboard` (`id`, `domain_id`, `name`, `label`, `url`, `permission`, `permission_operator`, `column_no`, `is_minimized`, `fullscreen_url`, `is_fullscreen`, `is_active`, `is_reserved`, `weight`) VALUES (1,1,'blog','CiviCRM News','civicrm/dashlet/blog&reset=1&snippet=5','access CiviCRM',NULL,0,0,'civicrm/dashlet/blog&reset=1&snippet=5&context=dashletFullscreen',1,1,1,0),(2,1,'activity','Activities','civicrm/dashlet/activity&reset=1&snippet=5','access CiviCRM',NULL,0,0,'civicrm/dashlet/activity&reset=1&snippet=5&context=dashletFullscreen',1,1,1,1),(3,1,'myCases','My Cases','civicrm/dashlet/myCases&reset=1&snippet=5','access my cases and activities',NULL,0,0,'civicrm/dashlet/myCases&reset=1&snippet=5&context=dashletFullscreen',1,1,1,2),(4,1,'allCases','All Cases','civicrm/dashlet/allCases&reset=1&snippet=5','access all cases and activities',NULL,0,0,'civicrm/dashlet/allCases&reset=1&snippet=5&context=dashletFullscreen',1,1,1,3),(5,1,'casedashboard','Case Dashboard Dashlet','civicrm/dashlet/casedashboard&reset=1&snippet=5','access CiviCase',NULL,0,0,'civicrm/dashlet/casedashboard&reset=1&snippet=5&context=dashletFullscreen',1,1,1,4),(6,1,'report/6','Donor Summary','civicrm/report/instance/6&reset=1&section=1&snippet=5&charts=barChart','access CiviContribute','AND',0,0,'civicrm/report/instance/6&reset=1&section=1&snippet=5&charts=barChart&context=dashletFullscreen',1,1,0,4),(7,1,'report/13','Top Donors','civicrm/report/instance/13&reset=1&section=2&snippet=5','access CiviContribute','AND',0,0,'civicrm/report/instance/13&reset=1&section=2&snippet=5&context=dashletFullscreen',1,1,0,5),(8,1,'report/25','Event Income Summary','civicrm/report/instance/25&reset=1&section=1&snippet=5&charts=pieChart','access CiviEvent','AND',0,0,'civicrm/report/instance/25&reset=1&section=1&snippet=5&charts=pieChart&context=dashletFullscreen',1,1,0,6),(9,1,'report/20','Membership Summary','civicrm/report/instance/20&reset=1&section=2&snippet=5','access CiviMember','AND',0,0,'civicrm/report/instance/20&reset=1&section=2&snippet=5&context=dashletFullscreen',1,1,0,7);
 /*!40000 ALTER TABLE `civicrm_dashboard` ENABLE KEYS */;
 UNLOCK TABLES;
 
diff --git a/tests/phpunit/api/v3/DashboardContactTest.php b/tests/phpunit/api/v3/DashboardContactTest.php
new file mode 100644 (file)
index 0000000..012a98e
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**
+ *  File for the TestActionSchedule class
+ *
+ *  (PHP 5)
+ *
+ *   CiviCRM is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU Affero General Public License
+ *   as published by the Free Software Foundation; either version 3 of
+ *   the License, or (at your option) any later version.
+ *
+ *   CiviCRM is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public
+ *   License along with this program.  If not, see
+ *   <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ *  Include class definitions
+ */
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ *  Test APIv3 civicrm_action_schedule functions
+ *
+ *  @package CiviCRM_APIv3
+ *  @subpackage API_ActionSchedule
+ */
+
+class api_v3_DashboardContactTest extends CiviUnitTestCase {
+  protected $_params;
+  protected $_params2;
+  protected $_entity = 'dashborad_contact';
+  protected $_apiversion = 3;
+
+  public $_eNoticeCompliant = TRUE;
+  /**
+   *  Test setup for every test
+   *
+   *  Connect to the database, truncate the tables that will be used
+   *  and redirect stdin to a temporary file
+   */
+  public function setUp() {
+    //  Connect to the database
+    parent::setUp();
+  }
+
+  /**
+   * Tears down the fixture, for example, closes a network connection.
+   * This method is called after a test is executed.
+   *
+   * @access protected
+   */
+  function tearDown() {
+    $tablesToTruncate = array(
+      'civicrm_dashboard',
+      'civicrm_dashboard_contact',
+    );
+    $this->quickCleanup($tablesToTruncate, TRUE);
+  }
+  
+  function testDashboardContactCreate() {
+    $dashParams = array(
+      'version' => 3,
+      'label' => 'New Dashlet element',
+      'name' => 'New Dashlet element',
+      'url' => 'civicrm/report/list&compid=99&reset=1&snippet=5',
+      'fullscreen_url' => 'civicrm/report/list&compid=99&reset=1&snippet=5&context=dashletFullscreen',
+    );
+       $dashresult = $this->callAPISuccess('dashboard', 'create', $dashParams);
+       $contact = $this->callAPISuccess('contact', 'create', array( 'first_name' => 'abc1',
+      'contact_type' => 'Individual',
+      'last_name' => 'xyz1',
+      'email' => 'abc@abc.com')
+     );
+       $oldCount = CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_dashboard_contact where contact_id = {$contact['id']} AND is_active = 1 AND dashboard_id = {$dashresult['id']}");
+    $params = array(
+      'version' => 3,
+      'contact_id' => $contact['id'],
+      'dashboard_id' => $dashresult['id'],
+      'is_active' => 1,
+    );
+       $dashboradContact = $this->callAPISuccess('dashboard_contact', 'create', $params);
+       $newCount = CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_dashboard_contact where contact_id = {$contact['id']} AND is_active = 1 AND dashboard_id = {$dashresult['id']}");
+       $this->assertEquals($oldCount+1, $newCount);
+  }
+}
\ No newline at end of file
diff --git a/tests/phpunit/api/v3/DashboardTest.php b/tests/phpunit/api/v3/DashboardTest.php
new file mode 100644 (file)
index 0000000..baba27c
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ *  File for the TestActionSchedule class
+ *
+ *  (PHP 5)
+ *
+ *   CiviCRM is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU Affero General Public License
+ *   as published by the Free Software Foundation; either version 3 of
+ *   the License, or (at your option) any later version.
+ *
+ *   CiviCRM is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public
+ *   License along with this program.  If not, see
+ *   <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ *  Include class definitions
+ */
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ *  Test APIv3 civicrm_action_schedule functions
+ *
+ *  @package CiviCRM_APIv3
+ *  @subpackage API_ActionSchedule
+ */
+
+class api_v3_DashboardTest extends CiviUnitTestCase {
+  protected $_params;
+  protected $_params2;
+  protected $_entity = 'dashboard';
+  protected $_apiversion = 3;
+
+  public $_eNoticeCompliant = TRUE;
+  /**
+   *  Test setup for every test
+   *
+   *  Connect to the database, truncate the tables that will be used
+   *  and redirect stdin to a temporary file
+  */
+  public function setUp() {
+   //  Connect to the database
+    parent::setUp();
+  }
+
+  /**
+   * Tears down the fixture, for example, closes a network connection.
+   * This method is called after a test is executed.
+   *
+   * @access protected
+   */
+  function tearDown() {
+    $tablesToTruncate = array(
+      'civicrm_dashboard',
+    );
+    $this->quickCleanup($tablesToTruncate, TRUE);
+  }
+
+  function testDashboardCreate() {
+       $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
+    $params = array(
+      'version' => 3,
+      'label' => 'New Dashlet element',
+      'name' => 'New Dashlet element',
+      'url' => 'civicrm/report/list&reset=1&compid=99&snippet=5',
+      'fullscreen_url' => 'civicrm/report/list&compid=99&reset=1&snippet=5&context=dashletFullscreen',
+    );
+    $dashboard = $this->callAPISuccess('dashboard', 'create', $params);
+    $this->assertTrue(is_numeric($dashboard['id']), "In line " . __LINE__);
+    $this->assertTrue($dashboard['id'] > 0, "In line " . __LINE__);
+    $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
+    $this->assertEquals($oldCount+1, $newCount);
+    $this->DashboardDelete($dashboard['id'],$oldCount);
+  }
+
+  function DashboardDelete($id, $oldCount) {
+    $params = array(
+      'version' => 3,
+      'id' => $id,
+    );
+    $dashboardget = $this->callAPISuccess('dashboard', 'get', $params);
+    $this->assertEquals($id, $dashboardget['id']);
+    $dashboard = $this->callAPISuccess('dashboard', 'delete', $params);
+    $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
+    $this->assertEquals($oldCount, $newCount);
+  }
+}
\ No newline at end of file
index c7631f63f0ad8686e7d1b3143ced1be1ffd98e63..38ac526470f16f6290a5efb532887f00eb9cf84f 100644 (file)
@@ -156,7 +156,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
  * @return multitype:string |multitype:multitype:string
  */
   public static function toBeSkipped_automock($sequential = FALSE) {
-    $entitiesWithoutGet = array('MailingContact', 'EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients',  'CustomSearch', 'Extension', 'ReportTemplate', 'System');
+    $entitiesWithoutGet = array('MailingContact', 'EntityTag', 'Participant', 'ParticipantPayment', 'Setting', 'SurveyRespondant', 'MailingRecipients',  'CustomSearch', 'Extension', 'ReportTemplate', 'System', 'DashboardContact');
     if ($sequential === TRUE) {
       return $entitiesWithoutGet;
     }
@@ -235,6 +235,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       'MailSettings',
       'Setting',
       'MailingContact',
+      'DashboardContact',
     );
     if ($sequential === TRUE) {
       return $entitiesWithout;