Skip to content

Commit 9476896

Browse files
[5.x] Localized entry dates fall back to the origin (#10282)
Co-authored-by: Jason Varga <[email protected]>
1 parent 7ace3c1 commit 9476896

File tree

5 files changed

+104
-7
lines changed

5 files changed

+104
-7
lines changed

src/Entries/Entry.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public function date($date = null)
559559
return null;
560560
}
561561

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

564564
if (! $this->hasTime()) {
565565
$date->startOfDay();
@@ -813,10 +813,6 @@ public function makeLocalization($site)
813813
$localization->afterSave($callback);
814814
}
815815

816-
if ($this->collection()->dated()) {
817-
$localization->date($this->date());
818-
}
819-
820816
return $localization;
821817
}
822818

src/Http/Controllers/CP/Collections/EntriesController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ public function update(Request $request, $collection, $entry)
211211
$values = $values->except(['slug', 'published']);
212212

213213
if ($entry->collection()->dated()) {
214-
$entry->date($entry->blueprint()->field('date')->fieldtype()->augment($values->pull('date')));
214+
$date = $entry->blueprint()->field('date')->fieldtype()->augment($values->pull('date'));
215+
if ($entry->hasOrigin()) {
216+
$entry->date(in_array('date', $request->input('_localized')) ? $date : null);
217+
} else {
218+
$entry->date($date);
219+
}
215220
}
216221

217222
if ($entry->hasOrigin()) {

tests/Data/Entries/EntryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,28 @@ public function setting_date_on_entry_that_doesnt_have_a_collection_set_throws_e
10471047
(new Entry)->date('2023-04-19');
10481048
}
10491049

1050+
#[Test]
1051+
public function it_falls_back_to_the_origin_for_the_date()
1052+
{
1053+
Collection::make('dated')->dated(true)->save();
1054+
1055+
$origin = tap((new Entry)->collection('dated')->id('origin')->date('2024-06-10'))->save();
1056+
$descendant = tap((new Entry)->collection('dated')->id('descendant')->origin($origin))->save();
1057+
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-10')->startOfDay()->eq($descendant->date()));
1058+
1059+
$origin->date('2024-06-11')->save();
1060+
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-11')->startOfDay()->eq($descendant->date()));
1061+
1062+
$descendant->date('2024-06-12')->save();
1063+
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-12')->startOfDay()->eq($descendant->date()));
1064+
1065+
$origin->date('2024-06-13')->save();
1066+
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-12')->startOfDay()->eq($descendant->date()));
1067+
1068+
$descendant->date(null)->save();
1069+
$this->assertTrue(Carbon::createFromFormat('Y-m-d', '2024-06-13')->startOfDay()->eq($descendant->date()));
1070+
}
1071+
10501072
#[Test]
10511073
public function it_gets_the_order_from_the_collections_structure()
10521074
{

tests/Feature/Entries/LocalizeEntryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function setUp(): void
3434
public function it_localizes_an_entry()
3535
{
3636
$user = $this->user();
37-
$entry = EntryFactory::collection(tap(Collection::make('blog')->revisionsEnabled(false))->save())->slug('test')->create();
37+
$entry = EntryFactory::collection(tap(Collection::make('blog')->dated(true)->revisionsEnabled(false))->save())->slug('test')->date('2013-08-24')->create();
3838
$this->assertNull($entry->in('fr'));
3939

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

5051
$this->assertCount(0, $entry->in('fr')->revisions());

tests/Feature/Entries/UpdateEntryTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,79 @@ public function entry_gets_updated()
9595
$this->assertEquals('updated-entry', $entry->slug());
9696
}
9797

98+
#[Test]
99+
public function date_gets_set_in_origin()
100+
{
101+
[$user, $collection] = $this->seedUserAndCollection();
102+
$collection->dated(true)->save();
103+
104+
$entry = EntryFactory::collection($collection)
105+
->slug('existing-entry')
106+
->data(['title' => 'Existing Entry'])
107+
->date('2021-01-01')
108+
->create();
109+
110+
$this
111+
->actingAs($user)
112+
->update($entry, [
113+
'title' => 'Updated Entry',
114+
'slug' => 'updated-entry',
115+
'date' => ['date' => '2021-02-02'],
116+
'_localized' => [], // empty to show that date doesn't need to be in here.
117+
])
118+
->assertOk();
119+
120+
$entry = $entry->fresh();
121+
$this->assertEquals('2021-02-02', $entry->date()->format('Y-m-d'));
122+
}
123+
124+
#[Test]
125+
#[DataProvider('savesDateProvider')]
126+
public function date_gets_set_in_localization_when_contained_in_localized_array($shouldBeInArray, $expectedDate)
127+
{
128+
$this->setSites([
129+
'en' => ['url' => '/', 'locale' => 'en'],
130+
'fr' => ['url' => '/two/', 'locale' => 'fr'],
131+
]);
132+
133+
[$user, $collection] = $this->seedUserAndCollection();
134+
$collection->dated(true)->save();
135+
136+
$entry = EntryFactory::collection($collection)
137+
->slug('existing-entry')
138+
->data(['title' => 'Existing Entry'])
139+
->date('2021-01-01')
140+
->create();
141+
142+
$localized = EntryFactory::collection($collection)
143+
->slug('existing-entry')
144+
->data(['title' => 'Existing Entry'])
145+
->origin($entry->id())
146+
->locale('fr')
147+
->create();
148+
149+
$this
150+
->actingAs($user)
151+
->update($localized, [
152+
'title' => 'Updated Entry',
153+
'slug' => 'updated-entry',
154+
'date' => ['date' => '2021-02-02'],
155+
'_localized' => $shouldBeInArray ? ['date'] : [],
156+
])
157+
->assertOk();
158+
159+
$localized = $localized->fresh();
160+
$this->assertEquals($expectedDate, $localized->date()->format('Y-m-d'));
161+
}
162+
163+
public static function savesDateProvider()
164+
{
165+
return [
166+
'date is in localized array' => [true, '2021-02-02'],
167+
'date is not in localized array' => [false, '2021-01-01'],
168+
];
169+
}
170+
98171
#[Test]
99172
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()
100173
{

0 commit comments

Comments
 (0)