Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/anonymous causer #605

Merged
merged 11 commits into from
Oct 6, 2019
Merged
18 changes: 18 additions & 0 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function on(Model $model)
return $this->performedOn($model);
}

public function onAnonymous()
{
return $this->causedByAnonymous();
}

public function causedBy($modelOrId)
{
if ($modelOrId === null) {
Expand All @@ -72,11 +77,24 @@ public function causedBy($modelOrId)
return $this;
}

public function causedByAnonymous()
Gummibeer marked this conversation as resolved.
Show resolved Hide resolved
{
$this->activity->causer_id = null;
$this->activity->causer_type = null;

return $this;
}

public function by($modelOrId)
{
return $this->causedBy($modelOrId);
}

public function byAnonymous()
{
return $this->causedByAnonymous();
}

public function withProperties($properties)
{
$this->getActivity()->properties = collect($properties);
Expand Down
30 changes: 30 additions & 0 deletions tests/ActivityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ 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
*
* @requires !Travis
*/
public function it_will_override_the_logged_in_user_as_the_causer_when_an_anonymous_causer_is_specified()
Jhnbrn90 marked this conversation as resolved.
Show resolved Hide resolved
{
$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()
{
Expand Down