Skip to content

Commit

Permalink
Handle encrypted columns which are set to null (#19)
Browse files Browse the repository at this point in the history
* 🧪 Add a test for a null value

* 🐛 Don't consider null types for decryption
  • Loading branch information
22Nick22 authored Apr 7, 2024
1 parent 7371aac commit 65f9000
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function decodeVirtualColumn(): void
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);

if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
if ($value && $attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
$this->attributes[$key] = $value;
} else {
$this->setAttribute($key, $value);
Expand Down
8 changes: 6 additions & 2 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,16 @@ public function encrypted_casts_work_with_virtual_column() {
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
'custom' => 'foo', // Custom castable – 'EncryptedCast::class'
'null_value' => null, // 'encrypted'
]);

foreach($encryptedAttributes as $key => $expectedValue) {
$savedValue = $model->getAttributes()[$key]; // Encrypted

$this->assertTrue($model->valueEncrypted($savedValue));
$this->assertNotEquals($expectedValue, $savedValue);
if ($savedValue !== null) {
$this->assertTrue($model->valueEncrypted($savedValue));
$this->assertNotEquals($expectedValue, $savedValue);
}

$retrievedValue = $model->$key; // Decrypted

Expand Down Expand Up @@ -178,6 +181,7 @@ class MyModel extends ParentModel
'json' => 'encrypted:json',
'object' => 'encrypted:object',
'custom' => EncryptedCast::class,
'null_value' => 'encrypted',
];
}

Expand Down

0 comments on commit 65f9000

Please sign in to comment.