2026-07-21 14:04:45 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use App\Jobs\PublishPostJob;
|
|
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Models\Site;
|
|
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use App\Support\Publishing\PublisherManager;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
|
|
|
|
|
|
test('it publishes and updates the post on success', function () {
|
|
|
|
|
Http::fake([
|
2026-07-21 20:03:25 +03:00
|
|
|
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
|
|
|
|
'*/wp-json/wp/v2/media' => Http::response(['id' => 99], 201),
|
2026-07-21 14:04:45 +03:00
|
|
|
'*/wp-json/wp/v2/posts' => Http::response(['link' => 'https://wp.example.com/post'], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$site = Site::factory()->wordpress()->create();
|
2026-07-21 20:03:25 +03:00
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'image_url' => 'https://images.example.com/photo.jpg', 'status' => 'publishing']);
|
2026-07-21 14:04:45 +03:00
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
expect($post->fresh())
|
|
|
|
|
->status->toBe('published')
|
|
|
|
|
->published_url->toBe('https://wp.example.com/post')
|
|
|
|
|
->publish_error->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it marks the post failed when the publish attempt fails', function () {
|
|
|
|
|
Http::fake([
|
2026-07-21 20:03:25 +03:00
|
|
|
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
|
|
|
|
'*/wp-json/wp/v2/media' => Http::response(['id' => 99], 201),
|
2026-07-21 14:04:45 +03:00
|
|
|
'*/wp-json/wp/v2/posts' => Http::response(['message' => 'nope'], 401),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$site = Site::factory()->wordpress()->create();
|
2026-07-21 20:03:25 +03:00
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'image_url' => 'https://images.example.com/photo.jpg', 'status' => 'publishing']);
|
2026-07-21 14:04:45 +03:00
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
expect($post->fresh())
|
|
|
|
|
->status->toBe('failed')
|
|
|
|
|
->publish_error->toBe('nope');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it reverts a rejected post to draft instead of failed', function () {
|
|
|
|
|
$site = Site::factory()->wordpress()->create();
|
|
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => null, 'status' => 'publishing']);
|
|
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
expect($post->fresh())
|
|
|
|
|
->status->toBe('draft')
|
|
|
|
|
->publish_error->toContain('no title');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it reverts to draft when no publisher supports the post type', function () {
|
|
|
|
|
$post = Post::factory()->create(['type' => 'twitter', 'status' => 'publishing']);
|
|
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
expect($post->fresh())
|
|
|
|
|
->status->toBe('draft')
|
|
|
|
|
->publish_error->toContain('No publisher available');
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-21 19:29:44 +03:00
|
|
|
test('it skips a duplicate job for a post no longer in publishing state', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/wp-json/wp/v2/posts' => Http::response(['link' => 'https://wp.example.com/post'], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$site = Site::factory()->wordpress()->create();
|
|
|
|
|
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'published', 'published_url' => 'https://wp.example.com/already-there']);
|
|
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
Http::assertNothingSent();
|
|
|
|
|
expect($post->fresh()->published_url)->toBe('https://wp.example.com/already-there');
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
test('it publishes a reddit post via the sidecar', function () {
|
|
|
|
|
Process::fake([
|
|
|
|
|
'*' => Process::result(output: json_encode(['success' => true, 'url' => 'https://reddit.com/r/test/comments/abc'])),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$account = SocialAccount::factory()->create(['network' => 'reddit']);
|
|
|
|
|
$post = Post::factory()->create([
|
|
|
|
|
'type' => 'reddit',
|
|
|
|
|
'title' => 'Hi',
|
|
|
|
|
'target' => 'test',
|
|
|
|
|
'social_account_id' => $account->id,
|
|
|
|
|
'status' => 'publishing',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
|
|
|
|
|
|
|
|
|
|
expect($post->fresh())
|
|
|
|
|
->status->toBe('published')
|
|
|
|
|
->published_url->toBe('https://reddit.com/r/test/comments/abc');
|
|
|
|
|
});
|