Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Http/Controllers/CP/Traits/PreparesModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ protected function prepareModelForSaving(Resource $resource, Model &$model, Requ
return;
}

// Skip read-only timestamp columns so Laravel's automatic
// timestamp handling isn't prevented by a stale value.
if (
$field->visibility() === 'read_only'
&& $model->usesTimestamps()
&& in_array($field->handle(), [$model->getCreatedAtColumn(), $model->getUpdatedAtColumn()])
) {
return;
}

$model->setAttribute($field->handle(), $processedValue);
});
}
Expand Down
42 changes: 41 additions & 1 deletion tests/Http/Controllers/CP/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,47 @@ public function can_update_resource_and_ensure_computed_field_isnt_saved_to_data
}

#[Test]
public function can_update_resource_and_ensure__field_isnt_saved_to_database()
public function can_update_resource_and_ensure_read_only_timestamp_field_doesnt_prevent_timestamp_update()
{
$postBlueprint = Blueprint::find('runway::post');

Blueprint::shouldReceive('find')->with('user')->andReturn(new \Statamic\Fields\Blueprint);
Blueprint::shouldReceive('find')->with('runway::author')->andReturn(new \Statamic\Fields\Blueprint);
Blueprint::shouldReceive('find')->with('runway::post')->andReturn($postBlueprint->ensureField('updated_at', [
'type' => 'date',
'time_enabled' => true,
'visibility' => 'read_only',
]));
Blueprint::shouldReceive('getAdditionalNamespaces')->andReturn(collect(['runway' => base_path('resources/blueprints/runway')]))->zeroOrMoreTimes();

$post = Post::factory()->create();
$user = User::make()->makeSuper()->save();

$originalUpdatedAt = $post->updated_at;

$this->travel(5)->minutes();

$this
->actingAs($user)
->patch(cp_route('runway.update', ['resource' => 'post', 'model' => $post->id]), [
'published' => true,
'title' => 'Santa is coming home',
'slug' => 'santa-is-coming-home',
'body' => $post->body,
'author_id' => [$post->author_id],
'updated_at' => $originalUpdatedAt->toIso8601ZuluString('millisecond'),
])
->assertOk()
->assertJsonStructure(['data', 'saved']);

$post->refresh();

$this->assertEquals('Santa is coming home', $post->title);
$this->assertTrue($post->updated_at->gt($originalUpdatedAt));
}

#[Test]
public function can_update_resource_and_ensure_field_with_save_false_isnt_saved_to_database()
{
$post = Post::factory()->create();
$user = User::make()->makeSuper()->save();
Expand Down
Loading