Skip to content

Commit 68e4e6d

Browse files
committed
fixes
1 parent 4265a66 commit 68e4e6d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tests/Unit/Cms/Services/Post/UpdaterTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,69 @@ public function testInvalidHourThrowsException(): void
658658

659659
$this->_updater->update( $dto );
660660
}
661+
662+
/**
663+
* Test that when changing a previously published post to draft status,
664+
* the historical published_at date is preserved in the database.
665+
*
666+
* This prevents data loss when users:
667+
* 1. Have a published post with a published_at date
668+
* 2. Change the status back to 'draft' for editing
669+
* 3. Later republish the post
670+
*
671+
* Bug scenario: The published_at date should NOT be cleared when
672+
* changing to draft, so users can see and edit the existing date.
673+
*/
674+
public function testPreservesPublishedAtWhenChangingPublishedPostToDraft(): void
675+
{
676+
// Create a post that was previously published
677+
$post = new Post();
678+
$post->setId( 1 );
679+
$post->setTitle( 'Previously Published Post' );
680+
$post->setStatus( Post::STATUS_PUBLISHED );
681+
$historicalPublishedAt = new \DateTimeImmutable( '2024-06-15 10:30:00' );
682+
$post->setPublishedAt( $historicalPublishedAt );
683+
684+
$this->_mockPostRepository
685+
->expects( $this->once() )
686+
->method( 'findById' )
687+
->with( 1 )
688+
->willReturn( $post );
689+
690+
$this->_mockCategoryRepository
691+
->method( 'findByIds' )
692+
->willReturn( [] );
693+
694+
$this->_mockTagResolver
695+
->method( 'resolveFromString' )
696+
->willReturn( [] );
697+
698+
$this->_mockPostRepository
699+
->expects( $this->once() )
700+
->method( 'update' )
701+
->with( $this->callback( function( Post $p ) use ( $historicalPublishedAt ) {
702+
// Verify the post is now draft AND the published_at is preserved
703+
return $p->getStatus() === Post::STATUS_DRAFT
704+
&& $p->getPublishedAt() === $historicalPublishedAt;
705+
} ) );
706+
707+
// Update the post to draft status WITHOUT providing a published_at value
708+
// (simulating the form submission where the field is hidden for drafts)
709+
$dto = $this->createDto(
710+
1,
711+
'Updated Draft Post',
712+
'{"blocks":[{"type":"paragraph","data":{"text":"Updated content"}}]}',
713+
Post::STATUS_DRAFT
714+
// Note: No published_at provided - field is hidden in UI for drafts
715+
);
716+
717+
$result = $this->_updater->update( $dto );
718+
719+
// Assert the status changed to draft
720+
$this->assertEquals( Post::STATUS_DRAFT, $result->getStatus() );
721+
722+
// Assert the historical published_at date is preserved (not cleared)
723+
$this->assertSame( $historicalPublishedAt, $result->getPublishedAt() );
724+
$this->assertEquals( '2024-06-15 10:30:00', $result->getPublishedAt()->format( 'Y-m-d H:i:s' ) );
725+
}
661726
}

0 commit comments

Comments
 (0)