Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
potential laravel 5.5/5.6/5.7 fix for newly created models re: #30
Browse files Browse the repository at this point in the history
  • Loading branch information
austinheap committed Nov 20, 2018
1 parent 33db743 commit 8fffdca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Traits/HasEncryptedAttributes.php
Expand Up @@ -318,6 +318,6 @@ protected function getArrayableAttributes()
*/
public function getAttributes()
{
return $this->doDecryptAttributes(parent::getAttributes());
return $this->exists ? $this->doDecryptAttributes(parent::getAttributes()) : parent::getAttributes();
}
}
27 changes: 22 additions & 5 deletions tests/Traits/DatabaseTest.php
Expand Up @@ -42,39 +42,56 @@ public function testUpdate()

public function testUpdateShouldBeEncrypted()
{
$model = DatabaseModel::create($this->randomValues());
$strings = $this->randomValues();
$model = DatabaseModel::create($strings);

$this->assertTrue($model->exists);
$this->assertTrue(self::callProtectedMethod($model, 'shouldEncrypt', ['should_be_encrypted']));

$this->assertNotEquals($strings['should_be_encrypted'], $model->getOriginal('should_be_encrypted'));
$this->assertEquals($strings['should_be_encrypted'], $model->should_be_encrypted);

$strings = $this->randomValues();
$new_model = DatabaseModel::findOrFail($model->id);
$new_model->update(['should_be_encrypted' => $strings['should_be_encrypted']]);

$this->assertTrue(self::callProtectedMethod($model, 'shouldEncrypt', ['should_be_encrypted']));

$new_model->update(['should_be_encrypted' => $strings['should_be_encrypted']]);

$this->assertNotEquals($model->getOriginal('should_be_encrypted'), $new_model->getOriginal('should_be_encrypted'));
$this->assertNotEquals($model->should_be_encrypted, $new_model->should_be_encrypted);
$this->assertEquals($model->getOriginal('shouldnt_be_encrypted'), $new_model->getOriginal('shouldnt_be_encrypted'));
$this->assertEquals($model->shouldnt_be_encrypted, $new_model->shouldnt_be_encrypted);
}

public function testUpdateShouldntBeEncrypted()
{
$model = DatabaseModel::create($this->randomValues());
$strings = $this->randomValues();
$model = DatabaseModel::create($strings);

$this->assertTrue($model->exists);
$this->assertFalse(self::callProtectedMethod($model, 'shouldEncrypt', ['shouldnt_be_encrypted']));

$this->assertEquals($strings['shouldnt_be_encrypted'], $model->getOriginal('shouldnt_be_encrypted'));
$this->assertEquals($strings['shouldnt_be_encrypted'], $model->shouldnt_be_encrypted);

$strings = $this->randomValues();
$new_model = DatabaseModel::findOrFail($model->id);

$this->assertFalse(self::callProtectedMethod($model, 'shouldEncrypt', ['shouldnt_be_encrypted']));

$new_model->update(['shouldnt_be_encrypted' => $strings['shouldnt_be_encrypted']]);

$this->assertFalse(self::callProtectedMethod($model, 'shouldEncrypt', ['shouldnt_be_encrypted']));
$this->assertEquals($model->getOriginal('should_be_encrypted'), $new_model->getOriginal('should_be_encrypted'));
$this->assertEquals($model->should_be_encrypted, $new_model->should_be_encrypted);
$this->assertNotEquals($model->getOriginal('shouldnt_be_encrypted'), $new_model->getOriginal('shouldnt_be_encrypted'));
$this->assertNotEquals($model->shouldnt_be_encrypted, $new_model->shouldnt_be_encrypted);
}

public function testGetArrayableAttributes()
{
$strings = $this->randomValues();
$model = DatabaseModel::create($strings);
$model = DatabaseModel::create($strings);

$this->assertTrue($model->exists);

Expand Down

0 comments on commit 8fffdca

Please sign in to comment.