1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 20:34:04 +00:00

Test casting of all default 'encrypted' castables

This commit is contained in:
lukinovec 2023-08-08 13:58:29 +02:00
parent 885e2112c2
commit de9d9d7cab

View file

@ -7,6 +7,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Database\Eloquent\Model;
use Stancl\VirtualColumn\VirtualColumn;
use Illuminate\Contracts\Encryption\DecryptException;
class VirtualColumnTest extends TestCase
{
@ -110,23 +111,32 @@ class VirtualColumnTest extends TestCase
/** @test */
public function encrypted_casts_work_with_virtual_column() {
$model = MyModel::create(['password' => $password = 'foo']);
$model = MyModel::create([
'password' => $password = 'foo',
'array' => $array = ['foo', 'bar'],
'collection' => $collection = collect(['foo', 'bar']),
'json' => $json = json_encode(['foo', 'bar']),
'object' => $object = (object) json_encode(['foo', 'bar']),
// todo1 'custom' => 'foo',
]);
// Virtual column gets encrypted
$rawEncryptedPassword = (array) DB::table('my_models')
->select('data->password')
->where('id', $model->id)
->first();
$attributeGetsCastCorrectly = function (string $attribute, mixed $expectedValue) use ($model): void {
$savedValue = $model->getAttributes()[$attribute]; // Encrypted
$encryptedPassword = $rawEncryptedPassword[array_key_first((array) $rawEncryptedPassword)];
$this->assertTrue(VirtualColumn::valueEncrypted($savedValue));
$this->assertNotEquals($savedValue, $expectedValue);
$this->assertNotSame($encryptedPassword, $password);
$this->assertSame(Crypt::decryptString($encryptedPassword), $password);
$retrievedValue = $model->$attribute; // Decrypted
// Virtual column gets decrypted
$this->assertSame($model->password, $password);
$this->assertEquals($retrievedValue, $expectedValue);
};
// todo1 Check if *all* encrypted casts work
$attributeGetsCastCorrectly('password', $password); // 'encrypted'
$attributeGetsCastCorrectly('array', $array); // 'encrypted:array'
$attributeGetsCastCorrectly('collection', $collection); // 'encrypted:collection'
$attributeGetsCastCorrectly('json', $json); // 'encrypted:json'
$attributeGetsCastCorrectly('object', $object); // 'encrypted:object'
// todo1 $attributeGetsCastCorrectly('custom', $custom); // 'CustomCast::class'
}
}
@ -137,7 +147,12 @@ class MyModel extends Model
protected $guarded = [];
public $timestamps = false;
public $casts = [
'password' => 'encrypted'
'password' => 'encrypted',
'array' => 'encrypted:array',
'collection' => 'encrypted:collection',
'json' => 'encrypted:json',
'object' => 'encrypted:object',
// todo1 'custom' => CustomCast::class,
];
public static function getCustomColumns(): array