4 * Class CRM_Utils_versionCheckTest
7 class CRM_Utils_versionCheckTest
extends CiviUnitTestCase
{
9 public function setUp() {
16 protected $sampleVersionInfo = [
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],
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],
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],
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],
84 'status' => 'testing',
86 ['version' => '4.6.alpha1', 'date' => '2015-02-01'],
87 ['version' => '4.6.beta1', 'date' => '2015-03-01'],
92 public function tearDown() {
94 $vc = new CRM_Utils_VersionCheck();
95 if (file_exists($vc->cacheFile
)) {
96 unlink($vc->cacheFile
);
100 public function testCronFallback() {
101 // Fake "remote" source data
102 $tmpSrc = '/tmp/versionCheckTestFile.json';
103 file_put_contents($tmpSrc, json_encode($this->sampleVersionInfo
));
105 $vc = new CRM_Utils_VersionCheck();
106 $vc->pingbackUrl
= $tmpSrc;
108 // If the cachefile doesn't exist, fallback should kick in
109 if (file_exists($vc->cacheFile
)) {
110 unlink($vc->cacheFile
);
113 $this->assertEquals($this->sampleVersionInfo
, $vc->versionInfo
);
116 // Update "remote" source data
117 $remoteData = ['4.3' => $this->sampleVersionInfo
['4.3']];
118 file_put_contents($tmpSrc, json_encode($remoteData));
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;
124 $this->assertEquals($this->sampleVersionInfo
, $vc->versionInfo
);
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
);
134 $this->assertEquals($remoteData, $vc->versionInfo
);
137 public function testGetSiteStats() {
138 // Create domain address so the domain country will come up in the stats.
145 $country_result = civicrm_api3('country', 'get', $country_params);
146 $country = $country_result['values'][0];
149 'id' => CRM_Core_Config
::domainID(),
151 CRM_Core_BAO_Domain
::retrieve($domain_params, $domain_defaults);
152 $location_type = CRM_Core_BAO_LocationType
::getDefault();
154 'contact_id' => $domain_defaults['contact_id'],
155 'location_type_id' => $location_type->id
,
158 'street_address' => '1 Main St.',
159 'city' => 'Anywhere',
160 'postal_code' => '99999',
161 'country_id' => $country['id'],
163 $address_result = civicrm_api3('address', 'create', $address_params);
165 // Build stats and test them.
166 $vc = new ReflectionClass('CRM_Utils_VersionCheck');
167 $vc_instance = $vc->newInstance();
169 $statsBuilder = $vc->getMethod('getSiteStats');
170 $statsBuilder->setAccessible(TRUE);
171 $statsBuilder->invoke($vc_instance, NULL);
173 $statsProperty = $vc->getProperty('stats');
174 $statsProperty->setAccessible(TRUE);
175 $stats = $statsProperty->getValue($vc_instance);
177 // Stats array should have correct elements.
178 $this->assertArrayHasKey('version', $stats);
180 // See CRM_Utils_VersionCheck::getSiteStats where alpha versions don't get
181 // full stats generated
182 if (array_key_exists('version', $stats) && strpos($stats['version'], 'alpha') === FALSE) {
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']);
198 // Assert $stats['domain_isoCode'] is correct.
199 $this->assertEquals($country['iso_code'], $stats['domain_isoCode']);
202 foreach ($stats['entities'] as $entity) {
203 $entity_names[] = $entity['name'];
204 $this->assertType('int', $entity['size'], "Stats entity {$entity['name']} has integer size?");
207 $expected_entity_names = [
215 'ContributionProduct',
232 sort($expected_entity_names);
233 $this->assertEquals($expected_entity_names, $entity_names);
235 // TODO: Also test for enabled extensions.