Add an image field, required for WordPress posts
Posts get a nullable image_url. WordPressPublisher now downloads it and uploads it to WordPress's media endpoint before creating the post, setting it as featured_media — and rejects publishing (without touching status) if a blog post has no image set, mirroring the existing title requirement. Every other network treats it as optional. Not enforced at save time, same as title — you can still draft an incomplete WordPress post, it just can't be published until an image is set. Exposed through the UI form (with a thumbnail/""missing"" indicator in the table), the JSON API, and the Python CLI wrapper. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,9 +29,20 @@ test('it fails when the post has no title', function () {
|
||||
->assertExitCode(1);
|
||||
});
|
||||
|
||||
test('it fails when the post has no image', function () {
|
||||
$site = Site::factory()->wordpress()->create();
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'image_url' => null]);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('no image')
|
||||
->assertExitCode(1);
|
||||
|
||||
expect($post->fresh()->status)->toBe($post->status);
|
||||
});
|
||||
|
||||
test('it fails when the site is not configured as WordPress', function () {
|
||||
$site = Site::factory()->create(['cms_type' => 'custom']);
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi']);
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'image_url' => 'https://example.com/image.jpg']);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('not configured as WordPress')
|
||||
@@ -40,15 +51,17 @@ test('it fails when the site is not configured as WordPress', function () {
|
||||
|
||||
test('it fails when the WordPress site is missing credentials', function () {
|
||||
$site = Site::factory()->create(['cms_type' => 'wordpress', 'wp_username' => null, 'wp_app_password' => null]);
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi']);
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'image_url' => 'https://example.com/image.jpg']);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('missing WordPress credentials')
|
||||
->assertExitCode(1);
|
||||
});
|
||||
|
||||
test('it publishes successfully and updates the post', function () {
|
||||
test('it publishes successfully, uploading the image as the featured media', function () {
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
||||
'*/wp-json/wp/v2/media' => Http::response(['id' => 99], 201),
|
||||
'*/wp-json/wp/v2/posts' => Http::response([
|
||||
'id' => 42,
|
||||
'link' => 'https://wp.example.com/2026/07/hello-world/',
|
||||
@@ -61,6 +74,7 @@ test('it publishes successfully and updates the post', function () {
|
||||
'type' => 'blog',
|
||||
'title' => 'Hello World',
|
||||
'body' => 'Post content',
|
||||
'image_url' => 'https://images.example.com/photo.jpg',
|
||||
'status' => 'scheduled',
|
||||
]);
|
||||
|
||||
@@ -72,16 +86,69 @@ test('it publishes successfully and updates the post', function () {
|
||||
->status->toBe('published')
|
||||
->published_url->toBe('https://wp.example.com/2026/07/hello-world/');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://wp.example.com/wp-json/wp/v2/media'
|
||||
&& $request->hasHeader('Content-Disposition', 'attachment; filename="photo.jpg"')
|
||||
&& $request->body() === 'fake-image-bytes';
|
||||
});
|
||||
|
||||
Http::assertSent(function ($request) use ($site) {
|
||||
return $request->url() === "{$site->url}/wp-json/wp/v2/posts"
|
||||
&& $request['title'] === 'Hello World'
|
||||
&& $request['content'] === 'Post content'
|
||||
&& $request['status'] === 'publish';
|
||||
&& $request['status'] === 'publish'
|
||||
&& $request['featured_media'] === 99;
|
||||
});
|
||||
});
|
||||
|
||||
test('it marks the post as failed when the image cannot be downloaded', function () {
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('not found', 404),
|
||||
'*/wp-json/*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$site = Site::factory()->wordpress()->create();
|
||||
$post = Post::factory()->create([
|
||||
'site_id' => $site->id,
|
||||
'type' => 'blog',
|
||||
'title' => 'Hi',
|
||||
'image_url' => 'https://images.example.com/missing.jpg',
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('could not download image')
|
||||
->assertExitCode(1);
|
||||
|
||||
expect($post->fresh()->status)->toBe('failed');
|
||||
});
|
||||
|
||||
test('it marks the post as failed when WordPress rejects the image upload', function () {
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
||||
'*/wp-json/wp/v2/media' => Http::response(['message' => 'Sorry, you are not allowed to upload files.'], 401),
|
||||
]);
|
||||
|
||||
$site = Site::factory()->wordpress()->create();
|
||||
$post = Post::factory()->create([
|
||||
'site_id' => $site->id,
|
||||
'type' => 'blog',
|
||||
'title' => 'Hi',
|
||||
'image_url' => 'https://images.example.com/photo.jpg',
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('not allowed to upload files')
|
||||
->assertExitCode(1);
|
||||
|
||||
expect($post->fresh()->status)->toBe('failed');
|
||||
});
|
||||
|
||||
test('it marks the post as failed when WordPress returns an error', function () {
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
||||
'*/wp-json/wp/v2/media' => Http::response(['id' => 99], 201),
|
||||
'*/wp-json/wp/v2/posts' => Http::response([
|
||||
'code' => 'rest_cannot_create',
|
||||
'message' => 'Sorry, you are not allowed to create posts.',
|
||||
@@ -89,7 +156,13 @@ test('it marks the post as failed when WordPress returns an error', function ()
|
||||
]);
|
||||
|
||||
$site = Site::factory()->wordpress()->create();
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'draft']);
|
||||
$post = Post::factory()->create([
|
||||
'site_id' => $site->id,
|
||||
'type' => 'blog',
|
||||
'title' => 'Hi',
|
||||
'image_url' => 'https://images.example.com/photo.jpg',
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('not allowed to create posts')
|
||||
@@ -99,12 +172,21 @@ test('it marks the post as failed when WordPress returns an error', function ()
|
||||
});
|
||||
|
||||
test('it marks the post as failed when the site is unreachable', function () {
|
||||
Http::fake(function () {
|
||||
throw new \Illuminate\Http\Client\ConnectionException('Could not connect');
|
||||
});
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
||||
'*/wp-json/*' => function () {
|
||||
throw new \Illuminate\Http\Client\ConnectionException('Could not connect');
|
||||
},
|
||||
]);
|
||||
|
||||
$site = Site::factory()->wordpress()->create();
|
||||
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'draft']);
|
||||
$post = Post::factory()->create([
|
||||
'site_id' => $site->id,
|
||||
'type' => 'blog',
|
||||
'title' => 'Hi',
|
||||
'image_url' => 'https://images.example.com/photo.jpg',
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
$this->artisan('posts:publish', ['post' => $post->id])
|
||||
->expectsOutputToContain('could not connect')
|
||||
@@ -115,6 +197,8 @@ test('it marks the post as failed when the site is unreachable', function () {
|
||||
|
||||
test('it correctly builds the WordPress endpoint URL when the site URL has a trailing slash', function () {
|
||||
Http::fake([
|
||||
'https://images.example.com/*' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
|
||||
'*/wp-json/wp/v2/media' => Http::response(['id' => 99], 201),
|
||||
'*/wp-json/wp/v2/posts' => Http::response([
|
||||
'id' => 42,
|
||||
'link' => 'https://wp.example.com/2026/07/hello-world/',
|
||||
@@ -127,6 +211,7 @@ test('it correctly builds the WordPress endpoint URL when the site URL has a tra
|
||||
'type' => 'blog',
|
||||
'title' => 'Hello World',
|
||||
'body' => 'Post content',
|
||||
'image_url' => 'https://images.example.com/photo.jpg',
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
@@ -136,4 +221,8 @@ test('it correctly builds the WordPress endpoint URL when the site URL has a tra
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://wp.example.com/wp-json/wp/v2/posts';
|
||||
});
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://wp.example.com/wp-json/wp/v2/media';
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user