From 78e2707a0dfc428ebfd764a1b0fcc36c9e9df3ce Mon Sep 17 00:00:00 2001 From: Milwad Date: Mon, 9 Sep 2024 19:10:44 +0330 Subject: [PATCH 1/6] add `restored` event for Actionable --- src/Traits/Actionable.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Traits/Actionable.php b/src/Traits/Actionable.php index 4caf4c1..9b08d5b 100644 --- a/src/Traits/Actionable.php +++ b/src/Traits/Actionable.php @@ -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 From 181171afd4458e6e04230dc177e9a93dea41e2f6 Mon Sep 17 00:00:00 2001 From: Milwad Date: Mon, 9 Sep 2024 19:11:05 +0330 Subject: [PATCH 2/6] Create ProductSoftDelete.php --- tests/SetUp/Models/ProductSoftDelete.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/SetUp/Models/ProductSoftDelete.php diff --git a/tests/SetUp/Models/ProductSoftDelete.php b/tests/SetUp/Models/ProductSoftDelete.php new file mode 100644 index 0000000..28c4b34 --- /dev/null +++ b/tests/SetUp/Models/ProductSoftDelete.php @@ -0,0 +1,17 @@ + Date: Mon, 9 Sep 2024 19:11:10 +0330 Subject: [PATCH 3/6] Update 2023_07_23_155120_create_products_table.php --- .../Migrations/2023_07_23_155120_create_products_table.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php b/tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php index 265baa5..70e938c 100644 --- a/tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php +++ b/tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php @@ -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(); }); } From 57a1b0fa7c35b54871207a9685c27edf6025ea87 Mon Sep 17 00:00:00 2001 From: Milwad Date: Mon, 9 Sep 2024 19:16:54 +0330 Subject: [PATCH 4/6] add `restore a model in acting monitoring` test --- tests/Feature/ActionMonitoringTest.php | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Feature/ActionMonitoringTest.php b/tests/Feature/ActionMonitoringTest.php index 7970c24..2aefa6b 100644 --- a/tests/Feature/ActionMonitoringTest.php +++ b/tests/Feature/ActionMonitoringTest.php @@ -194,3 +194,31 @@ 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('/')]); +}); From 9e60e7a031990690d699868885239bb0398282e1 Mon Sep 17 00:00:00 2001 From: Milwad Date: Mon, 9 Sep 2024 19:18:21 +0330 Subject: [PATCH 5/6] add `action not stored when the on_restore config is false` test --- tests/Feature/ActionMonitoringTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Feature/ActionMonitoringTest.php b/tests/Feature/ActionMonitoringTest.php index 2aefa6b..60abe66 100644 --- a/tests/Feature/ActionMonitoringTest.php +++ b/tests/Feature/ActionMonitoringTest.php @@ -222,3 +222,29 @@ 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('/')]); +}); From 166701af53e5da4070684f5037b5b12af759e538 Mon Sep 17 00:00:00 2001 From: Milwad Date: Mon, 9 Sep 2024 19:18:39 +0330 Subject: [PATCH 6/6] Update user-monitoring.php --- config/user-monitoring.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/user-monitoring.php b/config/user-monitoring.php index b97e530..792b1d7 100644 --- a/config/user-monitoring.php +++ b/config/user-monitoring.php @@ -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, ],