trypost/tests/Unit/PostPlatformTest.php
Paulo Castellano 3f6032c152 fix(facebook): empty-message rejection + state consistency + no re-publish on terminal
Production incident: a customer's Facebook Page post failed with 'The post
is empty. Please enter a message to share.' (error code 197) and ended up
with a contradictory DB state (status=published + error_message=set).

Three independent bugs were uncovered:

A. FacebookPublisher sends 'message'/'description' as null when the user
   posts media without text. Graph API requires the key be omitted, not
   null. Fixed in publishSingleImagePost, publishMultiImagePost,
   publishVideoPost, publishReel.

B. markAsPublished/markAsFailed leak stale fields across transitions
   (a published row could retain error_message from a prior failure,
   vice-versa). Both transitions now explicitly clear the opposite
   side's fields.

C. status='failed' was editable in the UI and the backend, so users
   were re-clicking Publish, generating duplicate failure emails and
   the contradictory state from bug B. The frontend isReadOnly check
   and the UpdatePost backend guard now treat Published/PartiallyPublished/
   Failed/Publishing as terminal. To retry, the user duplicates the post.

11 new tests guarantee these can't regress silently: FB payload shape
per content type, PostPlatform field-clearing on transitions, and the
terminal-status block at the controller level.
2026-05-19 12:47:18 -03:00

64 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\PostPlatform\Status;
use App\Models\Post;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->user->id,
]);
$this->socialAccount = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$this->postPlatform = PostPlatform::factory()->create([
'post_id' => $this->post->id,
'social_account_id' => $this->socialAccount->id,
]);
});
test('markAsPublished clears stale error_message and error_context', function () {
$this->postPlatform->update([
'status' => Status::Failed,
'error_message' => 'The post is empty. Please enter a message to share.',
'error_context' => ['platform_error_code' => 197, 'content_length' => 0],
]);
$this->postPlatform->markAsPublished('platform_post_abc', 'https://www.facebook.com/platform_post_abc');
$this->postPlatform->refresh();
expect($this->postPlatform->status)->toBe(Status::Published)
->and($this->postPlatform->platform_post_id)->toBe('platform_post_abc')
->and($this->postPlatform->error_message)->toBeNull()
->and($this->postPlatform->error_context)->toBeNull();
});
test('markAsFailed clears stale platform_post_id and platform_url', function () {
$this->postPlatform->update([
'status' => Status::Published,
'platform_post_id' => 'old_post_id',
'platform_url' => 'https://www.facebook.com/old_post_id',
'published_at' => now(),
]);
$this->postPlatform->markAsFailed('Something went wrong.', ['platform_error_code' => 500]);
$this->postPlatform->refresh();
expect($this->postPlatform->status)->toBe(Status::Failed)
->and($this->postPlatform->error_message)->toBe('Something went wrong.')
->and($this->postPlatform->platform_post_id)->toBeNull()
->and($this->postPlatform->platform_url)->toBeNull();
});