diff --git a/migrations/add_event_column_to_activity_log_table.php.stub b/migrations/add_event_column_to_activity_log_table.php.stub new file mode 100644 index 00000000..7006ee68 --- /dev/null +++ b/migrations/add_event_column_to_activity_log_table.php.stub @@ -0,0 +1,26 @@ +table(config('activitylog.table_name'), function (Blueprint $table) { + $table->string('event')->nullable()->after('subject_type'); + }); + } + + /** + * Reverse the migrations. + */ + public function down() + { + Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name')); + } +} diff --git a/src/ActivityLogger.php b/src/ActivityLogger.php index dd5a7be7..67ac5ba8 100644 --- a/src/ActivityLogger.php +++ b/src/ActivityLogger.php @@ -91,6 +91,18 @@ public function byAnonymous() return $this->causedByAnonymous(); } + public function event(string $event) + { + return $this->setEvent($event); + } + + public function setEvent(string $event) + { + $this->activity->event = $event; + + return $this; + } + public function withProperties($properties) { $this->getActivity()->properties = collect($properties); diff --git a/src/ActivitylogServiceProvider.php b/src/ActivitylogServiceProvider.php index bd7fc512..b7d53cc2 100644 --- a/src/ActivitylogServiceProvider.php +++ b/src/ActivitylogServiceProvider.php @@ -26,6 +26,15 @@ public function boot() __DIR__.'/../migrations/create_activity_log_table.php.stub' => database_path("/migrations/{$timestamp}_create_activity_log_table.php"), ], 'migrations'); } + + if (! class_exists('AddEventColumnToActivityLogTable')) { + $timestamp = date('Y_m_d_His', time() + 1); + + $this->publishes([ + __DIR__ + .'/../migrations/add_event_column_to_activity_log_table.php.stub' => database_path("/migrations/{$timestamp}_update_activity_log_table.php"), + ], 'migrations'); + } } public function register() diff --git a/src/Contracts/Activity.php b/src/Contracts/Activity.php index 63239c82..2e187729 100644 --- a/src/Contracts/Activity.php +++ b/src/Contracts/Activity.php @@ -21,5 +21,7 @@ public function scopeInLog(Builder $query, ...$logNames): Builder; public function scopeCausedBy(Builder $query, Model $causer): Builder; + public function scopeForEvent(Builder $query, string $event): Builder; + public function scopeForSubject(Builder $query, Model $subject): Builder; } diff --git a/src/Models/Activity.php b/src/Models/Activity.php index d9bc91b9..5ee4d0ab 100644 --- a/src/Models/Activity.php +++ b/src/Models/Activity.php @@ -85,4 +85,9 @@ public function scopeForSubject(Builder $query, Model $subject): Builder ->where('subject_type', $subject->getMorphClass()) ->where('subject_id', $subject->getKey()); } + + public function scopeForEvent(Builder $query, string $event): Builder + { + return $query->where('event', $event); + } } diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index 9ccc2080..23a8acbc 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -40,6 +40,7 @@ protected static function bootLogsActivity() $logger = app(ActivityLogger::class) ->useLog($logName) + ->event($eventName) ->performedOn($model) ->withProperties($attrs); diff --git a/tests/ActivityLoggerTest.php b/tests/ActivityLoggerTest.php index dd4bcf01..d614dedd 100644 --- a/tests/ActivityLoggerTest.php +++ b/tests/ActivityLoggerTest.php @@ -239,6 +239,18 @@ public function it_can_replace_the_placeholders() $this->assertEquals($expectedDescription, $this->getLastActivity()->description); } + /** @test */ + public function it_can_log_an_activity_with_event() + { + $article = Article::create(['name' => 'article name']); + activity() + ->performedOn($article) + ->event('create') + ->log('test event'); + + $this->assertEquals('create', $this->getLastActivity()->event); + } + /** @test */ public function it_will_not_replace_non_placeholders() { diff --git a/tests/ActivityModelTest.php b/tests/ActivityModelTest.php index c957653f..163cb526 100644 --- a/tests/ActivityModelTest.php +++ b/tests/ActivityModelTest.php @@ -70,6 +70,19 @@ public function it_provides_a_scope_to_get_log_items_for_a_specific_causer() $this->assertEquals('Foo', $activities->first()->description); } + /** @test */ + public function it_provides_a_scope_to_get_log_items_for_a_specific_event() + { + $subject = Article::first(); + activity() + ->on($subject) + ->event('create') + ->log('Foo'); + $activities = Activity::forEvent('create')->get(); + $this->assertCount(1, $activities); + $this->assertEquals('create', $activities->first()->event); + } + /** @test */ public function it_provides_a_scope_to_get_log_items_for_a_specific_subject() { diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index b6100c1e..54a8b532 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -43,6 +43,7 @@ public function it_will_log_the_creation_of_the_model() $this->assertInstanceOf(get_class($this->article), $this->getLastActivity()->subject); $this->assertEquals($article->id, $this->getLastActivity()->subject->id); $this->assertEquals('created', $this->getLastActivity()->description); + $this->assertEquals('created', $this->getLastActivity()->event); } /** @test */ @@ -74,6 +75,7 @@ public function it_can_switch_on_activity_logging_after_disabling_it() $this->assertInstanceOf(get_class($this->article), $this->getLastActivity()->subject); $this->assertEquals($article->id, $this->getLastActivity()->subject->id); $this->assertEquals('updated', $this->getLastActivity()->description); + $this->assertEquals('updated', $this->getLastActivity()->event); } /** @test */ @@ -99,6 +101,7 @@ public function it_will_log_an_update_of_the_model() $this->assertInstanceOf(get_class($this->article), $this->getLastActivity()->subject); $this->assertEquals($article->id, $this->getLastActivity()->subject->id); $this->assertEquals('updated', $this->getLastActivity()->description); + $this->assertEquals('updated', $this->getLastActivity()->event); } /** @test */ @@ -113,10 +116,11 @@ public function it_will_log_the_deletion_of_a_model_without_softdeletes() $article->save(); $this->assertEquals('created', $this->getLastActivity()->description); + $this->assertEquals('created', $this->getLastActivity()->event); $article->delete(); - $this->assertEquals('deleted', $this->getLastActivity()->description); + $this->assertEquals('deleted', $this->getLastActivity()->event); } /** @test */ @@ -131,12 +135,14 @@ public function it_will_log_the_deletion_of_a_model_with_softdeletes() $this->assertEquals(get_class($this->article), $this->getLastActivity()->subject_type); $this->assertEquals($article->id, $this->getLastActivity()->subject_id); $this->assertEquals('deleted', $this->getLastActivity()->description); + $this->assertEquals('deleted', $this->getLastActivity()->event); $article->forceDelete(); $this->assertCount(3, Activity::all()); $this->assertEquals('deleted', $this->getLastActivity()->description); + $this->assertEquals('deleted', $this->getLastActivity()->event); $this->assertNull($article->fresh()); } @@ -154,6 +160,7 @@ public function it_will_log_the_restoring_of_a_model_with_softdeletes() $this->assertEquals(get_class($this->article), $this->getLastActivity()->subject_type); $this->assertEquals($article->id, $this->getLastActivity()->subject_id); $this->assertEquals('restored', $this->getLastActivity()->description); + $this->assertEquals('restored', $this->getLastActivity()->event); } /** @test */ @@ -188,6 +195,7 @@ public function it_can_fetch_soft_deleted_models() $this->assertEquals(get_class($this->article), $this->getLastActivity()->subject_type); $this->assertEquals($article->id, $this->getLastActivity()->subject_id); $this->assertEquals('deleted', $this->getLastActivity()->description); + $this->assertEquals('deleted', $this->getLastActivity()->event); $this->assertEquals('changed name', $this->getLastActivity()->subject->name); } @@ -252,6 +260,7 @@ public function it_will_not_log_an_update_of_the_model_if_only_ignored_attribute $this->assertInstanceOf(get_class($articleClass), $this->getLastActivity()->subject); $this->assertEquals($article->id, $this->getLastActivity()->subject->id); $this->assertEquals('created', $this->getLastActivity()->description); + $this->assertEquals('created', $this->getLastActivity()->event); } /** @test */ @@ -326,7 +335,8 @@ public function tapActivity(Activity $activity, string $eventName) $this->assertInstanceOf(Collection::class, $firstActivity->properties); $this->assertEquals('value', $firstActivity->getExtraProperty('property.subProperty')); - $this->assertEquals('created', $firstActivity->getExtraProperty('event')); + $this->assertEquals('created', $firstActivity->description); + $this->assertEquals('created', $firstActivity->event); $this->assertEquals(Carbon::yesterday()->startOfDay()->format('Y-m-d H:i:s'), $firstActivity->created_at->format('Y-m-d H:i:s')); } @@ -350,6 +360,26 @@ public function tapActivity(Activity $activity, string $eventName) $this->assertEquals('my custom description', $firstActivity->description); } + /** @test */ + public function it_can_log_activity_when_event_is_changed_with_tap() + { + $model = new class() extends Article { + use LogsActivity; + + public function tapActivity(Activity $activity, string $eventName) + { + $activity->event = 'my custom event'; + } + }; + + $entity = new $model(); + $entity->save(); + + $firstActivity = $entity->activities()->first(); + + $this->assertEquals('my custom event', $firstActivity->event); + } + /** @test */ public function it_will_not_submit_log_when_there_is_no_changes() { diff --git a/tests/Models/Activity.php b/tests/Models/Activity.php index 0305b192..67301643 100644 --- a/tests/Models/Activity.php +++ b/tests/Models/Activity.php @@ -79,6 +79,11 @@ public function scopeForSubject(Builder $query, Model $subject): Builder ->where('subject_id', $subject->getKey()); } + public function scopeForEvent(Builder $query, string $event): Builder + { + return $query->where('event', $event); + } + public function getCustomPropertyAttribute() { return $this->changes(); diff --git a/tests/Models/AnotherInvalidActivity.php b/tests/Models/AnotherInvalidActivity.php index 19d38769..d77c6d1a 100644 --- a/tests/Models/AnotherInvalidActivity.php +++ b/tests/Models/AnotherInvalidActivity.php @@ -104,4 +104,9 @@ public function getCustomPropertyAttribute() { return $this->changes(); } + + public function scopeForEvent(Builder $query, string $event): Builder + { + return $query->where('event', $event); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 2aabb074..473e5927 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Spatie\Activitylog\Test; +use AddEventColumnToActivityLogTable; use CreateActivityLogTable; use Illuminate\Database\Schema\Blueprint; use Illuminate\Encryption\Encrypter; @@ -67,7 +68,10 @@ protected function createActivityLogTable() { include_once __DIR__.'/../migrations/create_activity_log_table.php.stub'; + include_once __DIR__.'/../migrations/add_event_column_to_activity_log_table.php.stub'; + (new CreateActivityLogTable())->up(); + (new AddEventColumnToActivityLogTable())->up(); } protected function createTables(...$tableNames)