trypost/tests/Feature/Actions/Post/DuplicatePostTest.php
Paulo Castellano bd3a349f56
fix: skip removed social accounts when duplicating posts (#226)
* fix: skip removed social accounts when duplicating posts

DuplicatePost was copying orphan post_platform rows left after disconnect
(null social_account_id with snapshot name/username/avatar), which showed
broken avatars in the draft editor. Skip platforms without a live account.

Co-authored-by: Paulo Castellano <hello@paulocastellano.com>

* refactor: filter duplicate platforms with whereHas

Use whereHas('socialAccount') instead of null checks in the loop.

Co-authored-by: Paulo Castellano <hello@paulocastellano.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-08-03 13:04:42 -03:00

131 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\Post\DuplicatePost;
use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Enums\PostPlatform\Status as PostPlatformStatus;
use App\Enums\SocialAccount\Platform;
use App\Events\PostCreated;
use App\Models\Post;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Event;
test('execute clones the post as a draft created via web', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$original = Post::factory()->create([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
'content' => 'Original content',
'created_via' => CreatedVia::Api,
'status' => PostStatus::Published,
]);
$copy = DuplicatePost::execute($original, $user);
expect($copy->id)->not->toBe($original->id)
->and($copy->content)->toBe('Original content')
->and($copy->status)->toBe(PostStatus::Draft)
->and($copy->created_via)->toBe(CreatedVia::Web)
->and($copy->scheduled_at)->toBeNull()
->and($copy->published_at)->toBeNull();
});
test('execute relies on the observer to dispatch PostCreated for the duplicate', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$original = Post::factory()->create([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
]);
Event::fake([PostCreated::class]);
$copy = DuplicatePost::execute($original, $user);
Event::assertDispatched(
PostCreated::class,
fn (PostCreated $event) => $event->post->id === $copy->id,
);
});
test('execute skips platform rows whose social account was removed', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$liveAccount = SocialAccount::factory()->create([
'workspace_id' => $workspace->id,
'platform' => Platform::X,
'display_name' => 'Live Account',
'username' => 'live_user',
]);
$original = Post::factory()->create([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
'status' => PostStatus::Published,
]);
PostPlatform::factory()->x()->published()->create([
'post_id' => $original->id,
'social_account_id' => $liveAccount->id,
'platform_name' => 'Live Account',
'platform_username' => 'live_user',
'platform_avatar' => 'avatars/live.jpg',
'enabled' => true,
]);
// History row left after disconnect: FK nullOnDelete + snapshot fields.
PostPlatform::factory()->tiktok()->published()->create([
'post_id' => $original->id,
'social_account_id' => null,
'platform_name' => 'Removed Account',
'platform_username' => 'gone_user',
'platform_avatar' => 'avatars/orphan.jpg',
'enabled' => true,
]);
$copy = DuplicatePost::execute($original->load(['postPlatforms', 'labels']), $user);
$copiedPlatforms = $copy->postPlatforms()->get();
expect($copiedPlatforms)->toHaveCount(1)
->and($copiedPlatforms->first()->social_account_id)->toBe($liveAccount->id)
->and($copiedPlatforms->first()->platform)->toBe(Platform::X)
->and($copiedPlatforms->first()->platform_name)->toBe('Live Account')
->and($copiedPlatforms->first()->status)->toBe(PostPlatformStatus::Pending)
->and($copiedPlatforms->first()->enabled)->toBeTrue();
});
test('execute copies only platforms that still have a social account relation', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$account = SocialAccount::factory()->create(['workspace_id' => $workspace->id]);
$original = Post::factory()->create([
'workspace_id' => $workspace->id,
'user_id' => $user->id,
'status' => PostStatus::Published,
]);
$platform = PostPlatform::factory()->published()->create([
'post_id' => $original->id,
'social_account_id' => $account->id,
'platform_name' => $account->display_name,
'platform_username' => $account->username,
'enabled' => true,
]);
$account->delete();
$platform->refresh();
expect($platform->social_account_id)->toBeNull();
$copy = DuplicatePost::execute($original->fresh(['postPlatforms', 'labels']), $user);
expect($copy->postPlatforms)->toHaveCount(0);
});