-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathDateTest.php
More file actions
793 lines (743 loc) · 28.7 KB
/
DateTest.php
File metadata and controls
793 lines (743 loc) · 28.7 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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
<?php
namespace Tests\Fieldtypes;
use Illuminate\Support\Carbon;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Fields\Field;
use Statamic\Fields\Fields;
use Statamic\Fieldtypes\Date;
use Tests\TestCase;
class DateTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
// Set "now" to an arbitrary time so we can make sure that when date
// instances are created in the fieldtype, they aren't inheriting
// default values from the current time.
Carbon::setTestNow(Carbon::createFromFormat('Y-m-d H:i', '2010-12-25 13:43'));
}
#[Test]
#[DataProvider('augmentProvider')]
public function it_augments($timezone, $config, $value, $expected)
{
config()->set('app.timezone', $timezone);
$augmented = $this->fieldtype($config)->augment($value);
$this->assertInstanceOf(Carbon::class, $augmented);
$this->assertEquals($expected, $augmented->format('Y M d H:i:s'));
}
public static function augmentProvider()
{
return [
'date' => [
'UTC',
[],
'2012-01-04',
'2012 Jan 04 00:00:00',
],
'date with custom format' => [
'UTC',
['format' => 'Y--m--d'],
'2012--01--04',
'2012 Jan 04 00:00:00',
],
'date in a different timezone' => [
'America/New_York', // -5000
[],
'2012-01-04',
'2012 Jan 04 05:00:00',
],
// The time and seconds configs are important, otherwise
// when when parsing dates without times, the time would inherit from "now".
// We need to rely on the configs to know when or when not to reset the time.
'date with time' => [
'UTC',
['time_enabled' => true],
'2012-01-04 15:32',
'2012 Jan 04 15:32:00',
],
'date with time but seconds disabled' => [
'UTC',
['time_enabled' => true],
'2012-01-04 15:32:54',
'2012 Jan 04 15:32:00',
],
'date with time and seconds' => [
'UTC',
['time_enabled' => true, 'time_seconds_enabled' => true],
'2012-01-04 15:32:54',
'2012 Jan 04 15:32:54',
],
'date with time in a different timezone' => [
'America/New_York', // -5000
['time_enabled' => true],
'2012-01-04 15:32',
'2012 Jan 04 20:32:00',
],
];
}
#[Test]
public function it_augments_null()
{
$augmented = $this->fieldtype()->augment(null);
$this->assertNull($augmented);
}
#[Test]
public function it_augments_a_carbon_instance()
{
// Could happen if you are using the date fieldtype to augment a manually provided value.
$instance = new Carbon;
$augmented = $this->fieldtype()->augment($instance);
$this->assertSame($instance, $augmented);
}
#[Test]
public function it_augments_a_range()
{
$augmented = $this->fieldtype(['mode' => 'range'])->augment([
'start' => '2012-01-04',
'end' => '2013-02-06',
]);
$this->assertIsArray($augmented);
$this->assertEquals(['start', 'end'], array_keys($augmented));
$this->assertInstanceOf(Carbon::class, $augmented['start']);
$this->assertInstanceOf(Carbon::class, $augmented['end']);
$this->assertEquals('2012 Jan 04 00:00', $augmented['start']->format('Y M d H:i'));
$this->assertEquals('2013 Feb 06 00:00', $augmented['end']->format('Y M d H:i'));
}
#[Test]
public function it_augments_a_range_in_a_positive_offset_timezone()
{
config()->set('app.timezone', 'Europe/Berlin');
$augmented = $this->fieldtype(['mode' => 'range'])->augment([
'start' => '2026-02-02',
'end' => '2026-02-05',
]);
$this->assertIsArray($augmented);
$this->assertInstanceOf(Carbon::class, $augmented['start']);
$this->assertInstanceOf(Carbon::class, $augmented['end']);
$this->assertEquals('2026 Feb 02', $augmented['start']->setTimezone('Europe/Berlin')->format('Y M d'));
$this->assertEquals('2026 Feb 05', $augmented['end']->setTimezone('Europe/Berlin')->format('Y M d'));
}
#[Test]
public function it_augments_a_null_range()
{
$augmented = $this->fieldtype(['mode' => 'range'])->augment(null);
$this->assertNull($augmented);
}
#[Test]
#[DataProvider('processProvider')]
public function it_processes_on_save($timezone, $config, $value, $expected)
{
config()->set('app.timezone', $timezone);
$this->assertSame($expected, $this->fieldtype($config)->process($value));
}
public static function processProvider()
{
return [
'null' => [
'UTC',
[],
null,
null,
],
'date with default format' => [
'UTC',
[],
'2012-08-29T00:00:00Z',
'2012-08-29 00:00',
],
'date with custom format' => [
'UTC',
['format' => 'Y--m--d H/i'],
'2012-08-29T00:00:00Z',
'2012--08--29 00/00',
],
'date in a different timezone' => [
'America/New_York', // -4000
[],
'2012-08-29T00:00:00Z',
'2012-08-28 20:00',
],
'date with time' => [
'UTC',
['time_enabled' => true],
'2012-08-29T13:43:00Z',
'2012-08-29 13:43',
],
'null range' => [
'UTC',
['mode' => 'range'],
['start' => null, 'end' => null],
null,
],
'range with default format' => [
'UTC',
['mode' => 'range'],
['start' => '2012-08-29T00:00:00Z', 'end' => '2013-09-27T23:59:00Z'],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 23:59'],
],
'range with custom format' => [
'UTC',
['mode' => 'range', 'format' => 'Y--m--d H/i'],
['start' => '2012-08-29T00:00:00Z', 'end' => '2013-09-27T23:59:00Z'],
['start' => '2012--08--29 00/00', 'end' => '2013--09--27 23/59'],
],
'range in a different timezone' => [
'America/New_York', // -4000
['mode' => 'range'],
['start' => '2012-08-29T00:00:00Z', 'end' => '2013-09-27T23:59:00Z'],
['start' => '2012-08-28 20:00', 'end' => '2013-09-27 19:59'],
],
'date-only format' => [
'UTC',
['format' => 'Y-m-d'],
'2012-08-29',
'2012-08-29',
],
'date-only format in a different timezone' => [
'America/New_York',
['format' => 'Y-m-d'],
'2012-08-29',
'2012-08-29',
],
'date-only custom format' => [
'UTC',
['format' => 'Y--m--d'],
'2012-08-29',
'2012--08--29',
],
'date-only format range' => [
'UTC',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
],
'date-only format range in a different timezone' => [
'America/New_York',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
],
];
}
#[Test]
public function it_saves_date_as_integer_if_format_results_in_a_number()
{
$this->assertSame(20120829, $this->fieldtype(['format' => 'Ymd'])->process('2012-08-29T00:00:00Z'));
}
#[Test]
public function it_saves_ranges_as_integers_if_format_results_in_a_number()
{
$fieldtype = $this->fieldtype(['mode' => 'range', 'format' => 'YmdHi']);
$this->assertSame(
['start' => 201208290000, 'end' => 201309272359],
$fieldtype->process(['start' => '2012-08-29T00:00:00Z', 'end' => '2013-09-27T23:59:00Z'])
);
}
#[Test]
#[DataProvider('preProcessProvider')]
public function it_preprocesses($timezone, $config, $value, $expected)
{
config()->set('app.timezone', $timezone);
$this->assertSame($expected, $this->fieldtype($config)->preProcess($value));
}
public static function preProcessProvider()
{
return [
'null' => [
'UTC',
[],
null,
null,
],
'now' => [
'UTC',
[],
'now', // this would happen if the value was null, but default was "now"
'now', // handled on the frontend
],
'date without time' => [
'UTC',
[],
'2012-08-29',
'2012-08-29T00:00:00.000Z',
],
'date with default format' => [
'UTC',
[],
'2012-08-29 00:00',
'2012-08-29T00:00:00.000Z',
],
'date with custom format' => [
'UTC',
['format' => 'Y--m--d H/i'],
'2012--08--29 00/00',
'2012-08-29T00:00:00.000Z',
],
'date in a different timezone' => [
'America/New_York', // -0400
[],
'2012-08-29 00:00',
'2012-08-29T04:00:00.000Z',
],
'date with time' => [
'UTC',
['time_enabled' => true],
'2012-08-29 13:43',
'2012-08-29T13:43:00.000Z',
],
'date with time in a different timezone' => [
'America/New_York', // -0400
['time_enabled' => true],
'2012-08-29 13:43',
'2012-08-29T17:43:00.000Z',
],
'date-only format' => [
'UTC',
['format' => 'Y-m-d'],
'2012-08-29',
'2012-08-29',
],
'date-only format in a different timezone' => [
'America/New_York',
['format' => 'Y-m-d'],
'2012-08-29',
'2012-08-29',
],
'date-only format in a positive offset timezone' => [
'Australia/Sydney',
['format' => 'Y-m-d'],
'2012-08-29',
'2012-08-29',
],
'date-only custom format' => [
'America/New_York',
['format' => 'Y--m--d'],
'2012--08--29',
'2012-08-29',
],
'null range' => [
'UTC',
['mode' => 'range'],
null,
null,
],
'range with default format' => [
'UTC',
['mode' => 'range'],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 23:59'],
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2013-09-27T23:59:00.000Z'],
],
'range with custom format' => [
'UTC',
['mode' => 'range', 'format' => 'Y--m--d H/i'],
['start' => '2012--08--29 00/00', 'end' => '2013--09--27 23/59'],
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2013-09-27T23:59:00.000Z'],
],
'range in a different timezone' => [
'America/New_York', // -4000
['mode' => 'range'],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 23:59'],
['start' => '2012-08-29T04:00:00.000Z', 'end' => '2013-09-28T03:59:00.000Z'],
],
'date-only format range' => [
'UTC',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
],
'date-only format range in a different timezone' => [
'America/New_York',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
],
'range where single date has been provided' => [
'UTC',
// e.g. If it was once a non-range field.
// Use the single date as both the start and end dates.
['mode' => 'range'],
'2012-08-29',
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2012-08-29T23:59:59.999Z'],
],
'range where single date has been provided with date-only custom format' => [
'UTC',
['mode' => 'range', 'format' => 'Y--m--d'],
'2012--08--29',
['start' => '2012-08-29', 'end' => '2012-08-29'],
],
'date where range has been provided' => [
'UTC',
// e.g. If it was once a range field. Use the start date.
[],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 23:59'],
'2012-08-29T00:00:00.000Z',
],
'date where range has been provided with custom format' => [
'UTC',
['format' => 'Y--m--d H/i'],
['start' => '2012--08--29 00/00', 'end' => '2013--09--27 23/59'],
'2012-08-29T00:00:00.000Z',
],
];
}
#[Test]
#[DataProvider('preProcessIndexProvider')]
public function it_preprocesses_for_index($timezone, $config, $value, $expected)
{
config()->set('app.timezone', $timezone);
$this->assertSame($expected, $this->fieldtype($config)->preProcessIndex($value));
}
public static function preProcessIndexProvider()
{
return [
'null' => [
'UTC',
[],
null,
null,
],
'date with default format' => [
'UTC',
[],
'2012-08-29 00:00',
['date' => '2012-08-29T00:00:00.000Z', 'mode' => 'single', 'time_enabled' => false],
],
'date with custom format' => [
'UTC',
['format' => 'Y--m--d H/i'],
'2012--08--29 00/00',
['date' => '2012-08-29T00:00:00.000Z', 'mode' => 'single', 'time_enabled' => false],
],
'date in a different timezone' => [
'America/New_York', // -0400
[],
'2012-08-29 00:00',
['date' => '2012-08-29T04:00:00.000Z', 'mode' => 'single', 'time_enabled' => false],
],
'date with time' => [
'UTC',
['time_enabled' => true],
'2012-08-29 13:43',
['date' => '2012-08-29T13:43:00.000Z', 'mode' => 'single', 'time_enabled' => true],
],
'date with time and custom format' => [
'UTC',
['time_enabled' => true, 'format' => 'Y--m--d H:i'],
'2012--08--29 13:43',
['date' => '2012-08-29T13:43:00.000Z', 'mode' => 'single', 'time_enabled' => true],
],
'date with time in a different timezone' => [
'America/New_York', // -0400
['time_enabled' => true],
'2012-08-29 13:43',
['date' => '2012-08-29T17:43:00.000Z', 'mode' => 'single', 'time_enabled' => true],
],
'null range' => [
'UTC',
['mode' => 'range'],
null,
null,
],
'range with default format' => [
'UTC',
['mode' => 'range'],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 00:00'],
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2013-09-27T00:00:00.000Z', 'mode' => 'range', 'time_enabled' => false],
],
'range with custom format' => [
'UTC',
['mode' => 'range', 'format' => 'Y--m--d H/i'],
['start' => '2012--08--29 00/00', 'end' => '2013--09--27 00/00'],
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2013-09-27T00:00:00.000Z', 'mode' => 'range', 'time_enabled' => false],
],
'range in a different timezone' => [
'America/New_York', // -4000
['mode' => 'range'],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 00:00'],
['start' => '2012-08-29T04:00:00.000Z', 'end' => '2013-09-27T04:00:00.000Z', 'mode' => 'range', 'time_enabled' => false],
],
'range where single date has been provided' => [
// e.g. If it was once a non-range field.
// Use the single date as both the start and end dates.
'UTC',
['mode' => 'range'],
'2012-08-29',
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2012-08-29T00:00:00.000Z', 'mode' => 'range', 'time_enabled' => false],
],
'range where single date has been provided with custom format' => [
'UTC',
['mode' => 'range', 'format' => 'Y--m--d H/i'],
'2012--08--29 00/00',
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2012-08-29T00:00:00.000Z', 'mode' => 'range', 'time_enabled' => false],
],
'date where range has been provided' => [
// e.g. If it was once a range field. Use the start date.
'UTC',
[],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 00:00'],
['date' => '2012-08-29T00:00:00.000Z', 'mode' => 'single', 'time_enabled' => false],
],
'date where range has been provided with custom format' => [
'UTC',
['format' => 'Y--m--d H/i'],
['start' => '2012--08--29 00/00', 'end' => '2013--09--27 00/00'],
['date' => '2012-08-29T00:00:00.000Z', 'mode' => 'single', 'time_enabled' => false],
],
'range where time has been enabled' => [
'UTC',
['mode' => 'range', 'time_enabled' => true],
['start' => '2012-08-29 00:00', 'end' => '2013-09-27 00:00'],
['start' => '2012-08-29T00:00:00.000Z', 'end' => '2013-09-27T00:00:00.000Z', 'mode' => 'range', 'time_enabled' => true],
],
'date-only format' => [
'UTC',
['format' => 'Y-m-d'],
'2012-08-29',
['date' => '2012-08-29', 'mode' => 'single', 'time_enabled' => false],
],
'date-only format in a different timezone' => [
'America/New_York',
['format' => 'Y-m-d'],
'2012-08-29',
['date' => '2012-08-29', 'mode' => 'single', 'time_enabled' => false],
],
'date-only format range' => [
'UTC',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27', 'mode' => 'range', 'time_enabled' => false],
],
'date-only format range in a different timezone' => [
'America/New_York',
['mode' => 'range', 'format' => 'Y-m-d'],
['start' => '2012-08-29', 'end' => '2013-09-27'],
['start' => '2012-08-29', 'end' => '2013-09-27', 'mode' => 'range', 'time_enabled' => false],
],
];
}
#[Test]
#[DataProvider('validatablesProvider')]
public function it_preprocess_validatables($config, $input, $expected)
{
$fieldtype = $this->fieldtype($config);
$value = $fieldtype->preProcessValidatable($input);
if ($expected === null) {
$this->assertNull($value);
} else {
$this->assertInstanceOf(Carbon::class, $value);
$this->assertEquals($expected, $value->toIso8601ZuluString('millisecond'));
}
}
public static function validatablesProvider()
{
// This only contains valid values. Invalid ones would throw a validation exception, tested in "it_validates" below.
return [
'null' => [
[],
null,
null,
],
'null date when not required' => [
[],
null,
null,
],
'valid date' => [
[],
'2012-01-29T00:00:00.000Z',
'2012-01-29T00:00:00.000Z',
],
'valid date and time' => [
['time_enabled' => true],
'2012-01-29T13:00:00.000Z',
'2012-01-29T13:00:00.000Z',
],
'valid date and time with seconds' => [
['time_enabled' => true, 'time_seconds_enabled' => true],
'2012-01-29T13:14:15.000Z',
'2012-01-29T13:14:15.000Z',
],
// A carbon instance would be passed in if it was already processed.
// e.g. if it was nested inside a Replicator.
'carbon instance' => [
[],
Carbon::parse('2012-01-29'),
'2012-01-29T00:00:00.000Z',
],
];
}
#[Test]
#[DataProvider('rangeValidatablesProvider')]
public function it_preprocess_range_validatables($config, $input, $expected)
{
$fieldtype = $this->fieldtype($config);
$value = $fieldtype->preProcessValidatable($input);
if ($expected === null) {
$this->assertNull($value);
} else {
$this->assertArrayHasKey('start', $value);
$this->assertArrayHasKey('end', $value);
$this->assertInstanceOf(Carbon::class, $start = $value['start']);
$this->assertInstanceOf(Carbon::class, $end = $value['end']);
$this->assertEquals($expected, [
'start' => $start->toIso8601ZuluString('millisecond'),
'end' => $end->toIso8601ZuluString('millisecond'),
]);
}
}
public static function rangeValidatablesProvider()
{
// This only contains valid values. Invalid ones would throw a validation exception, tested in "it_validates" below.
return [
'null' => [
['mode' => 'range'],
null,
null,
],
'valid date range' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => '2012-01-30T00:00:00.000Z'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => '2012-01-30T00:00:00.000Z'],
],
// Start/end array with carbon instances would be passed in if it was already processed.
// e.g. if it was nested inside a Replicator.
'carbon instances' => [
['mode' => 'range'],
['start' => Carbon::parse('2012-01-29'), 'end' => Carbon::parse('2012-02-14')],
[
'start' => '2012-01-29T00:00:00.000Z',
'end' => '2012-02-14T00:00:00.000Z',
],
],
];
}
#[Test]
#[DataProvider('validationProvider')]
public function it_validates($config, $input, $expected)
{
$messages = [];
$field = $this->fieldtype($config)->field();
$fields = (new Fields)->setFields(collect([$field]))->addValues(['test' => $input]);
try {
$fields->validate();
} catch (ValidationException $e) {
$messages = $e->validator->errors()->all();
}
$this->assertEquals($expected, $messages);
}
public static function validationProvider()
{
return [
'valid date' => [
[],
'2012-01-29T00:00:00.000Z',
[],
],
'null' => [
[],
null,
[],
],
'invalid date format' => [
[],
'2024-01-29',
['Not a valid date.'],
],
'valid date-only format' => [
['format' => 'Y-m-d'],
'2024-01-29',
[],
],
'invalid date-only format' => [
['format' => 'Y-m-d'],
'marchtember oneteenth',
['Not a valid date.'],
],
'ridiculous invalid date format' => [
[],
'marchtember oneteenth',
['Not a valid date.'],
],
'invalid date' => [
[],
'2010-06-50T00:00:00.000Z',
['Not a valid date.'],
],
'valid date range' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => '2012-01-30T00:00:00.000Z'],
[],
],
'null date in range mode required via validate' => [
['mode' => 'range', 'validate' => 'required'],
null,
['The Test field is required.'],
],
'missing start date' => [
['mode' => 'range'],
['end' => '2012-01-30T00:00:00.000Z'],
['Start date is required.'],
],
'missing end date' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z'],
['End date is required.'],
],
'null start date' => [
['mode' => 'range'],
['start' => null, 'end' => '2012-01-30T00:00:00.000Z'],
['Start date is required.'],
],
'null end date' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => null],
['End date is required.'],
],
'both dates null' => [
['mode' => 'range'],
['start' => null, 'end' => null],
['Date is required.'], // Invalid because if you want null, the whole value should be null.
],
'both dates null, required via bool' => [
['mode' => 'range', 'required' => true],
['start' => null, 'end' => null],
['Date is required.'],
],
'both dates null, required via validate' => [
['mode' => 'range', 'validate' => 'required'],
['start' => null, 'end' => null],
['Date is required.'],
],
'invalid start date' => [
['mode' => 'range'],
['start' => '2010-06-50T00:00:00.000Z', 'end' => '2012-01-30T00:00:00.000Z'],
['Not a valid start date.'],
],
'invalid end date' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => '2010-06-50T00:00:00.000Z'],
['Not a valid end date.'],
],
'invalid start date format' => [
['mode' => 'range'],
['start' => 'marchtember oneteenth', 'end' => '2012-01-30T00:00:00.000Z'],
['Not a valid start date.'],
],
'invalid end date format' => [
['mode' => 'range'],
['start' => '2012-01-29T00:00:00.000Z', 'end' => 'marchtember oneteenth'],
['Not a valid end date.'],
],
];
}
public function fieldtype($config = [])
{
$field = new Field('test', array_replace([
'type' => 'date',
'mode' => 'single',
], $config));
return (new Date)->setField($field);
}
}