Skip to content

Commit b44be78

Browse files
committed
Fix activitylog cleanup to require integer days input
1 parent d52f99b commit b44be78

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/CleanActivitylogCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function handle()
2929
$log = $this->argument('log');
3030

3131
$maxAgeInDays = $this->option('days') ?? config('activitylog.delete_records_older_than_days');
32+
if (filter_var($maxAgeInDays, FILTER_VALIDATE_INT) === false) {
33+
$this->error('The days option must be an integer.');
34+
35+
return 1;
36+
}
37+
38+
$maxAgeInDays = (int) $maxAgeInDays;
3239

3340
$cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');
3441

tests/CleanActivitylogCommandTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,47 @@
4646

4747
expect(Activity::where('created_at', '<', $cutOffDate)->get())->toHaveCount(0);
4848
});
49+
50+
it('does not clean the activity log when days config value is invalid', function (mixed $days) {
51+
app()['config']->set('activitylog.delete_records_older_than_days', $days);
52+
53+
collect(range(1, 60))->each(function (int $index) {
54+
Activity::create([
55+
'description' => "item {$index}",
56+
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
57+
]);
58+
});
59+
60+
expect(Activity::all())->toHaveCount(60);
61+
62+
$exitCode = Artisan::call('activitylog:clean');
63+
64+
expect($exitCode)->toBe(1);
65+
expect(Activity::all())->toHaveCount(60);
66+
})->with([
67+
false,
68+
'abc',
69+
'7.5',
70+
'',
71+
]);
72+
73+
it('does not clean the activity log when days option value is invalid', function (mixed $days) {
74+
collect(range(1, 60))->each(function (int $index) {
75+
Activity::create([
76+
'description' => "item {$index}",
77+
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
78+
]);
79+
});
80+
81+
expect(Activity::all())->toHaveCount(60);
82+
83+
$exitCode = Artisan::call('activitylog:clean', ['--days' => $days]);
84+
85+
expect($exitCode)->toBe(1);
86+
expect(Activity::all())->toHaveCount(60);
87+
})->with([
88+
false,
89+
'abc',
90+
'7.5',
91+
'',
92+
]);

0 commit comments

Comments
 (0)