fix: never publish a missed schedule, refuse trashed posts, fail on meta read errors
- PostGuard::editable() refuses trashed entries unless the caller opts in (content-get, revisions-list, post-trash); publish/schedule/unpublish/update/ restore/duplicate/replace now refuse them. - PostGuard::statusStable(): content-update, revision-restore and replace-with-copy refuse a future post whose date has passed (any save would publish it); the content-update rollback and PostCloner row writes re-check. - PostCloner::ownMeta() treats a failed SELECT as an error; replace() reads the copy before its first write. - replace-with-copy needs create_posts (backup clone) and reports backup_id in write_failed/partial_write. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -24,17 +24,30 @@ final class PostClonerTest extends TestCase
|
||||
|
||||
private bool $insertFails = false;
|
||||
|
||||
/** Post IDs whose meta SELECT fails: 'null' returns null, 'error' sets $wpdb->last_error. */
|
||||
private array $metaReadFails = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Functions\when('wp_cache_delete')->justReturn(true);
|
||||
Functions\when('get_post')->justReturn(null);
|
||||
}
|
||||
|
||||
private function cloner(): PostCloner
|
||||
{
|
||||
$db = Mockery::mock(\wpdb::class)->makePartial();
|
||||
$db->allows('get_results')->andReturnUsing(function (string $sql): array {
|
||||
$db->allows('get_results')->andReturnUsing(function (string $sql) use ($db): ?array {
|
||||
preg_match('/post_id = (\d+)/', $sql, $match);
|
||||
$db->last_error = '';
|
||||
$failure = $this->metaReadFails[(int) $match[1]] ?? null;
|
||||
if ($failure === 'null') {
|
||||
return null;
|
||||
}
|
||||
if ($failure === 'error') {
|
||||
$db->last_error = 'Table wp_postmeta is marked as crashed';
|
||||
return [];
|
||||
}
|
||||
$rows = [];
|
||||
foreach ($this->meta[(int) $match[1]] ?? [] as $key => $values) {
|
||||
foreach ($values as $value) {
|
||||
@@ -246,6 +259,7 @@ final class PostClonerTest extends TestCase
|
||||
|
||||
public function testAReplaceRejectedByWordPressNamesOnlyThePostRow(): void
|
||||
{
|
||||
Functions\when('get_object_taxonomies')->justReturn([]);
|
||||
Functions\when('wp_update_post')->justReturn(new WP_Error('x', 'kaputt'));
|
||||
Functions\expect('delete_post_meta')->never();
|
||||
|
||||
@@ -272,6 +286,96 @@ final class PostClonerTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/** @return array<string, array{0: string}> */
|
||||
public function metaReadFailures(): array
|
||||
{
|
||||
return ['null result' => ['null'], 'database error' => ['error']];
|
||||
}
|
||||
|
||||
/** @dataProvider metaReadFailures */
|
||||
public function testAFailedMetaReadIsAnErrorNotEmptyMeta(string $failure): void
|
||||
{
|
||||
$this->metaReadFails = [5 => $failure];
|
||||
Functions\when('get_object_taxonomies')->justReturn([]);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('Meta of post 5 could not be read');
|
||||
|
||||
$this->cloner()->snapshot($this->original());
|
||||
}
|
||||
|
||||
/** @dataProvider metaReadFailures */
|
||||
public function testReplaceReadsTheCopyBeforeWritingAnything(string $failure): void
|
||||
{
|
||||
$this->metaReadFails = [77 => $failure];
|
||||
Functions\when('get_object_taxonomies')->justReturn([]);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
Functions\expect('delete_post_meta')->never();
|
||||
|
||||
try {
|
||||
$this->cloner()->replace($this->original(), $this->copy());
|
||||
self::fail('ReplaceFailed expected');
|
||||
} catch (ReplaceFailed $e) {
|
||||
self::assertSame([], $e->parts);
|
||||
self::assertStringContainsString('Meta of post 77 could not be read', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testADuplicateWithAFailedMetaReadIsDeletedAgain(): void
|
||||
{
|
||||
$this->metaReadFails = [5 => 'error'];
|
||||
Functions\when('get_current_user_id')->justReturn(3);
|
||||
Functions\when('wp_insert_post')->justReturn(90);
|
||||
Functions\expect('wp_delete_post')->once()->with(90, true)->andReturn(new WP_Post(['ID' => 90]));
|
||||
|
||||
try {
|
||||
$this->cloner()->duplicate($this->original());
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertTrue($e->rolledBack);
|
||||
}
|
||||
}
|
||||
|
||||
private function missedSchedule(): void
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post([
|
||||
'ID' => 5,
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'future',
|
||||
'post_date_gmt' => gmdate('Y-m-d H:i:s', time() - 60),
|
||||
]));
|
||||
}
|
||||
|
||||
public function testReplaceRefusesAMissedScheduleBeforeWriting(): void
|
||||
{
|
||||
$this->missedSchedule();
|
||||
Functions\when('get_object_taxonomies')->justReturn([]);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
try {
|
||||
$this->cloner()->replace($this->original(), $this->copy());
|
||||
self::fail('ReplaceFailed expected');
|
||||
} catch (ReplaceFailed $e) {
|
||||
self::assertSame([], $e->parts);
|
||||
self::assertStringContainsString('missed schedule', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testRestoreNeverRewritesTheRowOfAMissedSchedule(): void
|
||||
{
|
||||
$this->missedSchedule();
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('missed schedule');
|
||||
|
||||
$this->cloner()->restore($this->original(), [
|
||||
'post' => ['post_title' => 'Team', 'post_content' => '', 'post_excerpt' => ''],
|
||||
'meta' => [],
|
||||
'terms' => [],
|
||||
], ['post']);
|
||||
}
|
||||
|
||||
public function testSnapshotAndRestoreBringBackRowMetaAndTerms(): void
|
||||
{
|
||||
$this->meta = [5 => ['_edit_lock' => ['1'], 'role' => ['b:0;']]];
|
||||
|
||||
@@ -74,6 +74,16 @@ final class PostPublishAbilityTest extends TestCase
|
||||
self::assertStringContainsString('bereits veröffentlicht', $result['summary']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefused(): void
|
||||
{
|
||||
$this->stubPost(['post_status' => 'trash']);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testPermissionNeedsPublishRights(): void
|
||||
{
|
||||
$this->stubPost(['post_status' => 'draft']);
|
||||
|
||||
@@ -31,6 +31,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$this->copy = new WP_Post(['ID' => 77, 'post_type' => 'page', 'post_title' => 'Team (Kopie)', 'post_status' => 'draft']);
|
||||
Functions\when('get_post')->alias(fn (int $id) => $id === 5 ? $this->original : $this->copy);
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\when('get_post_type_object')->justReturn((object) ['cap' => (object) ['create_posts' => 'edit_pages']]);
|
||||
Functions\when('get_post_meta')->justReturn('5');
|
||||
Functions\when('get_permalink')->justReturn('https://kunde.de/team/');
|
||||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
|
||||
@@ -105,6 +106,20 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testAMissedScheduleOfTheOriginalIsRefused(): void
|
||||
{
|
||||
$this->original->post_status = 'future';
|
||||
$this->original->post_date_gmt = gmdate('Y-m-d H:i:s', time() - 600);
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('backup')->never();
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertArrayHasKey('original_id', $result->get_error_data()['fields']);
|
||||
}
|
||||
|
||||
public function testAPendingCopyIsAccepted(): void
|
||||
{
|
||||
$this->copy->post_status = 'pending';
|
||||
@@ -215,6 +230,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertTrue($e->rolledBack);
|
||||
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
|
||||
self::assertSame(88, $e->toError()->get_error_data()['backup_id']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,10 +255,26 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
self::assertFalse($e->rolledBack);
|
||||
self::assertSame(5, $e->postId);
|
||||
self::assertSame(900, $e->revisionId);
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
$error = $e->toError();
|
||||
self::assertSame('lit_partial_write', $error->get_error_code());
|
||||
self::assertSame(88, $error->get_error_data()['backup_id']);
|
||||
self::assertStringContainsString('Sicherung', $error->get_error_message());
|
||||
self::assertStringContainsString('88', $error->get_error_message());
|
||||
}
|
||||
}
|
||||
|
||||
public function testNeedsTheRightToCreateEntriesForTheBackup(): void
|
||||
{
|
||||
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'edit_pages');
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('backup')->never();
|
||||
$cloner->expects('replace')->never();
|
||||
$ability = $this->ability($cloner, Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame('lit_forbidden', $ability->permission($this->input())->get_error_code());
|
||||
self::assertSame('lit_forbidden', $ability->execute($this->input())->get_error_code());
|
||||
}
|
||||
|
||||
public function testAFailedBackupAbortsBeforeTheOriginalIsTouched(): void
|
||||
{
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
|
||||
@@ -52,6 +52,16 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
self::assertStringContainsString('2099-06-01 09:30', $result['summary']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefused(): void
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'post', 'post_status' => 'trash']));
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testPastAndImpossibleDatesAreRejected(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
@@ -60,6 +60,18 @@ final class PostTrashAbilityTest extends TestCase
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testAnAlreadyTrashedPostIsANoOp(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Alt', 'post_status' => 'trash']));
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
|
||||
$result = $ability->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
self::assertSame([], $result['changed']);
|
||||
self::assertStringContainsString('bereits im Papierkorb', $result['summary']);
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndIndirect(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
|
||||
@@ -37,6 +37,15 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
self::assertSame('unpublished', $result['changed'][0]['action']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefusedInsteadOfBecomingADraft(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class), 'trash')->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testDraftIsANoOp(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
Reference in New Issue
Block a user