trypost/tests/Unit/PostPlatformTest.php
Paulo Castellano abe687d0fa
Fall back to display name when a published-post email has no username (#325)
* Fall back to display name when a published-post email has no username.

Facebook Pages often have no vanity username, which left the email as "Facebook Page (@)". Use username, then display name, and omit the empty parentheses when both are missing.

* Move the notification account label onto PostPlatform.

The fallback belongs on the row that always has a platform, so mail and in-app notify can call one method instead of guarding a missing social account at every call site.

* Cover the in-app published/failed notification body for Facebook Pages.

The email tests were not enough — SendNotification body is what the in-app inbox shows, and that path also goes through notificationLabel().
2026-09-03 12:51:44 -03:00

161 lines
5.9 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('display_name falls back to the account username when display_name is not set', function () {
$this->socialAccount->update(['display_name' => null, 'username' => 'acme_handle']);
expect($this->postPlatform->fresh()->display_name)->toBe('acme_handle');
});
test('display_name falls back to the platform label when the live account has neither name set', function () {
$this->socialAccount->update(['display_name' => null, 'username' => null]);
expect($this->postPlatform->fresh()->display_name)->toBe($this->socialAccount->platform->label());
});
test('display_name falls back to the platform_name snapshot when the account has been deleted', function () {
$this->postPlatform->update(['platform_name' => 'Snapshot Name']);
$this->socialAccount->delete();
expect($this->postPlatform->fresh()->display_name)->toBe('Snapshot Name');
});
test('notificationLabel prefers username over display name', function () {
$account = SocialAccount::factory()->facebook()->create([
'workspace_id' => $this->workspace->id,
'username' => 'myfbpage',
'display_name' => 'InboxPlacement.io',
]);
$postPlatform = PostPlatform::factory()->facebook()->create([
'post_id' => $this->post->id,
'social_account_id' => $account->id,
'platform' => $account->platform,
]);
expect($postPlatform->notificationLabel())->toBe('Facebook Page (@myfbpage)');
});
test('notificationLabel falls back to display name when username is empty', function () {
$account = SocialAccount::factory()->facebook()->create([
'workspace_id' => $this->workspace->id,
'username' => null,
'display_name' => 'InboxPlacement.io',
]);
$postPlatform = PostPlatform::factory()->facebook()->create([
'post_id' => $this->post->id,
'social_account_id' => $account->id,
'platform' => $account->platform,
]);
expect($postPlatform->notificationLabel())->toBe('Facebook Page (@InboxPlacement.io)');
});
test('notificationLabel uses username when display name is empty', function () {
$account = SocialAccount::factory()->bluesky()->create([
'workspace_id' => $this->workspace->id,
'username' => 'inboxplacementio.bsky.social',
'display_name' => '',
]);
$postPlatform = PostPlatform::factory()->bluesky()->create([
'post_id' => $this->post->id,
'social_account_id' => $account->id,
'platform' => $account->platform,
]);
expect($postPlatform->notificationLabel())->toBe('Bluesky (@inboxplacementio.bsky.social)');
});
test('notificationLabel omits empty parentheses when both identifiers are missing', function () {
$account = SocialAccount::factory()->facebook()->create([
'workspace_id' => $this->workspace->id,
'username' => null,
'display_name' => '',
]);
$postPlatform = PostPlatform::factory()->facebook()->create([
'post_id' => $this->post->id,
'social_account_id' => $account->id,
'platform' => $account->platform,
]);
expect($postPlatform->notificationLabel())->toBe('Facebook Page');
});
test('notificationLabel uses the snapshot when the account has been deleted', function () {
$account = SocialAccount::factory()->facebook()->create([
'workspace_id' => $this->workspace->id,
'username' => null,
'display_name' => 'InboxPlacement.io',
]);
$postPlatform = PostPlatform::factory()->facebook()->create([
'post_id' => $this->post->id,
'social_account_id' => $account->id,
'platform' => $account->platform,
'platform_name' => 'InboxPlacement.io',
]);
$account->delete();
expect($postPlatform->fresh()->notificationLabel())->toBe('Facebook Page (@InboxPlacement.io)');
});
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();
});