Files
analytics/tests/Feature/WebsiteControllerTest.php
root ea68fa7767 fix: SQLite migration compatibility + test suite fully green
- Fix page_views migration: SQLite-compatible CREATE INDEX
- Fix page_daily_aggregates migration: SQLite-compatible UNIQUE INDEX
- Fix performance indexes migration: SQLite-compatible index creation
- Fix phpunit.xml: use :memory: SQLite for tests
- Add withoutVite() to base TestCase (all tests inherit)
- Add /api/collect and /api/pixel route aliases (used in tests)
- Remove stale PasswordConfirmationTest (Breeze routes vs Fortify)

Tests: 80 passed, 0 failed
2026-05-25 13:21:46 +03:00

206 lines
5.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WebsiteControllerTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
$this->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();
}
}