Skip to content

Commit a81d7fe

Browse files
authored
Updated logic to keep the updated_at timestamp of the previous revision correct (#63)
1 parent 2e0cc05 commit a81d7fe

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/Concerns/HasDrafts.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,16 @@ protected function newRevision(): void
9999
return;
100100
}
101101

102-
$revision = $this->fresh()?->replicate();
102+
$updatingModel = $this->fresh();
103+
$revision = $updatingModel?->replicate();
103104

104-
static::saved(function (Model $model) use ($revision): void {
105+
static::saved(function (Model $model) use ($updatingModel, $revision): void {
105106
if ($model->isNot($this)) {
106107
return;
107108
}
108109

109-
$revision->{$this->getCreatedAtColumn()} = $this->{$this->getCreatedAtColumn()};
110-
$revision->{$this->getUpdatedAtColumn()} = $this->{$this->getUpdatedAtColumn()};
110+
$revision->{$this->getCreatedAtColumn()} = $updatingModel->{$this->getCreatedAtColumn()};
111+
$revision->{$this->getUpdatedAtColumn()} = $updatingModel->{$this->getUpdatedAtColumn()};
111112
$revision->is_current = false;
112113
$revision->is_published = false;
113114

tests/RevisionsTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Oddvalue\LaravelDrafts\Tests\Post;
44
use Oddvalue\LaravelDrafts\Tests\SoftDeletingPost;
5+
use function Spatie\PestPluginTestTime\testTime;
56

67
it('keeps the correct number of revisions', function () {
78
config(['drafts.revisions.keep' => 3]);
@@ -14,7 +15,6 @@
1415
}
1516
};
1617

17-
config(['drafts.revisions.keep' => 3]);
1818
$post = Post::factory()->create(['title' => 'Rev 1']);
1919
$revsExist('Rev 1');
2020
$this->travel(1)->minutes();
@@ -39,6 +39,46 @@
3939
]);
4040
});
4141

42+
it('sets the correct timestamps on revisions', function () {
43+
$recordsExist = function (...$records) {
44+
foreach ($records as $record) {
45+
$this->assertDatabaseHas('posts', $record);
46+
}
47+
};
48+
49+
testTime()->freeze('2021-01-02 14:00:00');
50+
51+
$post = Post::factory()->create(['title' => 'Rev 1']);
52+
$recordsExist(['title' => 'Rev 1', 'updated_at' => '2021-01-02 14:00:00']);
53+
54+
testTime()->addMinute();
55+
$post->fresh()->update(['title' => 'Rev 2']);
56+
57+
$recordsExist(
58+
['title' => 'Rev 1', 'updated_at' => '2021-01-02 14:00:00'],
59+
['title' => 'Rev 2', 'updated_at' => '2021-01-02 14:01:00'],
60+
);
61+
62+
testTime()->addMinute();
63+
$post->fresh()->update(['title' => 'Rev 3']);
64+
65+
$recordsExist(
66+
['title' => 'Rev 1', 'updated_at' => '2021-01-02 14:00:00'],
67+
['title' => 'Rev 2', 'updated_at' => '2021-01-02 14:01:00'],
68+
['title' => 'Rev 3', 'updated_at' => '2021-01-02 14:02:00'],
69+
);
70+
71+
testTime()->addMinute();
72+
$post->fresh()->update(['title' => 'Rev 4']);
73+
74+
$recordsExist(
75+
['title' => 'Rev 1', 'updated_at' => '2021-01-02 14:00:00'],
76+
['title' => 'Rev 2', 'updated_at' => '2021-01-02 14:01:00'],
77+
['title' => 'Rev 3', 'updated_at' => '2021-01-02 14:02:00'],
78+
['title' => 'Rev 4', 'updated_at' => '2021-01-02 14:03:00'],
79+
);
80+
});
81+
4282
it('can disable revisions', function () {
4383
config(['drafts.revisions.keep' => 0]);
4484
$post = Post::factory()->create(['title' => 'Foo']);

0 commit comments

Comments
 (0)