test: FakeChangeLog and SchemaFixture instead of repeated mocks (T2)
- tests/Support/FakeChangeLog extends ChangeLog: records record()/recordPartial() calls, increasing ids, optional failure; new FakeChangeLog(id) / partialsOnly() / any(id) / forbidden() mirror what the replaced mocks allowed. assertRecorded() / assertPartials() check count and the named arguments. Calls a fake does not allow throw and are also verified in TestCase::tearDown (one assertion per test that uses a fake), so swallowed change-log calls still fail like a never() expectation. - All ChangeLog mocks of ability/integration tests replaced (the admin tests that stub query/purge/clear keep their mocks); expectException tests that checked the partial log now catch and assert. - SchemaFixture::managed(?divi) replaces the 21 isManaged-only ContentSchema mocks; DiviFixtures::schema() uses it. Tests 1397 -> 1404 (FakeChangeLogTest); no test has fewer assertions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,9 +8,10 @@ use Brain\Monkey\Actions;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostDuplicateAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use WP_Post;
|
||||
@@ -34,8 +35,7 @@ final class PostDuplicateAbilityTest extends TestCase
|
||||
|
||||
private function ability(PostCloner $cloner, ChangeLog $log): PostDuplicateAbility
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostDuplicateAbility($schema, $cloner, $log);
|
||||
}
|
||||
@@ -45,8 +45,7 @@ final class PostDuplicateAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('duplicate')->with($this->original)->andReturn(77);
|
||||
Actions\expectDone('lit_mcp_after_save')->once()->with(77, 'lit/post-duplicate', ['post_id' => 5]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/post-duplicate', 'page', '77', 'Kopie von "Team" (ID 5) angelegt', null)->andReturn(8);
|
||||
$log = new FakeChangeLog(8);
|
||||
|
||||
$result = $this->ability($cloner, $log)->execute(['post_id' => 5]);
|
||||
|
||||
@@ -55,6 +54,13 @@ final class PostDuplicateAbilityTest extends TestCase
|
||||
self::assertSame(8, $result['log_id']);
|
||||
self::assertSame(Rev::ofPost($this->copy), $result['rev']);
|
||||
self::assertSame([], $result['warnings']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/post-duplicate',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '77',
|
||||
'summary' => 'Kopie von "Team" (ID 5) angelegt',
|
||||
'revision_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testNeedsTheRightToCreateEntriesOfThatType(): void
|
||||
@@ -63,7 +69,7 @@ final class PostDuplicateAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('duplicate')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_forbidden', $result->get_error_code());
|
||||
}
|
||||
@@ -74,14 +80,14 @@ final class PostDuplicateAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('duplicate')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testIsIndirectAndNotIdempotent(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(PostCloner::class), Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(Mockery::mock(PostCloner::class), FakeChangeLog::forbidden());
|
||||
|
||||
self::assertFalse($ability->isDirect());
|
||||
self::assertFalse($ability->annotations()['idempotent']);
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Status;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostPublishAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
@@ -18,8 +18,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
{
|
||||
private function ability(ChangeLog $log): PostPublishAbility
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostPublishAbility($schema, $log);
|
||||
}
|
||||
@@ -37,14 +36,20 @@ final class PostPublishAbilityTest extends TestCase
|
||||
$this->stubPost(['post_status' => 'draft', 'post_date_gmt' => '0000-00-00 00:00:00']);
|
||||
Functions\expect('wp_update_post')->once()->with(['ID' => 5, 'post_status' => 'publish'], true)->andReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('publish');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/post-publish', 'page', '5', '"Team" veröffentlicht', null)->andReturn(9);
|
||||
$log = new FakeChangeLog(9);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('published', $result['changed'][0]['action']);
|
||||
self::assertSame(9, $result['log_id']);
|
||||
self::assertSame('https://kunde.de/team/', $result['preview_url']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/post-publish',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'summary' => '"Team" veröffentlicht',
|
||||
'revision_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testScheduledPostIsPublishedNowInsteadOfStayingFuture(): void
|
||||
@@ -59,8 +64,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
'edit_date' => true,
|
||||
], true)->andReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('publish');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(1);
|
||||
$log = new FakeChangeLog(1);
|
||||
|
||||
$this->ability($log)->execute(['post_id' => 5]);
|
||||
}
|
||||
@@ -70,7 +74,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
$this->stubPost(['post_status' => 'publish']);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame([], $result['changed']);
|
||||
self::assertStringContainsString('bereits veröffentlicht', $result['summary']);
|
||||
@@ -82,8 +86,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
Functions\when('wp_update_post')->alias(static function (): void {
|
||||
throw new RuntimeException('save_post-Hook kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/post-publish', 'page', '5', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($log)->execute(['post_id' => 5]);
|
||||
@@ -94,6 +97,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
}
|
||||
$log->assertPartials(['ability' => 'lit/post-publish', 'object_type' => 'page', 'object_id' => '5']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefused(): void
|
||||
@@ -101,7 +105,7 @@ final class PostPublishAbilityTest extends TestCase
|
||||
$this->stubPost(['post_status' => 'trash']);
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -111,6 +115,6 @@ final class PostPublishAbilityTest extends TestCase
|
||||
$this->stubPost(['post_status' => 'draft']);
|
||||
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'publish_post');
|
||||
|
||||
self::assertSame('lit_forbidden', $this->ability(Mockery::mock(ChangeLog::class))->permission(['post_id' => 5])->get_error_code());
|
||||
self::assertSame('lit_forbidden', $this->ability(FakeChangeLog::forbidden())->permission(['post_id' => 5])->get_error_code());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,11 @@ use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Rev;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostCloner;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostReplaceWithCopyAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Status\ReplaceFailed;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
@@ -39,8 +40,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
|
||||
private function ability(PostCloner $cloner, ChangeLog $log, bool $trash = true): PostReplaceWithCopyAbility
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostReplaceWithCopyAbility($schema, $cloner, $log, static fn (): bool => $trash);
|
||||
}
|
||||
@@ -65,7 +65,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())
|
||||
->execute(['original_id' => 5, 'copy_id' => 77, 'rev' => Rev::ofPost($this->original)]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
@@ -77,7 +77,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -101,7 +101,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->expects('backup')->never();
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -114,7 +114,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->expects('backup')->never();
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertArrayHasKey('original_id', $result->get_error_data()['fields']);
|
||||
@@ -129,8 +129,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->allows('backup')->andReturn(88);
|
||||
$cloner->expects('replace');
|
||||
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 77]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(11);
|
||||
$log = new FakeChangeLog(11);
|
||||
|
||||
self::assertTrue($this->ability($cloner, $log)->execute($this->input())['ok']);
|
||||
}
|
||||
@@ -141,7 +140,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->expects('replace')->never();
|
||||
Functions\expect('wp_save_post_revision')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input(['rev' => 'veraltet']));
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input(['rev' => 'veraltet']));
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
||||
}
|
||||
@@ -151,7 +150,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('replace')->never();
|
||||
|
||||
$result = $this->ability($cloner, Mockery::mock(ChangeLog::class), false)->execute($this->input());
|
||||
$result = $this->ability($cloner, FakeChangeLog::forbidden(), false)->execute($this->input());
|
||||
|
||||
self::assertSame('lit_unsupported', $result->get_error_code());
|
||||
}
|
||||
@@ -181,8 +180,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$order[] = 'trash';
|
||||
return new WP_Post(['ID' => 77]);
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/post-replace-with-copy', 'page', '5', Mockery::type('string'), 900)->andReturn(11);
|
||||
$log = new FakeChangeLog(11);
|
||||
|
||||
$result = $this->ability($cloner, $log)->execute($this->input());
|
||||
|
||||
@@ -194,6 +192,12 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
self::assertSame(Rev::ofPost($this->original), $result['rev']);
|
||||
self::assertSame(['replaced', 'trashed'], array_column($result['changed'], 'action'));
|
||||
self::assertSame([], $result['warnings']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/post-replace-with-copy',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'revision_id' => 900,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testAFailedTrashOfTheCopyIsAWarningOnly(): void
|
||||
@@ -204,8 +208,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->allows('backup')->andReturn(88);
|
||||
$cloner->expects('replace');
|
||||
Functions\when('wp_trash_post')->justReturn(false);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(11);
|
||||
$log = new FakeChangeLog(11);
|
||||
|
||||
$result = $this->ability($cloner, $log)->execute($this->input());
|
||||
|
||||
@@ -225,7 +228,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
|
||||
try {
|
||||
$this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
$this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertTrue($e->rolledBack);
|
||||
@@ -246,10 +249,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner->expects('replace')->andThrow(new RuntimeException('hook kaputt'));
|
||||
$cloner->expects('restore')->with($this->original, self::SNAPSHOT, PostCloner::PARTS)->andThrow(new RuntimeException('Restore incomplete: terms'));
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')
|
||||
->with('lit/post-replace-with-copy', 'page', '5', Mockery::type('string'), 900)
|
||||
->andReturn(12);
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($cloner, $log)->execute($this->input());
|
||||
@@ -264,6 +264,12 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
self::assertStringContainsString('Sicherung', $error->get_error_message());
|
||||
self::assertStringContainsString('88', $error->get_error_message());
|
||||
}
|
||||
$log->assertPartials([
|
||||
'ability' => 'lit/post-replace-with-copy',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'revision_id' => 900,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testNeedsTheRightToCreateEntriesForTheBackup(): void
|
||||
@@ -272,7 +278,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
$cloner = Mockery::mock(PostCloner::class);
|
||||
$cloner->expects('backup')->never();
|
||||
$cloner->expects('replace')->never();
|
||||
$ability = $this->ability($cloner, Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability($cloner, FakeChangeLog::forbidden());
|
||||
|
||||
self::assertSame('lit_forbidden', $ability->permission($this->input())->get_error_code());
|
||||
self::assertSame('lit_forbidden', $ability->execute($this->input())->get_error_code());
|
||||
@@ -289,7 +295,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
|
||||
try {
|
||||
$this->ability($cloner, Mockery::mock(ChangeLog::class))->execute($this->input());
|
||||
$this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
|
||||
@@ -298,7 +304,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
|
||||
|
||||
public function testRequiresRevAndIsDestructive(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(PostCloner::class), Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(Mockery::mock(PostCloner::class), FakeChangeLog::forbidden());
|
||||
|
||||
self::assertContains('rev', $ability->inputSchema()['required']);
|
||||
self::assertContains('backup_id', $ability->outputSchema()['required']);
|
||||
|
||||
@@ -8,10 +8,10 @@ use Brain\Monkey\Functions;
|
||||
use DateTimeZone;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostScheduleAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
@@ -29,8 +29,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
|
||||
private function ability(ChangeLog $log): PostScheduleAbility
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostScheduleAbility($schema, $log);
|
||||
}
|
||||
@@ -45,13 +44,13 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
'edit_date' => true,
|
||||
], true)->andReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame('scheduled', $result['changed'][0]['action']);
|
||||
self::assertStringContainsString('2099-06-01 09:30', $result['summary']);
|
||||
$log->assertRecorded([]);
|
||||
}
|
||||
|
||||
public function testWarnsThatAPrivatePostBecomesPublic(): void
|
||||
@@ -59,8 +58,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'post', 'post_title' => 'Intern', 'post_status' => 'private']));
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
@@ -73,8 +71,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
{
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
@@ -86,8 +83,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
Functions\when('wp_update_post')->alias(static function (): void {
|
||||
throw new RuntimeException('save_post-Hook kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/post-schedule', 'post', '5', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
@@ -98,6 +94,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
}
|
||||
$log->assertPartials(['ability' => 'lit/post-schedule', 'object_type' => 'post', 'object_id' => '5']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefused(): void
|
||||
@@ -105,7 +102,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
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']);
|
||||
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -113,7 +110,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
public function testPastAndImpossibleDatesAreRejected(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(FakeChangeLog::forbidden());
|
||||
|
||||
$past = $ability->execute(['post_id' => 5, 'date' => '2020-01-01 10:00']);
|
||||
$impossible = $ability->execute(['post_id' => 5, 'date' => '2099-13-45 10:00']);
|
||||
@@ -131,7 +128,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
->setTimezone(new DateTimeZone('Europe/Berlin'));
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))
|
||||
$result = $this->ability(FakeChangeLog::forbidden())
|
||||
->execute(['post_id' => 5, 'date' => $soon->format('Y-m-d H:i')]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Status;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostTrashAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
@@ -33,8 +33,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
{
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Alt', 'post_status' => 'publish']));
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostTrashAbility($schema, $log);
|
||||
}
|
||||
@@ -43,7 +42,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]);
|
||||
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
}
|
||||
@@ -52,13 +51,19 @@ final class PostTrashAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_trash_post')->once()->with(5)->andReturn(new WP_Post(['ID' => 5]));
|
||||
Functions\expect('wp_delete_post')->never();
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/post-trash', 'page', '5', '"Alt" in den Papierkorb gelegt', null)->andReturn(2);
|
||||
$log = new FakeChangeLog(2);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
self::assertSame('trashed', $result['changed'][0]['action']);
|
||||
self::assertStringContainsString('Papierkorb', $result['summary']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/post-trash',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'summary' => '"Alt" in den Papierkorb gelegt',
|
||||
'revision_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRefusesWhenTheTrashIsDisabledInsteadOfDeleting(): void
|
||||
@@ -66,9 +71,8 @@ final class PostTrashAbilityTest extends TestCase
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_status' => 'publish']));
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\expect('wp_trash_post')->never();
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$ability = new PostTrashAbility($schema, Mockery::mock(ChangeLog::class), static fn (): bool => false);
|
||||
$schema = SchemaFixture::managed();
|
||||
$ability = new PostTrashAbility($schema, FakeChangeLog::forbidden(), static fn (): bool => false);
|
||||
|
||||
$result = $ability->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
@@ -80,8 +84,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
Functions\when('wp_trash_post')->alias(static function (): void {
|
||||
throw new RuntimeException('save_post-Hook kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/post-trash', 'page', '5', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($log)->execute(['post_id' => 5, 'confirm' => true]);
|
||||
@@ -92,11 +95,12 @@ final class PostTrashAbilityTest extends TestCase
|
||||
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
}
|
||||
$log->assertPartials(['ability' => 'lit/post-trash', 'object_type' => 'page', 'object_id' => '5']);
|
||||
}
|
||||
|
||||
public function testAnAlreadyTrashedPostIsANoOp(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(FakeChangeLog::forbidden());
|
||||
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();
|
||||
|
||||
@@ -110,8 +114,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
{
|
||||
$this->options(['show_on_front' => 'page', 'page_on_front' => '5', 'page_for_posts' => '9']);
|
||||
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 5]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(3);
|
||||
$log = new FakeChangeLog(3);
|
||||
$ability = $this->ability($log);
|
||||
|
||||
$confirm = $ability->execute(['post_id' => 5]);
|
||||
@@ -141,8 +144,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
: []
|
||||
);
|
||||
Functions\when('wp_trash_post')->justReturn(new WP_Post(['ID' => 5]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(3);
|
||||
$log = new FakeChangeLog(3);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'confirm' => true]);
|
||||
|
||||
@@ -153,7 +155,7 @@ final class PostTrashAbilityTest extends TestCase
|
||||
|
||||
public function testIsDestructiveAndIndirect(): void
|
||||
{
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(FakeChangeLog::forbidden());
|
||||
|
||||
self::assertTrue($ability->annotations()['destructive']);
|
||||
self::assertFalse($ability->isDirect());
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Status;
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostUnpublishAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
@@ -22,8 +22,7 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/?p=5&preview=true');
|
||||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostUnpublishAbility($schema, $log);
|
||||
}
|
||||
@@ -31,12 +30,12 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
public function testPublishedPostBecomesDraft(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->once()->with(['ID' => 5, 'post_status' => 'draft'], true)->andReturn(5);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andReturn(3);
|
||||
$log = new FakeChangeLog(3);
|
||||
|
||||
$result = $this->ability($log, 'publish')->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('unpublished', $result['changed'][0]['action']);
|
||||
$log->assertRecorded([]);
|
||||
}
|
||||
|
||||
public function testAHookThrowingDuringTheWriteIsAPartialWrite(): void
|
||||
@@ -44,8 +43,7 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
Functions\when('wp_update_post')->alias(static function (): void {
|
||||
throw new RuntimeException('save_post-Hook kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/post-unpublish', 'page', '5', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($log, 'publish')->execute(['post_id' => 5]);
|
||||
@@ -56,13 +54,14 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
}
|
||||
$log->assertPartials(['ability' => 'lit/post-unpublish', 'object_type' => 'page', 'object_id' => '5']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefusedInsteadOfBecomingADraft(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class), 'trash')->execute(['post_id' => 5]);
|
||||
$result = $this->ability(FakeChangeLog::forbidden(), 'trash')->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -71,7 +70,7 @@ final class PostUnpublishAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class), 'draft')->execute(['post_id' => 5]);
|
||||
$result = $this->ability(FakeChangeLog::forbidden(), 'draft')->execute(['post_id' => 5]);
|
||||
|
||||
self::assertSame([], $result['changed']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user