Files
muninn/tests/Feature/Livewire/SitesTest.php
T
m105andClaude Sonnet 5 9278d2e8a0 Add a Publisher abstraction and queued job to publish from the UI
Introduce Publisher/PublisherManager (WordPress + Reddit implementations)
so publishing logic is shared between the CLI, the queue, and future
network integrations. Add a "Publish" button on the Posts page that
dispatches PublishPostJob instead of blocking the request — Reddit's
publisher shells out to the existing Playwright sidecar and can take
tens of seconds, so it can't run synchronously in a web request.

Adds a `target` field (e.g. subreddit) and `publishing` status to Post,
plus a `publish_error` field so failures surface in the UI. Refactors
posts:publish to delegate to WordPressPublisher without changing its
CLI output contract (existing tests pass unchanged).

Also fixes two bugs found along the way: UpdatePostRequest was silently
dropping `published_url` from PATCH requests (never in its validation
rules, despite AI_API.md documenting it), and three Livewire index-page
tests were broken by the earlier auth change (never re-run after adding
the `auth` middleware) since they hit routes via HTTP without a logged-in
user.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 14:04:45 +03:00

124 lines
3.7 KiB
PHP

<?php
use App\Livewire\Sites\Index;
use App\Models\Site;
use App\Models\User;
use Livewire\Livewire;
test('sites index page lists existing sites', function () {
$site = Site::factory()->create(['name' => 'Example Site']);
$this->actingAs(User::factory()->create())
->get(route('sites.index'))
->assertOk()
->assertSee('Example Site');
});
test('a new site can be created', function () {
Livewire::test(Index::class)
->set('name', 'New Site')
->set('url', 'https://newsite.test')
->set('description', 'Some description')
->call('save');
expect(Site::where('name', 'New Site')->exists())->toBeTrue();
});
test('name and url are required to create a site', function () {
Livewire::test(Index::class)
->set('name', '')
->set('url', '')
->call('save')
->assertHasErrors(['name', 'url']);
});
test('an existing site can be edited', function () {
$site = Site::factory()->create(['name' => 'Old Name']);
Livewire::test(Index::class)
->call('edit', $site->id)
->set('name', 'Updated Name')
->call('save');
expect($site->fresh()->name)->toBe('Updated Name');
});
test('a site can be deleted', function () {
$site = Site::factory()->create();
Livewire::test(Index::class)->call('delete', $site->id);
expect(Site::find($site->id))->toBeNull();
});
test('a new WordPress site can be created with credentials', function () {
Livewire::test(Index::class)
->set('name', 'WP Site')
->set('url', 'https://wp.example.com')
->set('cmsType', 'wordpress')
->set('wpUsername', 'admin')
->set('wpAppPassword', 'abcd 1234 efgh 5678')
->call('save');
$site = Site::where('name', 'WP Site')->first();
expect($site)
->cms_type->toBe('wordpress')
->wp_username->toBe('admin')
->wp_app_password->toBe('abcd 1234 efgh 5678');
});
test('WordPress username and app password are required together when cms_type is wordpress', function () {
Livewire::test(Index::class)
->set('name', 'WP Site')
->set('url', 'https://wp.example.com')
->set('cmsType', 'wordpress')
->set('wpUsername', '')
->set('wpAppPassword', '')
->call('save')
->assertHasErrors(['wpUsername', 'wpAppPassword']);
});
test('a site defaults to custom cms_type with no WordPress fields required', function () {
Livewire::test(Index::class)
->set('name', 'Plain Site')
->set('url', 'https://plain.example.com')
->call('save')
->assertHasNoErrors();
expect(Site::where('name', 'Plain Site')->first()->cms_type)->toBe('custom');
});
test('editing a site loads its WordPress fields', function () {
$site = Site::factory()->wordpress()->create(['wp_username' => 'editor']);
Livewire::test(Index::class)
->call('edit', $site->id)
->assertSet('cmsType', 'wordpress')
->assertSet('wpUsername', 'editor');
});
test('WordPress app password can be revealed and hidden in the table', function () {
$site = Site::factory()->wordpress()->create();
Livewire::test(Index::class)
->call('toggleReveal', $site->id)
->assertSet('revealedId', $site->id)
->call('toggleReveal', $site->id)
->assertSet('revealedId', null);
});
test('switching a WordPress site to a non-WordPress cms_type clears its credentials', function () {
$site = Site::factory()->wordpress()->create(['wp_username' => 'admin']);
Livewire::test(Index::class)
->call('edit', $site->id)
->set('cmsType', 'custom')
->call('save');
expect($site->fresh())
->cms_type->toBe('custom')
->wp_username->toBeNull()
->wp_app_password->toBeNull();
});