original = new WP_Post(['ID' => 5, 'post_type' => 'page', 'post_title' => 'Team']); $this->copy = new WP_Post(['ID' => 77, 'post_type' => 'page', 'post_title' => 'Team (Kopie)']); 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_preview_post_link')->justReturn('https://kunde.de/?p=77&preview=true'); Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=77&action=edit'); } private function ability(PostCloner $cloner, ChangeLog $log): PostDuplicateAbility { $schema = Mockery::mock(ContentSchema::class); $schema->allows('isManaged')->andReturn(true); return new PostDuplicateAbility($schema, $cloner, $log); } public function testCreatesADraftCopyAndReportsIt(): void { $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')->andReturn(8); $result = $this->ability($cloner, $log)->execute(['post_id' => 5]); self::assertSame([['id' => '77', 'type' => 'page', 'action' => 'created', 'label' => 'Team (Kopie)']], $result['changed']); self::assertSame(5, $result['original_id']); self::assertSame(8, $result['log_id']); self::assertSame(Rev::ofPost($this->copy), $result['rev']); self::assertSame([], $result['warnings']); } public function testNeedsTheRightToCreateEntriesOfThatType(): void { Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'edit_pages'); $cloner = Mockery::mock(PostCloner::class); $cloner->expects('duplicate')->never(); $result = $this->ability($cloner, Mockery::mock(ChangeLog::class))->execute(['post_id' => 5]); self::assertSame('lit_forbidden', $result->get_error_code()); } public function testIsIndirectAndNotIdempotent(): void { $ability = $this->ability(Mockery::mock(PostCloner::class), Mockery::mock(ChangeLog::class)); self::assertFalse($ability->isDirect()); self::assertFalse($ability->annotations()['idempotent']); self::assertFalse($ability->annotations()['destructive']); } }