Skip to content
Permalink
Browse files

Change null fields to be strictly compared when logging only dirty fi…

…elds (#453)
  • Loading branch information...
adamlehmann authored and AlexVanderbist committed Sep 4, 2019
1 parent 6135416 commit 9aec5d26e78fd69f2ff24b9bcdfe9006e107248b
Showing with 90 additions and 0 deletions.
  1. +4 −0 src/Traits/DetectsChanges.php
  2. +86 −0 tests/DetectsChangesTest.php
@@ -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;
}
);
@@ -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()
{

0 comments on commit 9aec5d2

Please sign in to comment.
You can’t perform that action at this time.