From ce607a9767696abd7719ee7e9e888b2a2b7b8bdd Mon Sep 17 00:00:00 2001 From: Niels Postma Date: Tue, 16 Apr 2024 20:32:53 +0200 Subject: [PATCH] Add causer and subject type scopes to Activity model --- src/Models/Activity.php | 12 +++++++ tests/ActivityModelTest.php | 70 +++++++++++++++++++++++++++++++++++++ tests/Models/Admin.php | 12 +++++++ tests/Models/Post.php | 8 +++++ tests/TestCase.php | 8 ++++- 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/Models/Admin.php create mode 100644 tests/Models/Post.php diff --git a/src/Models/Activity.php b/src/Models/Activity.php index abde1b6d..4c4d9c2b 100644 --- a/src/Models/Activity.php +++ b/src/Models/Activity.php @@ -108,6 +108,12 @@ public function scopeCausedBy(Builder $query, Model $causer): Builder ->where('causer_id', $causer->getKey()); } + public function scopeCausedByType(Builder $query, string|Model $type): Builder + { + $type = is_string($type) ? $type : $type->getMorphClass(); + return $query->where('causer_type', $type); + } + public function scopeForSubject(Builder $query, Model $subject): Builder { return $query @@ -115,6 +121,12 @@ public function scopeForSubject(Builder $query, Model $subject): Builder ->where('subject_id', $subject->getKey()); } + public function scopeForSubjectType(Builder $query, string|Model $type): Builder + { + $type = is_string($type) ? $type : $type->getMorphClass(); + return $query->where('subject_type', $type); + } + public function scopeForEvent(Builder $query, string $event): Builder { return $query->where('event', $event); diff --git a/tests/ActivityModelTest.php b/tests/ActivityModelTest.php index b751700a..758d54fc 100644 --- a/tests/ActivityModelTest.php +++ b/tests/ActivityModelTest.php @@ -2,7 +2,9 @@ use Illuminate\Database\Eloquent\Relations\Relation; use Spatie\Activitylog\Models\Activity; +use Spatie\Activitylog\Test\Models\Admin; use Spatie\Activitylog\Test\Models\Article; +use Spatie\Activitylog\Test\Models\Post; use Spatie\Activitylog\Test\Models\User; beforeEach(function () { @@ -55,6 +57,40 @@ expect($activities->first()->description)->toEqual('Foo'); }); +it('provides a scope to get log items for a specific causer class of given model', function () { + $subject = Article::first(); + $causer = User::first(); + + activity()->on($subject)->by($causer)->log('Foo'); + activity()->on($subject)->by(Admin::create([ + 'name' => 'An Admin User', + ]))->log('Bar'); + + $activities = Activity::causedByType($causer)->get(); + + expect($activities)->toHaveCount(1); + expect($activities->first()->causer_id)->toEqual($causer->getKey()); + expect($activities->first()->causer_type)->toEqual(get_class($causer)); + expect($activities->first()->description)->toEqual('Foo'); +}); + +it('provides a scope to get log items for a specific causer class of given class name', function () { + $subject = Article::first(); + $causer = User::first(); + + activity()->on($subject)->by($causer)->log('Foo'); + activity()->on($subject)->by(Admin::create([ + 'name' => 'An Admin User', + ]))->log('Bar'); + + $activities = Activity::causedByType(User::class)->get(); + + expect($activities)->toHaveCount(1); + expect($activities->first()->causer_id)->toEqual($causer->getKey()); + expect($activities->first()->causer_type)->toEqual(get_class($causer)); + expect($activities->first()->description)->toEqual('Foo'); +}); + it('provides a scope to get log items for a specific event', function () { $subject = Article::first(); activity() @@ -83,6 +119,40 @@ expect($activities->first()->description)->toEqual('Foo'); }); +it('provides a scope to get log items for a specific subject class of given model', function () { + $subject = Article::first(); + $causer = User::first(); + + activity()->on($subject)->by($causer)->log('Foo'); + activity()->on(Post::create([ + 'name' => 'A Post', + ]))->by($causer)->log('Bar'); + + $activities = Activity::forSubjectType($subject)->get(); + + expect($activities)->toHaveCount(1); + expect($activities->first()->subject_id)->toEqual($subject->getKey()); + expect($activities->first()->subject_type)->toEqual(get_class($subject)); + expect($activities->first()->description)->toEqual('Foo'); +}); + +it('provides a scope to get log items for a specific subject class of given class name', function () { + $subject = Article::first(); + $causer = User::first(); + + activity()->on($subject)->by($causer)->log('Foo'); + activity()->on(Post::create([ + 'name' => 'A Post', + ]))->by($causer)->log('Bar'); + + $activities = Activity::forSubjectType(Article::class)->get(); + + expect($activities)->toHaveCount(1); + expect($activities->first()->subject_id)->toEqual($subject->getKey()); + expect($activities->first()->subject_type)->toEqual(get_class($subject)); + expect($activities->first()->description)->toEqual('Foo'); +}); + it('provides a scope to get log items for a specific morphmapped causer', function () { Relation::morphMap([ 'articles' => 'Spatie\Activitylog\Test\Models\Article', diff --git a/tests/Models/Admin.php b/tests/Models/Admin.php new file mode 100644 index 00000000..aa679cb6 --- /dev/null +++ b/tests/Models/Admin.php @@ -0,0 +1,12 @@ +migrateActivityLogTable(); - $this->createTables('articles', 'users'); + $this->createTables('articles', 'posts', 'users', 'admins'); $this->seedModels(Article::class, User::class); } @@ -82,6 +84,10 @@ protected function createTables(...$tableNames) $table->decimal('price')->nullable(); $table->string('status')->nullable(); } + if ($tableName === 'posts') { + $table->integer('user_id')->unsigned()->nullable(); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + } }); }); }