vincent15000's avatar

Problem with PHPUnit and encrypted casting fields while upgrading from Laravel 8 to Laravel 11

Hello,

While upgrading from Laravel 8 to 11, I have a problem to test some functionalities with models using the encrypted casting for some fields.

I get an error saying that the payload is not valid.

I really don't see what I have to change so that the tests work.

Any idea ?

Thanks for your help.

V

0 likes
5 replies
vincent15000's avatar

Is there any reason why it could fail ?

use RefreshDatabase;

protected function setUp(): void
{
    parent::setUp();
    
    $this->app->make(PermissionRegistrar::class)->forgetCachedPermissions();
}

public function test_user_without_permission_cannot_view_contacts_entreprise()
{
    $user = User::factory()->actif()->create();
    $entreprise = Entreprise::factory()->create();
    $contact = Contact::factory()->create(['entreprise_id' => $entreprise->id]);

    $response = $this->actingAs($user, 'web')->get("/entreprises/{$entreprise->id}/contacts");

    $response->assertForbidden();
}
JussiMannisto's avatar
Level 50

It would be easier if you included the full error message, but it sounds like the column isn't encrypted on creation, and decryption then fails because it isn't valid ciphertext.

Show the factories and classes for those models that use encrypted fields.

1 like
vincent15000's avatar

I don't have any other error message than the one saying that the payload is invalid.

It was effectively the case.

In the factory, I just feed some fields, but not the encrypted fields.

I have added fake datas for the encrypted fields and it works.

Please or to participate in this conversation.