1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
<?php
namespace Illuminate\Tests\Integration\Database;
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Orchestra\Testbench\TestCase;
class EloquentModelEncryptedDirtyTest extends TestCase
{
public function testDirtyAttributeBehaviorWithNoPreviousKeys()
{
config(['app.key' => str_repeat('a', 32)]);
Model::$encrypter = null;
$model = new EncryptedDirtyAttributeCast([
'secret' => 'some-secret',
'secret_array_object' => [1, 2, 3],
]);
$model->syncOriginal();
$this->assertFalse($model->isDirty('secret'));
$this->assertFalse($model->isDirty('secret_array_object'));
$model->secret = 'some-secret';
$model->secret_array_object = [1, 2, 3];
// Encrypted attributes should always be considered dirty if updated in any way because of rotatable encryption keys...
$this->assertFalse($model->isDirty('secret'));
$this->assertFalse($model->isDirty('secret_array_object'));
$model->secret = 'some-other-secret';
$model->secret_array_object = [4, 5, 6];
// Encrypted attributes should always be considered dirty if updated in any way because of rotatable encryption keys...
$this->assertTrue($model->isDirty('secret'));
$this->assertTrue($model->isDirty('secret_array_object'));
}
public function testDirtyAttributeBehaviorWithPreviousKeys()
{
config(['app.key' => str_repeat('a', 32)]);
config(['app.previous_keys' => [str_repeat('b', 32)]]);
Model::$encrypter = null;
$model = new EncryptedDirtyAttributeCast([
'secret' => 'some-secret',
'secret_array_object' => [1, 2, 3],
]);
$model->syncOriginal();
$this->assertFalse($model->isDirty('secret'));
$this->assertFalse($model->isDirty('secret_array_object'));
$model->secret = 'some-secret';
$model->secret_array_object = [1, 2, 3];
// Encrypted attributes should always be considered dirty if updated in any way because of rotatable encryption keys...
$this->assertTrue($model->isDirty('secret'));
$this->assertTrue($model->isDirty('secret_array_object'));
$model->secret = 'some-other-secret';
$model->secret_array_object = [4, 5, 6];
// Encrypted attributes should always be considered dirty if updated in any way because of rotatable encryption keys...
$this->assertTrue($model->isDirty('secret'));
$this->assertTrue($model->isDirty('secret_array_object'));
}
}
/**
* @property $secret
* @property $secret_array
* @property $secret_json
* @property $secret_object
* @property $secret_collection
*/
class EncryptedDirtyAttributeCast extends Model
{
protected $guarded = [];
public $casts = [
'secret' => 'encrypted',
'secret_array' => 'encrypted:array',
'secret_json' => 'encrypted:json',
'secret_object' => 'encrypted:object',
'secret_collection' => 'encrypted:collection',
'secret_array_object' => AsEncryptedArrayObject::class,
];
}
|