Skip to content

Commit

Permalink
Change null fields to be strictly compared when logging only dirty fi…
Browse files Browse the repository at this point in the history
…elds (#453)
  • Loading branch information
adamlehmann authored and AlexVanderbist committed Sep 4, 2019
1 parent 6135416 commit 9aec5d2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public function attributeValuesToBeLogged(string $processingEvent): array
$properties['attributes'],
$properties['old'],
function ($new, $old) {
if ($old === null || $new === null) {
return $new === $old ? 0 : 1;
}

return $new <=> $old;
}
);
Expand Down
86 changes: 86 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,92 @@ public function it_can_use_wildcard_when_updating_model()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_when_a_boolean_field_is_changed_from_null_to_false()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['*'];
public static $logOnlyDirty = true;

protected $casts = [
'text' => 'boolean',
];

use LogsActivity;
};

$user = User::create([
'name' => 'user name',
]);

Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$article = $articleClass::create([
'name' => 'article name',
'text' => null,
'user_id' => $user->id,
]);

$article->text = false;
Carbon::setTestNow(Carbon::create(2018, 1, 1, 12, 0, 0));
$article->save();

$expectedChanges = [
'attributes' => [
'text' => false,
'updated_at' => '2018-01-01 12:00:00',
],
'old' => [
'text' => null,
'updated_at' => '2017-01-01 12:00:00',
],
];

$this->assertSame($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_when_a_boolean_field_is_changed_from_false_to_null()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['*'];
public static $logOnlyDirty = true;

protected $casts = [
'text' => 'boolean',
];

use LogsActivity;
};

$user = User::create([
'name' => 'user name',
]);

Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$article = $articleClass::create([
'name' => 'article name',
'text' => false,
'user_id' => $user->id,
]);

$article->text = null;
Carbon::setTestNow(Carbon::create(2018, 1, 1, 12, 0, 0));
$article->save();

$expectedChanges = [
'attributes' => [
'text' => null,
'updated_at' => '2018-01-01 12:00:00',
],
'old' => [
'text' => false,
'updated_at' => '2017-01-01 12:00:00',
],
];

$this->assertSame($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_use_ignored_attributes_while_updating()
{
Expand Down

0 comments on commit 9aec5d2

Please sign in to comment.