opnform/tests/Feature/Forms/CleanDatabaseTest.php

49 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
use Illuminate\Support\Facades\Artisan;
it('check form statistic for views & submissions counts', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, []);
// Create 10 views & submissions (in the past of 1 day so that it's cleaned)
2024-02-23 10:54:12 +00:00
for ($i = 1; $i <= 10; $i++) {
2022-09-20 19:59:52 +00:00
$submission = $form->submissions()->create();
$submission->created_at = now()->subDay();
$submission->save();
$view = $form->views()->create();
$view->created_at = now()->subDay();
$view->save();
}
// Create a submission & a view for another date
$submission = $form->submissions()->create();
$submission->created_at = now()->subDays(2);
$submission->save();
$view = $form->views()->create();
$view->created_at = now()->subDays(2);
$view->save();
// Run Command
Artisan::call('forms:database-cleanup');
// Create 5 views & submissions
2024-02-23 10:54:12 +00:00
for ($i = 1; $i <= 5; $i++) {
2022-09-20 19:59:52 +00:00
$form->views()->create();
$form->submissions()->create();
}
// Now check counters
$statistics = $form->statistics()->get();
expect($form->views_count)->toBe(16);
expect($form->submissions_count)->toBe(16);
expect($form->views()->count())->toBe(5);
expect($form->submissions()->count())->toBe(16);
expect(count($statistics))->toBe(2); // 1 per day for 2 different dates
2024-02-23 10:54:12 +00:00
expect($statistics[0]['date'])->toBe(now()->subDays(2)->toDateString());
expect($statistics[0]['data'])->toBe(['views' => 1, 'submissions' => 1]);
expect($statistics[1]['date'])->toBe(now()->subDay()->toDateString());
expect($statistics[1]['data'])->toBe(['views' => 10, 'submissions' => 10]);
});