get(route('recordings.index')) ->assertRedirect(route('login')); } public function test_login_page_is_shown(): void { $this->get(route('login')) ->assertOk() ->assertSee('Log in'); } public function test_register_page_is_shown(): void { $this->get(route('register')) ->assertOk() ->assertSee('Create an account'); } public function test_users_can_register_and_are_authenticated(): void { $response = $this->post(route('register.store'), [ 'name' => 'Ada Lovelace', 'email' => 'ada@example.com', 'password' => 'password', 'password_confirmation' => 'password', ]); $response->assertRedirect(route('recordings.index')); $this->assertAuthenticated(); $this->assertDatabaseHas('users', ['email' => 'ada@example.com']); } public function test_users_can_log_in(): void { $user = User::factory()->create([ 'email' => 'ben@example.com', 'password' => 'password', ]); $response = $this->post(route('login.store'), [ 'email' => 'ben@example.com', 'password' => 'password', ]); $response->assertRedirect(route('recordings.index')); $this->assertAuthenticatedAs($user); } public function test_first_registered_user_claims_orphaned_recordings(): void { Recording::query()->create([ 'title' => 'Orphan', 'original_filename' => 'orphan.mp3', 'file_path' => 'recordings/orphan.mp3', 'file_size_bytes' => 10, 'transcription_status' => 'pending', 'user_id' => null, ]); $this->post(route('register.store'), [ 'name' => 'First User', 'email' => 'first@example.com', 'password' => 'password', 'password_confirmation' => 'password', ])->assertRedirect(route('recordings.index')); $user = User::query()->where('email', 'first@example.com')->first(); $this->assertNotNull($user); $this->assertSame($user->id, Recording::query()->first()->user_id); } public function test_second_registered_user_does_not_claim_orphans(): void { User::factory()->create(); Recording::query()->create([ 'title' => 'Still orphan', 'original_filename' => 'still.mp3', 'file_path' => 'recordings/still.mp3', 'file_size_bytes' => 10, 'transcription_status' => 'pending', 'user_id' => null, ]); $this->post(route('register.store'), [ 'name' => 'Second User', 'email' => 'second@example.com', 'password' => 'password', 'password_confirmation' => 'password', ])->assertRedirect(route('recordings.index')); $this->assertNull(Recording::query()->where('title', 'Still orphan')->value('user_id')); } }