From af3c359e5764374f8a93dfe214ba5c077f4867a8 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 18 Jan 2024 14:37:12 +0000 Subject: [PATCH] [4.x] Don't create revision when localizing entry unless revisions are enabled on the collection (#8908) --- src/Entries/Collection.php | 1 + .../CP/Collections/CollectionsController.php | 2 +- src/Revisions/Revisable.php | 18 ++++++++----- tests/Feature/Entries/LocalizeEntryTest.php | 26 ++++++++++++++++++- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Entries/Collection.php b/src/Entries/Collection.php index 94351777db..a96358a074 100644 --- a/src/Entries/Collection.php +++ b/src/Entries/Collection.php @@ -615,6 +615,7 @@ public function pastDateBehavior($behavior = null) ->args(func_get_args()); } + /** @deprecated */ public function revisions($enabled = null) { return $this diff --git a/src/Http/Controllers/CP/Collections/CollectionsController.php b/src/Http/Controllers/CP/Collections/CollectionsController.php index 1a6919fe06..a17eda29ce 100644 --- a/src/Http/Controllers/CP/Collections/CollectionsController.php +++ b/src/Http/Controllers/CP/Collections/CollectionsController.php @@ -234,7 +234,7 @@ public function update(Request $request, $collection) ->defaultPublishState($values['default_publish_state']) ->sortDirection($values['sort_direction']) ->mount($values['mount'] ?? null) - ->revisions($values['revisions'] ?? false) + ->revisionsEnabled($values['revisions'] ?? false) ->taxonomies($values['taxonomies'] ?? []) ->futureDateBehavior(array_get($values, 'future_date_behavior')) ->pastDateBehavior(array_get($values, 'past_date_behavior')) diff --git a/src/Revisions/Revisable.php b/src/Revisions/Revisable.php index c6008672f8..4ad1e7e15b 100644 --- a/src/Revisions/Revisable.php +++ b/src/Revisions/Revisable.php @@ -111,16 +111,20 @@ public function unpublishWorkingCopy($options = []) public function store($options = []) { - $this + $return = $this ->published(false) ->updateLastModified($user = $options['user'] ?? false) ->save(); - return $this - ->makeRevision() - ->user($user) - ->message($options['message'] ?? false) - ->save(); + if ($this->revisionsEnabled()) { + $return = $this + ->makeRevision() + ->user($user) + ->message($options['message'] ?? false) + ->save(); + } + + return $return; } public function createRevision($options = []) @@ -135,7 +139,7 @@ public function createRevision($options = []) public function revisionsEnabled() { - return config('statamic.revisions.enabled') || ! Statamic::pro(); + return config('statamic.revisions.enabled') && Statamic::pro(); } abstract protected function revisionKey(); diff --git a/tests/Feature/Entries/LocalizeEntryTest.php b/tests/Feature/Entries/LocalizeEntryTest.php index b396e12663..5e9d1b6ce1 100644 --- a/tests/Feature/Entries/LocalizeEntryTest.php +++ b/tests/Feature/Entries/LocalizeEntryTest.php @@ -34,7 +34,7 @@ public function setUp(): void public function it_localizes_an_entry() { $user = $this->user(); - $entry = EntryFactory::collection('blog')->slug('test')->create(); + $entry = EntryFactory::collection(tap(Collection::make('blog')->revisionsEnabled(false))->save())->slug('test')->create(); $this->assertNull($entry->in('fr')); $response = $this @@ -46,6 +46,8 @@ public function it_localizes_an_entry() $this->assertNotNull($localized); $this->assertEquals($user, $localized->lastModifiedBy()); $response->assertJson(['handle' => 'fr', 'url' => $localized->editUrl()]); + + $this->assertCount(0, $entry->in('fr')->revisions()); } /** @test */ @@ -195,6 +197,28 @@ public function it_adds_an_entry_to_the_end_of_the_structure_tree_if_the_parent_ ], Collection::findByHandle('pages')->structure()->in('fr')->tree()); } + /** @test */ + public function it_localizes_an_entry_with_revisions() + { + config(['statamic.revisions.enabled' => true]); + + $user = $this->user(); + $entry = EntryFactory::collection(tap(Collection::make('blog')->revisionsEnabled(true))->save())->slug('test')->create(); + $this->assertNull($entry->in('fr')); + + $response = $this + ->actingAs($user) + ->localize($entry, ['site' => 'fr']) + ->assertOk(); + + $localized = $entry->fresh()->in('fr'); + $this->assertNotNull($localized); + $this->assertEquals($user, $localized->lastModifiedBy()); + $response->assertJson(['handle' => 'fr', 'url' => $localized->editUrl()]); + + $this->assertCount(1, $entry->in('fr')->revisions()); + } + private function localize($entry, $params = []) { $url = cp_route('collections.entries.localize', [