96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
use App\Models\ActivityEvent;
|
|
use App\Models\Story;
|
|
use App\Models\User;
|
|
use App\Notifications\StoryStatusNotification;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Support\Str;
|
|
|
|
function createPendingReviewStory(User $creator): Story
|
|
{
|
|
return Story::query()->create([
|
|
'creator_id' => $creator->id,
|
|
'title' => 'Pending Story ' . Str::random(6),
|
|
'slug' => 'pending-story-' . Str::lower(Str::random(8)),
|
|
'content' => '<p>Pending review content</p>',
|
|
'story_type' => 'creator_story',
|
|
'status' => 'pending_review',
|
|
'submitted_for_review_at' => now(),
|
|
]);
|
|
}
|
|
|
|
it('non moderator cannot access admin stories review queue', function () {
|
|
$user = User::factory()->create(['role' => 'user']);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('admin.stories.review'))
|
|
->assertStatus(403);
|
|
});
|
|
|
|
it('admin can approve a pending story and notify creator', function () {
|
|
Notification::fake();
|
|
|
|
$admin = User::factory()->create(['role' => 'admin']);
|
|
$creator = User::factory()->create();
|
|
|
|
$story = createPendingReviewStory($creator);
|
|
|
|
$response = $this->actingAs($admin)
|
|
->post(route('admin.stories.approve', ['story' => $story->id]));
|
|
|
|
$response->assertRedirect();
|
|
|
|
$story->refresh();
|
|
|
|
expect($story->status)->toBe('published');
|
|
expect($story->reviewed_by_id)->toBe($admin->id);
|
|
expect($story->reviewed_at)->not->toBeNull();
|
|
expect($story->published_at)->not->toBeNull();
|
|
|
|
Notification::assertSentTo($creator, StoryStatusNotification::class);
|
|
});
|
|
|
|
it('moderator can reject a pending story with reason and notify creator', function () {
|
|
Notification::fake();
|
|
|
|
$moderator = User::factory()->create(['role' => 'moderator']);
|
|
$creator = User::factory()->create();
|
|
|
|
$story = createPendingReviewStory($creator);
|
|
|
|
$response = $this->actingAs($moderator)
|
|
->post(route('admin.stories.reject', ['story' => $story->id]), [
|
|
'reason' => 'Please remove promotional external links and resubmit.',
|
|
]);
|
|
|
|
$response->assertRedirect();
|
|
|
|
$story->refresh();
|
|
|
|
expect($story->status)->toBe('rejected');
|
|
expect($story->reviewed_by_id)->toBe($moderator->id);
|
|
expect($story->reviewed_at)->not->toBeNull();
|
|
expect($story->rejected_reason)->toContain('promotional external links');
|
|
|
|
Notification::assertSentTo($creator, StoryStatusNotification::class);
|
|
});
|
|
|
|
it('admin approval records a story publish activity event', function () {
|
|
$admin = User::factory()->create(['role' => 'admin']);
|
|
$creator = User::factory()->create();
|
|
|
|
$story = createPendingReviewStory($creator);
|
|
|
|
$this->actingAs($admin)
|
|
->post(route('admin.stories.approve', ['story' => $story->id]))
|
|
->assertRedirect();
|
|
|
|
$this->assertDatabaseHas('activity_events', [
|
|
'actor_id' => $creator->id,
|
|
'type' => ActivityEvent::TYPE_UPLOAD,
|
|
'target_type' => ActivityEvent::TARGET_STORY,
|
|
'target_id' => $story->id,
|
|
]);
|
|
});
|