]> git.agnieray.net Git - galette.git/blob - tests/Galette/Util/tests/units/Telemetry.php
Remove 'svn' lines
[galette.git] / tests / Galette / Util / tests / units / Telemetry.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Telemetry tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Util
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since 2017-10-07
35 */
36
37 namespace Galette\Util\test\units;
38
39 use PHPUnit\Framework\TestCase;
40
41 /**
42 * Telemetry tests class
43 *
44 * @category Util
45 * @name Telemetry
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2017-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
51 * @since 2017-10-07
52 */
53 class Telemetry extends TestCase
54 {
55 private \Galette\Core\Db $zdb;
56 private \Galette\Core\Preferences $preferences;
57 private \Galette\Core\Plugins $plugins;
58
59 /**
60 * Tear down tests
61 *
62 * @return void
63 */
64 public function tearDown(): void
65 {
66 if (TYPE_DB === 'mysql') {
67 $this->assertSame([], $this->zdb->getWarnings());
68 }
69
70 $this->preferences->pref_instance_uuid = '';
71 $this->preferences->pref_registration_uuid = '';
72 $this->preferences->store();
73 }
74
75 /**
76 * Set up tests
77 *
78 * @return void
79 */
80 public function setUp(): void
81 {
82 $this->zdb = new \Galette\Core\Db();
83 $this->preferences = new \Galette\Core\Preferences($this->zdb);
84
85 $this->plugins = new \Galette\Core\Plugins();
86 }
87
88 /**
89 * Test Galette infos
90 *
91 * @return void
92 */
93 public function testGrabGaletteInfos()
94 {
95 $expected = [
96 'uuid' => 'TO BE SET',
97 'version' => GALETTE_VERSION,
98 'plugins' => [],
99 'default_language' => \Galette\Core\I18n::DEFAULT_LANG,
100 'usage' => [
101 'avg_members' => '0-50',
102 'avg_contributions' => '0-50',
103 'avg_transactions' => '0-50'
104 ]
105 ];
106
107 $telemetry = new \Galette\Util\Telemetry(
108 $this->zdb,
109 $this->preferences,
110 $this->plugins
111 );
112
113 $result = $telemetry->grabGaletteInfos();
114 $this->assertSame(40, strlen($result['uuid']));
115 $expected['uuid'] = $result['uuid'];
116 $this->assertSame($expected, $result);
117
118 $this->plugins->loadModules($this->preferences, GALETTE_PLUGINS_PATH);
119 $telemetry = new \Galette\Util\Telemetry(
120 $this->zdb,
121 $this->preferences,
122 $this->plugins
123 );
124
125 $plugins_list = $this->plugins->getModules();
126 foreach ($plugins_list as $plugin) {
127 $expected['plugins'][] = [
128 'key' => $plugin['name'],
129 'version' => $plugin['version']
130 ];
131 }
132
133 $result = $telemetry->grabGaletteInfos();
134 $this->assertSame($expected, $result);
135
136 $telemetry = $this->getMockBuilder(\Galette\Util\Telemetry::class)
137 ->setConstructorArgs([$this->zdb, $this->preferences, $this->plugins])
138 ->onlyMethods(array('getCount'))
139 ->getMock();
140 $telemetry->method('getCount')
141 ->will(
142 $this->returnCallback(
143 function ($table, $where) {
144 switch ($table) {
145 case \Galette\Entity\Adherent::TABLE:
146 return 56;
147 case \Galette\Entity\Contribution::TABLE:
148 return 402;
149 case \Galette\Entity\Transaction::TABLE:
150 return 100;
151 }
152 return 0;
153 }
154 )
155 );
156 $result = $telemetry->grabGaletteInfos();
157
158 $expected['usage']['avg_members'] = '50-250';
159 $expected['usage']['avg_contributions'] = '250-500';
160 $expected['usage']['avg_transactions'] = '50-250';
161 $this->assertSame($expected, $result);
162 }
163
164 /**
165 * Test DB infos
166 *
167 * @return void
168 */
169 public function testGrabDbInfos()
170 {
171 $telemetry = new \Galette\Util\Telemetry(
172 $this->zdb,
173 $this->preferences,
174 $this->plugins
175 );
176
177 $infos = $telemetry->grabDbInfos();
178
179 $this->assertNotEmpty($infos['engine']);
180 $this->assertNotEmpty($infos['version']);
181 $this->assertNotNull($infos['size']);
182 if (!$this->zdb->isPostgres()) {
183 //no sql mode for postgres databases
184 $this->assertNotEmpty($infos['sql_mode']);
185 }
186 }
187
188 /**
189 * Test web server infos
190 *
191 * @return void
192 */
193 public function testGrabWebserverInfos()
194 {
195 $telemetry = new \Galette\Util\Telemetry(
196 $this->zdb,
197 $this->preferences,
198 $this->plugins
199 );
200 $result = $telemetry->grabWebserverInfos();
201
202 $this->assertSame(['engine', 'version'], array_keys($result));
203 //no webserver infos from CLI
204 $this->assertEmpty($result['engine']);
205 $this->assertEmpty($result['version']);
206 }
207
208 /**
209 * Test PHP infos
210 *
211 * @return void
212 */
213 public function testGrabPhpInfos()
214 {
215 $expected = [
216 'version' => str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION),
217 'modules' => get_loaded_extensions(),
218 'setup' => [
219 'max_execution_time' => ini_get('max_execution_time'),
220 'memory_limit' => ini_get('memory_limit'),
221 'post_max_size' => ini_get('post_max_size'),
222 'safe_mode' => ini_get('safe_mode'),
223 'session' => ini_get('session.save_handler'),
224 'upload_max_filesize' => ini_get('upload_max_filesize')
225 ]
226 ];
227
228 $telemetry = new \Galette\Util\Telemetry(
229 $this->zdb,
230 $this->preferences,
231 $this->plugins
232 );
233 $result = $telemetry->grabPhpInfos();
234 $this->assertSame($expected, $result);
235 }
236
237 /**
238 * Test OS infos
239 *
240 * @return void
241 */
242 public function testGrabOsInfos()
243 {
244 $distro = '';
245 if (file_exists('/etc/redhat-release')) {
246 $distro = preg_replace('/\s+$/S', '', file_get_contents('/etc/redhat-release'));
247 }
248 if (file_exists('/etc/fedora-release')) {
249 $distro = preg_replace('/\s+$/S', '', file_get_contents('/etc/fedora-release'));
250 }
251
252 $expected = [
253 'family' => php_uname('s'),
254 'distribution' => $distro,
255 'version' => php_uname('r')
256 ];
257
258 $telemetry = new \Galette\Util\Telemetry(
259 $this->zdb,
260 $this->preferences,
261 $this->plugins
262 );
263 $result = $telemetry->grabOsInfos();
264 $this->assertSame($expected, $result);
265 }
266
267 /**
268 * Test whole Telemetry infos
269 *
270 * @return void
271 */
272 public function testGetTelemetryInfos()
273 {
274 $telemetry = new \Galette\Util\Telemetry(
275 $this->zdb,
276 $this->preferences,
277 $this->plugins
278 );
279 $result = $telemetry->getTelemetryInfos();
280
281 $this->assertSame(
282 array_keys($result),
283 [
284 'galette',
285 'system'
286 ]
287 );
288
289 $this->assertSame(
290 array_keys($result['galette']),
291 [
292 'uuid',
293 'version',
294 'plugins',
295 'default_language',
296 'usage'
297 ]
298 );
299
300 $this->assertSame(
301 array_keys($result['system']),
302 [
303 'db',
304 'web_server',
305 'php',
306 'os'
307 ]
308 );
309 }
310 }