2026-01-19 00:49:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
refactor: settings redesign, Spanish translations, language system, strict_types
Settings pages:
- Redesign layout to match Sendkit (max-w-4xl, space-y-12, Separator sections)
- Merge Members page into Workspace settings with Table, invite Dialog, ConfirmDeleteModal
- Add workspace logo upload/delete routes and controller methods
- Translate all hardcoded strings in Workspace.vue modals
Language system:
- Drop languages table, replace language_id FK with locale string column on users
- Create config/languages.php for available languages and default locale
- Add Spanish (es) translations (13 files)
- Simplify HandleInertiaRequests, ProfileController, RegisteredUserController
Code quality:
- Add declare(strict_types=1) to all PHP files
- Fix MastodonPublisher using wrong attribute (filename -> original_filename)
- Fix HasMediaTest for new has_photo/photo_url accessors
- Fix PublishToSocialPlatformTest type error revealed by strict_types
- Remove orphaned Language model from AppServiceProvider morph map
- Update User TypeScript interface (has_photo, photo_url, locale)
- Eager load media relation on workspaces to prevent N+1
- Add 8 new tests for workspace logo upload/delete
- Update workspace settings test to assert members/invitations props
All 710 tests passing.
2026-03-30 03:20:43 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-19 00:49:13 +00:00
|
|
|
use App\Enums\PostPlatform\ContentType;
|
|
|
|
|
use App\Enums\SocialAccount\Platform;
|
2026-04-01 00:04:39 +00:00
|
|
|
use App\Exceptions\Social\XPublishException;
|
2026-01-19 00:49:13 +00:00
|
|
|
use App\Exceptions\TokenExpiredException;
|
|
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Models\PostPlatform;
|
|
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use App\Models\Workspace;
|
|
|
|
|
use App\Services\Social\XPublisher;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
$this->workspace = Workspace::factory()->create();
|
|
|
|
|
$this->socialAccount = SocialAccount::factory()->create([
|
|
|
|
|
'workspace_id' => $this->workspace->id,
|
|
|
|
|
'platform' => Platform::X,
|
|
|
|
|
'platform_user_id' => 'x-123',
|
|
|
|
|
'username' => 'testuser',
|
|
|
|
|
'access_token' => 'test-token',
|
|
|
|
|
'token_expires_at' => now()->addDays(30),
|
|
|
|
|
]);
|
2026-04-15 23:11:36 +00:00
|
|
|
$this->post = Post::factory()->create(['workspace_id' => $this->workspace->id, 'content' => 'Test tweet']);
|
2026-01-19 00:49:13 +00:00
|
|
|
$this->postPlatform = PostPlatform::factory()->create([
|
|
|
|
|
'post_id' => $this->post->id,
|
|
|
|
|
'social_account_id' => $this->socialAccount->id,
|
|
|
|
|
'content_type' => ContentType::XPost,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher publishes text only tweet', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/2/tweets' => Http::response([
|
|
|
|
|
'data' => ['id' => 'tweet-123'],
|
|
|
|
|
], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
$result = $publisher->publish($this->postPlatform);
|
|
|
|
|
|
|
|
|
|
expect($result['id'])->toBe('tweet-123');
|
|
|
|
|
expect($result['url'])->toBe('https://x.com/testuser/status/tweet-123');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher throws token expired exception on 401', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'title' => 'Unauthorized',
|
|
|
|
|
'detail' => 'Unauthorized',
|
|
|
|
|
], 401),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
|
|
|
|
->toThrow(TokenExpiredException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher refreshes token when expired', function () {
|
|
|
|
|
$this->socialAccount->update([
|
|
|
|
|
'token_expires_at' => now()->subHour(),
|
|
|
|
|
'refresh_token' => 'refresh-token-123',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/2/oauth2/token' => Http::response([
|
|
|
|
|
'access_token' => 'new-access-token',
|
|
|
|
|
'refresh_token' => 'new-refresh-token',
|
|
|
|
|
'expires_in' => 7200,
|
|
|
|
|
], 200),
|
|
|
|
|
'*/2/tweets' => Http::response([
|
|
|
|
|
'data' => ['id' => 'tweet-123'],
|
|
|
|
|
], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
$result = $publisher->publish($this->postPlatform);
|
|
|
|
|
|
|
|
|
|
expect($result['id'])->toBe('tweet-123');
|
|
|
|
|
$this->socialAccount->refresh();
|
|
|
|
|
expect($this->socialAccount->access_token)->toBe('new-access-token');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher throws exception when no refresh token', function () {
|
|
|
|
|
$this->socialAccount->update([
|
|
|
|
|
'token_expires_at' => now()->subHour(),
|
|
|
|
|
'refresh_token' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
|
|
|
|
->toThrow(TokenExpiredException::class, 'No refresh token available');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher throws exception on api error', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'title' => 'Bad Request',
|
|
|
|
|
'detail' => 'Invalid tweet content',
|
|
|
|
|
], 400),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
2026-03-29 22:24:28 +00:00
|
|
|
->toThrow(Exception::class);
|
2026-01-19 00:49:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher returns unknown id when no id in response', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/2/tweets' => Http::response([
|
|
|
|
|
'data' => [],
|
|
|
|
|
], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
$result = $publisher->publish($this->postPlatform);
|
|
|
|
|
|
|
|
|
|
expect($result['id'])->toBe('unknown');
|
|
|
|
|
expect($result['url'])->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher handles token refresh failure', function () {
|
|
|
|
|
$this->socialAccount->update([
|
|
|
|
|
'token_expires_at' => now()->subHour(),
|
|
|
|
|
'refresh_token' => 'invalid-refresh-token',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/2/oauth2/token' => Http::response([
|
|
|
|
|
'title' => 'Unauthorized',
|
|
|
|
|
'detail' => 'Invalid refresh token',
|
|
|
|
|
], 401),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
|
|
|
|
->toThrow(TokenExpiredException::class);
|
|
|
|
|
});
|
2026-01-19 01:01:55 +00:00
|
|
|
|
2026-07-03 11:30:58 +00:00
|
|
|
test('x publisher does NOT rotate the token when it is only expiring soon but still valid', function () {
|
2026-01-19 01:01:55 +00:00
|
|
|
$this->socialAccount->update([
|
|
|
|
|
'token_expires_at' => now()->addMinutes(5),
|
|
|
|
|
'refresh_token' => 'refresh-token-123',
|
|
|
|
|
]);
|
2026-07-03 11:30:58 +00:00
|
|
|
$originalAccessToken = $this->socialAccount->access_token;
|
2026-01-19 01:01:55 +00:00
|
|
|
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*/2/oauth2/token' => Http::response([
|
|
|
|
|
'access_token' => 'new-access-token',
|
|
|
|
|
'refresh_token' => 'new-refresh-token',
|
|
|
|
|
'expires_in' => 7200,
|
|
|
|
|
], 200),
|
|
|
|
|
'*/2/tweets' => Http::response([
|
|
|
|
|
'data' => ['id' => 'tweet-123'],
|
|
|
|
|
], 201),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
$result = $publisher->publish($this->postPlatform);
|
|
|
|
|
|
|
|
|
|
expect($result['id'])->toBe('tweet-123');
|
2026-07-03 11:30:58 +00:00
|
|
|
|
|
|
|
|
// A still-valid token is used as-is — the single-use refresh_token is not rotated.
|
|
|
|
|
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/2/oauth2/token'));
|
2026-01-19 01:01:55 +00:00
|
|
|
$this->socialAccount->refresh();
|
2026-07-03 11:30:58 +00:00
|
|
|
expect($this->socialAccount->access_token)->toBe($originalAccessToken);
|
2026-01-19 01:01:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher handles 403 error as generic error', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'title' => 'Forbidden',
|
|
|
|
|
'detail' => 'You do not have access',
|
|
|
|
|
], 403),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
2026-04-01 00:04:39 +00:00
|
|
|
->toThrow(XPublishException::class);
|
2026-01-19 01:01:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('x publisher handles empty error response', function () {
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response(null, 500),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$publisher = new XPublisher;
|
|
|
|
|
|
|
|
|
|
expect(fn () => $publisher->publish($this->postPlatform))
|
2026-03-29 22:24:28 +00:00
|
|
|
->toThrow(Exception::class);
|
2026-01-19 01:01:55 +00:00
|
|
|
});
|