diff --git a/src/ActivityLogger.php b/src/ActivityLogger.php index 147a6e90..40532ebc 100644 --- a/src/ActivityLogger.php +++ b/src/ActivityLogger.php @@ -77,6 +77,19 @@ public function by($modelOrId) return $this->causedBy($modelOrId); } + public function causedByAnonymous() + { + $this->activity->causer_id = null; + $this->activity->causer_type = null; + + return $this; + } + + public function byAnonymous() + { + return $this->causedByAnonymous(); + } + public function withProperties($properties) { $this->getActivity()->properties = collect($properties); diff --git a/tests/ActivityLoggerTest.php b/tests/ActivityLoggerTest.php index 5040f952..8cb7bf34 100644 --- a/tests/ActivityLoggerTest.php +++ b/tests/ActivityLoggerTest.php @@ -195,6 +195,32 @@ public function it_will_use_the_logged_in_user_as_the_causer_by_default() $this->assertEquals($userId, $this->getLastActivity()->causer->id); } + /** @test */ + public function it_can_log_activity_using_an_anonymous_causer() + { + activity() + ->causedByAnonymous() + ->log('hello poetsvrouwman'); + + $this->assertNull($this->getLastActivity()->causer_id); + $this->assertNull($this->getLastActivity()->causer_type); + } + + /** @test */ + public function it_will_override_the_logged_in_user_as_the_causer_when_an_anonymous_causer_is_specified() + { + $userId = 1; + + Auth::login(User::find($userId)); + + activity() + ->byAnonymous() + ->log('hello poetsvrouwman'); + + $this->assertNull($this->getLastActivity()->causer_id); + $this->assertNull($this->getLastActivity()->causer_type); + } + /** @test */ public function it_can_replace_the_placeholders() {