'My first post', 'slug' => '', 'excerpt' => 'A short text', 'body' => "# Hello\n\nSome **markdown**.", 'cover_image' => '', 'category' => 'Tutorial', 'tags' => ['Laravel', ' Vue ', ''], 'is_featured' => true, 'is_published' => true, 'published_at' => '', ...$overrides, ]; } function post(array $attributes = []): Post { static $n = 0; $n++; return Post::create([ 'title' => "Post {$n}", 'slug' => "post-{$n}", 'body' => 'Body', 'published_at' => now()->subDay(), ...$attributes, ]); } beforeEach(fn () => $this->actingAs(makeUser(superAdmin: true))); describe('post list', function () { it('shows drafts first, then the newest posts, with their status', function () { post(['title' => 'Published']); post(['title' => 'Scheduled', 'published_at' => now()->addWeek()]); post(['title' => 'Draft', 'published_at' => null]); $this->getJson('/manager/posts')->assertOk() ->assertJsonCount(3, 'posts') ->assertJsonPath('posts.0.status', 'draft') ->assertJsonPath('posts.1.status', 'scheduled') ->assertJsonPath('posts.2.status', 'published'); }); }); describe('creating a post', function () { it('offers the configured categories, the ones in use, the ones of the site, and tags', function () { post(['category' => 'Tutorial', 'tags' => ['php']]); post(['category' => 'Tutorial']); post(['category' => null]); app(PostLabels::class)->suggestCategories(fn () => ['Services']); $this->getJson('/manager/posts/create')->assertOk() ->assertJsonPath('post', null) ->assertJsonPath('categories', ['Services', 'General', 'Tutorial']) ->assertJsonPath('known_tags', ['php']); }); it('publishes now when no date is given, and makes the slug from the title', function () { $this->freezeSecond(); $this->postJson('/manager/posts', postForm())->assertCreated(); $post = Post::sole(); expect($post->slug)->toBe('my-first-post') ->and($post->tags)->toBe(['Laravel', 'Vue']) ->and($post->is_featured)->toBeTrue() ->and($post->published_at?->equalTo(now()))->toBeTrue() ->and(Post::published()->count())->toBe(1); }); it('saves a draft without a publish date', function () { $this->postJson('/manager/posts', postForm(['is_published' => false, 'published_at' => '2026-01-01T10:00'])); expect(Post::sole()->published_at)->toBeNull()->and(Post::sole()->status())->toBe('draft'); }); it('schedules a post with a date in the future', function () { $this->travelTo('2026-11-17 12:00'); $this->postJson('/manager/posts', postForm(['published_at' => '2026-12-01T09:30'])); $post = Post::sole(); expect($post->published_at?->format('Y-m-d H:i'))->toBe('2026-12-01 09:30') ->and($post->isPublished())->toBeFalse() ->and($post->status())->toBe('scheduled') ->and(Post::published()->count())->toBe(0); }); it('gives a second post with the same title its own slug', function () { $this->postJson('/manager/posts', postForm()); $this->postJson('/manager/posts', postForm()); expect(Post::orderBy('id')->pluck('slug')->all())->toBe(['my-first-post', 'my-first-post-2']); }); it('stores empty optional fields as null', function () { $this->postJson('/manager/posts', postForm(['excerpt' => '', 'cover_image' => '', 'category' => '', 'tags' => []])); $post = Post::sole(); expect($post->excerpt)->toBeNull()->and($post->cover_image)->toBeNull()->and($post->category)->toBeNull()->and($post->tags)->toBe([]); }); it('validates the form', function (array $input, string $field) { post(['slug' => 'taken']); $this->postJson('/manager/posts', postForm($input))->assertJsonValidationErrors($field); expect(Post::count())->toBe(1); })->with([ 'missing title' => [['title' => ''], 'title'], 'missing body' => [['body' => ''], 'body'], 'slug in use' => [['slug' => 'taken'], 'slug'], 'slug with spaces' => [['slug' => 'Not A Slug'], 'slug'], 'invalid date' => [['published_at' => 'tomorrow-ish'], 'published_at'], ]); }); describe('editing a post', function () { it('shows the form filled in', function () { $post = post(['published_at' => '2026-03-04 15:30:00']); $this->getJson("/manager/posts/{$post->id}/edit")->assertOk() ->assertJsonPath('post.id', $post->id) ->assertJsonPath('post.is_published', true) ->assertJsonPath('post.published_at', '2026-03-04T15:30'); }); it('updates the post and keeps its own slug valid', function () { $post = post(['slug' => 'my-slug']); $this->putJson("/manager/posts/{$post->id}", postForm(['title' => 'New title', 'slug' => 'my-slug']))->assertOk(); expect($post->refresh()->title)->toBe('New title')->and($post->slug)->toBe('my-slug'); }); it('keeps the original publish date when saving without a date', function () { $post = post(['published_at' => '2024-04-09 10:00:00']); $this->putJson("/manager/posts/{$post->id}", postForm(['published_at' => ''])); expect($post->refresh()->published_at?->toDateTimeString())->toBe('2024-04-09 10:00:00'); }); it('unpublishes a post, which hides it from the site', function () { $post = post(['slug' => 'my-slug']); $this->putJson("/manager/posts/{$post->id}", postForm(['slug' => 'my-slug', 'is_published' => false])); expect($post->refresh()->published_at)->toBeNull()->and(Post::published()->count())->toBe(0); }); it('puts a post online and takes it off again', function () { $post = post(['published_at' => null]); $this->putJson("/manager/posts/{$post->id}/online", ['online' => true])->assertJsonPath('status', 'published'); $this->putJson("/manager/posts/{$post->id}/online", ['online' => false])->assertJsonPath('status', 'draft'); }); it('previews a draft, with the markdown rendered and raw html escaped', function () { $post = post(['published_at' => null, 'body' => "**Draft** text \n\n[out](https://example.org)"]); $html = $this->getJson("/manager/posts/{$post->id}/preview")->assertOk()->assertJsonPath('preview', true)->json('post.body_html'); expect($html)->toContain('Draft')->not->toContain('