2026-07-18 17:13:50 +03:00
|
|
|
<?php
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
use App\Jobs\PublishPostJob;
|
2026-07-18 17:13:50 +03:00
|
|
|
use App\Livewire\Posts\Index;
|
|
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Models\Site;
|
|
|
|
|
use App\Models\SocialAccount;
|
2026-07-21 14:04:45 +03:00
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Support\Facades\Queue;
|
2026-07-18 17:13:50 +03:00
|
|
|
use Livewire\Livewire;
|
|
|
|
|
|
|
|
|
|
test('posts index page lists existing posts', function () {
|
|
|
|
|
$site = Site::factory()->create(['name' => 'My Blog']);
|
|
|
|
|
Post::factory()->create(['site_id' => $site->id, 'title' => 'Hello World Post']);
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
|
->get(route('posts.index'))
|
2026-07-18 17:13:50 +03:00
|
|
|
->assertOk()
|
|
|
|
|
->assertSee('Hello World Post')
|
|
|
|
|
->assertSee('My Blog');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a new post can be created with required fields only', function () {
|
|
|
|
|
$site = Site::factory()->create();
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)
|
|
|
|
|
->set('siteId', $site->id)
|
|
|
|
|
->set('type', 'blog')
|
|
|
|
|
->set('body', 'New post body')
|
|
|
|
|
->call('save');
|
|
|
|
|
|
|
|
|
|
$post = Post::where('body', 'New post body')->first();
|
|
|
|
|
expect($post)->not->toBeNull();
|
|
|
|
|
expect($post->site_id)->toBe($site->id);
|
|
|
|
|
expect($post->status)->toBe('draft');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a new post can be created with an account, title, status, and schedule', function () {
|
|
|
|
|
$site = Site::factory()->create();
|
|
|
|
|
$account = SocialAccount::factory()->create();
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)
|
|
|
|
|
->set('siteId', $site->id)
|
|
|
|
|
->set('socialAccountId', $account->id)
|
|
|
|
|
->set('type', 'twitter')
|
|
|
|
|
->set('title', 'Launch')
|
|
|
|
|
->set('body', 'We launched')
|
|
|
|
|
->set('status', 'scheduled')
|
|
|
|
|
->set('scheduledAt', '2026-08-01T09:00')
|
|
|
|
|
->call('save');
|
|
|
|
|
|
|
|
|
|
$post = Post::where('title', 'Launch')->first();
|
|
|
|
|
expect($post->social_account_id)->toBe($account->id);
|
|
|
|
|
expect($post->status)->toBe('scheduled');
|
|
|
|
|
expect($post->scheduled_at)->not->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('site, type, and body are required to create a post', function () {
|
|
|
|
|
Livewire::test(Index::class)
|
|
|
|
|
->set('siteId', '')
|
|
|
|
|
->set('type', '')
|
|
|
|
|
->set('body', '')
|
|
|
|
|
->call('save')
|
|
|
|
|
->assertHasErrors(['siteId', 'type', 'body']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('an existing post can be edited', function () {
|
|
|
|
|
$site = Site::factory()->create();
|
|
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'title' => 'Old Title']);
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)
|
|
|
|
|
->call('edit', $post->id)
|
|
|
|
|
->set('title', 'New Title')
|
|
|
|
|
->call('save');
|
|
|
|
|
|
|
|
|
|
expect($post->fresh()->title)->toBe('New Title');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a post can be deleted', function () {
|
|
|
|
|
$post = Post::factory()->create();
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)->call('delete', $post->id);
|
|
|
|
|
|
|
|
|
|
expect(Post::find($post->id))->toBeNull();
|
|
|
|
|
});
|
2026-07-21 14:04:45 +03:00
|
|
|
|
|
|
|
|
test('publishing a post queues the job and marks it publishing', function () {
|
|
|
|
|
Queue::fake();
|
|
|
|
|
|
|
|
|
|
$site = Site::factory()->wordpress()->create();
|
|
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'draft']);
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)->call('publish', $post->id);
|
|
|
|
|
|
|
|
|
|
expect($post->fresh()->status)->toBe('publishing');
|
|
|
|
|
Queue::assertPushed(PublishPostJob::class, fn ($job) => $job->postId === $post->id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('publishing a post with no available publisher shows a notice and does not queue anything', function () {
|
|
|
|
|
Queue::fake();
|
|
|
|
|
|
|
|
|
|
$post = Post::factory()->create(['type' => 'twitter', 'status' => 'draft']);
|
|
|
|
|
|
|
|
|
|
Livewire::test(Index::class)
|
|
|
|
|
->call('publish', $post->id)
|
|
|
|
|
->assertSee("No publisher available yet for post type 'twitter'");
|
|
|
|
|
|
|
|
|
|
expect($post->fresh()->status)->toBe('draft');
|
|
|
|
|
Queue::assertNotPushed(PublishPostJob::class);
|
|
|
|
|
});
|