Merge pull request #17037 from eileenmcnaughton/dupe
[civicrm-core.git] / tests / phpunit / api / v3 / SystemCheckTest.php
CommitLineData
8ca6be76
J
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
8ca6be76 5 | |
7d61e75f
TO
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 |
8ca6be76
J
9 +--------------------------------------------------------------------+
10 */
11
8ca6be76
J
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
acb109b7 20 * @group headless
8ca6be76
J
21 */
22class api_v3_SystemCheckTest extends CiviUnitTestCase {
8ca6be76 23
8ca6be76 24 public function setUp() {
8ca6be76
J
25 parent::setUp();
26 $this->useTransaction(TRUE);
27 }
28
29 /**
2d932085 30 * Ensure that without any StatusPreference set, checkDefaultMailbox shows up.
b31fd595 31 *
2d932085 32 * @param int $version
b31fd595 33 *
2d932085 34 * @dataProvider versionThreeAndFour
b31fd595 35 *
36 * @throws \CRM_Core_Exception
8ca6be76 37 */
2d932085
CW
38 public function testSystemCheckBasic($version) {
39 $this->_apiversion = $version;
b31fd595 40 $this->runStatusCheck([], ['severity_id' => 3]);
8ca6be76
J
41 }
42
c77f49b7 43 /**
afa33a28 44 * Permanently hushed items should never show up.
b31fd595 45 *
2d932085 46 * @param int $version
b31fd595 47 *
2d932085 48 * @dataProvider versionThreeAndFour
b31fd595 49 * @throws \CRM_Core_Exception
c77f49b7 50 */
2d932085
CW
51 public function testSystemCheckHushForever($version) {
52 $this->_apiversion = $version;
b31fd595 53 $this->runStatusCheck([
8ca6be76 54 'name' => 'checkDefaultMailbox',
afa33a28 55 'ignore_severity' => 7,
b31fd595 56 ], ['is_visible' => 0]);
1da2f9e8
J
57 }
58
b4628ea6 59 /**
60 * Disabled items should never show up.
61 *
62 * @param int $version
63 *
64 * @dataProvider versionThreeAndFour
b31fd595 65 *
66 * @throws \CRM_Core_Exception
b4628ea6 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
c77f49b7 82 /**
d1fa280a 83 * Items hushed through tomorrow shouldn't show up.
b4628ea6 84 *
2d932085 85 * @param int $version
b4628ea6 86 *
2d932085 87 * @dataProvider versionThreeAndFour
b4628ea6 88 * @throws \Exception
c77f49b7 89 */
2d932085
CW
90 public function testSystemCheckHushFuture($version) {
91 $this->_apiversion = $version;
1da2f9e8 92 $tomorrow = new DateTime('tomorrow');
b31fd595 93 $this->runStatusCheck([
1da2f9e8 94 'name' => 'checkDefaultMailbox',
f62b5bd7 95 'ignore_severity' => 7,
1da2f9e8 96 'hush_until' => $tomorrow->format('Y-m-d'),
b31fd595 97 ], ['is_visible' => 0]);
8ca6be76
J
98 }
99
c77f49b7 100 /**
d1fa280a 101 * Items hushed through today should show up.
b31fd595 102 *
2d932085 103 * @param int $version
b31fd595 104 *
2d932085 105 * @dataProvider versionThreeAndFour
b31fd595 106 * @throws \CRM_Core_Exception
c77f49b7 107 */
2d932085
CW
108 public function testSystemCheckHushToday($version) {
109 $this->_apiversion = $version;
c77f49b7 110 $today = new DateTime('today');
b31fd595 111 $this->runStatusCheck([
c77f49b7 112 'name' => 'checkDefaultMailbox',
f62b5bd7 113 'ignore_severity' => 7,
c77f49b7 114 'hush_until' => $today->format('Y-m-d'),
b31fd595 115 ], ['is_visible' => 1]);
c77f49b7
J
116 }
117
118 /**
d1fa280a 119 * Items hushed through yesterday should show up.
b31fd595 120 *
2d932085 121 * @param int $version
b31fd595 122 *
2d932085 123 * @dataProvider versionThreeAndFour
b31fd595 124 *
125 * @throws \CRM_Core_Exception
c77f49b7 126 */
2d932085
CW
127 public function testSystemCheckHushYesterday($version) {
128 $this->_apiversion = $version;
c77f49b7 129 $yesterday = new DateTime('yesterday');
b31fd595 130 $this->runStatusCheck([
c77f49b7 131 'name' => 'checkDefaultMailbox',
f62b5bd7 132 'ignore_severity' => 7,
c77f49b7 133 'hush_until' => $yesterday->format('Y-m-d'),
b31fd595 134 ], ['is_visible' => 1]);
f62b5bd7
J
135 }
136
137 /**
d1fa280a 138 * Items hushed above current severity should be hidden.
b31fd595 139 *
2d932085 140 * @param int $version
b31fd595 141 *
2d932085 142 * @dataProvider versionThreeAndFour
b31fd595 143 *
144 * @throws \CRM_Core_Exception
f62b5bd7 145 */
2d932085
CW
146 public function testSystemCheckHushAboveSeverity($version) {
147 $this->_apiversion = $version;
b31fd595 148 $this->runStatusCheck([
f62b5bd7
J
149 'name' => 'checkDefaultMailbox',
150 'ignore_severity' => 4,
b31fd595 151 ], ['is_visible' => 0]);
f62b5bd7
J
152 }
153
154 /**
d1fa280a 155 * Items hushed at current severity should be hidden.
b31fd595 156 *
2d932085 157 * @param int $version
b31fd595 158 *
2d932085 159 * @dataProvider versionThreeAndFour
b31fd595 160 *
161 * @throws \CRM_Core_Exception
f62b5bd7 162 */
2d932085
CW
163 public function testSystemCheckHushAtSeverity($version) {
164 $this->_apiversion = $version;
b31fd595 165 $this->runStatusCheck([
f62b5bd7
J
166 'name' => 'checkDefaultMailbox',
167 'ignore_severity' => 3,
b31fd595 168 ], ['is_visible' => 0]);
f62b5bd7
J
169 }
170
171 /**
d1fa280a 172 * Items hushed below current severity should be shown.
b31fd595 173 *
2d932085 174 * @param int $version
b31fd595 175 *
2d932085 176 * @dataProvider versionThreeAndFour
b31fd595 177 * @throws \CRM_Core_Exception
f62b5bd7 178 */
2d932085
CW
179 public function testSystemCheckHushBelowSeverity($version) {
180 $this->_apiversion = $version;
b31fd595 181 $this->runStatusCheck([
f62b5bd7
J
182 'name' => 'checkDefaultMailbox',
183 'ignore_severity' => 2,
b31fd595 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 }
9099cab3 200 $result = $this->callAPISuccess('System', 'check', []);
f62b5bd7 201 foreach ($result['values'] as $check) {
b31fd595 202 if ($check['name'] === 'checkDefaultMailbox') {
203 foreach ($expected as $key => $value) {
204 $this->assertEquals($check[$key], $value);
205 }
206 return;
afa33a28 207 }
f62b5bd7 208 }
b31fd595 209 throw new CRM_Core_Exception('checkDefaultMailbox not in results');
c77f49b7
J
210 }
211
8ca6be76 212}