289 lines
12 KiB
PHP
289 lines
12 KiB
PHP
<?php
|
|
|
|
use IriksIT\Manager\Models\Post;
|
|
use IriksIT\Manager\Modules\Posts\PostLabels;
|
|
use IriksIT\Manager\Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
function postForm(array $overrides = []): array
|
|
{
|
|
return [
|
|
'title' => '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 <script>alert(1)</script>\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('<strong>Draft</strong>')->not->toContain('<script>')->toContain('target="_blank"');
|
|
});
|
|
});
|
|
|
|
describe('deleting a post', function () {
|
|
it('removes it', function () {
|
|
$post = post();
|
|
|
|
$this->deleteJson("/manager/posts/{$post->id}")->assertOk();
|
|
|
|
expect(Post::count())->toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('who may do what', function () {
|
|
it('lets a writer work on drafts, but never publish or touch what is live', function () {
|
|
$this->actingAs(makeUser(['posts.write', 'posts.delete']));
|
|
$live = post();
|
|
$draft = post(['published_at' => null]);
|
|
|
|
// Asking to publish still saves a draft.
|
|
$this->postJson('/manager/posts', postForm(['is_published' => true]))->assertCreated();
|
|
expect(Post::latest('id')->first()->published_at)->toBeNull();
|
|
|
|
$this->putJson("/manager/posts/{$draft->id}", postForm(['slug' => $draft->slug, 'is_published' => true]))->assertOk();
|
|
expect($draft->refresh()->published_at)->toBeNull();
|
|
|
|
$this->getJson("/manager/posts/{$live->id}/edit")->assertForbidden();
|
|
$this->putJson("/manager/posts/{$live->id}", postForm(['slug' => $live->slug]))->assertForbidden();
|
|
$this->deleteJson("/manager/posts/{$live->id}")->assertForbidden();
|
|
$this->putJson("/manager/posts/{$draft->id}/online", ['online' => true])->assertForbidden();
|
|
|
|
$this->deleteJson("/manager/posts/{$draft->id}")->assertOk();
|
|
});
|
|
|
|
it('does not show the delete button for a live post to a writer', function () {
|
|
$this->actingAs(makeUser(['posts.write', 'posts.delete']));
|
|
post();
|
|
|
|
$this->getJson('/manager/posts')->assertJsonPath('posts.0.can_change', false)->assertJsonPath('posts.0.can_delete', false);
|
|
});
|
|
|
|
it('keeps a viewer out of the form', function () {
|
|
$this->actingAs(makeUser(['posts.view']));
|
|
|
|
$this->getJson('/manager/posts')->assertOk();
|
|
$this->postJson('/manager/posts', postForm())->assertForbidden();
|
|
});
|
|
});
|
|
|
|
describe('categories and tags', function () {
|
|
it('lists every category and tag with the number of posts, drafts included', function () {
|
|
post(['category' => 'Laravel', 'tags' => ['php', 'Tutorial']]);
|
|
post(['category' => 'Laravel', 'tags' => ['php', 'php'], 'published_at' => null]);
|
|
post(['category' => 'devops', 'tags' => []]);
|
|
post(['category' => null, 'tags' => null]);
|
|
|
|
$this->getJson('/manager/labels')->assertOk()
|
|
->assertJsonPath('categories', [['name' => 'devops', 'posts' => 1], ['name' => 'Laravel', 'posts' => 2]])
|
|
->assertJsonPath('tags', [['name' => 'php', 'posts' => 2], ['name' => 'Tutorial', 'posts' => 1]]);
|
|
});
|
|
|
|
it('renames a category on every post, and merges two by using a name that exists', function () {
|
|
$one = post(['category' => 'Devops']);
|
|
$two = post(['category' => 'Dev ops']);
|
|
$other = post(['category' => 'Laravel']);
|
|
|
|
$this->putJson('/manager/labels', ['type' => 'category', 'name' => 'Devops', 'new_name' => ' DevOps '])->assertJsonPath('posts', 1);
|
|
$this->putJson('/manager/labels', ['type' => 'category', 'name' => 'Dev ops', 'new_name' => 'DevOps'])->assertJsonPath('posts', 1);
|
|
|
|
expect($one->refresh()->category)->toBe('DevOps')->and($two->refresh()->category)->toBe('DevOps')->and($other->refresh()->category)->toBe('Laravel');
|
|
});
|
|
|
|
it('renames a tag, keeps the order, and keeps it once in a post that had both names', function () {
|
|
$one = post(['tags' => ['first', 'laravel', 'last']]);
|
|
$both = post(['tags' => ['Laravel', 'laravel', 'php']]);
|
|
$other = post(['tags' => ['php']]);
|
|
|
|
$this->putJson('/manager/labels', ['type' => 'tag', 'name' => 'laravel', 'new_name' => 'Laravel'])->assertJsonPath('posts', 2);
|
|
|
|
expect($one->refresh()->tags)->toBe(['first', 'Laravel', 'last'])
|
|
->and($both->refresh()->tags)->toBe(['Laravel', 'php'])
|
|
->and($other->refresh()->tags)->toBe(['php']);
|
|
});
|
|
|
|
it('takes a label off every post and leaves the posts', function () {
|
|
$post = post(['category' => 'Old', 'tags' => ['old', 'php']]);
|
|
|
|
$this->deleteJson('/manager/labels', ['type' => 'category', 'name' => 'Old'])->assertJsonPath('posts', 1);
|
|
$this->deleteJson('/manager/labels', ['type' => 'tag', 'name' => 'old'])->assertJsonPath('posts', 1);
|
|
|
|
expect($post->refresh()->category)->toBeNull()->and($post->tags)->toBe(['php'])->and(Post::count())->toBe(1);
|
|
});
|
|
|
|
it('makes a label by hand, which a rename and a removal follow', function () {
|
|
$this->postJson('/manager/labels', ['type' => 'category', 'name' => 'Future'])->assertCreated();
|
|
$this->postJson('/manager/labels', ['type' => 'category', 'name' => 'Future'])->assertStatus(422);
|
|
$this->getJson('/manager/labels')->assertJsonPath('categories', [['name' => 'Future', 'posts' => 0]]);
|
|
|
|
$this->putJson('/manager/labels', ['type' => 'category', 'name' => 'Future', 'new_name' => 'Soon']);
|
|
$this->getJson('/manager/labels')->assertJsonPath('categories', [['name' => 'Soon', 'posts' => 0]]);
|
|
|
|
$this->deleteJson('/manager/labels', ['type' => 'category', 'name' => 'Soon']);
|
|
$this->getJson('/manager/labels')->assertJsonPath('categories', []);
|
|
});
|
|
|
|
it('refuses a tag with a comma and an unknown type, and needs its own permission', function () {
|
|
$this->postJson('/manager/labels', ['type' => 'tag', 'name' => 'a,b'])->assertJsonValidationErrors('name');
|
|
$this->postJson('/manager/labels', ['type' => 'other', 'name' => 'x'])->assertJsonValidationErrors('type');
|
|
|
|
$this->actingAs(makeUser(['posts.write']));
|
|
$this->getJson('/manager/labels')->assertForbidden();
|
|
});
|
|
});
|