-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathFieldtypeTest.php
More file actions
723 lines (619 loc) · 21.9 KB
/
FieldtypeTest.php
File metadata and controls
723 lines (619 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<?php
namespace Tests\Fields;
use Facades\Statamic\Fields\FieldtypeRepository;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Fields\Blueprint;
use Statamic\Fields\ConfigFields;
use Statamic\Fields\Field;
use Statamic\Fields\Fields;
use Statamic\Fields\Fieldtype;
use Tests\TestCase;
class FieldtypeTest extends TestCase
{
#[Test]
public function it_gets_the_field()
{
$fieldtype = new TestFieldtype;
$field = new Field('test', ['foo' => 'bar']);
$this->assertNull($fieldtype->field());
$return = $fieldtype->setField($field);
$this->assertEquals($fieldtype, $return);
$this->assertEquals($field, $fieldtype->field());
}
#[Test]
public function the_handle_is_snake_cased_from_the_class_by_default()
{
$this->assertEquals(
'test_multi_word',
(new TestMultiWordFieldtype)->handle()
);
$this->assertEquals(
'test_multi_word_with_no_fieldtype_suffix',
(new TestMultiWordWithNoFieldtypeSuffix)->handle()
);
}
#[Test]
public function handle_can_be_defined_as_a_property()
{
$fieldtype = new class extends Fieldtype
{
protected static $handle = 'example';
};
$this->assertEquals('example', $fieldtype->handle());
}
#[Test]
public function title_is_the_humanized_handle_by_default()
{
$this->assertEquals(
'Test Multi Word',
(new TestMultiWordFieldtype)->title()
);
$this->assertEquals(
'Test Multi Word With No Fieldtype Suffix',
(new TestMultiWordWithNoFieldtypeSuffix)->title()
);
}
#[Test]
public function title_can_be_defined_as_a_property()
{
$fieldtype = new class extends Fieldtype
{
protected static $title = 'Super Cool Example';
};
$this->assertEquals('Super Cool Example', $fieldtype->title());
}
#[Test]
public function localization_can_be_disabled()
{
$this->assertTrue((new TestFieldtype)->localizable());
$fieldtype = new class extends Fieldtype
{
protected $localizable = false;
};
$this->assertFalse($fieldtype->localizable());
}
#[Test]
public function validation_can_be_disabled()
{
$this->assertTrue((new TestFieldtype)->validatable());
$fieldtype = new class extends Fieldtype
{
protected $validatable = false;
};
$this->assertFalse($fieldtype->validatable());
}
#[Test]
public function default_values_can_be_disabled()
{
$this->assertTrue((new TestFieldtype)->defaultable());
$fieldtype = new class extends Fieldtype
{
protected $defaultable = false;
};
$this->assertFalse($fieldtype->defaultable());
}
#[Test]
public function it_can_be_flagged_as_hidden_from_the_fieldtype_selector()
{
$this->assertTrue((new TestFieldtype)->selectable());
$fieldtype = new class extends Fieldtype
{
protected $selectable = false;
};
$this->assertFalse($fieldtype->selectable());
}
#[Test]
public function it_can_be_flagged_as_a_relationship_fieldtype()
{
$this->assertFalse((new TestFieldtype)->isRelationship());
$fieldtype = new class extends Fieldtype
{
protected $relationship = true;
};
$this->assertTrue($fieldtype->isRelationship());
}
#[Test]
public function converts_to_an_array()
{
$fieldtype = new TestFieldtype;
$this->assertEquals([
'handle' => 'test',
'title' => 'Test',
'localizable' => true,
'validatable' => true,
'defaultable' => true,
'categories' => [],
'keywords' => [],
'icon' => 'fieldtype-test',
'config' => [],
], $fieldtype->toArray());
}
#[Test]
public function config_uses_publish_array_when_converting_to_array()
{
$fields = Mockery::mock(Fields::class);
$fields->shouldReceive('toPublishArray')->once()->andReturn(['example', 'publish', 'array']);
$fieldtype = new class($fields) extends Fieldtype
{
protected $mock;
protected static $handle = 'test';
public function __construct($mock)
{
$this->mock = $mock;
}
public function configFields(): Fields
{
return $this->mock;
}
};
$this->assertArraySubset([
'config' => ['example', 'publish', 'array'],
], $fieldtype->toArray());
}
#[Test]
public function it_gets_custom_validation_rules_as_an_array()
{
$this->assertEquals([], (new TestFieldtype)->rules());
$arrayDefined = new class extends Fieldtype
{
protected $rules = ['required', 'min:2'];
};
$this->assertEquals(['required', 'min:2'], $arrayDefined->rules());
$stringDefined = new class extends Fieldtype
{
protected $rules = 'required|min:2';
};
$this->assertEquals(['required', 'min:2'], $stringDefined->rules());
}
#[Test]
public function it_gets_extra_custom_validation_rules_as_an_array()
{
$this->assertEquals([], (new TestFieldtype)->rules());
$arrayDefined = new class extends Fieldtype
{
protected $extraRules = [
'extra.one' => ['required', 'min:2'],
'extra.two' => ['array'],
];
};
$this->assertEquals([
'extra.one' => ['required', 'min:2'],
'extra.two' => ['array'],
], $arrayDefined->extraRules());
$stringDefined = new class extends Fieldtype
{
protected $extraRules = [
'extra.one' => 'required|min:2',
'extra.two' => 'array',
];
};
$this->assertEquals([
'extra.one' => ['required', 'min:2'],
'extra.two' => ['array'],
], $stringDefined->extraRules());
}
#[Test]
public function it_can_have_a_default_value()
{
$this->assertNull((new TestFieldtype)->defaultValue());
$fieldtype = new class extends Fieldtype
{
protected $defaultValue = 'test';
};
$this->assertEquals('test', $fieldtype->defaultValue());
}
#[Test]
public function it_gets_the_config_fields()
{
tap(new TestFieldtype, function ($fieldtype) {
$fields = $fieldtype->configFields();
$this->assertInstanceOf(Fields::class, $fields);
$this->assertCount(0, $fields->all());
});
$fieldtype = new class extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'max_items' => ['type' => 'integer'],
];
};
$fields = $fieldtype->configFields();
$this->assertInstanceOf(ConfigFields::class, $fields);
$this->assertCount(2, $all = $fields->all());
tap($all['foo'], function ($field) {
$this->assertEquals('textarea', $field->type());
});
tap($all['max_items'], function ($field) {
$this->assertEquals('integer', $field->type());
});
}
#[Test]
public function it_can_append_a_single_config_field()
{
TestAppendConfigFields::appendConfigField('group', ['type' => 'text']);
$fields = (new TestAppendConfigFields())->configFields();
$this->assertCount(3, $fields->all());
$this->assertEquals('text', $fields->get('group')->type());
}
#[Test]
public function it_can_append_multiple_config_fields()
{
TestAppendConfigFields::appendConfigFields([
'group' => [
'type' => 'text',
],
'description' => [
'type' => 'textarea',
],
]);
$fields = (new TestAppendConfigFields())->configFields();
$this->assertCount(4, $fields->all());
$this->assertEquals('text', $fields->get('group')->type());
$this->assertEquals('textarea', $fields->get('description')->type());
}
#[Test]
public function it_can_append_new_section_config_fields()
{
TestAppendConfigSectionFields::appendConfigFields([
[
'display' => __('Extra section'),
'fields' => [
'more_options' => [
'display' => __('Options'),
'instructions' => __('Instructions for this field'),
'type' => 'array',
],
'extra_html_class' => [
'display' => __('Append HTML Classes options'),
'instructions' => __('Instructions for this field'),
'type' => 'textarea',
],
],
],
]);
$fields = (new TestAppendConfigSectionFields())->configFields();
$this->assertCount(4, $fields->all());
$this->assertEquals('array', $fields->get('more_options')->type());
$this->assertEquals('textarea', $fields->get('extra_html_class')->type());
}
#[Test]
public function it_can_append_new_sections_config_fields()
{
TestAppendConfigSectionFields::appendConfigFields([
[
'display' => __('Extra section'),
'fields' => [
'more_options' => [
'display' => __('Options'),
'instructions' => __('Instructions for this field'),
'type' => 'array',
],
],
],
[
'display' => __('New Extra section'),
'fields' => [
'extra_html_class' => [
'display' => __('Append HTML Classes options'),
'instructions' => __('Instructions for this field'),
'type' => 'textarea',
],
],
],
]);
$fields = (new TestAppendConfigSectionFields())->configFields();
$this->assertCount(4, $fields->all());
$this->assertEquals('array', $fields->get('more_options')->type());
$this->assertEquals('textarea', $fields->get('extra_html_class')->type());
}
#[Test]
public function it_wont_override_previously_appended_config_fields()
{
TestAppendConfigFields::appendConfigFields([
'group' => [
'type' => 'text',
],
'description' => [
'type' => 'textarea',
],
]);
TestAppendConfigFields::appendConfigField('another', ['type' => 'text']);
$fields = (new TestAppendConfigFields())->configFields();
$this->assertCount(5, $fields->all());
$this->assertEquals('text', $fields->get('group')->type());
$this->assertEquals('textarea', $fields->get('description')->type());
$this->assertEquals('text', $fields->get('another')->type());
}
#[Test]
public function it_will_only_append_config_fields_to_the_intended_fieldtype()
{
$fieldtype = new class extends Fieldtype
{
};
$fieldtypeWithAppendedConfig = new class extends Fieldtype
{
};
$fieldtypeWithAppendedConfig::appendConfigField('group', ['type' => 'text']);
$this->assertCount(0, $fieldtype->configFields()->all());
$this->assertCount(1, $fieldtypeWithAppendedConfig->configFields()->all());
}
#[Test]
#[DataProvider('configBlueprintProvider')]
public function it_gets_the_config_blueprint($property, $expectedSections, $expectedConfigFields)
{
$fieldtype = new TestFieldtypeWithConfigFieldsProperty($property);
TestFieldtypeWithConfigFieldsProperty::appendConfigField('appended', ['type' => 'text']);
$this->assertInstanceOf(Blueprint::class, $blueprint = $fieldtype->configBlueprint());
$this->assertEquals(['tabs' => [
'main' => [
'sections' => $expectedSections,
],
]], $blueprint->contents());
$this->assertEquals($expectedConfigFields, $fieldtype->configFields()->all()->map(fn ($field) => $field->type())->all());
}
public static function configBlueprintProvider()
{
return [
'linear fields results in one section' => [
$configFields = [
'alfa' => ['type' => 'bravo'],
'charlie' => ['type' => 'delta'],
],
$expectedSections = [
[
'fields' => [
['handle' => 'alfa', 'field' => ['type' => 'bravo']],
['handle' => 'charlie', 'field' => ['type' => 'delta']],
['handle' => 'appended', 'field' => ['type' => 'text']], // appended field to the end of line section
],
],
],
$expectedConfigFields = [
'alfa' => 'bravo',
'charlie' => 'delta',
'appended' => 'text',
],
],
'single section' => [
$configFields = [
[
'fields' => [
'alfa' => ['type' => 'bravo'],
'charlie' => ['type' => 'delta'],
],
],
],
$expectedSections = [
[
'fields' => [
['handle' => 'alfa', 'field' => ['type' => 'bravo']],
['handle' => 'charlie', 'field' => ['type' => 'delta']],
['handle' => 'appended', 'field' => ['type' => 'text']], // appended field to the end of lone section
],
],
],
$expectedConfigFields = [
'alfa' => 'bravo',
'charlie' => 'delta',
'appended' => 'text',
],
],
'multiple sections' => [
$configFields = [
[
'fields' => [
'alfa' => ['type' => 'bravo'],
'charlie' => ['type' => 'delta'],
],
],
[
'fields' => [
'echo' => ['type' => 'foxtrot'],
'golf' => ['type' => 'hotel'],
],
],
],
$expectedSections = [
[
'fields' => [
['handle' => 'alfa', 'field' => ['type' => 'bravo']],
['handle' => 'charlie', 'field' => ['type' => 'delta']],
],
],
[
'fields' => [
['handle' => 'echo', 'field' => ['type' => 'foxtrot']],
['handle' => 'golf', 'field' => ['type' => 'hotel']],
],
],
[
'fields' => [
['handle' => 'appended', 'field' => ['type' => 'text']], // appended field goes into its own section
],
],
],
$expectedConfigFields = [
'alfa' => 'bravo',
'charlie' => 'delta',
'echo' => 'foxtrot',
'golf' => 'hotel',
'appended' => 'text',
],
],
];
}
#[Test]
public function it_can_have_an_icon()
{
$this->assertEquals('fieldtype-test', (new TestFieldtype)->icon());
$customHandle = new class extends Fieldtype
{
protected static $handle = 'custom_handle';
};
$this->assertEquals('fieldtype-custom_handle', $customHandle->icon());
$customIcon = new class extends Fieldtype
{
protected $icon = 'foo';
};
$this->assertEquals('foo', $customIcon->icon());
}
#[Test]
public function no_processing_happens_by_default()
{
$this->assertEquals('test', (new TestFieldtype)->process('test'));
}
#[Test]
public function no_pre_processing_happens_by_default()
{
$this->assertEquals('test', (new TestFieldtype)->preProcess('test'));
}
#[Test]
public function no_pre_processing_happens_by_default_for_the_index()
{
$this->assertEquals('test', (new TestFieldtype)->preProcessIndex('test'));
}
#[Test]
public function it_gets_a_config_value()
{
(new class extends Fieldtype
{
protected static $handle = 'fieldtype_with_array_default';
protected $defaultValue = [];
})::register();
$field = new Field('test', [
'foo' => 'bar', // doesn't exist as a config field
'alfa' => 'overridden', // doesn't have a default
'bravo' => 'also overridden', // does have a default
]);
$fieldtype = (new TestFieldtypeWithConfigFields)->setField($field);
$this->assertEquals([
'foo' => 'bar',
'alfa' => 'overridden',
'bravo' => 'also overridden',
'charlie' => 'charlie!',
// Toggle fields (has default of boolean false)
'delta' => false, // No default set
'echo' => true, // Default set
'foxtrot' => false, // Default set
// Test fields (has default of empty array)
'golf' => [], // No default set
'hotel' => ['hotel!'], // Default set
], $fieldtype->config());
$this->assertEquals('bar', $fieldtype->config('foo'));
$this->assertEquals('overridden', $fieldtype->config('alfa'));
$this->assertEquals('also overridden', $fieldtype->config('bravo'));
$this->assertEquals('charlie!', $fieldtype->config('charlie'));
$this->assertEquals(false, $fieldtype->config('delta'));
$this->assertEquals(true, $fieldtype->config('echo'));
$this->assertEquals(false, $fieldtype->config('foxtrot'));
$this->assertEquals([], $fieldtype->config('golf'));
$this->assertEquals(['hotel!'], $fieldtype->config('hotel'));
$this->assertNull($fieldtype->config('unknown'));
$this->assertEquals('fallback', $fieldtype->config('unknown', 'fallback'));
}
#[Test]
#[Group('graphql')]
public function it_gets_the_graphql_type_of_string_by_default()
{
$type = (new TestFieldtype)->toGqlType();
$this->assertInstanceOf(\GraphQL\Type\Definition\StringType::class, $type);
}
#[Test]
public function it_can_make_a_fieldtype_selectable_in_forms()
{
$fieldtype = new class extends Fieldtype
{
public static $handle = 'test-selectable';
protected $selectableInForms = false;
};
$this->assertFalse($fieldtype->selectableInForms());
$fieldtype::makeSelectableInForms();
$this->assertTrue($fieldtype->selectableInForms());
$this->assertTrue(FieldtypeRepository::hasBeenMadeSelectableInForms('test-selectable'));
$this->assertTrue(FieldtypeRepository::selectableInFormIsOverriden('test-selectable'));
}
#[Test]
public function it_can_make_a_fieldtype_unselectable_in_forms()
{
$fieldtype = new class extends Fieldtype
{
public static $handle = 'test-unselectable';
protected $selectableInForms = true;
};
$this->assertTrue($fieldtype->selectableInForms());
$fieldtype::makeUnselectableInForms();
$this->assertFalse($fieldtype->selectableInForms());
$this->assertFalse(FieldtypeRepository::hasBeenMadeSelectableInForms('test-unselectable'));
$this->assertTrue(FieldtypeRepository::selectableInFormIsOverriden('test-unselectable'));
}
}
class TestFieldtype extends Fieldtype
{
//
}
class TestFieldtypeWithConfigFields extends Fieldtype
{
protected $configFields = [
'alfa' => [
'type' => 'text',
],
'bravo' => [
'type' => 'text',
'default' => 'bravo!',
],
'charlie' => [
'type' => 'text',
'default' => 'charlie!',
],
'delta' => [
'type' => 'toggle',
],
'echo' => [
'type' => 'toggle',
'default' => true,
],
'foxtrot' => [
'type' => 'toggle',
'default' => false,
],
'golf' => [
'type' => 'fieldtype_with_array_default',
],
'hotel' => [
'type' => 'fieldtype_with_array_default',
'default' => ['hotel!'],
],
];
}
class TestMultiWordFieldtype extends Fieldtype
{
//
}
class TestMultiWordWithNoFieldtypeSuffix extends Fieldtype
{
//
}
class TestAppendConfigFields extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'max_items' => ['type' => 'integer'],
];
}
class TestAppendConfigSectionFields extends Fieldtype
{
protected $configFields = [
'foo' => ['type' => 'textarea'],
'max_items' => ['type' => 'integer'],
];
}
class TestFieldtypeWithConfigFieldsProperty extends Fieldtype
{
public function __construct($property)
{
$this->configFields = $property;
}
}