Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Support/EloquentCasts/DataCollectionEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public function set($model, string $key, $value, array $attributes): ?string

public function compare($model, string $key, $firstValue, $secondValue): bool
{
if (in_array('encrypted', $this->arguments) && ! empty(Crypt::getPreviousKeys())) {
return false;
}

return $this->get($model, $key, $firstValue, [])->toArray() === $this->get($model, $key, $secondValue, [])->toArray();
}

Expand Down
4 changes: 4 additions & 0 deletions src/Support/EloquentCasts/DataEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function set($model, string $key, $value, array $attributes): ?string

public function compare($model, string $key, $firstValue, $secondValue): bool
{
if (in_array('encrypted', $this->arguments) && ! empty(Crypt::getPreviousKeys())) {
return false;
}

return $this->get($model, $key, $firstValue, [])?->toArray() === $this->get($model, $key, $secondValue, [])?->toArray();
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Support/EloquentCasts/DataCollectionEloquentCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Spatie\LaravelData\Tests\Fakes\Models\DummyModelWithCasts;
use Spatie\LaravelData\Tests\Fakes\Models\DummyModelWithCustomCollectionCasts;
use Spatie\LaravelData\Tests\Fakes\Models\DummyModelWithDefaultCasts;
use Spatie\LaravelData\Tests\Fakes\Models\DummyModelWithEncryptedCasts;
use Spatie\LaravelData\Tests\Fakes\Models\DummyModelWithJson;
use Spatie\LaravelData\Tests\Fakes\MultiData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
Expand Down Expand Up @@ -265,3 +266,33 @@ public function __construct(public string $b)
->and($model->getAttributes()['data_collection'])->toBe('[{"first":"First","second":"Second"},{"first":"Third","second":"Fourth"}]')
->and($model->isDirty('data_collection'))->toBeFalse();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));

it('flags the attribute as dirty when it is encrypted and there are previous encryption keys', function () {
config()->set('app.previous_keys', ['base64:'.base64_encode(random_bytes(32))]);

$model = new DummyModelWithEncryptedCasts();
$model->data_collection = [
new SimpleData('First'),
new SimpleData('Second'),
];
$model->save();

$model->data_collection = $model->data_collection;

expect($model->getRawOriginal('data_collection'))->not->toBe($model->getAttributes()['data_collection'])
->and($model->isDirty('data_collection'))->toBeTrue();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));

it('does not flag the attribute as dirty when it is encrypted and there are no previous encryption keys', function () {
$model = new DummyModelWithEncryptedCasts();
$model->data_collection = [
new SimpleData('First'),
new SimpleData('Second'),
];
$model->save();

$model->data_collection = $model->data_collection;

expect($model->getRawOriginal('data_collection'))->not->toBe($model->getAttributes()['data_collection'])
->and($model->isDirty('data_collection'))->toBeFalse();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));
24 changes: 24 additions & 0 deletions tests/Support/EloquentCasts/DataEloquentCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,30 @@ public function __construct(public string $a)
->and($model->isDirty('data'))->toBeTrue();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));

it('flags the attribute as dirty when it is encrypted and there are previous encryption keys', function () {
config()->set('app.previous_keys', ['base64:'.base64_encode(random_bytes(32))]);

$model = new DummyModelWithEncryptedCasts();
$model->data = new SimpleData('First');
$model->save();

$model->data = $model->data;

expect($model->getRawOriginal('data'))->not->toBe($model->getAttributes()['data'])
->and($model->isDirty('data'))->toBeTrue();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));

it('does not flag the attribute as dirty when it is encrypted and there are no previous encryption keys', function () {
$model = new DummyModelWithEncryptedCasts();
$model->data = new SimpleData('First');
$model->save();

$model->data = $model->data;

expect($model->getRawOriginal('data'))->not->toBe($model->getAttributes()['data'])
->and($model->isDirty('data'))->toBeFalse();
})->skip(fn () => version_compare(app()->version(), '12.18.0', '<'));

it('can update a model where the cast is initially null', function () {
$model = DummyModelWithCasts::create([
'data' => null,
Expand Down