|
6 | 6 | use Relaticle\CustomFields\Enums\VisibilityLogic; |
7 | 7 | use Relaticle\CustomFields\Enums\VisibilityMode; |
8 | 8 | use Relaticle\CustomFields\Enums\VisibilityOperator; |
| 9 | +use Relaticle\CustomFields\Facades\CustomFieldsType; |
| 10 | +use Relaticle\CustomFields\FieldTypeSystem\BaseFieldType; |
| 11 | +use Relaticle\CustomFields\FieldTypeSystem\FieldSchema; |
9 | 12 | use Relaticle\CustomFields\Models\CustomField; |
10 | 13 | use Relaticle\CustomFields\Models\CustomFieldSection; |
11 | 14 | use Relaticle\CustomFields\Services\Visibility\BackendVisibilityService; |
12 | 15 | use Relaticle\CustomFields\Services\Visibility\CoreVisibilityLogicService; |
13 | 16 | use Relaticle\CustomFields\Services\Visibility\FrontendVisibilityService; |
| 17 | +use Relaticle\CustomFields\Tests\Fixtures\Models\Post; |
14 | 18 | use Relaticle\CustomFields\Tests\Fixtures\Models\User; |
15 | 19 |
|
16 | 20 | beforeEach(function (): void { |
|
318 | 322 | $jsExpression = $this->frontendService->buildVisibilityExpression($this->conditionalField, $fields); |
319 | 323 | expect($jsExpression)->toBeString(); // Should generate valid expression even with null comparison |
320 | 324 | }); |
| 325 | + |
| 326 | +test('withoutUserOptions multi-choice fields do not crash during value extraction', function (): void { |
| 327 | + CustomFieldsType::register([ |
| 328 | + 'test-repeater' => TestRepeaterFieldType::class, |
| 329 | + ]); |
| 330 | + |
| 331 | + $section = CustomFieldSection::factory()->create([ |
| 332 | + 'name' => 'Repeater Section', |
| 333 | + 'entity_type' => Post::class, |
| 334 | + 'active' => true, |
| 335 | + ]); |
| 336 | + |
| 337 | + $repeaterField = CustomField::factory()->create([ |
| 338 | + 'custom_field_section_id' => $section->id, |
| 339 | + 'name' => 'Repeater', |
| 340 | + 'code' => 'repeater', |
| 341 | + 'type' => 'test-repeater', |
| 342 | + 'active' => true, |
| 343 | + ]); |
| 344 | + |
| 345 | + $post = Post::factory()->create(); |
| 346 | + $post->saveCustomFieldValue($repeaterField, [ |
| 347 | + ['value' => 'item 1'], |
| 348 | + ['value' => 'item 2'], |
| 349 | + ]); |
| 350 | + $post->load('customFieldValues.customField'); |
| 351 | + |
| 352 | + $backendService = app(BackendVisibilityService::class); |
| 353 | + |
| 354 | + $result = $backendService->extractFieldValues($post, collect([$repeaterField])); |
| 355 | + |
| 356 | + expect($result) |
| 357 | + ->toHaveKey('repeater') |
| 358 | + ->and($result['repeater'])->toBe([ |
| 359 | + ['value' => 'item 1'], |
| 360 | + ['value' => 'item 2'], |
| 361 | + ]); |
| 362 | + |
| 363 | + $visibleFields = $backendService->getVisibleFields($post, collect([$repeaterField])); |
| 364 | + |
| 365 | + expect($visibleFields)->toHaveCount(1); |
| 366 | +}); |
| 367 | + |
| 368 | +test('field types can override compatible visibility operators', function (): void { |
| 369 | + CustomFieldsType::register([ |
| 370 | + 'test-repeater' => TestRepeaterFieldType::class, |
| 371 | + ]); |
| 372 | + |
| 373 | + $fieldTypeData = CustomFieldsType::getFieldType('test-repeater'); |
| 374 | + |
| 375 | + expect($fieldTypeData->getCompatibleOperators())->toBe([ |
| 376 | + VisibilityOperator::IS_EMPTY, |
| 377 | + VisibilityOperator::IS_NOT_EMPTY, |
| 378 | + ]); |
| 379 | +}); |
| 380 | + |
| 381 | +test('field types without operator override use dataType defaults', function (): void { |
| 382 | + $fieldTypeData = CustomFieldsType::getFieldType('select'); |
| 383 | + |
| 384 | + expect($fieldTypeData->getCompatibleOperators()) |
| 385 | + ->toBe($fieldTypeData->dataType->getCompatibleOperators()); |
| 386 | +}); |
| 387 | + |
| 388 | +class TestRepeaterFieldType extends BaseFieldType |
| 389 | +{ |
| 390 | + public function configure(): FieldSchema |
| 391 | + { |
| 392 | + return FieldSchema::multiChoice() |
| 393 | + ->key('test-repeater') |
| 394 | + ->label('Test Repeater') |
| 395 | + ->icon('heroicon-o-squares-plus') |
| 396 | + ->searchable(false) |
| 397 | + ->sortable(false) |
| 398 | + ->filterable(false) |
| 399 | + ->withoutUserOptions() |
| 400 | + ->visibilityOperators([ |
| 401 | + VisibilityOperator::IS_EMPTY, |
| 402 | + VisibilityOperator::IS_NOT_EMPTY, |
| 403 | + ]); |
| 404 | + } |
| 405 | +} |
0 commit comments