Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / SystemCheckTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * System.check API has many special test cases, so they have their own class.
14 *
15 * We presume that in a test environment, checkDefaultMailbox and
16 * checkDomainNameEmail always fail with a warning, and checkLastCron fails with
17 * an error.
18 *
19 * @package CiviCRM_APIv3
20 * @group headless
21 */
22 class api_v3_SystemCheckTest extends CiviUnitTestCase {
23
24 public function setUp() {
25 parent::setUp();
26 $this->useTransaction(TRUE);
27 }
28
29 /**
30 * Ensure that without any StatusPreference set, checkDefaultMailbox shows up.
31 *
32 * @param int $version
33 *
34 * @dataProvider versionThreeAndFour
35 *
36 * @throws \CRM_Core_Exception
37 */
38 public function testSystemCheckBasic($version) {
39 $this->_apiversion = $version;
40 $this->runStatusCheck([], ['severity_id' => 3]);
41 }
42
43 /**
44 * Permanently hushed items should never show up.
45 *
46 * @param int $version
47 *
48 * @dataProvider versionThreeAndFour
49 * @throws \CRM_Core_Exception
50 */
51 public function testSystemCheckHushForever($version) {
52 $this->_apiversion = $version;
53 $this->runStatusCheck([
54 'name' => 'checkDefaultMailbox',
55 'ignore_severity' => 7,
56 ], ['is_visible' => 0]);
57 }
58
59 /**
60 * Disabled items should never show up.
61 *
62 * @param int $version
63 *
64 * @dataProvider versionThreeAndFour
65 *
66 * @throws \CRM_Core_Exception
67 */
68 public function testIsInactive($version) {
69 $this->_apiversion = $version;
70 $this->callAPISuccess('StatusPreference', 'create', [
71 'name' => 'checkDefaultMailbox',
72 'is_active' => 0,
73 ]);
74 $result = $this->callAPISuccess('System', 'check', [])['values'];
75 foreach ($result as $check) {
76 if ($check['name'] === 'checkDefaultMailbox') {
77 $this->fail('Check should have been skipped');
78 }
79 }
80 }
81
82 /**
83 * Items hushed through tomorrow shouldn't show up.
84 *
85 * @param int $version
86 *
87 * @dataProvider versionThreeAndFour
88 * @throws \Exception
89 */
90 public function testSystemCheckHushFuture($version) {
91 $this->_apiversion = $version;
92 $tomorrow = new DateTime('tomorrow');
93 $this->runStatusCheck([
94 'name' => 'checkDefaultMailbox',
95 'ignore_severity' => 7,
96 'hush_until' => $tomorrow->format('Y-m-d'),
97 ], ['is_visible' => 0]);
98 }
99
100 /**
101 * Items hushed through today should show up.
102 *
103 * @param int $version
104 *
105 * @dataProvider versionThreeAndFour
106 * @throws \CRM_Core_Exception
107 */
108 public function testSystemCheckHushToday($version) {
109 $this->_apiversion = $version;
110 $today = new DateTime('today');
111 $this->runStatusCheck([
112 'name' => 'checkDefaultMailbox',
113 'ignore_severity' => 7,
114 'hush_until' => $today->format('Y-m-d'),
115 ], ['is_visible' => 1]);
116 }
117
118 /**
119 * Items hushed through yesterday should show up.
120 *
121 * @param int $version
122 *
123 * @dataProvider versionThreeAndFour
124 *
125 * @throws \CRM_Core_Exception
126 */
127 public function testSystemCheckHushYesterday($version) {
128 $this->_apiversion = $version;
129 $yesterday = new DateTime('yesterday');
130 $this->runStatusCheck([
131 'name' => 'checkDefaultMailbox',
132 'ignore_severity' => 7,
133 'hush_until' => $yesterday->format('Y-m-d'),
134 ], ['is_visible' => 1]);
135 }
136
137 /**
138 * Items hushed above current severity should be hidden.
139 *
140 * @param int $version
141 *
142 * @dataProvider versionThreeAndFour
143 *
144 * @throws \CRM_Core_Exception
145 */
146 public function testSystemCheckHushAboveSeverity($version) {
147 $this->_apiversion = $version;
148 $this->runStatusCheck([
149 'name' => 'checkDefaultMailbox',
150 'ignore_severity' => 4,
151 ], ['is_visible' => 0]);
152 }
153
154 /**
155 * Items hushed at current severity should be hidden.
156 *
157 * @param int $version
158 *
159 * @dataProvider versionThreeAndFour
160 *
161 * @throws \CRM_Core_Exception
162 */
163 public function testSystemCheckHushAtSeverity($version) {
164 $this->_apiversion = $version;
165 $this->runStatusCheck([
166 'name' => 'checkDefaultMailbox',
167 'ignore_severity' => 3,
168 ], ['is_visible' => 0]);
169 }
170
171 /**
172 * Items hushed below current severity should be shown.
173 *
174 * @param int $version
175 *
176 * @dataProvider versionThreeAndFour
177 * @throws \CRM_Core_Exception
178 */
179 public function testSystemCheckHushBelowSeverity($version) {
180 $this->_apiversion = $version;
181 $this->runStatusCheck([
182 'name' => 'checkDefaultMailbox',
183 'ignore_severity' => 2,
184 ], ['is_visible' => 1]);
185 }
186
187 /**
188 * Run check and assert result is as expected.
189 *
190 * @param array $params
191 * Values to update the status check with.
192 * @param array $expected
193 *
194 * @throws \CRM_Core_Exception
195 */
196 protected function runStatusCheck($params, $expected) {
197 if (!empty($params)) {
198 $this->callAPISuccess('StatusPreference', 'create', $params);
199 }
200 $result = $this->callAPISuccess('System', 'check', []);
201 foreach ($result['values'] as $check) {
202 if ($check['name'] === 'checkDefaultMailbox') {
203 foreach ($expected as $key => $value) {
204 $this->assertEquals($check[$key], $value);
205 }
206 return;
207 }
208 }
209 throw new CRM_Core_Exception('checkDefaultMailbox not in results');
210 }
211
212 }