Skip to content

Commit

Permalink
Merge pull request #42 from binafy/feat/add-soft-delete
Browse files Browse the repository at this point in the history
[1.x] Add `SoftDelete` event to Actions
  • Loading branch information
milwad-dev authored Sep 9, 2024
2 parents b25040e + 166701a commit 928ec62
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/user-monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
'on_update' => true,
'on_destroy' => true,
'on_read' => true,
'on_restore' => false, // Release for next version :)
'on_restore' => false,
'on_replicate' => false,
],

Expand Down
10 changes: 5 additions & 5 deletions src/Traits/Actionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ protected static function boot(): void
});
}

// if (config('user-monitoring.action_monitoring.on_replicate', false)) {
// static::restored(function (mixed $model) {
// static::insertActionMonitoring($model, ActionType::ACTION_REPLICATE);
// });
// }TODO: Release next version
if (config('user-monitoring.action_monitoring.on_restore', false)) {
static::restored(function (mixed $model) {
static::insertActionMonitoring($model, ActionType::ACTION_RESTORED);
});
}
/*
* Events:
* trashed
Expand Down
54 changes: 54 additions & 0 deletions tests/Feature/ActionMonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,57 @@
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 3);
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
});

test('restore a model in acting monitoring', function () {
config()->set('user-monitoring.action_monitoring.on_restore', true);

$user = createUser();
auth()->login($user);

$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
'title' => 'milwad',
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
]);

$product->delete();
$product->restore();

// Assertions
expect(ActionMonitoring::query()->value('table_name'))
->toBe('products')
->and(ActionMonitoring::query()->where('id', 4)->value('action_type'))
->toBe(ActionType::ACTION_RESTORED)
->and($user->name)
->toBe(ActionMonitoring::first()->user->name);

// DB Assertions
assertDatabaseCount('products', 1);
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 4);
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
});

test('action not stored when the on_restore config is false', function () {
$user = createUser();
auth()->login($user);

$product = \Tests\SetUp\Models\ProductSoftDelete::query()->create([
'title' => 'milwad',
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
]);

$product->delete();
$product->restore();

// Assertions
expect(ActionMonitoring::query()->value('table_name'))
->toBe('products')
->and(ActionMonitoring::query()->where('id', 4)->value('action_type'))
->toBeNull()
->and($user->name)
->toBe(ActionMonitoring::first()->user->name);

// DB Assertions
assertDatabaseCount('products', 1);
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 3);
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();

$table->string('title');
$table->text('description')->nullable();
$table->softDeletes();

$table->timestamps();
});
}
Expand Down
17 changes: 17 additions & 0 deletions tests/SetUp/Models/ProductSoftDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\SetUp\Models;

use Binafy\LaravelUserMonitoring\Traits\Actionable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProductSoftDelete extends Model
{
use HasFactory, Actionable, SoftDeletes;

protected $guarded = [];

protected $table = 'products';
}

0 comments on commit 928ec62

Please sign in to comment.