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
20 changes: 17 additions & 3 deletions src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,29 @@ public function preProcess($data)
}

/**
* Generate a fake value for form submission testing.
* A deterministic sample value, used wherever a value is needed for previewing but no real one exists.
* For example, form email previews.
*
* Return null to fall back to FakeSubmissionGenerator's built-in handling.
* Return the value in its processed (stored) shape. This may run in production, so it must not use Faker.
* The field's config is available via `$this->field()`. Return null if the fieldtype doesn't collect a value.
*/
public function fakeValue(): mixed
public function previewValue(): mixed
{
return null;
}

/**
* A realistic random sample value, used where Faker is available. For example, fake form submissions.
*
* Return the value in its processed (stored) shape. This only runs in local environments.
* Defaults to `previewValue()`, so override it only when you want more realistic data.
* The field's config is available via `$this->field()`.
*/
public function fakeValue(\Faker\Generator $faker): mixed
{
return $this->previewValue();
}

public function preProcessConfig($data)
{
return $this->preProcess($data);
Expand Down
51 changes: 51 additions & 0 deletions src/Fields/SampleValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Statamic\Fields;

use Faker\Generator;
use Illuminate\Support\Arr;

class SampleValue
{
public static function sentence(): string
{
return 'Lorem ipsum dolor sit amet';
}

public static function paragraph(): string
{
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
}

public static function randomKeys(array $keys, Generator $faker, mixed $max = null): array
{
if ($keys === []) {
return [];
}

$limit = min((int) $max > 0 ? (int) $max : 3, count($keys));

return $faker->randomElements($keys, $faker->numberBetween(1, $limit));
}

public static function optionKeys(Field $field): array
{
$options = Arr::wrap($field->get('options', []));

if ($options === []) {
return [];
}

if (Arr::isAssoc($options)) {
return array_keys($options);
}

return array_values(array_filter(array_map(function ($option) {
if (! is_array($option)) {
return $option;
}

return Arr::get($option, 'key') ?? Arr::get($option, 'value');
}, $options), fn ($value) => $value !== null && $value !== ''));
}
}
12 changes: 12 additions & 0 deletions src/Fieldtypes/Checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\SampleValue;
use Statamic\Fieldtypes\Concerns\MigratesLegacyInlineConfig;

use function Statamic\trans as __;
Expand Down Expand Up @@ -78,4 +80,14 @@ public function process($data)
->values()
->all();
}

public function previewValue(): mixed
{
return array_slice(SampleValue::optionKeys($this->field()), 0, 1);
}

public function fakeValue(Generator $faker): mixed
{
return SampleValue::randomKeys(SampleValue::optionKeys($this->field()), $faker);
}
}
11 changes: 11 additions & 0 deletions src/Fieldtypes/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Fieldtypes;

use Carbon\Exceptions\InvalidFormatException;
use Faker\Generator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -481,4 +482,14 @@ private function preProcessRangeValidatable($value)
'end' => Carbon::parse($value['end']),
];
}

public function previewValue(): mixed
{
return now()->toDateString();
}

public function fakeValue(Generator $faker): mixed
{
return $faker->date('Y-m-d');
}
}
37 changes: 37 additions & 0 deletions src/Fieldtypes/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Statamic\Dictionaries\Dictionary as DictionaryInstance;
use Statamic\Dictionaries\Item;
use Statamic\Exceptions\DictionaryNotFoundException;
use Statamic\Exceptions\UndefinedDictionaryException;
use Statamic\Facades\Dictionary as Dictionaries;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\SampleValue;
use Statamic\Support\Arr;

use function Statamic\trans as __;
Expand Down Expand Up @@ -195,4 +197,39 @@ public function keywords(): array
->merge(['select', 'option', 'choice', 'dropdown', 'list'])
->unique()->values()->all();
}

public function previewValue(): mixed
{
if (($key = array_key_first($this->sampleOptions())) === null) {
return null;
}

return $this->field()->get('max_items') === 1 ? $key : [$key];
}

public function fakeValue(Generator $faker): mixed
{
$keys = array_keys($this->sampleOptions());

if ($keys === []) {
return null;
}

$maxItems = $this->field()->get('max_items');

if ($maxItems === 1) {
return $faker->randomElement($keys);
}

return SampleValue::randomKeys($keys, $faker, $maxItems);
}

private function sampleOptions(): array
{
try {
return $this->dictionary()->options();
} catch (\Throwable) {
return [];
}
}
}
13 changes: 13 additions & 0 deletions src/Fieldtypes/Floatval.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
use Statamic\Query\Scopes\Filters\Fields\Floatval as FloatFilter;
Expand Down Expand Up @@ -100,4 +101,16 @@ public function rules(): array

return $rules;
}

public function previewValue(): mixed
{
$min = $this->field()->get('min');

return is_numeric($min) ? $min + 0 : 1;
}

public function fakeValue(Generator $faker): mixed
{
return $faker->randomFloat(2, 1, 1000);
}
}
28 changes: 28 additions & 0 deletions src/Fieldtypes/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Illuminate\Support\Collection;
use Statamic\Data\NestedFieldUpdater;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Field;
use Statamic\Fields\Fields;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Values;
use Statamic\Forms\Fields\FormField;
use Statamic\Forms\Fields\FormFields;
use Statamic\GraphQL\Types\GroupType;
use Statamic\Support\Arr;
Expand Down Expand Up @@ -224,4 +228,28 @@ public function iterateReferenceFields($data, NestedFieldUpdater $updater): void

$updater->update(new Fields($fieldsConfig));
}

public function previewValue(): mixed
{
return $this->sampleFields()
->map(fn (Field $field) => $field->fieldtype()->previewValue())
->all();
}

public function fakeValue(Generator $faker): mixed
{
return $this->sampleFields()
->map(fn (Field $field) => $field->fieldtype()->fakeValue($faker))
->all();
}

private function sampleFields(): Collection
{
// TODO: Use $this->fields() once the form Group fieldtype converts its nested fields to core fieldtypes.
return collect($this->field()->get('fields', []))
->filter(fn ($config) => is_array($config) && isset($config['handle'], $config['field']))
->mapWithKeys(fn (array $config) => [
$config['handle'] => new Field($config['handle'], (new FormField($config['handle'], $config['field']))->toFieldArray()),
]);
}
}
22 changes: 22 additions & 0 deletions src/Fieldtypes/HasSelectOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Illuminate\Support\Collection;
use Statamic\Facades\GraphQL;
use Statamic\Fields\LabeledValue;
use Statamic\Fields\SampleValue;
use Statamic\GraphQL\Types\LabeledValueType;
use Statamic\Support\Arr;

Expand Down Expand Up @@ -217,4 +219,24 @@ public function extraRenderableFieldData(): array
->all(),
];
}

public function previewValue(): mixed
{
$keys = SampleValue::optionKeys($this->field());

return $this->multiple()
? array_slice($keys, 0, 1)
: ($keys[0] ?? null);
}

public function fakeValue(Generator $faker): mixed
{
$keys = SampleValue::optionKeys($this->field());

if ($this->multiple()) {
return SampleValue::randomKeys($keys, $faker, $this->field()->get('max_items'));
}

return $keys === [] ? null : $faker->randomElement($keys);
}
}
5 changes: 5 additions & 0 deletions src/Fieldtypes/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ protected function configFieldItems(): array
],
];
}

public function previewValue(): mixed
{
return $this->field()->defaultValue();
}
}
22 changes: 22 additions & 0 deletions src/Fieldtypes/ImageChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Statamic\Facades\Asset;
use Statamic\Facades\AssetContainer;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\SampleValue;
use Statamic\Support\Arr;
use Statamic\Support\Str;

Expand Down Expand Up @@ -91,4 +93,24 @@ private function resolveImageUrl(mixed $image): ?string
->first()
?->url();
}

public function previewValue(): mixed
{
$keys = SampleValue::optionKeys($this->field());

return $this->field()->get('multiple')
? array_slice($keys, 0, 1)
: ($keys[0] ?? null);
}

public function fakeValue(Generator $faker): mixed
{
if ($this->field()->get('multiple')) {
return SampleValue::randomKeys(SampleValue::optionKeys($this->field()), $faker);
}

$keys = SampleValue::optionKeys($this->field());

return $keys === [] ? null : $faker->randomElement($keys);
}
}
16 changes: 16 additions & 0 deletions src/Fieldtypes/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Fieldtypes;

use Faker\Generator;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
use Statamic\Query\Scopes\Filters\Fields\Integer as IntegerFilter;
Expand Down Expand Up @@ -109,4 +110,19 @@ public function filter()
{
return new IntegerFilter($this);
}

public function previewValue(): mixed
{
$min = $this->field()->get('min');

return is_numeric($min) ? $min + 0 : 1;
}

public function fakeValue(Generator $faker): mixed
{
$min = (int) ($this->field()->get('min') ?? 1);
$max = (int) ($this->field()->get('max') ?? 5000);

return $faker->numberBetween(min($min, $max), max($min, $max));
}
}
11 changes: 11 additions & 0 deletions src/Fieldtypes/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Fieldtypes;

use Facades\Statamic\Routing\ResolveRedirect;
use Faker\Generator;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
Expand Down Expand Up @@ -298,4 +299,14 @@ public function replaceAssetReferences($data, ?string $newValue, string $oldValu

return $newValue !== null ? "asset::{$container}::{$newValue}" : null;
}

public function previewValue(): mixed
{
return 'https://example.com';
}

public function fakeValue(Generator $faker): mixed
{
return $faker->url();
}
}
Loading
Loading