Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / DashboardTest.php
1 <?php
2 /**
3 * File for the TestActionSchedule class
4 *
5 * (PHP 5)
6 *
7 * CiviCRM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation; either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * CiviCRM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 /**
23 * Test APIv3 civicrm_action_schedule functions
24 *
25 * @package CiviCRM_APIv3
26 * @subpackage API_ActionSchedule
27 * @group headless
28 */
29 class api_v3_DashboardTest extends CiviUnitTestCase {
30 protected $_params;
31 protected $_params2;
32 protected $_entity = 'dashboard';
33 protected $_apiversion = 3;
34
35 /**
36 * Test setup for every test.
37 *
38 * Connect to the database, truncate the tables that will be used
39 * and redirect stdin to a temporary file
40 */
41 public function setUp() {
42 // Connect to the database
43 parent::setUp();
44 $this->useTransaction(TRUE);
45 }
46
47 /**
48 * @param int $version
49 * @dataProvider versionThreeAndFour
50 */
51 public function testDashboardCreate($version) {
52 $this->_apiversion = $version;
53 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
54 $params = [
55 'label' => 'New Dashlet element',
56 'name' => 'New Dashlet element',
57 'url' => 'civicrm/report/list&reset=1&compid=99',
58 'fullscreen_url' => 'civicrm/report/list&compid=99&reset=1&context=dashletFullscreen',
59 ];
60 $dashboard = $this->callAPISuccess('dashboard', 'create', $params);
61 $this->assertTrue(is_numeric($dashboard['id']), "In line " . __LINE__);
62 $this->assertTrue($dashboard['id'] > 0, "In line " . __LINE__);
63 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
64 $this->assertEquals($oldCount + 1, $newCount);
65 $this->DashboardDelete($dashboard['id'], $oldCount);
66 $this->assertEquals($dashboard['values'][$dashboard['id']]['is_active'], 1);
67 }
68
69 /**
70 * CRM-19534. Ensure that Dashboard create works fine for non admins
71 * @param int $version
72 * @dataProvider versionThreeAndFour
73 */
74 public function testDashboardCreateByNonAdmins($version) {
75 $this->_apiversion = $version;
76 $loggedInContactID = $this->createLoggedInUser();
77 CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
78 $params = [
79 'label' => 'New Dashlet element',
80 'name' => 'New Dashlet element',
81 'url' => 'civicrm/report/list&reset=1&compid=99',
82 'fullscreen_url' => 'civicrm/report/list&compid=99&reset=1&context=dashletFullscreen',
83 ];
84 $dashboard = $this->callAPISuccess('dashboard', 'create', $params);
85 $this->assertTrue(is_numeric($dashboard['id']), "In line " . __LINE__);
86 $this->assertTrue($dashboard['id'] > 0, "In line " . __LINE__);
87
88 $this->callAPISuccess('dashboard', 'create', $params);
89 $this->assertEquals($dashboard['values'][$dashboard['id']]['is_active'], 1);
90 }
91
92 /**
93 * CRM-19217. Ensure that where is_active is specifically set to 0 is_active returns 0.
94 * @param int $version
95 * @dataProvider versionThreeAndFour
96 */
97 public function testDashboardCreateNotActive($version) {
98 $this->_apiversion = $version;
99 $params = [
100 'label' => 'New Dashlet element',
101 'name' => 'New Dashlet element',
102 'url' => 'civicrm/report/list&reset=1&compid=99&snippet=5',
103 'fullscreen_url' => 'civicrm/report/list&compid=99&reset=1&snippet=5&context=dashletFullscreen',
104 'is_active' => 0,
105 ];
106 $dashboard = $this->callAPISuccess('dashboard', 'create', $params);
107 $this->assertEquals($dashboard['values'][$dashboard['id']]['is_active'], 0);
108 }
109
110 /**
111 * @param int $id
112 * @param $oldCount
113 */
114 public function DashboardDelete($id, $oldCount) {
115 $params = [
116 'id' => $id,
117 ];
118 $dashboardget = $this->callAPISuccess('dashboard', 'get', $params);
119 $this->assertEquals($id, $dashboardget['id']);
120 $dashboard = $this->callAPISuccess('dashboard', 'delete', $params);
121 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
122 $this->assertEquals($oldCount, $newCount);
123 }
124
125 }