From e15617a54cf3bf9ca7668e473e62f109fef353df Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 19 May 2020 16:00:46 +0200 Subject: [PATCH 1/3] #733 - do not call fresh() if retrieved event is processed --- src/Traits/DetectsChanges.php | 10 +++++++--- tests/LogsActivityTest.php | 15 +++++++++++++++ tests/Models/Issue733.php | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/Models/Issue733.php diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 75e7cfa6..2ed8436e 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -91,9 +91,13 @@ public function attributeValuesToBeLogged(string $processingEvent): array } $properties['attributes'] = static::logChanges( - $this->exists - ? $this->fresh() ?? $this - : $this + $processingEvent == 'retrieved' + ? $this + : ( + $this->exists + ? $this->fresh() ?? $this + : $this + ) ); if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index b6100c1e..ce51ccf8 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Collection; use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Test\Models\Article; +use Spatie\Activitylog\Test\Models\Issue733; use Spatie\Activitylog\Test\Models\User; use Spatie\Activitylog\Traits\LogsActivity; @@ -423,6 +424,20 @@ public function it_will_submit_a_log_with_json_changes() $this->assertSame($expectedChanges, $changes); } + /** @test */ + public function it_will_log_the_retrieval_of_the_model() + { + $article = Issue733::create(['name' => 'my name']); + + $retrieved = Issue733::whereKey($article->getKey())->first(); + $this->assertTrue($article->is($retrieved)); + + $this->assertInstanceOf(get_class($article), $this->getLastActivity()->subject); + $this->assertTrue($article->is($this->getLastActivity()->subject)); + $this->assertEquals('retrieved', $this->getLastActivity()->description); + $this->assertEquals('retrieved', $this->getLastActivity()->event); + } + public function loginWithFakeUser() { $user = new $this->user(); diff --git a/tests/Models/Issue733.php b/tests/Models/Issue733.php new file mode 100644 index 00000000..e2cab372 --- /dev/null +++ b/tests/Models/Issue733.php @@ -0,0 +1,18 @@ + Date: Tue, 19 May 2020 16:07:02 +0200 Subject: [PATCH 2/3] fix test --- tests/LogsActivityTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index ce51ccf8..c75d3f92 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -432,10 +432,12 @@ public function it_will_log_the_retrieval_of_the_model() $retrieved = Issue733::whereKey($article->getKey())->first(); $this->assertTrue($article->is($retrieved)); - $this->assertInstanceOf(get_class($article), $this->getLastActivity()->subject); - $this->assertTrue($article->is($this->getLastActivity()->subject)); - $this->assertEquals('retrieved', $this->getLastActivity()->description); - $this->assertEquals('retrieved', $this->getLastActivity()->event); + $activity = $this->getLastActivity(); + + $this->assertInstanceOf(get_class($article), $activity->subject); + $this->assertTrue($article->is($activity->subject)); + $this->assertEquals('retrieved', $activity->description); + $this->assertEquals('retrieved', $activity->event); } public function loginWithFakeUser() From 990c67579c3bc68ed9ca06ef0547d5fc81ad69e8 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 19 May 2020 16:08:12 +0200 Subject: [PATCH 3/3] event attribute is not available on current master --- tests/LogsActivityTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index c75d3f92..41847936 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -437,7 +437,6 @@ public function it_will_log_the_retrieval_of_the_model() $this->assertInstanceOf(get_class($article), $activity->subject); $this->assertTrue($article->is($activity->subject)); $this->assertEquals('retrieved', $activity->description); - $this->assertEquals('retrieved', $activity->event); } public function loginWithFakeUser()