2018-12-18 02:23:44 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Metrics;
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
use Carbon\Carbon;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Engelsystem\Controllers\Metrics\Stats;
|
2023-12-29 17:30:28 +01:00
|
|
|
use Engelsystem\Models\AngelType;
|
2020-12-06 00:16:15 +01:00
|
|
|
use Engelsystem\Models\Faq;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Engelsystem\Models\LogEntry;
|
2019-12-30 21:12:22 +01:00
|
|
|
use Engelsystem\Models\Message;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\News;
|
2020-09-12 22:51:32 +02:00
|
|
|
use Engelsystem\Models\NewsComment;
|
2020-11-22 14:45:24 +01:00
|
|
|
use Engelsystem\Models\OAuth;
|
2019-12-07 19:31:13 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2023-10-15 19:25:55 +02:00
|
|
|
use Engelsystem\Models\Location;
|
2023-01-03 22:19:03 +01:00
|
|
|
use Engelsystem\Models\Shifts\Shift;
|
2023-01-18 13:02:11 +01:00
|
|
|
use Engelsystem\Models\Shifts\ShiftEntry;
|
2023-12-29 17:30:28 +01:00
|
|
|
use Engelsystem\Models\Shifts\ShiftType;
|
2021-12-19 18:38:42 +01:00
|
|
|
use Engelsystem\Models\User\License;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Engelsystem\Models\User\PasswordReset;
|
2018-12-21 20:09:15 +01:00
|
|
|
use Engelsystem\Models\User\PersonalData;
|
2020-04-19 19:18:56 +02:00
|
|
|
use Engelsystem\Models\User\Settings;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Engelsystem\Models\User\State;
|
|
|
|
use Engelsystem\Models\User\User;
|
2020-09-12 19:45:25 +02:00
|
|
|
use Engelsystem\Models\Worklog;
|
2018-12-18 02:23:44 +01:00
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
|
|
|
use Illuminate\Support\Str;
|
2018-12-18 04:43:13 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2018-12-18 02:23:44 +01:00
|
|
|
|
|
|
|
class StatsTest extends TestCase
|
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
2018-12-21 20:09:15 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::vouchers
|
2020-08-20 14:29:27 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::vouchersQuery
|
2023-12-28 16:26:56 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::__construct
|
2018-12-21 20:09:15 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testVouchers(): void
|
2018-12-21 20:09:15 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(14, $stats->vouchers());
|
|
|
|
}
|
|
|
|
|
2020-08-20 14:29:27 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::vouchersBuckets
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testVouchersBuckets(): void
|
2020-08-20 14:29:27 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals([1 => 6, 3 => 8, '+Inf' => 9], $stats->vouchersBuckets([1, 3, '+Inf']));
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:09:15 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::tshirts
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testTshirts(): void
|
2018-12-21 20:09:15 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->tshirts());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::tshirtSizes
|
2019-12-25 09:57:11 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::raw
|
2018-12-21 20:09:15 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testTshirtSizes(): void
|
2018-12-21 20:09:15 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$sizes = $stats->tshirtSizes();
|
|
|
|
$this->assertCount(2, $sizes);
|
2019-12-25 09:57:11 +01:00
|
|
|
$this->assertEquals([
|
2020-04-19 19:18:56 +02:00
|
|
|
['shirt_size' => 'L', 'count' => 2],
|
|
|
|
['shirt_size' => 'XXL', 'count' => 1],
|
2019-12-25 09:57:11 +01:00
|
|
|
], $sizes->toArray());
|
2018-12-21 20:09:15 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 19:18:56 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::languages
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testLanguages(): void
|
2020-04-19 19:18:56 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$languages = $stats->languages();
|
|
|
|
$this->assertCount(2, $languages);
|
|
|
|
$this->assertEquals([
|
|
|
|
['language' => 'lo_RM', 'count' => 2],
|
|
|
|
['language' => 'te_ST', 'count' => 7],
|
|
|
|
], $languages->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::themes
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testThemes(): void
|
2020-04-19 19:18:56 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$themes = $stats->themes();
|
|
|
|
$this->assertCount(3, $themes);
|
|
|
|
$this->assertEquals([
|
2020-09-12 22:51:32 +02:00
|
|
|
['theme' => 0, 'count' => 6],
|
|
|
|
['theme' => 1, 'count' => 2],
|
2020-04-19 19:18:56 +02:00
|
|
|
['theme' => 4, 'count' => 1],
|
|
|
|
], $themes->toArray());
|
|
|
|
}
|
|
|
|
|
2021-12-19 18:38:42 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::licenses
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testLicenses(): void
|
2021-12-19 18:38:42 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(1, $stats->licenses('has_car'));
|
|
|
|
$this->assertEquals(1, $stats->licenses('forklift'));
|
2024-03-26 21:02:05 +01:00
|
|
|
$this->assertEquals(1, $stats->licenses('car'));
|
2021-12-19 18:38:42 +01:00
|
|
|
$this->assertEquals(0, $stats->licenses('3.5t'));
|
|
|
|
$this->assertEquals(0, $stats->licenses('7.5t'));
|
2024-03-26 21:02:05 +01:00
|
|
|
$this->assertEquals(0, $stats->licenses('12t'));
|
|
|
|
$this->assertEquals(1, $stats->licenses('ifsg_light'));
|
2023-08-10 11:33:06 +02:00
|
|
|
$this->assertEquals(0, $stats->licenses('ifsg'));
|
2024-03-26 21:02:05 +01:00
|
|
|
$this->assertEquals(0, $stats->licenses('forklift', true));
|
|
|
|
$this->assertEquals(1, $stats->licenses('car', true));
|
|
|
|
$this->assertEquals(0, $stats->licenses('3.5t', true));
|
|
|
|
$this->assertEquals(0, $stats->licenses('7.5t', true));
|
|
|
|
$this->assertEquals(1, $stats->licenses('12t', true));
|
|
|
|
$this->assertEquals(0, $stats->licenses('ifsg_light', true));
|
|
|
|
$this->assertEquals(1, $stats->licenses('ifsg', true));
|
2021-12-19 18:38:42 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 19:45:25 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::worklogSeconds
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testWorklogSeconds(): void
|
2020-09-12 19:45:25 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$worklogData = [
|
|
|
|
'user_id' => 1,
|
|
|
|
'creator_id' => 1,
|
|
|
|
'hours' => 2.4,
|
|
|
|
'comment' => '',
|
2023-02-05 18:03:00 +01:00
|
|
|
'worked_at' => new Carbon(),
|
2020-09-12 19:45:25 +02:00
|
|
|
];
|
|
|
|
(new Worklog($worklogData))->save();
|
|
|
|
(new Worklog(['hours' => 1.2, 'user_id' => 3] + $worklogData))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$seconds = $stats->worklogSeconds();
|
|
|
|
|
|
|
|
$this->assertEquals(2.4 * 60 * 60 + 1.2 * 60 * 60, $seconds);
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:02:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::worklogBuckets
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::getBuckets
|
|
|
|
*/
|
|
|
|
public function testWorklogBuckets(): void
|
|
|
|
{
|
|
|
|
Worklog::factory()->create(['hours' => 1.2, 'worked_at' => Carbon::now()->subDay()]);
|
|
|
|
Worklog::factory()->create(['hours' => 1.9, 'worked_at' => Carbon::now()->subDay()]);
|
|
|
|
Worklog::factory()->create(['hours' => 3, 'worked_at' => Carbon::now()->subDay()]);
|
|
|
|
Worklog::factory()->create(['hours' => 10, 'worked_at' => Carbon::now()->subDay()]);
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$buckets = $stats->worklogBuckets([
|
|
|
|
1 * 60 * 60,
|
|
|
|
2 * 60 * 60,
|
|
|
|
3 * 60 * 60,
|
|
|
|
4 * 60 * 60,
|
2023-02-05 18:03:00 +01:00
|
|
|
'+Inf',
|
2023-01-18 13:02:11 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
3600 => 0,
|
|
|
|
7200 => 2,
|
|
|
|
10800 => 3,
|
|
|
|
14400 => 3,
|
|
|
|
'+Inf' => 4,
|
|
|
|
], $buckets);
|
|
|
|
}
|
|
|
|
|
2020-09-06 23:50:36 +02:00
|
|
|
/**
|
2023-10-15 19:25:55 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::locations
|
2020-09-06 23:50:36 +02:00
|
|
|
*/
|
2023-10-15 19:25:55 +02:00
|
|
|
public function testLocations(): void
|
2020-09-06 23:50:36 +02:00
|
|
|
{
|
2023-10-15 19:25:55 +02:00
|
|
|
(new Location(['name' => 'Location 1']))->save();
|
|
|
|
(new Location(['name' => 'Second location']))->save();
|
|
|
|
(new Location(['name' => 'Another location']))->save();
|
|
|
|
(new Location(['name' => 'Old location']))->save();
|
2020-09-06 23:50:36 +02:00
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
2023-10-15 19:25:55 +02:00
|
|
|
$this->assertEquals(4, $stats->locations());
|
2020-09-06 23:50:36 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 17:30:28 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::angeltypes
|
|
|
|
*/
|
|
|
|
public function testAngeltypes(): void
|
|
|
|
{
|
|
|
|
(new AngelType(['name' => 'AngelType 1']))->save();
|
|
|
|
(new AngelType(['name' => 'Second AngelType']))->save();
|
|
|
|
(new AngelType(['name' => 'Another AngelType']))->save();
|
|
|
|
(new AngelType(['name' => 'Old AngelType']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(4, $stats->angeltypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::shifttypes
|
|
|
|
*/
|
|
|
|
public function testShifttypes(): void
|
|
|
|
{
|
|
|
|
(new ShiftType(['name' => 'ShiftType 1', 'description' => 'rtfm']))->save();
|
|
|
|
(new ShiftType(['name' => 'Second ShiftType', 'description' => 'pebkac']))->save();
|
|
|
|
(new ShiftType(['name' => 'Another ShiftType', 'description' => 'id10t error']))->save();
|
|
|
|
(new ShiftType(['name' => 'Old ShiftType', 'description' => 'layer 8']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(4, $stats->shifttypes());
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::shifts
|
|
|
|
*/
|
|
|
|
public function testShifts(): void
|
|
|
|
{
|
|
|
|
Shift::factory(5)->create();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(5, $stats->shifts());
|
|
|
|
}
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::announcements
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testAnnouncements(): void
|
2019-11-10 21:30:26 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$newsData = ['title' => 'Test', 'text' => 'Foo Bar', 'user_id' => 1];
|
|
|
|
|
|
|
|
(new News($newsData))->save();
|
|
|
|
(new News($newsData))->save();
|
|
|
|
(new News($newsData + ['is_meeting' => true]))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->announcements());
|
|
|
|
$this->assertEquals(2, $stats->announcements(false));
|
|
|
|
$this->assertEquals(1, $stats->announcements(true));
|
|
|
|
}
|
|
|
|
|
2020-09-12 22:51:32 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::comments
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testComments(): void
|
2020-09-12 22:51:32 +02:00
|
|
|
{
|
|
|
|
$user = $this->addUser();
|
|
|
|
|
|
|
|
$news = new News(['title' => 'Test', 'text' => 'Foo Bar', 'user_id' => $user->id]);
|
|
|
|
$news->save();
|
|
|
|
|
|
|
|
foreach (['Test', 'Another text!'] as $text) {
|
|
|
|
$comment = new NewsComment(['text' => $text]);
|
|
|
|
$comment->news()->associate($news);
|
|
|
|
$comment->user()->associate($user);
|
|
|
|
$comment->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->comments());
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:31:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::questions
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testQuestions(): void
|
2019-12-07 19:31:13 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$questionsData = ['text' => 'Lorem Ipsum', 'user_id' => 1];
|
|
|
|
|
|
|
|
(new Question($questionsData))->save();
|
|
|
|
(new Question($questionsData))->save();
|
|
|
|
(new Question($questionsData + ['answerer_id' => 2, 'answer' => 'Dolor sit!']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->questions());
|
|
|
|
$this->assertEquals(2, $stats->questions(false));
|
|
|
|
$this->assertEquals(1, $stats->questions(true));
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
/**
|
2023-12-28 16:26:56 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::usersState
|
2018-12-18 02:23:44 +01:00
|
|
|
*/
|
2023-12-28 16:26:56 +01:00
|
|
|
public function testUsersState(): void
|
2018-12-18 02:23:44 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
2023-01-18 13:02:11 +01:00
|
|
|
ShiftEntry::factory()->create(['user_id' => 3]);
|
|
|
|
ShiftEntry::factory()->create(['user_id' => 4]);
|
2023-12-28 16:26:56 +01:00
|
|
|
ShiftEntry::factory()->create(['user_id' => 1]);
|
2018-12-18 02:23:44 +01:00
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
2023-12-28 16:26:56 +01:00
|
|
|
$this->assertEquals(7, $stats->usersState());
|
|
|
|
$this->assertEquals(5, $stats->usersState(false));
|
|
|
|
$this->assertEquals(2, $stats->usersState(true));
|
|
|
|
$this->assertEquals(2, $stats->usersState(null, false));
|
|
|
|
$this->assertEquals(1, $stats->usersState(true, false));
|
|
|
|
$this->assertEquals(1, $stats->usersState(false, false));
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
|
|
|
|
2023-12-28 17:56:56 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::usersInfo
|
|
|
|
*/
|
|
|
|
public function testUsersInfo(): void
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(1, $stats->usersInfo());
|
|
|
|
}
|
|
|
|
|
2019-12-24 14:33:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::forceActiveUsers
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testForceActiveUsers(): void
|
2019-12-24 14:33:11 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->forceActiveUsers());
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:16:08 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::usersPronouns
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testUsersPronouns(): void
|
2021-04-13 12:16:08 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->usersPronouns());
|
|
|
|
}
|
|
|
|
|
2020-09-12 22:51:32 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::email
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testEmail(): void
|
2020-09-12 22:51:32 +02:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(0, $stats->email('not-available-option'));
|
|
|
|
$this->assertEquals(2, $stats->email('system'));
|
|
|
|
$this->assertEquals(3, $stats->email('humans'));
|
2021-11-30 00:19:44 +01:00
|
|
|
$this->assertEquals(1, $stats->email('goody'));
|
2020-12-28 16:04:05 +01:00
|
|
|
$this->assertEquals(1, $stats->email('news'));
|
2020-09-12 22:51:32 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 13:02:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::currentlyWorkingUsers
|
|
|
|
*/
|
|
|
|
public function testCurrentlyWorkingUsers(): void
|
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
/** @var Shift $shift */
|
|
|
|
$shift = Shift::factory()->create(['start' => Carbon::now()->subHour(), 'end' => Carbon::now()->addHour()]);
|
|
|
|
|
2023-02-03 12:37:01 +01:00
|
|
|
ShiftEntry::factory()->create(['shift_id' => $shift->id, 'freeloaded' => false]);
|
|
|
|
ShiftEntry::factory()->create(['shift_id' => $shift->id, 'freeloaded' => false]);
|
2023-01-18 13:02:11 +01:00
|
|
|
ShiftEntry::factory()->create(['shift_id' => $shift->id, 'freeloaded' => true]);
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->currentlyWorkingUsers());
|
|
|
|
$this->assertEquals(2, $stats->currentlyWorkingUsers(false));
|
|
|
|
$this->assertEquals(1, $stats->currentlyWorkingUsers(true));
|
|
|
|
}
|
|
|
|
|
2020-12-06 00:16:15 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::faq
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testFaq(): void
|
2020-12-06 00:16:15 +01:00
|
|
|
{
|
|
|
|
(new Faq(['question' => 'Foo?', 'text' => 'Bar!']))->save();
|
|
|
|
(new Faq(['question' => 'Lorem??', 'text' => 'Ipsum!!!']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->faq());
|
|
|
|
}
|
|
|
|
|
2019-12-30 21:12:22 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::messages
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testMessages(): void
|
2019-12-30 21:12:22 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
|
|
|
(new Message(['user_id' => 1, 'receiver_id' => 2, 'text' => 'Ohi?']))->save();
|
|
|
|
(new Message(['user_id' => 4, 'receiver_id' => 1, 'text' => 'Testing stuff?']))->save();
|
|
|
|
(new Message(['user_id' => 2, 'receiver_id' => 3, 'text' => 'Nope!', 'read' => true]))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(3, $stats->messages());
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::sessions
|
2020-04-19 19:18:56 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::getQuery
|
2018-12-18 04:43:13 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSessions(): void
|
2018-12-18 04:43:13 +01:00
|
|
|
{
|
|
|
|
$this->database
|
|
|
|
->getConnection()
|
|
|
|
->table('sessions')
|
|
|
|
->insert([
|
|
|
|
['id' => 'asd', 'payload' => 'data', 'last_activity' => new Carbon('1 month ago')],
|
|
|
|
['id' => 'efg', 'payload' => 'lorem', 'last_activity' => new Carbon('55 minutes ago')],
|
|
|
|
['id' => 'hij', 'payload' => 'ipsum', 'last_activity' => new Carbon('3 seconds ago')],
|
|
|
|
['id' => 'klm', 'payload' => 'dolor', 'last_activity' => new Carbon()],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(4, $stats->sessions());
|
|
|
|
}
|
|
|
|
|
2020-11-22 14:45:24 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::oauth
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testOauth(): void
|
2020-11-22 14:45:24 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
$user1 = User::find(1);
|
|
|
|
$user2 = User::find(2);
|
|
|
|
$user3 = User::find(3);
|
|
|
|
|
|
|
|
(new OAuth(['provider' => 'test', 'identifier' => '1']))->user()->associate($user1)->save();
|
|
|
|
(new OAuth(['provider' => 'test', 'identifier' => '2']))->user()->associate($user2)->save();
|
|
|
|
(new OAuth(['provider' => 'another-provider', 'identifier' => 'usr3']))->user()->associate($user3)->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$oauth = $stats->oauth();
|
|
|
|
|
|
|
|
$this->assertCount(2, $oauth);
|
|
|
|
$this->assertEquals([
|
|
|
|
['provider' => 'another-provider', 'count' => 1],
|
|
|
|
['provider' => 'test', 'count' => 2],
|
|
|
|
], $oauth->toArray());
|
|
|
|
}
|
|
|
|
|
2019-08-21 14:22:31 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::databaseRead
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::databaseWrite
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDatabase(): void
|
2019-08-21 14:22:31 +02:00
|
|
|
{
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
|
|
|
|
$read = $stats->databaseRead();
|
|
|
|
$write = $stats->databaseWrite();
|
|
|
|
|
|
|
|
$this->assertIsFloat($read);
|
|
|
|
$this->assertNotEmpty($read);
|
|
|
|
$this->assertIsFloat($write);
|
|
|
|
$this->assertNotEmpty($write);
|
|
|
|
}
|
|
|
|
|
2018-12-18 04:43:13 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::logEntries
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testLogEntries(): void
|
2018-12-18 04:43:13 +01:00
|
|
|
{
|
|
|
|
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Some info']))->save();
|
|
|
|
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Another info']))->save();
|
|
|
|
(new LogEntry(['level' => LogLevel::CRITICAL, 'message' => 'A critical error!']))->save();
|
|
|
|
(new LogEntry(['level' => LogLevel::DEBUG, 'message' => 'Verbose output!']))->save();
|
|
|
|
(new LogEntry(['level' => LogLevel::INFO, 'message' => 'Shutdown initiated']))->save();
|
|
|
|
(new LogEntry(['level' => LogLevel::WARNING, 'message' => 'Please be cautious']))->save();
|
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(6, $stats->logEntries());
|
|
|
|
$this->assertEquals(3, $stats->logEntries(LogLevel::INFO));
|
|
|
|
$this->assertEquals(1, $stats->logEntries(LogLevel::DEBUG));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\Stats::passwordResets
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testPasswordResets(): void
|
2018-12-18 04:43:13 +01:00
|
|
|
{
|
|
|
|
$this->addUsers();
|
|
|
|
|
2019-10-08 14:18:51 +02:00
|
|
|
(new PasswordReset(['user_id' => 1, 'token' => 'loremIpsum123']))->save();
|
|
|
|
(new PasswordReset(['user_id' => 3, 'token' => '5omeR4nd0mTok3N']))->save();
|
2018-12-18 04:43:13 +01:00
|
|
|
|
|
|
|
$stats = new Stats($this->database);
|
|
|
|
$this->assertEquals(2, $stats->passwordResets());
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
/**
|
|
|
|
* Add some example users
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function addUsers(): void
|
2018-12-18 02:23:44 +01:00
|
|
|
{
|
|
|
|
$this->addUser();
|
2020-09-12 22:51:32 +02:00
|
|
|
$this->addUser([], ['shirt_size' => 'L'], ['email_human' => true, 'email_shiftinfo' => true]);
|
2021-11-30 00:19:44 +01:00
|
|
|
$this->addUser(['arrived' => 1], [], ['email_human' => true, 'email_goody' => true, 'email_news' => true]);
|
2021-04-13 12:16:08 +02:00
|
|
|
$this->addUser(['arrived' => 1], ['pronoun' => 'unicorn'], ['language' => 'lo_RM', 'email_shiftinfo' => true]);
|
2020-04-19 19:18:56 +02:00
|
|
|
$this->addUser(['arrived' => 1, 'got_voucher' => 2], ['shirt_size' => 'XXL'], ['language' => 'lo_RM']);
|
2021-12-19 18:38:42 +01:00
|
|
|
$this->addUser(
|
2023-12-28 17:56:56 +01:00
|
|
|
['arrived' => 1, 'got_voucher' => 9, 'force_active' => true, 'user_info' => 'Info'],
|
2021-12-19 18:38:42 +01:00
|
|
|
[],
|
|
|
|
['theme' => 1],
|
2024-03-26 21:02:05 +01:00
|
|
|
['drive_car' => true, 'drive_12t' => true, 'drive_confirmed' => true, 'ifsg_certificate_light' => true]
|
2021-12-19 18:38:42 +01:00
|
|
|
);
|
2021-04-13 12:16:08 +02:00
|
|
|
$this->addUser(
|
|
|
|
['arrived' => 1, 'got_voucher' => 3],
|
|
|
|
['pronoun' => 'per'],
|
2021-12-19 18:38:42 +01:00
|
|
|
['theme' => 1, 'email_human' => true],
|
2024-03-26 21:02:05 +01:00
|
|
|
[
|
|
|
|
'has_car' => true,
|
|
|
|
'drive_forklift' => true,
|
|
|
|
'drive_car' => true,
|
|
|
|
'ifsg_certificate' => true,
|
|
|
|
'ifsg_confirmed' => true,
|
|
|
|
]
|
2021-04-13 12:16:08 +02:00
|
|
|
);
|
2019-12-24 14:33:11 +01:00
|
|
|
$this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true, 'force_active' => true]);
|
2020-04-19 19:18:56 +02:00
|
|
|
$this->addUser(['arrived' => 1, 'active' => 1, 'got_shirt' => true], ['shirt_size' => 'L'], ['theme' => 4]);
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
protected function addUser(
|
|
|
|
array $state = [],
|
|
|
|
array $personalData = [],
|
|
|
|
array $settings = [],
|
|
|
|
array $license = []
|
|
|
|
): User {
|
2018-12-18 02:23:44 +01:00
|
|
|
$name = 'user_' . Str::random(5);
|
|
|
|
|
|
|
|
$user = new User([
|
|
|
|
'name' => $name,
|
|
|
|
'password' => '',
|
|
|
|
'email' => $name . '@engel.example.com',
|
|
|
|
'api_key' => '',
|
|
|
|
]);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$state = new State($state);
|
|
|
|
$state->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
2018-12-21 20:09:15 +01:00
|
|
|
|
|
|
|
$personalData = new PersonalData($personalData);
|
|
|
|
$personalData->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
2020-04-19 19:18:56 +02:00
|
|
|
|
|
|
|
$settings = new Settings(array_merge([
|
|
|
|
'language' => 'te_ST',
|
|
|
|
'theme' => 0,
|
2020-09-12 22:51:32 +02:00
|
|
|
'email_human' => false,
|
|
|
|
'email_shiftinfo' => false,
|
2020-04-19 19:18:56 +02:00
|
|
|
], $settings));
|
|
|
|
$settings->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
2020-09-12 22:51:32 +02:00
|
|
|
|
2021-12-19 18:38:42 +01:00
|
|
|
$license = new License($license);
|
|
|
|
$license->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
|
|
|
|
2020-09-12 22:51:32 +02:00
|
|
|
return $user;
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|
2019-11-10 21:30:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the environment
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->initDatabase();
|
|
|
|
}
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|