Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

3.0 KB
Raw Blame
<?php

use App\Livewire\Team\Invitations;
use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\TeamInvitation;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
    InstanceSettings::unguarded(fn () => InstanceSettings::query()->updateOrCreate(['id' => 0], ['fqdn' => null]));

    $this->team = Team::factory()->create();
    $this->owner = User::factory()->create();
    $this->team->members()->attach($this->owner->id, ['role' => 'owner']);

    $this->actingAs($this->owner);
    session(['currentTeam' => $this->team]);
});

it('preserves a provisional user when revoking their invitation fails', function () {
    $provisionalUser = User::factory()->create([
        'email' => '[email protected]',
        'email_verified_at' => null,
        'force_password_reset' => true,
    ]);
    $invitation = TeamInvitation::create([
        'team_id' => $this->team->id,
        'uuid' => 'failing-invitation-delete',
        'email' => $provisionalUser->email,
        'role' => 'member',
        'link' => 'http://example.test/invitations/failing-invitation-delete',
        'via' => 'link',
    ]);

    TeamInvitation::deleting(function (): void {
        throw new RuntimeException('Invitation deletion failed.');
    });

    Livewire::test(Invitations::class, [
        'invitations' => collect([$invitation]),
    ])
        ->call('deleteInvitation', $invitation->id)
        ->assertDispatched('error');

    $this->assertDatabaseHas('users', ['id' => $provisionalUser->id]);
    $this->assertDatabaseHas('team_invitations', ['id' => $invitation->id]);
});

it('sorts pending invitations by the chosen column and ignores unknown sort values', function () {
    $invite = fn (string $email, string $role, string $via) => TeamInvitation::create([
        'team_id' => $this->team->id,
        'uuid' => $email,
        'email' => $email,
        'role' => $role,
        'link' => "http://example.test/invitations/{$email}",
        'via' => $via,
    ]);
    $invitations = collect([
        $invite('[email protected]', 'member', 'email'),
        $invite('[email protected]', 'owner', 'link'),
        $invite('[email protected]', 'admin', 'email'),
    ]);

    Livewire::test(Invitations::class, ['invitations' => $invitations])
        ->assertSeeInOrder(['[email protected]', '[email protected]', '[email protected]'])
        ->call('setSort', 'email-asc')
        ->assertSeeInOrder(['[email protected]', '[email protected]', '[email protected]'])
        ->call('setSort', 'role-desc')
        ->assertSeeInOrder(['[email protected]', '[email protected]', '[email protected]'])
        ->call('setSort', 'role-asc')
        ->assertSeeInOrder(['[email protected]', '[email protected]', '[email protected]'])
        ->call('setSort', 'via-desc')
        ->assertSeeInOrder(['[email protected]', '[email protected]', '[email protected]'])
        ->call('setSort', 'link-asc')
        ->assertSet('sort', 'via-desc');
});