3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2017-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
37 namespace Galette\Util\test\units
;
39 use PHPUnit\Framework\TestCase
;
42 * Telemetry tests class
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
53 class Telemetry
extends TestCase
55 private \Galette\Core\Db
$zdb;
56 private \Galette\Core\Preferences
$preferences;
57 private \Galette\Core\Plugins
$plugins;
64 public function tearDown(): void
66 if (TYPE_DB
=== 'mysql') {
67 $this->assertSame([], $this->zdb
->getWarnings());
70 $this->preferences
->pref_instance_uuid
= '';
71 $this->preferences
->pref_registration_uuid
= '';
72 $this->preferences
->store();
80 public function setUp(): void
82 $this->zdb
= new \Galette\Core\
Db();
83 $this->preferences
= new \Galette\Core\
Preferences($this->zdb
);
85 $this->plugins
= new \Galette\Core\
Plugins();
93 public function testGrabGaletteInfos()
96 'uuid' => 'TO BE SET',
97 'version' => GALETTE_VERSION
,
99 'default_language' => \Galette\Core\I18n
::DEFAULT_LANG
,
101 'avg_members' => '0-50',
102 'avg_contributions' => '0-50',
103 'avg_transactions' => '0-50'
107 $telemetry = new \Galette\Util\
Telemetry(
113 $result = $telemetry->grabGaletteInfos();
114 $this->assertSame(40, strlen($result['uuid']));
115 $expected['uuid'] = $result['uuid'];
116 $this->assertSame($expected, $result);
118 $this->plugins
->loadModules($this->preferences
, GALETTE_PLUGINS_PATH
);
119 $telemetry = new \Galette\Util\
Telemetry(
125 $plugins_list = $this->plugins
->getModules();
126 foreach ($plugins_list as $plugin) {
127 $expected['plugins'][] = [
128 'key' => $plugin['name'],
129 'version' => $plugin['version']
133 $result = $telemetry->grabGaletteInfos();
134 $this->assertSame($expected, $result);
136 $telemetry = $this->getMockBuilder(\Galette\Util\Telemetry
::class)
137 ->setConstructorArgs([$this->zdb
, $this->preferences
, $this->plugins
])
138 ->onlyMethods(array('getCount'))
140 $telemetry->method('getCount')
142 $this->returnCallback(
143 function ($table, $where) {
145 case \Galette\Entity\Adherent
::TABLE
:
147 case \Galette\Entity\Contribution
::TABLE
:
149 case \Galette\Entity\Transaction
::TABLE
:
156 $result = $telemetry->grabGaletteInfos();
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);
169 public function testGrabDbInfos()
171 $telemetry = new \Galette\Util\
Telemetry(
177 $infos = $telemetry->grabDbInfos();
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']);
189 * Test web server infos
193 public function testGrabWebserverInfos()
195 $telemetry = new \Galette\Util\
Telemetry(
200 $result = $telemetry->grabWebserverInfos();
202 $this->assertSame(['engine', 'version'], array_keys($result));
203 //no webserver infos from CLI
204 $this->assertEmpty($result['engine']);
205 $this->assertEmpty($result['version']);
213 public function testGrabPhpInfos()
216 'version' => str_replace(PHP_EXTRA_VERSION
, '', PHP_VERSION
),
217 'modules' => get_loaded_extensions(),
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')
228 $telemetry = new \Galette\Util\
Telemetry(
233 $result = $telemetry->grabPhpInfos();
234 $this->assertSame($expected, $result);
242 public function testGrabOsInfos()
245 if (file_exists('/etc/redhat-release')) {
246 $distro = preg_replace('/\s+$/S', '', file_get_contents('/etc/redhat-release'));
248 if (file_exists('/etc/fedora-release')) {
249 $distro = preg_replace('/\s+$/S', '', file_get_contents('/etc/fedora-release'));
253 'family' => php_uname('s'),
254 'distribution' => $distro,
255 'version' => php_uname('r')
258 $telemetry = new \Galette\Util\
Telemetry(
263 $result = $telemetry->grabOsInfos();
264 $this->assertSame($expected, $result);
268 * Test whole Telemetry infos
272 public function testGetTelemetryInfos()
274 $telemetry = new \Galette\Util\
Telemetry(
279 $result = $telemetry->getTelemetryInfos();
290 array_keys($result['galette']),
301 array_keys($result['system']),