Skip to content

Commit 928ec62

Browse files
authored
Merge pull request #42 from binafy/feat/add-soft-delete
[1.x] Add `SoftDelete` event to Actions
2 parents b25040e + 166701a commit 928ec62

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

config/user-monitoring.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
'on_update' => true,
9292
'on_destroy' => true,
9393
'on_read' => true,
94-
'on_restore' => false, // Release for next version :)
94+
'on_restore' => false,
9595
'on_replicate' => false,
9696
],
9797

src/Traits/Actionable.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ protected static function boot(): void
4545
});
4646
}
4747

48-
// if (config('user-monitoring.action_monitoring.on_replicate', false)) {
49-
// static::restored(function (mixed $model) {
50-
// static::insertActionMonitoring($model, ActionType::ACTION_REPLICATE);
51-
// });
52-
// }TODO: Release next version
48+
if (config('user-monitoring.action_monitoring.on_restore', false)) {
49+
static::restored(function (mixed $model) {
50+
static::insertActionMonitoring($model, ActionType::ACTION_RESTORED);
51+
});
52+
}
5353
/*
5454
* Events:
5555
* trashed

tests/Feature/ActionMonitoringTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,57 @@
194194
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 3);
195195
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
196196
});
197+
198+
test('restore a model in acting monitoring', function () {
199+
config()->set('user-monitoring.action_monitoring.on_restore', true);
200+
201+
$user = createUser();
202+
auth()->login($user);
203+
204+
$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
205+
'title' => 'milwad',
206+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
207+
]);
208+
209+
$product->delete();
210+
$product->restore();
211+
212+
// Assertions
213+
expect(ActionMonitoring::query()->value('table_name'))
214+
->toBe('products')
215+
->and(ActionMonitoring::query()->where('id', 4)->value('action_type'))
216+
->toBe(ActionType::ACTION_RESTORED)
217+
->and($user->name)
218+
->toBe(ActionMonitoring::first()->user->name);
219+
220+
// DB Assertions
221+
assertDatabaseCount('products', 1);
222+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 4);
223+
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
224+
});
225+
226+
test('action not stored when the on_restore config is false', function () {
227+
$user = createUser();
228+
auth()->login($user);
229+
230+
$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
231+
'title' => 'milwad',
232+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
233+
]);
234+
235+
$product->delete();
236+
$product->restore();
237+
238+
// Assertions
239+
expect(ActionMonitoring::query()->value('table_name'))
240+
->toBe('products')
241+
->and(ActionMonitoring::query()->where('id', 4)->value('action_type'))
242+
->toBeNull()
243+
->and($user->name)
244+
->toBe(ActionMonitoring::first()->user->name);
245+
246+
// DB Assertions
247+
assertDatabaseCount('products', 1);
248+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 3);
249+
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
250+
});

tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ public function up(): void
1313
{
1414
Schema::create('products', function (Blueprint $table) {
1515
$table->id();
16+
1617
$table->string('title');
1718
$table->text('description')->nullable();
19+
$table->softDeletes();
20+
1821
$table->timestamps();
1922
});
2023
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\SetUp\Models;
4+
5+
use Binafy\LaravelUserMonitoring\Traits\Actionable;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
10+
class ProductSoftDelete extends Model
11+
{
12+
use HasFactory, Actionable, SoftDeletes;
13+
14+
protected $guarded = [];
15+
16+
protected $table = 'products';
17+
}

0 commit comments

Comments
 (0)