Skip to content

Commit

Permalink
[5.x] Localized entry dates fall back to the origin (#10282)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
arthurperton and jasonvarga authored Jun 28, 2024
1 parent 7ace3c1 commit 9476896
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public function date($date = null)
return null;
}

$date = $date ?? $this->lastModified();
$date = $date ?? optional($this->origin())->date() ?? $this->lastModified();

if (! $this->hasTime()) {
$date->startOfDay();
Expand Down Expand Up @@ -813,10 +813,6 @@ public function makeLocalization($site)
$localization->afterSave($callback);
}

if ($this->collection()->dated()) {
$localization->date($this->date());
}

return $localization;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ public function update(Request $request, $collection, $entry)
$values = $values->except(['slug', 'published']);

if ($entry->collection()->dated()) {
$entry->date($entry->blueprint()->field('date')->fieldtype()->augment($values->pull('date')));
$date = $entry->blueprint()->field('date')->fieldtype()->augment($values->pull('date'));
if ($entry->hasOrigin()) {
$entry->date(in_array('date', $request->input('_localized')) ? $date : null);
} else {
$entry->date($date);
}
}

if ($entry->hasOrigin()) {
Expand Down
22 changes: 22 additions & 0 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,28 @@ public function setting_date_on_entry_that_doesnt_have_a_collection_set_throws_e
(new Entry)->date('2023-04-19');
}

#[Test]
public function it_falls_back_to_the_origin_for_the_date()
{
Collection::make('dated')->dated(true)->save();

$origin = tap((new Entry)->collection('dated')->id('origin')->date('2024-06-10'))->save();
$descendant = tap((new Entry)->collection('dated')->id('descendant')->origin($origin))->save();
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-10')->startOfDay()->eq($descendant->date()));

$origin->date('2024-06-11')->save();
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-11')->startOfDay()->eq($descendant->date()));

$descendant->date('2024-06-12')->save();
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-12')->startOfDay()->eq($descendant->date()));

$origin->date('2024-06-13')->save();
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-12')->startOfDay()->eq($descendant->date()));

$descendant->date(null)->save();
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-13')->startOfDay()->eq($descendant->date()));
}

#[Test]
public function it_gets_the_order_from_the_collections_structure()
{
Expand Down
3 changes: 2 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(tap(Collection::make('blog')->revisionsEnabled(false))->save())->slug('test')->create();
$entry = EntryFactory::collection(tap(Collection::make('blog')->dated(true)->revisionsEnabled(false))->save())->slug('test')->date('2013-08-24')->create();
$this->assertNull($entry->in('fr'));

$response = $this
Expand All @@ -45,6 +45,7 @@ public function it_localizes_an_entry()
$localized = $entry->fresh()->in('fr');
$this->assertNotNull($localized);
$this->assertEquals($user, $localized->lastModifiedBy());
$this->assertEquals('2013-08-24', $localized->date()->format('Y-m-d'));
$response->assertJson(['handle' => 'fr', 'url' => $localized->editUrl()]);

$this->assertCount(0, $entry->in('fr')->revisions());
Expand Down
73 changes: 73 additions & 0 deletions tests/Feature/Entries/UpdateEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,79 @@ public function entry_gets_updated()
$this->assertEquals('updated-entry', $entry->slug());
}

#[Test]
public function date_gets_set_in_origin()
{
[$user, $collection] = $this->seedUserAndCollection();
$collection->dated(true)->save();

$entry = EntryFactory::collection($collection)
->slug('existing-entry')
->data(['title' => 'Existing Entry'])
->date('2021-01-01')
->create();

$this
->actingAs($user)
->update($entry, [
'title' => 'Updated Entry',
'slug' => 'updated-entry',
'date' => ['date' => '2021-02-02'],
'_localized' => [], // empty to show that date doesn't need to be in here.
])
->assertOk();

$entry = $entry->fresh();
$this->assertEquals('2021-02-02', $entry->date()->format('Y-m-d'));
}

#[Test]
#[DataProvider('savesDateProvider')]
public function date_gets_set_in_localization_when_contained_in_localized_array($shouldBeInArray, $expectedDate)
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en'],
'fr' => ['url' => '/two/', 'locale' => 'fr'],
]);

[$user, $collection] = $this->seedUserAndCollection();
$collection->dated(true)->save();

$entry = EntryFactory::collection($collection)
->slug('existing-entry')
->data(['title' => 'Existing Entry'])
->date('2021-01-01')
->create();

$localized = EntryFactory::collection($collection)
->slug('existing-entry')
->data(['title' => 'Existing Entry'])
->origin($entry->id())
->locale('fr')
->create();

$this
->actingAs($user)
->update($localized, [
'title' => 'Updated Entry',
'slug' => 'updated-entry',
'date' => ['date' => '2021-02-02'],
'_localized' => $shouldBeInArray ? ['date'] : [],
])
->assertOk();

$localized = $localized->fresh();
$this->assertEquals($expectedDate, $localized->date()->format('Y-m-d'));
}

public static function savesDateProvider()
{
return [
'date is in localized array' => [true, '2021-02-02'],
'date is not in localized array' => [false, '2021-01-01'],
];
}

#[Test]
public function slug_is_not_required_and_will_get_created_from_the_submitted_title_if_slug_is_in_the_blueprint_and_the_submitted_slug_was_empty()
{
Expand Down

0 comments on commit 9476896

Please sign in to comment.