* Fix Facebook and Instagram-via-Facebook Page connect pagination. Follow Graph API paging.next on /me/accounts so authorized non-first Pages are found and multi-Page accounts get the picker instead of silently connecting the first result. Co-authored-by: Cursor <cursoragent@cursor.com> * Paginate Meta accounts until paging.next is exhausted. Drop the artificial 50-page cap and stop only when there is no next URL, or the same request URL repeats (broken pagination loop). Co-authored-by: Cursor <cursoragent@cursor.com> * Redact tokens in Graph pagination logs and harden test coverage. Cover happy-path and failure cases for Meta /me/accounts pagination, including mid-loop failures, invalid paging.next, and Instagram pages without a linked IG account. Co-authored-by: Cursor <cursoragent@cursor.com> * Fail closed on incomplete Meta accounts pagination. If a later /me/accounts page fails after earlier pages succeeded, throw instead of returning a truncated list that could auto-connect the wrong Page. Also revert the IG detail timeout that could wipe the whole connect list. Co-authored-by: Cursor <cursoragent@cursor.com> * Simplify Graph pagination helpers and page fetchers. Bake the first request query into the URL, drop requestKey, and let IncompleteGraphPaginationException bubble from the controllers without catch/rethrow noise. Co-authored-by: Cursor <cursoragent@cursor.com> * Move incomplete pagination exception under Social\Meta. Colocate it with GraphPaginator so the Meta scope is clear from the namespace instead of a generic Social exception name. Co-authored-by: Cursor <cursoragent@cursor.com> * Rename pagination exception to IncompleteMetaGraphPaginationException. Keep it under Exceptions/Social with Meta in the class name instead of moving it into Services. Co-authored-by: Cursor <cursoragent@cursor.com> * Make GraphPaginator results explicit before mapping pages. Assign the paginated accounts to a variable first so the Facebook and Instagram-via-Facebook fetchers read more clearly. Co-authored-by: Cursor <cursoragent@cursor.com> * Build Meta Graph pagination URLs with Laravel Uri. Replace manual http_build_query concatenation with Uri::of()->withQuery(). Co-authored-by: Cursor <cursoragent@cursor.com> * Use Laravel HTTP and Uri helpers in Meta Graph pagination. Prefer response collect/json key access, filled(), and Uri path parsing over manual array and parse_url handling. Co-authored-by: Cursor <cursoragent@cursor.com> * Simplify graphVersion using Uri path and str(). Drop basename and native string casts; Uri::path() already yields the Graph API version segment. Co-authored-by: Cursor <cursoragent@cursor.com> * Drop unnecessary str() around graph API config. Uri: :of() already accepts the string returned by config(). Co-authored-by: Cursor <cursoragent@cursor.com> * Simplify GraphPaginator with Laravel helpers. Consolidate failure handling via abort(), and use collect, when, throw_if, and Uri::value() for a shorter pagination loop. Co-authored-by: Cursor <cursoragent@cursor.com> * Refactor social OAuth page/channel selection handling. Update selectPage and selectChannel methods in Facebook, Instagram, and YouTube controllers to return popup callbacks instead of redirecting on session expiration or workspace not found. Enhance HandleInertiaRequests middleware to prevent deferring onboarding progress on social OAuth popup routes. Add tests to verify behavior for expired sessions and onboarding progress. * Unify Instagram connect behind one card with a method picker. Hide the Instagram-via-Facebook grid card and offer Instagram Login vs Facebook Pages from a single network entry, matching LinkedIn. Co-authored-by: Cursor <cursoragent@cursor.com> * Move social popup onboarding assertions into connection tests. Cover the deferred-prop popup regression on Facebook, Instagram, and YouTube select routes instead of a synthetic onboarding share check. Co-authored-by: Cursor <cursoragent@cursor.com> * Stop suppressing onboarding defer on all social routes. Override onboardingProgress only in popupCallback so picker pages stay deferred and the close page does not re-hit select after session clear. Co-authored-by: Cursor <cursoragent@cursor.com> * Always open the Instagram method dialog on connect. Drop connectMethods and the single-method OAuth shortcut; the picker always offers both Login and Facebook Pages. Co-authored-by: Cursor <cursoragent@cursor.com> * Filter Instagram dialog options by enabled platforms. Keep always opening the method picker, but only list OAuth entry points that are turned on. Co-authored-by: Cursor <cursoragent@cursor.com> * Extract Instagram connect methods into a dedicated helper. Keep connectableOptions focused on shaping grid options while the enabled OAuth list lives in instagramConnectMethods(). Co-authored-by: Cursor <cursoragent@cursor.com> * Harden Meta Graph pagination and localize Instagram connect copy. Fail closed on Graph request errors and pathological paging, keep Instagram connect going when profile detail lookups time out, and translate the Instagram method dialog strings. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
293 lines
11 KiB
PHP
293 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\Post\Status;
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Enums\UserWorkspace\Role;
|
|
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->workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
|
|
$this->user->update(['current_workspace_id' => $this->workspace->id]);
|
|
});
|
|
|
|
// Index tests
|
|
test('accounts index requires authentication', function () {
|
|
$response = $this->get(route('app.accounts'));
|
|
|
|
$response->assertRedirect(route('login'));
|
|
});
|
|
|
|
test('accounts index shows platforms and connected accounts', function () {
|
|
SocialAccount::factory()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'platform' => Platform::LinkedIn,
|
|
]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->has('workspace')
|
|
->has('platforms')
|
|
->has('platforms.0.network')
|
|
->has('connectedAccounts', 1)
|
|
);
|
|
});
|
|
|
|
test('accounts index lists platforms alphabetically by label', function () {
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', function ($platforms): bool {
|
|
$labels = collect($platforms)->pluck('label')->all();
|
|
|
|
$sorted = $labels;
|
|
natcasesort($sorted);
|
|
|
|
return array_values($labels) === array_values($sorted);
|
|
})
|
|
);
|
|
});
|
|
|
|
test('accounts index offers a single linkedin card and no standalone linkedin page card', function () {
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', fn ($platforms) => collect($platforms)->contains('value', Platform::LinkedIn->value)
|
|
&& ! collect($platforms)->contains('value', Platform::LinkedInPage->value)
|
|
)
|
|
);
|
|
});
|
|
|
|
test('the linkedin card still shows when only company pages are enabled', function () {
|
|
config(['trypost.platforms.linkedin.enabled' => false]);
|
|
config(['trypost.platforms.linkedin-page.enabled' => true]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', fn ($platforms) => collect($platforms)->contains('value', Platform::LinkedIn->value))
|
|
);
|
|
});
|
|
|
|
test('the linkedin card disappears only when both capabilities are disabled', function () {
|
|
config(['trypost.platforms.linkedin.enabled' => false]);
|
|
config(['trypost.platforms.linkedin-page.enabled' => false]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', fn ($platforms) => ! collect($platforms)->contains('value', Platform::LinkedIn->value))
|
|
);
|
|
});
|
|
|
|
test('a connected linkedin page account is still returned so it surfaces under the linkedin card', function () {
|
|
SocialAccount::factory()->linkedinPage()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
// The grid groups by the account's own `network`, so a linkedin-page account
|
|
// must report network=linkedin to surface under the single LinkedIn card.
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('connectedAccounts.0.platform', Platform::LinkedInPage->value)
|
|
->where('connectedAccounts.0.network', 'linkedin')
|
|
);
|
|
});
|
|
|
|
test('accounts index offers a single instagram card and no instagram-facebook card', function () {
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', fn ($platforms) => collect($platforms)->contains('value', Platform::Instagram->value)
|
|
&& ! collect($platforms)->contains('value', Platform::InstagramFacebook->value)
|
|
)
|
|
);
|
|
});
|
|
|
|
test('the instagram card still shows when only facebook business is enabled', function () {
|
|
config(['trypost.platforms.instagram.enabled' => false]);
|
|
config(['trypost.platforms.instagram-facebook.enabled' => true]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', function ($platforms): bool {
|
|
$instagram = collect($platforms)->firstWhere('value', Platform::Instagram->value);
|
|
|
|
return $instagram !== null
|
|
&& data_get($instagram, 'connect_methods') === [Platform::InstagramFacebook->value];
|
|
})
|
|
);
|
|
});
|
|
|
|
test('instagram card connect methods omit disabled facebook business entry', function () {
|
|
config(['trypost.platforms.instagram.enabled' => true]);
|
|
config(['trypost.platforms.instagram-facebook.enabled' => false]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', function ($platforms): bool {
|
|
$instagram = collect($platforms)->firstWhere('value', Platform::Instagram->value);
|
|
|
|
return data_get($instagram, 'connect_methods') === [Platform::Instagram->value];
|
|
})
|
|
);
|
|
});
|
|
|
|
test('the instagram card disappears only when both capabilities are disabled', function () {
|
|
config(['trypost.platforms.instagram.enabled' => false]);
|
|
config(['trypost.platforms.instagram-facebook.enabled' => false]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('platforms', fn ($platforms) => ! collect($platforms)->contains('value', Platform::Instagram->value))
|
|
);
|
|
});
|
|
|
|
test('a connected instagram-facebook account is still returned so it surfaces under the instagram card', function () {
|
|
SocialAccount::factory()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'platform' => Platform::InstagramFacebook,
|
|
'scopes' => Platform::InstagramFacebook->requiredPublishScopes(),
|
|
]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertOk();
|
|
$response->assertInertia(fn ($page) => $page
|
|
->component('accounts/Index', false)
|
|
->where('connectedAccounts.0.platform', Platform::InstagramFacebook->value)
|
|
->where('connectedAccounts.0.network', 'instagram')
|
|
);
|
|
});
|
|
|
|
test('an unsubscribed account can disconnect during onboarding (no active subscription required)', function () {
|
|
config(['trypost.self_hosted' => false]);
|
|
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$response = $this->actingAs($this->user)->delete(route('app.accounts.disconnect', $account));
|
|
|
|
$response->assertRedirect();
|
|
$response->assertSessionMissing('errors');
|
|
expect(SocialAccount::find($account->id))->toBeNull();
|
|
});
|
|
|
|
test('accounts index redirects if no workspace', function () {
|
|
$this->user->update(['current_workspace_id' => null]);
|
|
|
|
$response = $this->actingAs($this->user)->get(route('app.accounts'));
|
|
|
|
$response->assertRedirect(route('app.workspaces.create'));
|
|
});
|
|
|
|
// Disconnect tests
|
|
test('disconnect requires authentication', function () {
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$response = $this->delete(route('app.accounts.disconnect', $account));
|
|
|
|
$response->assertRedirect(route('login'));
|
|
});
|
|
|
|
test('disconnect removes social account', function () {
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$response = $this->actingAs($this->user)->delete(route('app.accounts.disconnect', $account));
|
|
|
|
$response->assertRedirect();
|
|
expect(SocialAccount::find($account->id))->toBeNull();
|
|
});
|
|
|
|
test('disconnect deletes pending platform rows from drafts and keeps published history', function () {
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$draftPost = Post::factory()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'user_id' => $this->user->id,
|
|
'status' => Status::Draft,
|
|
]);
|
|
$publishedPost = Post::factory()->create([
|
|
'workspace_id' => $this->workspace->id,
|
|
'user_id' => $this->user->id,
|
|
'status' => Status::Published,
|
|
]);
|
|
|
|
$pendingPlatform = PostPlatform::factory()->create([
|
|
'post_id' => $draftPost->id,
|
|
'social_account_id' => $account->id,
|
|
'status' => App\Enums\PostPlatform\Status::Pending,
|
|
]);
|
|
$publishedPlatform = PostPlatform::factory()->create([
|
|
'post_id' => $publishedPost->id,
|
|
'social_account_id' => $account->id,
|
|
'status' => App\Enums\PostPlatform\Status::Published,
|
|
'platform_name' => 'Snapshot Name',
|
|
'platform_avatar' => 'avatars/snapshot.jpg',
|
|
]);
|
|
|
|
$this->actingAs($this->user)->delete(route('app.accounts.disconnect', $account));
|
|
|
|
expect(PostPlatform::find($pendingPlatform->id))->toBeNull();
|
|
|
|
$publishedPlatform->refresh();
|
|
expect($publishedPlatform->social_account_id)->toBeNull();
|
|
expect($publishedPlatform->platform_name)->toBe('Snapshot Name');
|
|
expect($publishedPlatform->display_avatar)->toContain('avatars/snapshot.jpg');
|
|
});
|
|
|
|
test('disconnect returns 403 for other workspace account', function () {
|
|
$otherWorkspace = Workspace::factory()->create();
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $otherWorkspace->id]);
|
|
|
|
$response = $this->actingAs($this->user)->delete(route('app.accounts.disconnect', $account));
|
|
|
|
$response->assertForbidden();
|
|
});
|
|
|
|
// Member authorization tests
|
|
test('member cannot disconnect social account', function () {
|
|
$member = User::factory()->create([]);
|
|
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
|
|
$member->update(['current_workspace_id' => $this->workspace->id]);
|
|
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$this->actingAs($member)->delete(route('app.accounts.disconnect', $account))->assertForbidden();
|
|
});
|
|
|
|
test('member cannot toggle social account', function () {
|
|
$member = User::factory()->create([]);
|
|
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
|
|
$member->update(['current_workspace_id' => $this->workspace->id]);
|
|
|
|
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
|
|
|
|
$this->actingAs($member)->put(route('app.accounts.toggle', $account))->assertForbidden();
|
|
});
|