user = User::factory()->create(); } // -- Index -- public function test_guests_cannot_access_website_list(): void { $this->get(route('websites.index'))->assertRedirect(route('login')); } public function test_authenticated_user_can_see_their_websites(): void { $website = Website::factory()->for($this->user)->create(); Website::factory()->create(); // another user's website $response = $this->actingAs($this->user)->get(route('websites.index')); $response->assertOk(); $response->assertSeeText($website->name); } // -- Create / Store -- public function test_user_can_view_create_form(): void { $this->actingAs($this->user) ->get(route('websites.create')) ->assertOk(); } public function test_user_can_create_website(): void { $response = $this->actingAs($this->user)->post(route('websites.store'), [ 'name' => 'My Site', 'domain' => 'mysite.com', ]); $response->assertRedirect(route('websites.index')); $this->assertDatabaseHas('websites', [ 'user_id' => $this->user->id, 'name' => 'My Site', 'domain' => 'mysite.com', ]); } public function test_create_website_generates_tracking_id(): void { $this->actingAs($this->user)->post(route('websites.store'), [ 'name' => 'My Site', 'domain' => 'mysite.com', ]); $website = Website::where('domain', 'mysite.com')->first(); $this->assertNotNull($website->tracking_id); $this->assertStringStartsWith('TA-', $website->tracking_id); } public function test_create_website_validates_domain_format(): void { $response = $this->actingAs($this->user)->post(route('websites.store'), [ 'name' => 'My Site', 'domain' => 'not a domain!!', ]); $response->assertSessionHasErrors('domain'); } public function test_create_website_requires_name(): void { $response = $this->actingAs($this->user)->post(route('websites.store'), [ 'name' => '', 'domain' => 'mysite.com', ]); $response->assertSessionHasErrors('name'); } // -- Edit / Update -- public function test_owner_can_edit_website(): void { $website = Website::factory()->for($this->user)->create(); $this->actingAs($this->user) ->get(route('websites.edit', $website)) ->assertOk(); } public function test_non_owner_cannot_edit_website(): void { $otherUser = User::factory()->create(); $website = Website::factory()->for($otherUser)->create(); $this->actingAs($this->user) ->get(route('websites.edit', $website)) ->assertForbidden(); } public function test_owner_can_update_website(): void { $website = Website::factory()->for($this->user)->create(); $response = $this->actingAs($this->user)->put(route('websites.update', $website), [ 'name' => 'Updated Name', 'domain' => 'updated.com', ]); $response->assertRedirect(route('dashboard.show', $website)); $this->assertDatabaseHas('websites', [ 'id' => $website->id, 'name' => 'Updated Name', 'domain' => 'updated.com', ]); } public function test_non_owner_cannot_update_website(): void { $otherUser = User::factory()->create(); $website = Website::factory()->for($otherUser)->create(); $this->actingAs($this->user) ->put(route('websites.update', $website), [ 'name' => 'Hacked', 'domain' => 'hacked.com', ]) ->assertForbidden(); } // -- Delete -- public function test_owner_can_delete_website(): void { $website = Website::factory()->for($this->user)->create(); $response = $this->actingAs($this->user) ->delete(route('websites.destroy', $website)); $response->assertRedirect(route('websites.index')); $this->assertDatabaseMissing('websites', ['id' => $website->id]); } public function test_non_owner_cannot_delete_website(): void { $otherUser = User::factory()->create(); $website = Website::factory()->for($otherUser)->create(); $this->actingAs($this->user) ->delete(route('websites.destroy', $website)) ->assertForbidden(); $this->assertDatabaseHas('websites', ['id' => $website->id]); } // -- Show redirects to dashboard -- public function test_show_redirects_to_dashboard(): void { $website = Website::factory()->for($this->user)->create(); $this->actingAs($this->user) ->get(route('websites.show', $website)) ->assertRedirect(route('dashboard.show', $website)); } // -- Snippet -- public function test_owner_can_view_snippet(): void { $website = Website::factory()->for($this->user)->create(); $this->actingAs($this->user) ->get(route('websites.snippet', $website)) ->assertOk() ->assertSeeText($website->tracking_id); } public function test_non_owner_cannot_view_snippet(): void { $otherUser = User::factory()->create(); $website = Website::factory()->for($otherUser)->create(); $this->actingAs($this->user) ->get(route('websites.snippet', $website)) ->assertForbidden(); } }