From 58bf654b52ed767aea1eba97c0fcaead9afe28b6 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 17:41:10 +0000 Subject: [PATCH 1/4] refactor: share the guild creation rules between requests --- app/Http/Controllers/GuildController.php | 2 +- app/Http/Requests/CreateGuildRequest.php | 100 ++++++++++++++++------- 2 files changed, 70 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/GuildController.php b/app/Http/Controllers/GuildController.php index 339d19eb6..00f6e1ff9 100644 --- a/app/Http/Controllers/GuildController.php +++ b/app/Http/Controllers/GuildController.php @@ -48,7 +48,7 @@ public function store(CreateGuildRequest $request, #[CurrentUser] User $user, Cr try { $guild = $action->handle($user, $request->string('handle')->value(), $request->string('name')->value(), $request->string('description')->value(), $request->string('emoji')->value(), $icon, $request->isFlagged()); } catch (GuildModeratorRemoved) { - return back()->withErrors(['handle' => 'You cannot reclaim a guild you were removed from.']); + return back()->withErrors(['handle' => CreateGuildRequest::RECLAIM_REFUSED]); } if ($guild instanceof GuildSubmission) { diff --git a/app/Http/Requests/CreateGuildRequest.php b/app/Http/Requests/CreateGuildRequest.php index 30f8539b1..0af968a8a 100644 --- a/app/Http/Requests/CreateGuildRequest.php +++ b/app/Http/Requests/CreateGuildRequest.php @@ -7,6 +7,7 @@ use App\Enums\Feature; use App\Models\Guild; use App\Models\GuildSubmission; +use App\Models\User; use App\Rules\FlaggedGuild; use App\Services\EmojiShortcodes; use App\Services\Features; @@ -15,6 +16,7 @@ use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; use Illuminate\Validation\Rule; use Illuminate\Validation\Rules\In; use Illuminate\Validation\Rules\Unique; @@ -22,17 +24,18 @@ final class CreateGuildRequest extends FormRequest { + public const string RECLAIM_REFUSED = 'You cannot reclaim a guild you were removed from.'; + private const int MAX_PER_USER = 100; private bool $isFlagged = false; - public function authorize(Features $features): Response - { - return $features->available(Feature::Guilds, $this->user()) ? Response::allow() : Response::denyAsNotFound(); - } - - /** @return array> */ - public function rules(EmojiShortcodes $emoji): array + /** + * The rules a new guild follows, wherever it's created. + * + * @return array> + */ + public static function guildRules(EmojiShortcodes $emoji): array { return [ 'handle' => ['required', 'string', 'max:60', 'regex:/^\p{L}[\p{L}\p{M}\p{N}_]*$/uD', Rule::unique('guilds', 'handle')->whereNotNull('user_id')], @@ -44,6 +47,59 @@ public function rules(EmojiShortcodes $emoji): array ]; } + /** + * Refuses a name that doesn't match the hashtag, a guild the user was removed from, and more guilds than the user + * may have, counting the ones waiting for review. A guild that passes is moderated, and the answer says whether the + * moderation flagged it. + */ + public static function validateNewGuild(Validator $validator, FlaggedGuild $flagged, User $user, string $handle, string $name, string $description, ?UploadedFile $icon): bool + { + if (self::hashtagOf($name) !== $handle) { + $validator->errors()->add('name', "Guild display name doesn't match hashtag"); + } + + if (DB::table('guild_moderator_removals')->where('github_id', $user->github_id)->where('handle', $handle)->exists()) { + $validator->errors()->add('handle', self::RECLAIM_REFUSED); + } + + $guilds = Guild::query()->where('user_id', $user->id); + $submissions = GuildSubmission::query()->where('user_id', $user->id)->where('handle', '!=', $handle)->whereNotIn('handle', (clone $guilds)->select('handle')); + + if ($guilds->count() + $submissions->count() >= self::MAX_PER_USER) { + $validator->errors()->add('handle', 'You can create up to '.self::MAX_PER_USER.' guilds.'); + } + + return $validator->errors()->isEmpty() && $flagged->validate($validator, $name."\n\n".$description, $icon); + } + + /** + * The hashtag a display name stands for: its letters, numbers and underscores, lowercased. + */ + public static function hashtagOf(string $name): string + { + return Str::of($name)->replaceMatches('/[^\p{L}\p{M}\p{N}_]+/u', '')->lower()->value(); + } + + /** @return array */ + public static function guildMessages(): array + { + return [ + 'handle.unique' => 'A guild already uses this hashtag. Choose a different display name.', + 'handle.regex' => 'Use a display name whose hashtag starts with a letter and contains only letters, numbers and underscores.', + ]; + } + + public function authorize(Features $features): Response + { + return $features->available(Feature::Guilds, $this->user()) ? Response::allow() : Response::denyAsNotFound(); + } + + /** @return array> */ + public function rules(EmojiShortcodes $emoji): array + { + return self::guildRules($emoji); + } + /** @return list */ public function after(FlaggedGuild $flagged): array { @@ -52,25 +108,10 @@ public function after(FlaggedGuild $flagged): array return; } - if ($this->string('name')->replaceMatches('/[^\p{L}\p{M}\p{N}_]+/u', '')->lower()->value() !== $this->string('handle')->value()) { - $validator->errors()->add('name', "Guild display name doesn't match hashtag"); - } - - if (DB::table('guild_moderator_removals')->where('github_id', $this->user()?->github_id)->where('handle', $this->string('handle')->value())->exists()) { - $validator->errors()->add('handle', 'You cannot reclaim a guild you were removed from.'); - } - - $guilds = Guild::query()->where('user_id', $this->user()?->id); - $submissions = GuildSubmission::query()->where('user_id', $this->user()?->id)->where('handle', '!=', $this->string('handle')->value())->whereNotIn('handle', (clone $guilds)->select('handle')); - - if ($guilds->count() + $submissions->count() >= self::MAX_PER_USER) { - $validator->errors()->add('handle', 'You can create up to '.self::MAX_PER_USER.' guilds.'); - } - - if ($validator->errors()->isEmpty()) { - $icon = $this->file('images.0'); - $this->isFlagged = $flagged->validate($validator, $this->string('name')->value()."\n\n".$this->string('description')->value(), $icon instanceof UploadedFile ? $icon : null); - } + $user = $this->user(); + assert($user instanceof User); + $icon = $this->file('images.0'); + $this->isFlagged = self::validateNewGuild($validator, $flagged, $user, $this->string('handle')->value(), $this->string('name')->value(), $this->string('description')->value(), $icon instanceof UploadedFile ? $icon : null); }]; } @@ -85,16 +126,13 @@ public function isFlagged(): bool /** @return array */ public function messages(): array { - return [ - 'handle.unique' => 'A guild already uses this hashtag. Choose a different display name.', - 'handle.regex' => 'Use a display name whose hashtag starts with a letter and contains only letters, numbers and underscores.', - ]; + return self::guildMessages(); } public function prepareForValidation(): void { if (! $this->has('handle') && is_string($this->input('name'))) { - $this->merge(['handle' => $this->string('name')->replaceMatches('/[^\p{L}\p{M}\p{N}_]+/u', '')->lower()->value()]); + $this->merge(['handle' => self::hashtagOf($this->string('name')->value())]); } if (is_string($this->input('handle'))) { From 940606e688523bfa932ca5379902689b0091e800 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 17:41:10 +0000 Subject: [PATCH 2/4] test: cover creating a guild through the API --- .../Controllers/Api/GuildControllerTest.php | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/tests/Feature/Controllers/Api/GuildControllerTest.php b/tests/Feature/Controllers/Api/GuildControllerTest.php index e8e8a88ef..a9923642f 100644 --- a/tests/Feature/Controllers/Api/GuildControllerTest.php +++ b/tests/Feature/Controllers/Api/GuildControllerTest.php @@ -2,14 +2,21 @@ declare(strict_types=1); +use App\Actions\CreateGuild; use App\Actions\UpdateGuildMembership; use App\Actions\UpdateGuildMute; +use App\Contracts\Services\ModerationProvider; use App\Enums\Feature; use App\Models\Commit; use App\Models\Guild; +use App\Models\GuildSubmission; use App\Models\User; +use App\Models\UserTimeout; use App\Queries\GuildsQuery; use App\Services\Features; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; beforeEach(function (): void { resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); @@ -242,3 +249,131 @@ 'unknown guild' => [false, 'go'], 'admin preview' => [true, 'rust'], ]); + +it('creates a guild from its display name, with the user as its moderator and first member', function (): void { + Storage::fake(); + + signedApiRequestWithImages($this->keyPair, 'POST', route('api-guild.store'), [ + 'name' => 'Jo bs', 'description' => 'Hiring and freelance work.', 'emoji' => '💼', + ], [UploadedFile::fake()->image('icon.png', 80, 60)]) + ->assertCreated() + ->assertJsonPath('data.handle', 'jobs') + ->assertJsonPath('data.name', 'Jo bs') + ->assertJsonPath('data.member_count', 1) + ->assertJsonPath('data.is_joined', true) + ->assertJsonPath('data.is_paused', false); + + $guild = Guild::query()->sole(); + expect($guild->user_id)->toBe($this->user->id) + ->and($guild->icon_path)->toEndWith('.webp'); + Storage::assertExists($guild->icon_path); + signedApiRequest($this->keyPair, 'GET', route('api-guild.show', 'jobs'))->assertJsonPath('can_moderate', true); +}); + +it('claims a paused guild', function (): void { + $go = Guild::factory()->create(['handle' => 'go', 'user_id' => null]); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Go', 'description' => 'Gophers.', 'emoji' => '🐹']) + ->assertCreated()->assertJsonPath('data.handle', 'go')->assertJsonPath('data.is_paused', false); + + expect($go->fresh()->user_id)->toBe($this->user->id); +}); + +it('holds a flagged guild for review instead of creating it', function (): void { + $moderation = $this->mock(ModerationProvider::class); + $moderation->shouldReceive('moderate')->andReturn(['harassment' => 0.7]); + $moderation->shouldReceive('isHarmful')->andReturn(false); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Flagged words.', 'emoji' => '💼']) + ->assertAccepted() + ->assertExactJson(['message' => 'Your guild is waiting for review. It goes live once an admin approves it.']); + + $this->assertDatabaseCount('guilds', 0); + expect(GuildSubmission::query()->sole())->handle->toBe('jobs')->user_id->toBe($this->user->id); +}); + +it('rejects invalid guild details without creating a guild', function (array $payload, string $field): void { + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼', ...$payload]) + ->assertUnprocessable()->assertJsonValidationErrors($field); + + $this->assertDatabaseCount('guilds', 0); +})->with([ + 'missing name' => [['name' => null], 'name'], + 'name without a hashtag' => [['name' => '!!!'], 'handle'], + 'unrelated name' => [['handle' => 'jobs', 'name' => 'Job Board'], 'name'], +]); + +it('rejects an icon that is not an image', function (): void { + signedApiRequestWithImages($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼'], [ + UploadedFile::fake()->createWithContent('icon.svg', ''), + ])->assertUnprocessable()->assertJsonValidationErrors('images.0'); + + $this->assertDatabaseCount('guilds', 0); +}); + +it('refuses a taken hashtag, a guild the user was removed from, and more than a hundred guilds', function (string $case): void { + match ($case) { + 'taken' => Guild::factory()->create(['handle' => 'jobs']), + 'removed' => DB::table('guild_moderator_removals')->insert(['github_id' => $this->user->github_id, 'handle' => 'jobs', 'created_at' => now()]), + 'limit' => Guild::factory()->count(100)->create(['user_id' => $this->user->id]), + }; + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼']) + ->assertUnprocessable()->assertJsonValidationErrors('handle'); + + expect(Guild::query()->where('handle', 'jobs')->where('user_id', $this->user->id)->exists())->toBeFalse(); +})->with(['taken', 'removed', 'limit']); + +it('refuses the claim when a removal wins the race after validation', function (): void { + $this->app->beforeResolving(CreateGuild::class, function (): void { + DB::table('guild_moderator_removals')->insert(['github_id' => $this->user->github_id, 'handle' => 'jobs', 'created_at' => now()]); + }); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼']) + ->assertUnprocessable() + ->assertJsonPath('errors.handle', ['You cannot reclaim a guild you were removed from.']); + + $this->assertDatabaseCount('guilds', 0); +}); + +it('refuses the claim when another creation of the hashtag wins the race after validation', function (): void { + $other = User::factory()->create(); + $this->app->beforeResolving(CreateGuild::class, function () use ($other): void { + Guild::factory()->create(['handle' => 'jobs', 'user_id' => $other->id]); + }); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼']) + ->assertUnprocessable() + ->assertJsonPath('errors.handle', ['A guild already uses this hashtag. Choose a different display name.']); + + expect(Guild::query()->sole()->user_id)->toBe($other->id); +}); + +it('does not create guilds while they are not available to the user', function (): void { + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: true); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼'])->assertNotFound(); + + $this->assertDatabaseCount('guilds', 0); +}); + +it('requires a signed request to create a guild', function (): void { + $this->postJson(route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼'])->assertUnauthorized(); +}); + +it('refuses to create a guild while the user is in timeout', function (): void { + UserTimeout::factory()->for($this->user)->create(); + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼']) + ->assertForbidden()->assertJsonPath('code', 'timed_out'); + + $this->assertDatabaseCount('guilds', 0); +}); + +it('limits how often the user creates guilds', function (): void { + foreach (range(1, 10) as $attempt) { + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => '', 'emoji' => '💼'])->assertUnprocessable(); + } + + signedApiRequest($this->keyPair, 'POST', route('api-guild.store'), ['name' => 'Jobs', 'description' => 'Hiring.', 'emoji' => '💼'])->assertTooManyRequests(); +}); From b7bb5e022e55fda0b29ccd335c28b728ed5366a7 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 17:41:10 +0000 Subject: [PATCH 3/4] feat: let API clients create guilds --- KNOWLEDGE_BASE.md | 1 + app/Http/Controllers/Api/GuildController.php | 42 +++++++++++ app/Http/Requests/CreateApiGuildRequest.php | 74 ++++++++++++++++++++ routes/api.php | 3 + 4 files changed, 120 insertions(+) create mode 100644 app/Http/Requests/CreateApiGuildRequest.php diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 7b809d08b..ca7a2328d 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -597,6 +597,7 @@ A signed request sees what the web shows you, such as your own pending commits a - list guilds as the Guilds page does, 24 a page, searched by name, handle or description, with whether you joined or muted each one and whether it is paused, while guilds are available to you; - read a guild's board as its page does, 30 threads a page with reply previews, with the guild, its pinned commit and whether you moderate it, while guilds are available to you. The pinned commit also keeps its place in the list, so an app that shows it apart skips it there by its id; - list a guild's members as its members page does, the moderator first and then the newest joins, 50 a page, with whether you follow each one and whether each one follows you, and the moderator's id, while guilds are available to you; +- create a guild, or claim a paused one, as the Guilds page does, with the same rules and limit of 100, where a guild the moderation flags waits for an admin's review instead, while guilds are available to you; - join or leave a guild, where joining clears your mute of it, as the Join button does, while guilds are available to you; - report a guild you don't moderate to the admins, privately, as the Report button does, where reporting it again keeps your first report until an admin dismisses it, while guilds are available to you; - star or unstar, and list who starred your own commit, newest first, 20 a page; diff --git a/app/Http/Controllers/Api/GuildController.php b/app/Http/Controllers/Api/GuildController.php index 27ec13750..81a76d3d6 100644 --- a/app/Http/Controllers/Api/GuildController.php +++ b/app/Http/Controllers/Api/GuildController.php @@ -4,18 +4,25 @@ namespace App\Http\Controllers\Api; +use App\Actions\CreateGuild; +use App\Exceptions\GuildModeratorRemoved; +use App\Http\Requests\CreateApiGuildRequest; +use App\Http\Requests\CreateGuildRequest; use App\Http\Requests\ListApiGuildsRequest; use App\Http\Requests\ShowApiGuildRequest; use App\Http\Resources\CommitResource; use App\Http\Resources\GuildResource; use App\Models\Commit; use App\Models\Guild; +use App\Models\GuildSubmission; use App\Models\User; use App\Queries\GuildCommitsQuery; use App\Queries\GuildsQuery; use App\Queries\ReplyPreviewsQuery; use Illuminate\Container\Attributes\CurrentUser; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Illuminate\Http\UploadedFile; final readonly class GuildController { @@ -24,6 +31,33 @@ public function index(ListApiGuildsRequest $request, #[CurrentUser] User $user, return GuildResource::collection($query->paginate($request->term(), $request->integer('page', 1), $user)->withQueryString()); } + /** + * Creates the guild, or claims a paused one, with the user as its moderator and first member, as the web does. A + * guild the moderation flags waits for an admin instead, so it answers 202 with the web's message. + */ + public function store(CreateApiGuildRequest $request, #[CurrentUser] User $user, CreateGuild $action, GuildsQuery $guilds): JsonResponse + { + $icon = $request->file('images.0'); + assert($icon instanceof UploadedFile || $icon === null); + + try { + $guild = $action->handle($user, $request->string('handle')->value(), $request->string('name')->value(), $request->string('description')->value(), $request->string('emoji')->value(), $icon, $request->isFlagged()); + } catch (GuildModeratorRemoved) { + return $this->handleRefused(CreateGuildRequest::RECLAIM_REFUSED); + } + + if ($guild instanceof GuildSubmission) { + return response()->json(['message' => 'Your guild is waiting for review. It goes live once an admin approves it.'], 202); + } + + // Another creation of the same hashtag won the race after validation, and the action leaves it to its moderator... + if ($guild->user_id !== $user->id) { + return $this->handleRefused(CreateGuildRequest::guildMessages()['handle.unique']); + } + + return GuildResource::make($guilds->find($guild, $user))->response()->setStatusCode(201); + } + /** * The guild's board as its page on the web shows it: 30 threads per page, newest first. Everyone gets the pin the * moderator set beside them, and it keeps its place on the board, which the web's page skips. One snapshot reads the @@ -47,4 +81,12 @@ public function show(ShowApiGuildRequest $request, Guild $guild, #[CurrentUser] 'can_moderate' => $guild->user_id === $user->id, ]); } + + /** + * A race lost after validation, answered as the validation would have answered it. + */ + private function handleRefused(string $message): JsonResponse + { + return response()->json(['message' => $message, 'errors' => ['handle' => [$message]]], 422); + } } diff --git a/app/Http/Requests/CreateApiGuildRequest.php b/app/Http/Requests/CreateApiGuildRequest.php new file mode 100644 index 000000000..dc271c54c --- /dev/null +++ b/app/Http/Requests/CreateApiGuildRequest.php @@ -0,0 +1,74 @@ +available(Feature::Guilds, $this->user()) ? Response::allow() : Response::denyAsNotFound(); + } + + /** @return array> */ + public function rules(EmojiShortcodes $emoji): array + { + return CreateGuildRequest::guildRules($emoji); + } + + /** @return list */ + public function after(FlaggedGuild $flagged): array + { + return [function (Validator $validator) use ($flagged): void { + if ($validator->errors()->isNotEmpty()) { + return; + } + + $user = $this->user(); + assert($user instanceof User); + $icon = $this->file('images.0'); + $this->isFlagged = CreateGuildRequest::validateNewGuild($validator, $flagged, $user, $this->string('handle')->value(), $this->string('name')->value(), $this->string('description')->value(), $icon instanceof UploadedFile ? $icon : null); + }]; + } + + /** + * Whether the moderation flagged the guild, so it waits for an admin instead of being published. + */ + public function isFlagged(): bool + { + return $this->isFlagged; + } + + /** @return array */ + public function messages(): array + { + return CreateGuildRequest::guildMessages(); + } + + public function prepareForValidation(): void + { + if (! $this->has('handle') && is_string($this->input('name'))) { + $this->merge(['handle' => CreateGuildRequest::hashtagOf($this->string('name')->value())]); + } + + if (is_string($this->input('handle'))) { + $this->merge(['handle' => $this->string('handle')->trim()->lower()->value()]); + } + } +} diff --git a/routes/api.php b/routes/api.php index 09f3b5a1b..a9b9786de 100644 --- a/routes/api.php +++ b/routes/api.php @@ -245,6 +245,9 @@ // Guilds: the directory the web's Guilds page lists, a guild's board and its members... Route::get('guilds', [GuildController::class, 'index'])->name('api-guild.index'); + Route::post('guilds', [GuildController::class, 'store']) + ->middleware([PreventPostingInTimeout::class, 'throttle:10,1,guilds:']) + ->name('api-guild.store'); Route::get('guilds/{guild:handle}', [GuildController::class, 'show'])->name('api-guild.show'); Route::get('guilds/{guild:handle}/members', [GuildMembershipController::class, 'index'])->name('api-guild-membership.index'); From 184906950e6e4190d4d124e963f6635e3f1b95cd Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 18:13:43 +0000 Subject: [PATCH 4/4] fix: refuse a flagged guild whose hashtag was taken after validation --- app/Actions/CreateGuild.php | 7 +++++++ tests/Feature/Controllers/Api/GuildControllerTest.php | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/Actions/CreateGuild.php b/app/Actions/CreateGuild.php index c5fe66fbd..b4e61728a 100644 --- a/app/Actions/CreateGuild.php +++ b/app/Actions/CreateGuild.php @@ -31,6 +31,13 @@ public function handle(User $user, string $handle, string $name, string $descrip } if ($flagged) { + // A guild another creation published after validation keeps its hashtag, so nothing waits for review... + $taken = Guild::query()->where('handle', mb_strtolower($handle))->whereNotNull('user_id')->first(); + + if ($taken instanceof Guild) { + return $taken; + } + assert($icon instanceof UploadedFile || $icon === null); return $this->submit->handle($user, mb_strtolower($handle), $name, $description, $emoji, $icon, false); diff --git a/tests/Feature/Controllers/Api/GuildControllerTest.php b/tests/Feature/Controllers/Api/GuildControllerTest.php index a9923642f..9edb838cc 100644 --- a/tests/Feature/Controllers/Api/GuildControllerTest.php +++ b/tests/Feature/Controllers/Api/GuildControllerTest.php @@ -336,7 +336,13 @@ $this->assertDatabaseCount('guilds', 0); }); -it('refuses the claim when another creation of the hashtag wins the race after validation', function (): void { +it('refuses the claim when another creation of the hashtag wins the race after validation', function (bool $flagged): void { + if ($flagged) { + $moderation = $this->mock(ModerationProvider::class); + $moderation->shouldReceive('moderate')->andReturn(['harassment' => 0.7]); + $moderation->shouldReceive('isHarmful')->andReturn(false); + } + $other = User::factory()->create(); $this->app->beforeResolving(CreateGuild::class, function () use ($other): void { Guild::factory()->create(['handle' => 'jobs', 'user_id' => $other->id]); @@ -347,7 +353,8 @@ ->assertJsonPath('errors.handle', ['A guild already uses this hashtag. Choose a different display name.']); expect(Guild::query()->sole()->user_id)->toBe($other->id); -}); + $this->assertDatabaseCount('guild_submissions', 0); +})->with(['published' => false, 'flagged' => true]); it('does not create guilds while they are not available to the user', function (): void { resolve(Features::class)->update(Feature::Guilds, true, adminOnly: true);