Skip to content

Commit c277b85

Browse files
authored
Merge pull request #36 from binafy/add-replicate-event
[1.x] Add `replicate` event to ActionMonitoring
2 parents 78cd774 + e4e00a7 commit c277b85

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

config/user-monitoring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
'on_destroy' => true,
9393
'on_read' => true,
9494
'on_restore' => false, // Release for next version :)
95-
'on_replicate' => false, // Release for next version :)
95+
'on_replicate' => false,
9696
],
9797

9898
/*

src/Traits/Actionable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ protected static function boot(): void
3939
});
4040
}
4141

42-
// if (config('user-monitoring.action_monitoring.on_restore', false)) {
43-
// static::restored(function (mixed $model) {
44-
// static::insertActionMonitoring($model, ActionType::ACTION_RESTORED);
45-
// });
46-
// }TODO: Release next version
42+
if (config('user-monitoring.action_monitoring.on_replicate', false)) {
43+
static::replicating(function (mixed $model) {
44+
static::insertActionMonitoring($model, ActionType::ACTION_REPLICATE);
45+
});
46+
}
4747

4848
// if (config('user-monitoring.action_monitoring.on_replicate', false)) {
4949
// static::restored(function (mixed $model) {

tests/Feature/ActionMonitoringTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,32 @@
165165
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 2);
166166
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
167167
});
168+
169+
test('store action monitoring when a model replicate with login user', function () {
170+
config()->set('user-monitoring.action_monitoring.on_replicate', true);
171+
172+
$user = createUser();
173+
auth()->login($user);
174+
175+
$milwadPro = Product::query()->create([
176+
'title' => 'milwad',
177+
'description' => 'WE ARE HELPING TO OPEN-SOURCE WORLD'
178+
]);
179+
180+
$binafyPro = $milwadPro->replicate()->fill([
181+
'title' => 'binafy'
182+
])->save();
183+
184+
// Assertions
185+
expect(ActionMonitoring::query()->value('table_name'))
186+
->toBe('products')
187+
->and(ActionMonitoring::query()->where('id', 2)->value('action_type'))
188+
->toBe(ActionType::ACTION_REPLICATE)
189+
->and($user->name)
190+
->toBe(ActionMonitoring::first()->user->name);
191+
192+
// DB Assertions
193+
assertDatabaseCount('products', 2);
194+
assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 3);
195+
assertDatabaseHas(config('user-monitoring.action_monitoring.table'), ['page' => url('/')]);
196+
});

tests/SetUp/Migrations/2023_07_23_155120_create_products_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function up(): void
1414
Schema::create('products', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('title');
17+
$table->text('description')->nullable();
1718
$table->timestamps();
1819
});
1920
}

0 commit comments

Comments
 (0)