Merge pull request #16491 from mfb/undefined-index
[civicrm-core.git] / tests / phpunit / CRM / Utils / versionCheckTest.php
CommitLineData
986e458b
CW
1<?php
2
7fe37828
EM
3/**
4 * Class CRM_Utils_versionCheckTest
acb109b7 5 * @group headless
7fe37828 6 */
986e458b
CW
7class CRM_Utils_versionCheckTest extends CiviUnitTestCase {
8
00be9182 9 public function setUp() {
986e458b
CW
10 parent::setUp();
11 }
12
13 /**
14 * @var array
15 */
9099cab3
CW
16 protected $sampleVersionInfo = [
17 '4.2' => [
986e458b 18 'status' => 'eol',
9099cab3
CW
19 'releases' => [
20 ['version' => '4.2.0', 'date' => '2012-08-20'],
21 ['version' => '4.2.1', 'date' => '2012-09-12'],
22 ['version' => '4.2.2', 'date' => '2012-09-27'],
23 ['version' => '4.2.4', 'date' => '2012-10-18'],
24 ['version' => '4.2.6', 'date' => '2012-11-01', 'security' => TRUE],
25 ['version' => '4.2.7', 'date' => '2013-01-02', 'security' => TRUE],
26 ['version' => '4.2.8', 'date' => '2013-02-20'],
27 ['version' => '4.2.9', 'date' => '2013-04-03'],
28 ['version' => '4.2.10', 'date' => '2013-07-29', 'security' => TRUE],
29 ['version' => '4.2.11', 'date' => '2013-09-25'],
30 ['version' => '4.2.12', 'date' => '2013-10-02', 'security' => TRUE],
31 ['version' => '4.2.13', 'date' => '2013-11-06', 'security' => TRUE],
32 ['version' => '4.2.14', 'date' => '2013-11-20'],
33 ['version' => '4.2.15', 'date' => '2014-02-07', 'security' => TRUE],
34 ['version' => '4.2.16', 'date' => '2014-02-18'],
35 ['version' => '4.2.17', 'date' => '2014-07-01', 'security' => TRUE],
36 ['version' => '4.2.18', 'date' => '2014-08-06'],
37 ['version' => '4.2.19', 'date' => '2014-09-17', 'security' => TRUE],
38 ],
39 ],
40 '4.3' => [
986e458b 41 'status' => 'lts',
9099cab3
CW
42 'releases' => [
43 ['version' => '4.3.0', 'date' => '2013-04-10'],
44 ['version' => '4.3.1', 'date' => '2013-04-18'],
45 ['version' => '4.3.2', 'date' => '2013-05-02'],
46 ['version' => '4.3.3', 'date' => '2013-05-08'],
47 ['version' => '4.3.4', 'date' => '2013-06-10', 'security' => TRUE],
48 ['version' => '4.3.5', 'date' => '2013-07-08', 'security' => TRUE],
49 ['version' => '4.3.6', 'date' => '2013-09-25'],
50 ['version' => '4.3.7', 'date' => '2013-10-02', 'security' => TRUE],
51 ['version' => '4.3.8', 'date' => '2013-11-06', 'security' => TRUE],
52 ['version' => '4.3.9', 'date' => '2014-09-07', 'security' => TRUE],
53 ],
54 ],
55 '4.4' => [
986e458b 56 'status' => 'lts',
9099cab3
CW
57 'releases' => [
58 ['version' => '4.4.0', 'date' => '2013-10-23'],
59 ['version' => '4.4.1', 'date' => '2013-11-06', 'security' => TRUE],
60 ['version' => '4.4.2', 'date' => '2013-11-20'],
61 ['version' => '4.4.3', 'date' => '2013-12-05'],
62 ['version' => '4.4.4', 'date' => '2014-02-07', 'security' => TRUE],
63 ['version' => '4.4.5', 'date' => '2014-04-17'],
64 ['version' => '4.4.6', 'date' => '2014-07-01', 'security' => TRUE],
65 ['version' => '4.4.7', 'date' => '2014-09-17', 'security' => TRUE],
66 ['version' => '4.4.8', 'date' => '2014-10-14'],
67 ['version' => '4.4.9', 'date' => '2014-11-05'],
68 ['version' => '4.4.10', 'date' => '2014-11-19'],
69 ['version' => '4.4.11', 'date' => '2014-12-17', 'security' => TRUE],
70 ],
71 ],
72 '4.5' => [
986e458b 73 'status' => 'stable',
9099cab3
CW
74 'releases' => [
75 ['version' => '4.5.0', 'date' => '2014-09-18'],
76 ['version' => '4.5.1', 'date' => '2014-10-09'],
77 ['version' => '4.5.2', 'date' => '2014-10-14'],
78 ['version' => '4.5.3', 'date' => '2014-11-05'],
79 ['version' => '4.5.4', 'date' => '2014-11-19'],
80 ['version' => '4.5.5', 'date' => '2014-12-17', 'security' => TRUE],
81 ],
82 ],
83 '4.6' => [
986e458b 84 'status' => 'testing',
9099cab3
CW
85 'releases' => [
86 ['version' => '4.6.alpha1', 'date' => '2015-02-01'],
87 ['version' => '4.6.beta1', 'date' => '2015-03-01'],
88 ],
89 ],
90 ];
986e458b 91
88790378
TO
92 public function tearDown() {
93 parent::tearDown();
86448e8d 94 $vc = new CRM_Utils_VersionCheck();
88790378
TO
95 if (file_exists($vc->cacheFile)) {
96 unlink($vc->cacheFile);
97 }
986e458b 98 }
96025800 99
074e8131
CW
100 public function testCronFallback() {
101 // Fake "remote" source data
102 $tmpSrc = '/tmp/versionCheckTestFile.json';
103 file_put_contents($tmpSrc, json_encode($this->sampleVersionInfo));
104
105 $vc = new CRM_Utils_VersionCheck();
106 $vc->pingbackUrl = $tmpSrc;
107
108 // If the cachefile doesn't exist, fallback should kick in
109 if (file_exists($vc->cacheFile)) {
110 unlink($vc->cacheFile);
111 }
112 $vc->initialize();
113 $this->assertEquals($this->sampleVersionInfo, $vc->versionInfo);
114 unset($vc);
115
116 // Update "remote" source data
9099cab3 117 $remoteData = ['4.3' => $this->sampleVersionInfo['4.3']];
074e8131
CW
118 file_put_contents($tmpSrc, json_encode($remoteData));
119
120 // Cache was just updated, so fallback should not happen - assert we are still using cached data
121 $vc = new CRM_Utils_VersionCheck();
122 $vc->pingbackUrl = $tmpSrc;
123 $vc->initialize();
124 $this->assertEquals($this->sampleVersionInfo, $vc->versionInfo);
125 unset($vc);
126
127 // Ensure fallback happens if file is too old
128 $vc = new CRM_Utils_VersionCheck();
129 $vc->pingbackUrl = $tmpSrc;
130 // Set cachefile to be 1 minute older than expire time
131 touch($vc->cacheFile, time() - 60 - $vc::CACHEFILE_EXPIRE);
132 clearstatcache();
133 $vc->initialize();
134 $this->assertEquals($remoteData, $vc->versionInfo);
135 }
136
142a9b5f
AS
137 public function testGetSiteStats() {
138 // Create domain address so the domain country will come up in the stats.
9099cab3 139 $country_params = [
142a9b5f 140 'sequential' => 1,
9099cab3 141 'options' => [
142a9b5f 142 'limit' => 1,
9099cab3
CW
143 ],
144 ];
142a9b5f
AS
145 $country_result = civicrm_api3('country', 'get', $country_params);
146 $country = $country_result['values'][0];
147
9099cab3 148 $domain_params = [
142a9b5f 149 'id' => CRM_Core_Config::domainID(),
9099cab3 150 ];
142a9b5f
AS
151 CRM_Core_BAO_Domain::retrieve($domain_params, $domain_defaults);
152 $location_type = CRM_Core_BAO_LocationType::getDefault();
9099cab3 153 $address_params = [
142a9b5f
AS
154 'contact_id' => $domain_defaults['contact_id'],
155 'location_type_id' => $location_type->id,
156 'is_primary' => '1',
157 'is_billing' => '0',
158 'street_address' => '1 Main St.',
159 'city' => 'Anywhere',
160 'postal_code' => '99999',
161 'country_id' => $country['id'],
9099cab3 162 ];
142a9b5f
AS
163 $address_result = civicrm_api3('address', 'create', $address_params);
164
165 // Build stats and test them.
166 $vc = new ReflectionClass('CRM_Utils_VersionCheck');
167 $vc_instance = $vc->newInstance();
168
169 $statsBuilder = $vc->getMethod('getSiteStats');
170 $statsBuilder->setAccessible(TRUE);
171 $statsBuilder->invoke($vc_instance, NULL);
172
173 $statsProperty = $vc->getProperty('stats');
174 $statsProperty->setAccessible(TRUE);
175 $stats = $statsProperty->getValue($vc_instance);
176
177 // Stats array should have correct elements.
178 $this->assertArrayHasKey('version', $stats);
142a9b5f 179
692d270e
AH
180 // See CRM_Utils_VersionCheck::getSiteStats where alpha versions don't get
181 // full stats generated
8be29698 182 if (array_key_exists('version', $stats) && strpos($stats['version'], 'alpha') === FALSE) {
692d270e
AH
183 $this->assertArrayHasKey('hash', $stats);
184 $this->assertArrayHasKey('uf', $stats);
185 $this->assertArrayHasKey('lang', $stats);
186 $this->assertArrayHasKey('co', $stats);
187 $this->assertArrayHasKey('ufv', $stats);
188 $this->assertArrayHasKey('PHP', $stats);
189 $this->assertArrayHasKey('MySQL', $stats);
190 $this->assertArrayHasKey('communityMessagesUrl', $stats);
191 $this->assertArrayHasKey('domain_isoCode', $stats);
192 $this->assertArrayHasKey('PPTypes', $stats);
193 $this->assertArrayHasKey('entities', $stats);
194 $this->assertArrayHasKey('extensions', $stats);
195 $this->assertType('array', $stats['entities']);
196 $this->assertType('array', $stats['extensions']);
197
198 // Assert $stats['domain_isoCode'] is correct.
199 $this->assertEquals($country['iso_code'], $stats['domain_isoCode']);
200
9099cab3 201 $entity_names = [];
692d270e
AH
202 foreach ($stats['entities'] as $entity) {
203 $entity_names[] = $entity['name'];
204 $this->assertType('int', $entity['size'], "Stats entity {$entity['name']} has integer size?");
205 }
206
9099cab3 207 $expected_entity_names = [
692d270e
AH
208 'Activity',
209 'Case',
210 'Contact',
211 'Relationship',
212 'Campaign',
213 'Contribution',
214 'ContributionPage',
215 'ContributionProduct',
216 'Widget',
217 'Discount',
218 'PriceSetEntity',
219 'UFGroup',
220 'Event',
221 'Participant',
222 'Friend',
223 'Grant',
224 'Mailing',
225 'Membership',
226 'MembershipBlock',
227 'Pledge',
228 'PledgeBlock',
229 'Delivered',
9099cab3 230 ];
692d270e
AH
231 sort($entity_names);
232 sort($expected_entity_names);
233 $this->assertEquals($expected_entity_names, $entity_names);
234
235 // TODO: Also test for enabled extensions.
236 }
142a9b5f
AS
237 }
238
986e458b 239}