150 lines
4.3 KiB
PHP
150 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Projects;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\TestCase;
|
|
|
|
class ProjectControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Ensure guests cannot access authenticated project pages.
|
|
*/
|
|
public function test_guests_are_redirected_from_project_pages(): void
|
|
{
|
|
$this->get(route('projects.index'))
|
|
->assertRedirect(route('login'));
|
|
|
|
$this->get(route('projects.create'))
|
|
->assertRedirect(route('login'));
|
|
}
|
|
|
|
/**
|
|
* Ensure authenticated users can view the projects index.
|
|
*/
|
|
public function test_authenticated_users_can_view_the_projects_index(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$firstProject = Project::factory()->create([
|
|
'name' => 'Core Platform',
|
|
'key' => 'CORE',
|
|
]);
|
|
$secondProject = Project::factory()->create([
|
|
'name' => 'Authentication',
|
|
'key' => 'AUTH',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('projects.index'))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page
|
|
->component('projects/Index')
|
|
->has('projects', 2)
|
|
->where('projects.0.id', $secondProject->id)
|
|
->where('projects.1.id', $firstProject->id),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ensure authenticated users can view the project creation screen.
|
|
*/
|
|
public function test_authenticated_users_can_view_the_project_creation_screen(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('projects.create'))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page
|
|
->component('projects/Create'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ensure project keys are normalized to uppercase on creation.
|
|
*/
|
|
public function test_authenticated_users_can_create_projects_with_uppercase_keys(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)
|
|
->post(route('projects.store'), [
|
|
'name' => 'Kanban Foundation',
|
|
'key' => 'core',
|
|
]);
|
|
|
|
$project = Project::query()->firstOrFail();
|
|
|
|
$response->assertRedirect(route('projects.show', $project));
|
|
|
|
$this->assertDatabaseHas('projects', [
|
|
'name' => 'Kanban Foundation',
|
|
'key' => 'CORE',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Ensure project keys remain unique after normalization.
|
|
*/
|
|
public function test_project_keys_must_be_unique_case_insensitively(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
Project::factory()->create([
|
|
'key' => 'AUTH',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->from(route('projects.create'))
|
|
->post(route('projects.store'), [
|
|
'name' => 'Another Auth Project',
|
|
'key' => 'auth',
|
|
])
|
|
->assertSessionHasErrors('key')
|
|
->assertRedirect(route('projects.create'));
|
|
}
|
|
|
|
/**
|
|
* Ensure project keys cannot exceed five characters.
|
|
*/
|
|
public function test_project_keys_may_not_exceed_five_characters(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->from(route('projects.create'))
|
|
->post(route('projects.store'), [
|
|
'name' => 'Too Long',
|
|
'key' => 'KANBAN',
|
|
])
|
|
->assertSessionHasErrors('key')
|
|
->assertRedirect(route('projects.create'));
|
|
}
|
|
|
|
/**
|
|
* Ensure authenticated users can view the project hub page.
|
|
*/
|
|
public function test_authenticated_users_can_view_a_project_hub_page(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$project = Project::factory()->create([
|
|
'name' => 'Core Platform',
|
|
'key' => 'CORE',
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('projects.show', $project))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page
|
|
->component('projects/Show')
|
|
->where('project.id', $project->id)
|
|
->where('project.key', 'CORE'),
|
|
);
|
|
}
|
|
}
|