|
5 | 5 | use Facades\Statamic\Fields\FieldtypeRepository; |
6 | 6 | use PHPUnit\Framework\Attributes\Group; |
7 | 7 | use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Statamic\Facades\Field as Fields; |
8 | 9 | use Statamic\Fields\Field; |
9 | 10 | use Statamic\Fields\Fieldtype; |
10 | 11 | use Statamic\Fields\Value; |
@@ -294,6 +295,7 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo |
294 | 295 | ->andReturn(new class extends Fieldtype |
295 | 296 | { |
296 | 297 | protected $component = 'example'; |
| 298 | + |
297 | 299 | protected $configFields = [ |
298 | 300 | 'a_config_field_with_pre_processing' => ['type' => 'with_processing'], |
299 | 301 | 'a_config_field_without_pre_processing' => ['type' => 'without_processing'], |
@@ -474,6 +476,31 @@ public function defaultValue() |
474 | 476 | $this->assertEquals('fieldtype defined default preprocessed', $field->preProcess()->value()); |
475 | 477 | } |
476 | 478 |
|
| 479 | + #[Test] |
| 480 | + public function preprocessing_a_field_with_no_value_and_computed_default_value_will_use_the_computed_function() |
| 481 | + { |
| 482 | + FieldtypeRepository::shouldReceive('find') |
| 483 | + ->with('fieldtype') |
| 484 | + ->andReturn(new class extends Fieldtype |
| 485 | + { |
| 486 | + public function preProcess($data) |
| 487 | + { |
| 488 | + return $data.' preprocessed'; |
| 489 | + } |
| 490 | + |
| 491 | + public function defaultValue() |
| 492 | + { |
| 493 | + return 'fieldtype defined default'; |
| 494 | + } |
| 495 | + }); |
| 496 | + |
| 497 | + Fields::computedDefault('computed-value', fn () => 'computed defined default'); |
| 498 | + |
| 499 | + $field = (new Field('test', ['default' => 'computed:computed-value', 'type' => 'fieldtype'])); |
| 500 | + |
| 501 | + $this->assertEquals('computed defined default preprocessed', $field->preProcess()->value()); |
| 502 | + } |
| 503 | + |
477 | 504 | #[Test] |
478 | 505 | public function converting_to_an_array_will_inline_the_handle() |
479 | 506 | { |
|
0 commit comments