Skip to content

Commit

Permalink
[4.x] Don't create revision when localizing entry unless revisions ar…
Browse files Browse the repository at this point in the history
…e enabled on the collection (statamic#8908)
  • Loading branch information
duncanmcclean authored Jan 18, 2024
1 parent 686a7ee commit af3c359
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ public function pastDateBehavior($behavior = null)
->args(func_get_args());
}

/** @deprecated */
public function revisions($enabled = null)
{
return $this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
18 changes: 11 additions & 7 deletions src/Revisions/Revisable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand All @@ -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();
Expand Down
26 changes: 25 additions & 1 deletion tests/Feature/Entries/LocalizeEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 */
Expand Down Expand Up @@ -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', [
Expand Down

0 comments on commit af3c359

Please sign in to comment.