From 2ee52985ece5204ec434b2d751f02f3a5f1450af Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Wed, 26 Jul 2023 16:31:50 +0330 Subject: [PATCH 01/10] Update ActionMonitoringTest.php --- tests/Feature/ActionMonitoringTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Feature/ActionMonitoringTest.php b/tests/Feature/ActionMonitoringTest.php index ba05b46..8dfb845 100644 --- a/tests/Feature/ActionMonitoringTest.php +++ b/tests/Feature/ActionMonitoringTest.php @@ -24,7 +24,8 @@ ->toBe('products') ->and(ActionMonitoring::query()->value('action_type')) ->toBe(ActionType::ACTION_STORE) - ->and($user->name)->toBe(ActionMonitoring::first()->user->name); + ->and($user->name) + ->toBe(ActionMonitoring::first()->user->name); // DB Assertions assertDatabaseCount(config('user-monitoring.action_monitoring.table'), 1); From 2d3c96b7839c6f7b97ead0167736eb18bf10e774 Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Wed, 26 Jul 2023 17:25:16 +0330 Subject: [PATCH 02/10] wip --- config/user-monitoring.php | 8 ++++ .../RemoveVisitMonitoringRecordsCommand.php | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Commands/RemoveVisitMonitoringRecordsCommand.php diff --git a/config/user-monitoring.php b/config/user-monitoring.php index 4a0a8a7..299422d 100644 --- a/config/user-monitoring.php +++ b/config/user-monitoring.php @@ -25,6 +25,14 @@ 'expect_pages' => [ // 'home', ], + + /* + * If you want to delete visit rows after some days, you can change this like 360, + * but you don't like to delete rows you can change it to 0. + * + * For this feature you need Task-Scheduling => https://laravel.com/docs/10.x/scheduling + */ + 'delete_days' => 0, ], /* diff --git a/src/Commands/RemoveVisitMonitoringRecordsCommand.php b/src/Commands/RemoveVisitMonitoringRecordsCommand.php new file mode 100644 index 0000000..89394b7 --- /dev/null +++ b/src/Commands/RemoveVisitMonitoringRecordsCommand.php @@ -0,0 +1,44 @@ +error('Your delete days are 0, You can go to the config file and change it!'); + return; + } + + $date = now()->subDays($days)->toDateString(); + $table = config('user-monitoring.visit_monitoring.table'); + + DB::table($table) + ->where('created_at', '<', $date) + ->delete(); + + $this->info('Records have been removed successfully!'); + } +} From 06818fa9df2392a8144eb9b28e7af92de9f1c01e Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Wed, 26 Jul 2023 17:25:49 +0330 Subject: [PATCH 03/10] fix namespace of RemoveVisitMonitoringRecordsCommand --- src/Commands/RemoveVisitMonitoringRecordsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/RemoveVisitMonitoringRecordsCommand.php b/src/Commands/RemoveVisitMonitoringRecordsCommand.php index 89394b7..4b91d90 100644 --- a/src/Commands/RemoveVisitMonitoringRecordsCommand.php +++ b/src/Commands/RemoveVisitMonitoringRecordsCommand.php @@ -1,6 +1,6 @@ Date: Wed, 26 Jul 2023 17:26:36 +0330 Subject: [PATCH 04/10] register command --- src/Providers/LaravelUserMonitoringServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Providers/LaravelUserMonitoringServiceProvider.php b/src/Providers/LaravelUserMonitoringServiceProvider.php index 120861a..4c3ef2d 100644 --- a/src/Providers/LaravelUserMonitoringServiceProvider.php +++ b/src/Providers/LaravelUserMonitoringServiceProvider.php @@ -2,6 +2,7 @@ namespace Binafy\LaravelUserMonitoring\Providers; +use Binafy\LaravelUserMonitoring\Commands\RemoveVisitMonitoringRecordsCommand; use Binafy\LaravelUserMonitoring\Middlewares\MonitorVisitMiddleware; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -18,6 +19,7 @@ public function register() $this->loadViewsFrom(__DIR__ . '/../../resources/views/', 'LaravelUserMonitoring'); $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); $this->mergeConfigFrom(__DIR__ . '/../../config/user-monitoring.php', 'user-monitoring'); + $this->commands(RemoveVisitMonitoringRecordsCommand::class); $this->app['router']->aliasMiddleware('monitor-visit-middleware', MonitorVisitMiddleware::class); $this->app->register(LaravelUserMonitoringEventServiceProvider::class); From d26d35bfd06762ea15a37018b0b87e58b5fe0a8a Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Wed, 26 Jul 2023 17:27:37 +0330 Subject: [PATCH 05/10] Create DeleteVisitMonitoringRecordsTest.php --- tests/Feature/DeleteVisitMonitoringRecordsTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/Feature/DeleteVisitMonitoringRecordsTest.php diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php new file mode 100644 index 0000000..fe4cace --- /dev/null +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -0,0 +1,8 @@ + Date: Mon, 31 Jul 2023 14:04:02 +0330 Subject: [PATCH 06/10] wip --- ...ware.php => VisitMonitoringMiddleware.php} | 2 +- .../LaravelUserMonitoringServiceProvider.php | 6 +-- .../DeleteVisitMonitoringRecordsTest.php | 40 ++++++++++++++++++- tests/TestCase.php | 4 +- 4 files changed, 45 insertions(+), 7 deletions(-) rename src/Middlewares/{MonitorVisitMiddleware.php => VisitMonitoringMiddleware.php} (97%) diff --git a/src/Middlewares/MonitorVisitMiddleware.php b/src/Middlewares/VisitMonitoringMiddleware.php similarity index 97% rename from src/Middlewares/MonitorVisitMiddleware.php rename to src/Middlewares/VisitMonitoringMiddleware.php index 345e9e4..9ab8c45 100644 --- a/src/Middlewares/MonitorVisitMiddleware.php +++ b/src/Middlewares/VisitMonitoringMiddleware.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\DB; use Jenssegers\Agent\Agent; -class MonitorVisitMiddleware +class VisitMonitoringMiddleware { /** * Handle. diff --git a/src/Providers/LaravelUserMonitoringServiceProvider.php b/src/Providers/LaravelUserMonitoringServiceProvider.php index 4c3ef2d..82d97cb 100644 --- a/src/Providers/LaravelUserMonitoringServiceProvider.php +++ b/src/Providers/LaravelUserMonitoringServiceProvider.php @@ -3,7 +3,7 @@ namespace Binafy\LaravelUserMonitoring\Providers; use Binafy\LaravelUserMonitoring\Commands\RemoveVisitMonitoringRecordsCommand; -use Binafy\LaravelUserMonitoring\Middlewares\MonitorVisitMiddleware; +use Binafy\LaravelUserMonitoring\Middlewares\VisitMonitoringMiddleware; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -21,11 +21,11 @@ public function register() $this->mergeConfigFrom(__DIR__ . '/../../config/user-monitoring.php', 'user-monitoring'); $this->commands(RemoveVisitMonitoringRecordsCommand::class); - $this->app['router']->aliasMiddleware('monitor-visit-middleware', MonitorVisitMiddleware::class); + $this->app['router']->aliasMiddleware('monitor-visit-middleware', VisitMonitoringMiddleware::class); $this->app->register(LaravelUserMonitoringEventServiceProvider::class); Route::middleware('web') - ->middleware(MonitorVisitMiddleware::class) + ->middleware(VisitMonitoringMiddleware::class) ->group(__DIR__ . '/../../routes/web.php'); } diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php index fe4cace..24a7d62 100644 --- a/tests/Feature/DeleteVisitMonitoringRecordsTest.php +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -1,8 +1,46 @@ insert([ + [ + 'user_id' => $user->id, + 'browser_name' => 'Chrome', + 'platform' => 'Windows', + 'device' => 'WebKit', + 'ip' => '127.0.0.1', + 'page' => 'http://localhost:8000', + 'created_at' => now()->subDays(2), + ], + [ + 'user_id' => $user->id, + 'browser_name' => 'Firefox', + 'platform' => 'Linux', + 'device' => 'WebKit', + 'ip' => '123.12.531.11', + 'page' => 'http://localhost:8000/milwad', + 'created_at' => now()->subDays(2), + ] + ]); + + artisan('laravel-user-monitoring:remove-visit-monitoring-records'); + + // DB Assertions + assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 0); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index f3eb507..633759f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,7 @@ namespace Tests; -use Binafy\LaravelUserMonitoring\Middlewares\MonitorVisitMiddleware; +use Binafy\LaravelUserMonitoring\Middlewares\VisitMonitoringMiddleware; use Binafy\LaravelUserMonitoring\Providers\LaravelUserMonitoringServiceProvider; use Illuminate\Encryption\Encrypter; use Illuminate\Support\Facades\Route; @@ -56,7 +56,7 @@ protected function setUp(): void { parent::setUp(); - Route::middleware([MonitorVisitMiddleware::class, 'web'])->group(__DIR__ . '/SetUp/Routes/web_tests.php'); + Route::middleware([VisitMonitoringMiddleware::class, 'web'])->group(__DIR__ . '/SetUp/Routes/web_tests.php'); $this->loadMigrationsFrom(__DIR__.'/SetUp/Migrations'); } From 9dde4374a0bc0637ad9426f6c3fd1e71f7329082 Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:19:19 +0330 Subject: [PATCH 07/10] fix DeleteVisitMonitoringRecordsTest test --- tests/Feature/DeleteVisitMonitoringRecordsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php index 24a7d62..4ee0e51 100644 --- a/tests/Feature/DeleteVisitMonitoringRecordsTest.php +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -13,7 +13,7 @@ test('rows are delete by 1 days', function () { // Set delete days - config(['user-monitoring.visit_monitoring.delete_days', 1]); + config()->set('user-monitoring.visit_monitoring.delete_days', 1); // Store old records for 1 day age $user = createUser(); From 6f6d949834e72a83b6813b59980256f9deaca1b5 Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:23:34 +0330 Subject: [PATCH 08/10] add `rows are not deleted after 1 day` test --- .../DeleteVisitMonitoringRecordsTest.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php index 4ee0e51..20cc533 100644 --- a/tests/Feature/DeleteVisitMonitoringRecordsTest.php +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -11,7 +11,7 @@ */ uses(RefreshDatabase::class); -test('rows are delete by 1 days', function () { +test('rows are delete after 1 days', function () { // Set delete days config()->set('user-monitoring.visit_monitoring.delete_days', 1); @@ -44,3 +44,37 @@ // DB Assertions assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 0); }); + +test('rows are not deleted after 1 day', function () { + // Set delete days + config()->set('user-monitoring.visit_monitoring.delete_days', 1); + + // Store old records for 1 day age + $user = createUser(); + + DB::table(config('user-monitoring.visit_monitoring.table'))->insert([ + [ + 'user_id' => $user->id, + 'browser_name' => 'Chrome', + 'platform' => 'Windows', + 'device' => 'WebKit', + 'ip' => '127.0.0.1', + 'page' => 'http://localhost:8000', + 'created_at' => now(), + ], + [ + 'user_id' => $user->id, + 'browser_name' => 'Firefox', + 'platform' => 'Linux', + 'device' => 'WebKit', + 'ip' => '123.12.531.11', + 'page' => 'http://localhost:8000/milwad', + 'created_at' => now(), + ] + ]); + + artisan('laravel-user-monitoring:remove-visit-monitoring-records'); + + // DB Assertions + assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 2); +}); From a095dd6bbde804b482870bd829fcb2c49fc567b1 Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:24:08 +0330 Subject: [PATCH 09/10] fix tests name --- tests/Feature/DeleteVisitMonitoringRecordsTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php index 20cc533..5c73d8f 100644 --- a/tests/Feature/DeleteVisitMonitoringRecordsTest.php +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -4,14 +4,13 @@ use Illuminate\Support\Facades\DB; use function Pest\Laravel\artisan; use function Pest\Laravel\assertDatabaseCount; -use function Pest\Laravel\assertDatabaseMissing; /* * Use `RefreshDatabase` for delete migration data for each test. */ uses(RefreshDatabase::class); -test('rows are delete after 1 days', function () { +test('rows are deleted after 1 day', function () { // Set delete days config()->set('user-monitoring.visit_monitoring.delete_days', 1); From 8a7175076a5a7f68d48410e3e4e198376ee7e5d4 Mon Sep 17 00:00:00 2001 From: miladev-ent <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:29:04 +0330 Subject: [PATCH 10/10] add `throw an error when days equal to 0` test --- tests/Feature/DeleteVisitMonitoringRecordsTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Feature/DeleteVisitMonitoringRecordsTest.php b/tests/Feature/DeleteVisitMonitoringRecordsTest.php index 5c73d8f..82d5f82 100644 --- a/tests/Feature/DeleteVisitMonitoringRecordsTest.php +++ b/tests/Feature/DeleteVisitMonitoringRecordsTest.php @@ -77,3 +77,8 @@ // DB Assertions assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 2); }); + +test('throw an error when days equal to 0', function () { + artisan('laravel-user-monitoring:remove-visit-monitoring-records') + ->expectsOutput('Your delete days are 0, You can go to the config file and change it!'); +});