Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/GraphQL/Mutations/CreatePinnedTestMeasurement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\GraphQL\Mutations;

use App\Exceptions\GraphQLMutationException;
use App\Models\PinnedTestMeasurement;
use App\Models\Project;
use Illuminate\Support\Facades\Gate;
Expand All @@ -18,12 +19,18 @@ final class CreatePinnedTestMeasurement extends AbstractMutation
* projectId: int,
* name: string,
* } $args
*
* @throws GraphQLMutationException
*/
public function __invoke(null $_, array $args): self
{
$project = Project::find((int) $args['projectId']);
Gate::authorize('createPinnedTestMeasurement', $project);

if ($project?->pinnedTestMeasurements()->where('name', $args['name'])->exists() === true) {
throw new GraphQLMutationException('This test measurement is already pinned.');
}

$nextAvailablePosition = $project?->pinnedTestMeasurements()->max('position');
if ($nextAvailablePosition === null) {
$nextAvailablePosition = 1;
Expand Down
25 changes: 25 additions & 0 deletions database/migrations/2026_10_25_134934_measurement_unique_name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration {
public function up(): void
{
// Keep the first-positioned copy of each duplicate, since that's the one displayed.
DB::delete('
DELETE FROM measurement a
USING measurement b
WHERE
a.projectid = b.projectid
AND a.name = b.name
AND (a.position, a.id) > (b.position, b.id)
');

DB::statement('CREATE UNIQUE INDEX ON measurement (projectid, name)');
}

public function down(): void
{
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,79 @@ public function testCreatesMultipleMeasurementsInOrder(): void
self::assertContains($name1, $project->pinnedTestMeasurements()->pluck('name')->toArray());
self::assertContains($name2, $project->pinnedTestMeasurements()->pluck('name')->toArray());
}

public function testFailsWhenDuplicateName(): void
{
$project = $this->makePublicProject();
$user = User::factory()->adminUser()->create();

$name = Str::uuid()->toString();
$project->pinnedTestMeasurements()->create([
'name' => $name,
'position' => 1,
]);

$this->actingAs($user)->graphQL('
mutation createPinnedTestMeasurement($input: CreatePinnedTestMeasurementInput!) {
createPinnedTestMeasurement(input: $input) {
message
pinnedTestMeasurement {
name
position
}
}
}
', [
'input' => [
'projectId' => $project->id,
'name' => $name,
],
])->assertJsonPath('data.createPinnedTestMeasurement', null)
->assertGraphQLErrorMessage('This test measurement is already pinned.');

self::assertSame([$name], $project->pinnedTestMeasurements()->pluck('name')->toArray());
}

public function testAllowsSameNameInDifferentProjects(): void
{
$project1 = $this->makePublicProject();
$project2 = $this->makePublicProject();
$user = User::factory()->adminUser()->create();

$name = Str::uuid()->toString();
$project1->pinnedTestMeasurements()->create([
'name' => $name,
'position' => 1,
]);

$this->actingAs($user)->graphQL('
mutation createPinnedTestMeasurement($input: CreatePinnedTestMeasurementInput!) {
createPinnedTestMeasurement(input: $input) {
message
pinnedTestMeasurement {
name
position
}
}
}
', [
'input' => [
'projectId' => $project2->id,
'name' => $name,
],
])->assertExactJson([
'data' => [
'createPinnedTestMeasurement' => [
'message' => null,
'pinnedTestMeasurement' => [
'name' => $name,
'position' => 1,
],
],
],
]);

self::assertSame([$name], $project1->pinnedTestMeasurements()->pluck('name')->toArray());
self::assertSame([$name], $project2->pinnedTestMeasurements()->pluck('name')->toArray());
}
}
Loading