oddvalue / laravel-drafts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple published records when draft is published

jaysson opened this issue · comments

Hello, thank you for your work on this library.

Here's a failing test, which I thought would pass:

it('does not create multiple published records', function () {
    $post = Post::factory()->create();
    $post->saveAsDraft();
    $post->draft->publish()->save();
    $this->assertCount(1, Post::withoutDrafts()->get());
    // Failed asserting that actual size 2 matches expected size 1.
});

If this is the expected behaviour, how do I get only the latest published instances?

Hi @jaysson have same issue, when Publishing, Draft becomes published, but is not current, and old revision becomes, current and marked as published :/

Yeah, something's definitely not right. Will take a look, thanks 🙂

Hi @oddvalue

I think the problem is here:

public function setLive(): void
    {
        $published = $this->revisions()->excludeRevision($this)->published()->first();

If I'm editing a draft and call ->publish() it will exclude it and get the previous (currently published) and mark it as current.

So prevous published record always becomes current and published.

Right, been through it all and found a bug in the separation of the Publishes and HasDrafts traits. The Publishes trait was setting the is_published column to true on the record that the publish method was called on. I've moved things around to prevent that and added @jaysson's test.
Use v1.0.2 and you should be good to go 🙂