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