Add reusable project templates

This commit is contained in:
2026-05-25 01:26:08 +00:00
parent 3b17c9ee14
commit 58d2edcacd
24 changed files with 1154 additions and 88 deletions
+46
View File
@@ -6,6 +6,7 @@ use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
use App\Actions\Projects\DuplicateProjectConfiguration;
use App\Models\BoardColumn;
use App\Models\Project;
use App\Models\ProjectTemplate;
use App\Models\TicketStatus;
use App\Models\TicketType;
use App\Models\User;
@@ -55,6 +56,51 @@ class ProjectApiTest extends TestCase
$this->assertSame(1, $project->ticketStatuses()->count());
}
public function test_users_can_create_projects_from_templates_via_api(): void
{
$user = User::factory()->create();
$this->syncGlobalPermissions($user, ['create']);
$template = ProjectTemplate::factory()->create([
'name' => 'API Template',
'slug' => 'api-template',
'copy_areas' => [
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
DuplicateProjectConfiguration::COPY_TICKET_TYPES,
],
'configuration_snapshot' => [
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS => [
'show_author' => false,
'show_story_points' => false,
],
DuplicateProjectConfiguration::COPY_TICKET_TYPES => [
[
'name' => 'Spike',
'position' => 1,
'is_parent' => false,
],
],
],
]);
$this->withApplicationKey($user)
->postJson(route('api.projects.store'), [
'key' => 'tpl1',
'name' => 'Template API Project',
'creation_mode' => 'template',
'source_template_id' => $template->id,
])
->assertCreated()
->assertJsonPath('data.key', 'TPL1')
->assertJsonPath('data.name', 'Template API Project');
$project = Project::query()->where('key', 'TPL1')->firstOrFail();
$this->assertFalse($project->show_author);
$this->assertFalse($project->show_story_points);
$this->assertSame(['Spike'], $project->ticketTypes()->pluck('name')->all());
}
public function test_users_can_duplicate_projects_via_api(): void
{
$user = User::factory()->create();
@@ -7,6 +7,7 @@ use App\Actions\Projects\DuplicateProjectConfiguration;
use App\Models\BoardColumn;
use App\Models\NotificationAction;
use App\Models\Project;
use App\Models\ProjectTemplate;
use App\Models\RepositoryConnection;
use App\Models\Team;
use App\Models\Ticket;
@@ -115,16 +116,24 @@ class ProjectControllerTest extends TestCase
'name' => 'Hidden Source',
'key' => 'SRC2',
]);
$template = ProjectTemplate::factory()->create([
'name' => 'Reusable Setup',
'slug' => 'reusable-setup',
'source_project_id' => $visibleProject->id,
]);
$this->syncProjectPermissions($visibleProject, $user, ['view']);
$this->actingAs($user)
->get(route('projects.create'))
->get(route('projects.create', ['template' => $template->id]))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('projects/Create')
->has('copyAreaOptions', count(DuplicateProjectConfiguration::copyableAreas()))
->has('sourceProjects', 1)
->has('sourceTemplates', 1)
->where('sourceProjects.0.id', $visibleProject->id)
->where('sourceTemplates.0.id', $template->id)
->where('selectedSourceTemplateId', $template->id)
->where('copyAreaOptions.0.value', DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS),
);
}
@@ -207,6 +216,66 @@ class ProjectControllerTest extends TestCase
->assertRedirect(route('projects.create'));
}
public function test_authenticated_users_can_create_projects_from_saved_templates(): void
{
$user = User::factory()->create();
$this->syncGlobalPermissions($user, ['project:create']);
$sourceProject = Project::factory()->create([
'key' => 'SRC1',
'name' => 'Source Project',
'show_author' => false,
'show_story_points' => false,
]);
$template = ProjectTemplate::factory()->create([
'source_project_id' => $sourceProject->id,
'name' => 'Kanban Template',
'slug' => 'kanban-template',
'copy_areas' => [
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
DuplicateProjectConfiguration::COPY_TICKET_TYPES,
],
'configuration_snapshot' => [
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS => [
'show_author' => false,
'show_story_points' => false,
],
DuplicateProjectConfiguration::COPY_TICKET_TYPES => [
[
'name' => 'Bug',
'position' => 1,
'is_parent' => false,
],
],
],
]);
$response = $this->actingAs($user)
->post(route('projects.store'), [
'creation_mode' => 'template',
'name' => 'Template Based Project',
'key' => 'TMP1',
'source_template_id' => $template->id,
]);
$project = Project::query()->where('key', 'TMP1')->firstOrFail();
$response->assertRedirect(route('projects.show', $project));
$this->assertSame(false, $project->fresh()->show_author);
$this->assertSame(false, $project->fresh()->show_story_points);
$this->assertDatabaseHas('ticket_types', [
'project_id' => $project->id,
'name' => 'Bug',
'position' => 1,
'is_parent' => false,
]);
$this->assertDatabaseMissing('tickets', [
'project_id' => $project->id,
]);
}
public function test_authenticated_users_can_duplicate_projects_with_selected_settings_only(): void
{
$user = User::factory()->create();
@@ -0,0 +1,124 @@
<?php
namespace Tests\Feature\Projects;
use App\Actions\Projects\DuplicateProjectConfiguration;
use App\Models\BoardColumn;
use App\Models\Project;
use App\Models\ProjectTemplate;
use App\Models\TicketStatus;
use App\Models\TicketType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class ProjectTemplateControllerTest extends TestCase
{
use RefreshDatabase;
public function test_users_with_project_create_permission_can_view_templates_index_and_create_screen(): void
{
$user = User::factory()->create();
$project = Project::factory()->create([
'name' => 'Core Platform',
'key' => 'CORE',
]);
$template = ProjectTemplate::factory()->create([
'name' => 'Foundation Template',
'slug' => 'foundation-template',
'source_project_id' => $project->id,
]);
$this->syncGlobalPermissions($user, ['project:create']);
$this->syncProjectPermissions($project, $user, ['view']);
$this->actingAs($user)
->get('/project-templates')
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('project-templates/Index')
->has('templates', 1)
->where('templates.0.id', $template->id)
->where('templates.0.slug', 'foundation-template'));
$this->actingAs($user)
->get(route('projects.templates.create', $project))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('project-templates/Create')
->where('project.id', $project->id)
->has('copyAreaOptions', count(DuplicateProjectConfiguration::copyableAreas())));
}
public function test_users_can_save_and_delete_templates_from_projects(): void
{
$user = User::factory()->create();
$project = Project::factory()->create([
'name' => 'Source Project',
'key' => 'SRC1',
'show_author' => false,
'show_story_points' => false,
]);
$this->syncGlobalPermissions($user, ['project:create']);
$this->syncProjectPermissions($project, $user, ['view']);
$project->ticketTypes()->delete();
$project->ticketStatuses()->delete();
$project->boardColumns()->delete();
$boardColumn = BoardColumn::factory()->create([
'project_id' => $project->id,
'name' => 'Doing',
'position' => 1,
]);
$ticketStatus = TicketStatus::factory()->create([
'project_id' => $project->id,
'board_column_id' => $boardColumn->id,
'name' => 'Doing',
'position' => 1,
]);
$ticketStatus->allowedTargetStatuses()->sync([$ticketStatus->id]);
TicketType::factory()->create([
'project_id' => $project->id,
'name' => 'Bug',
'position' => 1,
]);
$this->actingAs($user)
->post(route('projects.templates.store', $project), [
'name' => 'Core Reusable',
'slug' => 'core-reusable',
'copy_areas' => [
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
DuplicateProjectConfiguration::COPY_TICKET_TYPES,
],
])
->assertRedirect('/project-templates');
$template = ProjectTemplate::query()->where('slug', 'core-reusable')->firstOrFail();
$this->assertSame([
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS => [
'show_author' => false,
'show_story_points' => false,
],
DuplicateProjectConfiguration::COPY_TICKET_TYPES => [
[
'name' => 'Bug',
'position' => 1,
'is_parent' => false,
],
],
], $template->configuration_snapshot);
$this->actingAs($user)
->delete(route('project-templates.destroy', $template))
->assertRedirect('/project-templates');
$this->assertDatabaseMissing('project_templates', [
'id' => $template->id,
]);
}
}