forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryTest.php
More file actions
2765 lines (2259 loc) · 106 KB
/
EntryTest.php
File metadata and controls
2765 lines (2259 loc) · 106 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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Tests\Data\Entries;
use BadMethodCallException;
use Facades\Statamic\Fields\BlueprintRepository;
use Facades\Statamic\Stache\Repositories\CollectionTreeRepository;
use Facades\Tests\Factories\EntryFactory;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\View;
use LogicException;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use ReflectionClass;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Data\AugmentedCollection;
use Statamic\Entries\AugmentedEntry;
use Statamic\Entries\Collection;
use Statamic\Entries\Entry;
use Statamic\Events\EntryBlueprintFound;
use Statamic\Events\EntryCreated;
use Statamic\Events\EntryCreating;
use Statamic\Events\EntryDeleted;
use Statamic\Events\EntryDeleting;
use Statamic\Events\EntrySaved;
use Statamic\Events\EntrySaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Fields\Blueprint;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Value;
use Statamic\Sites\Site;
use Statamic\Structures\CollectionStructure;
use Statamic\Structures\CollectionTree;
use Statamic\Structures\Page;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class EntryTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
#[Test]
public function it_sets_and_gets_the_locale()
{
$this->setSites([
'foo' => [],
'bar' => [],
]);
$entry = new Entry;
$this->assertEquals('foo', $entry->locale()); // defaults to the default site.
$return = $entry->locale('bar');
$this->assertEquals($entry, $return);
$this->assertEquals('bar', $entry->locale());
}
#[Test]
public function it_gets_the_site()
{
$this->setSites([
'en' => ['locale' => 'en_US'],
]);
$entry = (new Entry)->locale('en');
$site = $entry->site();
$this->assertInstanceOf(Site::class, $site);
$this->assertEquals('en_US', $site->locale());
}
#[Test]
public function it_sets_and_gets_the_slug()
{
$entry = new Entry;
$this->assertNull($entry->slug());
$return = $entry->slug('foo');
$this->assertEquals($entry, $return);
$this->assertEquals('foo', $entry->slug());
}
#[Test]
public function the_slug_gets_slugified()
{
$this->setSites([
'en' => ['locale' => 'en_US', 'url' => '/'],
'da' => ['locale' => 'da_DK', 'url' => '/da/'],
]);
$entry = new Entry;
$entry->slug('foo bar æøå');
$this->assertEquals('foo-bar-aeoa', $entry->slug());
$entry->locale('da');
$this->assertEquals('foo-bar-aeoeaa', $entry->slug()); // danish replaces æøå with aeoeaa
}
#[Test]
public function explicitly_setting_slug_to_null_will_return_null()
{
$entry = new Entry;
$entry->slug(null);
$this->assertNull($entry->slug());
}
#[Test]
public function if_the_slug_is_a_function_it_will_resolve_it()
{
$entry = new Entry;
$entry->set('title', 'Foo Bar');
$entry->slug(function ($entry) {
return $entry->get('title');
});
$this->assertEquals('foo-bar', $entry->slug());
}
#[Test]
public function if_the_slug_is_a_function_it_will_only_resolve_it_once()
{
$count = 0;
$slugWithinClosure = 'not yet set';
$entry = new Entry;
$entry->slug(function ($entry) use (&$count, &$slugWithinClosure) {
$count++;
// Call slug in here again to attempt infinite recursion. This could
// happen if something in the closure logic indirectly calls slug again.
$slugWithinClosure = $entry->slug();
return 'the-slug';
});
$this->assertEquals('the-slug', $entry->slug());
$this->assertNull($slugWithinClosure);
$this->assertEquals(1, $count);
// Ensure that the temporary null slug is reset back the actual one for subsequent calls.
$this->assertEquals('the-slug', $entry->slug());
}
#[Test]
public function it_resolves_the_slug_when_serializing()
{
$entry = new Entry;
$entry->slug(fn () => 'the-slug');
// This would throw an exception if the slug remained an unresolved closure.
$serialized = serialize($entry);
$this->assertEquals('the-slug', unserialize($serialized)->slug());
}
#[Test]
public function it_sets_gets_and_removes_data_values()
{
$collection = tap(Collection::make('test'))->save();
$entry = (new Entry)->collection('test');
$this->assertNull($entry->get('foo'));
$return = $entry->set('foo', 'bar');
$this->assertEquals($entry, $return);
$this->assertTrue($entry->has('foo'));
$this->assertEquals('bar', $entry->get('foo'));
$this->assertEquals('bar', $entry->value('foo'));
$this->assertEquals('fallback', $entry->get('unknown', 'fallback'));
$return = $entry->remove('foo');
$this->assertEquals($entry, $return);
$this->assertFalse($entry->has('foo'));
}
#[Test]
public function it_sets_data_values_using_magic_properties()
{
$entry = new Entry;
$this->assertNull($entry->get('foo'));
$entry->foo = 'bar';
$this->assertTrue($entry->has('foo'));
$this->assertEquals('bar', $entry->get('foo'));
}
#[Test]
public function it_gets_evaluated_augmented_value_using_magic_property()
{
(new class extends Fieldtype
{
protected static $handle = 'test';
public function augment($value)
{
return $value.' (augmented)';
}
})::register();
$blueprint = Facades\Blueprint::makeFromFields(['charlie' => ['type' => 'test']]);
BlueprintRepository::shouldReceive('in')->with('collections/blog')->andReturn(collect(['blog' => $blueprint]));
Collection::make('blog')->save();
$entry = (new Entry)->collection('blog')->id('123');
$entry->set('alfa', 'bravo');
$entry->set('charlie', 'delta');
$this->assertEquals('123', $entry->id);
$this->assertEquals('123', $entry['id']);
$this->assertEquals('bravo', $entry->alfa);
$this->assertEquals('bravo', $entry['alfa']);
$this->assertEquals('delta (augmented)', $entry->charlie);
$this->assertEquals('delta (augmented)', $entry['charlie']);
}
#[Test]
#[DataProvider('queryBuilderProvider')]
public function it_has_magic_property_and_methods_for_fields_that_augment_to_query_builders($builder)
{
$builder->shouldReceive('get')->times(2)->andReturn('query builder results');
app()->instance('mocked-builder', $builder);
(new class extends Fieldtype
{
protected static $handle = 'test';
public function augment($value)
{
return app('mocked-builder');
}
})::register();
$blueprint = Facades\Blueprint::makeFromFields(['foo' => ['type' => 'test']]);
BlueprintRepository::shouldReceive('in')->with('collections/blog')->andReturn(collect(['blog' => $blueprint]));
Collection::make('blog')->save();
$entry = (new Entry)->collection('blog');
$entry->set('foo', 'delta');
$this->assertEquals('query builder results', $entry->foo);
$this->assertEquals('query builder results', $entry['foo']);
$this->assertSame($builder, $entry->foo());
}
public static function queryBuilderProvider()
{
return [
'statamic' => [Mockery::mock(\Statamic\Query\Builder::class)],
'database' => [Mockery::mock(\Illuminate\Database\Query\Builder::class)],
'eloquent' => [Mockery::mock(\Illuminate\Database\Eloquent\Builder::class)],
];
}
#[Test]
public function calling_unknown_method_throws_exception()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method Statamic\Entries\Entry::thisFieldDoesntExist()');
Collection::make('blog')->save();
(new Entry)->collection('blog')->thisFieldDoesntExist();
}
#[Test]
public function it_gets_and_sets_all_data()
{
$entry = new Entry;
$this->assertEquals([], $entry->data()->all());
$return = $entry->data(['foo' => 'bar']);
$this->assertEquals($entry, $return);
$this->assertEquals(['foo' => 'bar'], $entry->data()->all());
}
#[Test]
public function it_merges_in_additional_data()
{
$entry = (new Entry)->data([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);
$return = $entry->merge([
'bar' => 'merged bar',
'qux' => 'merged qux',
]);
$this->assertEquals($entry, $return);
$this->assertEquals([
'foo' => 'bar',
'bar' => 'merged bar',
'baz' => 'qux',
'qux' => 'merged qux',
], $entry->data()->all());
}
#[Test]
public function values_fall_back_to_the_origin_then_the_collection()
{
$collection = tap(Collection::make('test'))->save();
$origin = EntryFactory::collection('test')->create();
$entry = EntryFactory::origin($origin)->collection('test')->create();
$this->assertNull($entry->value('test'));
$collection->cascade(['test' => 'from collection']);
$this->assertEquals('from collection', $entry->value('test'));
$origin->set('test', 'from origin');
$this->assertEquals('from origin', $entry->value('test'));
}
#[Test]
public function it_gets_values_from_origin_and_collection()
{
tap(Collection::make('test')->cascade([
'one' => 'one in collection',
'two' => 'two in collection',
'three' => 'three in collection',
]))->save();
$origin = EntryFactory::collection('test')->data([
'two' => 'two in origin',
'three' => 'three in origin',
])->create();
$entry = EntryFactory::origin($origin)->collection('test')->data([
'three' => 'three in entry',
])->create();
$this->assertEquals([
'one' => 'one in collection',
'two' => 'two in origin',
'three' => 'three in entry',
], $entry->values()->all());
$this->assertEquals('one in collection', $entry->value('one'));
$this->assertEquals('two in origin', $entry->value('two'));
$this->assertEquals('three in entry', $entry->value('three'));
}
#[Test]
public function if_the_value_is_explicitly_set_to_null_then_it_should_not_fall_back()
{
tap(Collection::make('test')->cascade([
'one' => 'one in collection',
'two' => 'two in collection',
'three' => 'three in collection',
'four' => 'four in collection',
]))->save();
$origin = EntryFactory::collection('test')->data([
'two' => null,
'three' => 'three in origin',
'four' => 'four in origin',
])->create();
$entry = EntryFactory::origin($origin)->collection('test')->data([
'three' => null,
'four' => 'four in entry',
])->create();
$this->assertEquals([
'one' => 'one in collection', // falls all the way back
'two' => null, // falls back from entry, stops at origin
'three' => null, // stops at entry
'four' => 'four in entry', // set in entry
], $entry->values()->all());
$this->assertEquals('one in collection', $entry->value('one'));
$this->assertEquals(null, $entry->value('two'));
$this->assertEquals(null, $entry->value('three'));
$this->assertEquals('four in entry', $entry->value('four'));
}
#[Test]
public function it_gets_custom_computed_data()
{
Facades\Collection::computed('articles', 'description', function ($entry) {
return $entry->get('title').' AND MORE!';
});
Facades\Collection::computed('articles', [
'tags' => function ($entry) {
return ['music', 'pop'];
},
'featured' => function ($entry) {
return true;
},
]);
$collection = tap(Collection::make('articles'))->save();
$entry = (new Entry)->collection($collection)->data(['title' => 'Pop Rocks']);
$expectedData = [
'title' => 'Pop Rocks',
];
$expectedComputedData = [
'description' => 'Pop Rocks AND MORE!',
'tags' => ['music', 'pop'],
'featured' => true,
];
$expectedValues = array_merge($expectedData, $expectedComputedData);
$this->assertArraySubset($expectedData, $entry->data()->all());
$this->assertEquals($expectedComputedData, $entry->computedData()->all());
$this->assertEquals($expectedValues, $entry->values()->all());
$this->assertEquals($expectedValues['title'], $entry->value('title'));
$this->assertEquals($expectedValues['description'], $entry->value('description'));
}
#[Test]
public function it_gets_empty_computed_data_by_default()
{
$collection = tap(Collection::make('test'))->save();
$entry = (new Entry)->collection($collection)->data(['title' => 'Pop Rocks']);
$this->assertEquals([], $entry->computedData()->all());
}
#[Test]
public function it_doesnt_recursively_get_computed_data_when_callback_uses_value_methods()
{
Facades\Collection::computed('articles', 'description', function ($entry) {
return $entry->value('title').' '.$entry->values()->get('suffix');
});
$collection = tap(Collection::make('articles'))->save();
$entry = (new Entry)->collection($collection)->data(['title' => 'Pop Rocks', 'suffix' => 'AND MORE!']);
$this->assertEquals('Pop Rocks AND MORE!', $entry->value('description'));
}
#[Test]
public function it_can_use_actual_data_to_compose_computed_data()
{
Facades\Collection::computed('articles', 'description', function ($entry, $value) {
return $value ?? 'N/A';
});
$collection = tap(Collection::make('articles'))->save();
$entry = (new Entry)->collection($collection);
$this->assertEquals('N/A', $entry->value('description'));
$entry->data(['description' => 'Raddest article ever!']);
$this->assertEquals('Raddest article ever!', $entry->value('description'));
}
#[Test]
public function it_can_use_origin_data_to_compose_computed_data()
{
Facades\Collection::computed('articles', 'description', function ($entry, $value) {
return $entry->value('description') ?? 'N/A';
});
$collection = tap(Collection::make('articles'))->save();
(new Entry)->collection($collection)->id('origin')->data([
'description' => 'Dill Pickles',
])->save();
$entry = (new Entry)->collection($collection)->origin('origin');
$this->assertEquals('Dill Pickles', $entry->values()->get('description'));
$this->assertEquals('Dill Pickles', $entry->value('description'));
$entry->data(['description' => 'Raddest article ever!']);
$this->assertEquals('Raddest article ever!', $entry->values()->get('description'));
$this->assertEquals('Raddest article ever!', $entry->value('description'));
}
#[Test]
public function it_properly_scopes_custom_computed_data_by_collection_handle()
{
Facades\Collection::computed('articles', 'description', function ($entry) {
return $entry->get('title').' AND MORE!';
});
Facades\Collection::computed('events', 'french_description', function ($entry) {
return $entry->get('title').' ET PLUS!';
});
$articleEntry = (new Entry)->collection(tap(Collection::make('articles'))->save())->data(['title' => 'Pop Rocks']);
$eventEntry = (new Entry)->collection(tap(Collection::make('events'))->save())->data(['title' => 'Jazz Concert']);
$this->assertEquals('Pop Rocks AND MORE!', $articleEntry->value('description'));
$this->assertNull($articleEntry->value('french_description'));
$this->assertNull($eventEntry->value('description'));
$this->assertEquals('Jazz Concert ET PLUS!', $eventEntry->value('french_description'));
}
#[Test]
public function it_only_evaluates_computed_data_closures_when_getting_values()
{
$count = 0;
Facades\Collection::computed('articles', 'description', function ($entry) use (&$count) {
$count++;
return $entry->get('title').' AND MORE!';
});
$articleEntry = (new Entry)->collection(tap(Collection::make('articles'))->save())->data(['title' => 'Pop Rocks']);
$this->assertEquals(0, $count);
$this->assertEquals(['title', 'description'], $articleEntry->keys()->all());
$this->assertEquals(['title' => 'Pop Rocks', 'description' => 'Pop Rocks AND MORE!'], $articleEntry->values()->all());
$this->assertEquals(1, $count);
}
#[Test]
public function it_gets_the_url_from_the_collection()
{
$this->setSites([
'en' => ['url' => 'http://domain.com/', 'locale' => 'en_US'],
'fr' => ['url' => 'http://domain.com/fr/', 'locale' => 'fr_FR'],
'de' => ['url' => 'http://domain.de/', 'locale' => 'de_DE'],
]);
$collection = (new Collection)->sites(['en', 'fr', 'de'])->handle('blog')->routes([
'en' => 'blog/{slug}',
'fr' => 'le-blog/{slug}',
'de' => 'das-blog/{slug}',
]);
$collection->save();
$entryEn = (new Entry)->collection($collection)->locale('en')->slug('foo');
$entryFr = (new Entry)->collection($collection)->locale('fr')->slug('le-foo');
$entryDe = (new Entry)->collection($collection)->locale('de')->slug('das-foo');
$redirectEntry = (new Entry)->collection($collection)->locale('en')->slug('redirected')->set('redirect', 'http://example.com/page');
$redirect404Entry = (new Entry)->collection($collection)->locale('en')->slug('redirect-404')->set('redirect', '404');
$this->assertEquals('/blog/foo', $entryEn->uri());
$this->assertEquals('/blog/foo', $entryEn->url());
$this->assertEquals('/blog/foo', $entryEn->urlWithoutRedirect());
$this->assertEquals('http://domain.com/blog/foo', $entryEn->absoluteUrl());
$this->assertEquals('http://domain.com/blog/foo', $entryEn->absoluteUrlWithoutRedirect());
$this->assertNull($entryEn->redirectUrl());
$this->assertEquals('/le-blog/le-foo', $entryFr->uri());
$this->assertEquals('/fr/le-blog/le-foo', $entryFr->url());
$this->assertEquals('/fr/le-blog/le-foo', $entryFr->urlWithoutRedirect());
$this->assertEquals('http://domain.com/fr/le-blog/le-foo', $entryFr->absoluteUrl());
$this->assertEquals('http://domain.com/fr/le-blog/le-foo', $entryFr->absoluteUrlWithoutRedirect());
$this->assertNull($entryFr->redirectUrl());
$this->assertEquals('/das-blog/das-foo', $entryDe->uri());
$this->assertEquals('/das-blog/das-foo', $entryDe->url());
$this->assertEquals('/das-blog/das-foo', $entryDe->urlWithoutRedirect());
$this->assertEquals('http://domain.de/das-blog/das-foo', $entryDe->absoluteUrl());
$this->assertEquals('http://domain.de/das-blog/das-foo', $entryDe->absoluteUrlWithoutRedirect());
$this->assertNull($entryDe->redirectUrl());
$this->assertEquals('/blog/redirected', $redirectEntry->uri());
$this->assertEquals('http://example.com/page', $redirectEntry->url());
$this->assertEquals('/blog/redirected', $redirectEntry->urlWithoutRedirect());
$this->assertEquals('http://example.com/page', $redirectEntry->absoluteUrl());
$this->assertEquals('http://domain.com/blog/redirected', $redirectEntry->absoluteUrlWithoutRedirect());
$this->assertEquals('http://example.com/page', $redirectEntry->redirectUrl());
$this->assertEquals('/blog/redirect-404', $redirect404Entry->uri());
$this->assertEquals('/blog/redirect-404', $redirect404Entry->url());
$this->assertEquals('/blog/redirect-404', $redirect404Entry->urlWithoutRedirect());
$this->assertEquals('http://domain.com/blog/redirect-404', $redirect404Entry->absoluteUrl());
$this->assertEquals('http://domain.com/blog/redirect-404', $redirect404Entry->absoluteUrlWithoutRedirect());
$this->assertEquals(404, $redirect404Entry->redirectUrl());
}
#[Test]
public function it_gets_the_uri_from_the_structure()
{
$structure = $this->partialMock(CollectionStructure::class);
$collection = tap((new Collection)->handle('test')->structure($structure)->routes('{parent_uri}/{slug}'))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('foo');
$structure->shouldReceive('entryUri')->with($entry)->once()->andReturn('/structured-uri');
$this->assertEquals('/structured-uri', $entry->uri());
}
#[Test]
public function entries_in_a_collection_without_a_route_dont_have_a_uri()
{
$collection = tap((new Collection)->handle('test'))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('foo');
$this->assertNull($entry->uri());
$this->assertNull($entry->url());
}
#[Test]
public function a_localized_entry_without_a_route_for_that_site_doesnt_have_a_uri()
{
$collection = tap((new Collection)->handle('test')->routes([
'en' => '/test/{slug}',
]))->save();
$entry = (new Entry)->collection($collection)->locale('fr')->slug('foo');
$this->assertNull($entry->uri());
$this->assertNull($entry->url());
}
#[Test]
public function entries_in_a_structured_collection_without_a_route_dont_have_a_uri()
{
$structure = $this->partialMock(CollectionStructure::class);
$collection = tap((new Collection)->handle('test')->structure($structure))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('foo');
$this->assertNull($entry->uri());
$this->assertNull($entry->url());
}
#[Test]
public function a_localized_entry_in_a_structured_collection_without_a_route_for_that_site_doesnt_have_a_uri()
{
$structure = $this->partialMock(CollectionStructure::class);
$collection = tap((new Collection)->handle('test')->structure($structure)->routes([
'en' => '/test/{slug}',
]))->save();
$entry = (new Entry)->collection($collection)->locale('fr')->slug('foo');
$this->assertNull($entry->uri());
$this->assertNull($entry->url());
}
#[Test]
#[DataProvider('firstChildRedirectProvider')]
public function it_gets_urls_for_first_child_redirects($value)
{
$this->setSites([
'en' => ['url' => 'http://domain.com/', 'locale' => 'en_US'],
]);
$collection = tap((new Collection)->handle('pages')->routes('{parent_uri}/{slug}'))->save();
$parent = tap((new Entry)->id('1')->locale('en')->collection($collection)->slug('parent')->set('redirect', $value))->save();
$child = tap((new Entry)->id('2')->locale('en')->collection($collection)->slug('child'))->save();
$noChildren = tap((new Entry)->id('3')->locale('en')->collection($collection)->slug('nochildren')->set('redirect', $value))->save();
$collection->structureContents([
'expects_root' => false, // irrelevant. just can't pass an empty array at the moment.
])->save();
$collection->structure()->in('en')->tree(
[
[
'entry' => '1',
'children' => [
['entry' => '2'],
],
],
[
'entry' => '3',
],
]
)->save();
$this->assertEquals('/parent', $parent->uri());
$this->assertEquals('/parent/child', $parent->url());
$this->assertEquals('/parent', $parent->urlWithoutRedirect());
$this->assertEquals('http://domain.com/parent/child', $parent->absoluteUrl());
$this->assertEquals('http://domain.com/parent', $parent->absoluteUrlWithoutRedirect());
$this->assertEquals('/parent/child', $parent->redirectUrl());
$this->assertEquals('/parent/child', $child->uri());
$this->assertEquals('/parent/child', $child->url());
$this->assertEquals('/parent/child', $child->urlWithoutRedirect());
$this->assertEquals('http://domain.com/parent/child', $child->absoluteUrl());
$this->assertEquals('http://domain.com/parent/child', $child->absoluteUrlWithoutRedirect());
$this->assertNull($child->redirectUrl());
$this->assertEquals('/nochildren', $noChildren->uri());
$this->assertEquals('/nochildren', $noChildren->url());
$this->assertEquals('/nochildren', $noChildren->urlWithoutRedirect());
$this->assertEquals('http://domain.com/nochildren', $noChildren->absoluteUrl());
$this->assertEquals('http://domain.com/nochildren', $noChildren->absoluteUrlWithoutRedirect());
$this->assertEquals(404, $noChildren->redirectUrl());
}
public static function firstChildRedirectProvider()
{
return [
'string' => ['@child'],
'array' => [['url' => '@child']],
];
}
#[Test]
public function it_gets_and_sets_supplemental_data()
{
$entry = new Entry;
$this->assertEquals([], $entry->supplements()->all());
$return = $entry->setSupplement('foo', 'bar')->setSupplement('baz', null);
$this->assertEquals($entry, $return);
$this->assertEquals('bar', $entry->getSupplement('foo'));
$this->assertNull($entry->getSupplement('bar'));
$this->assertNull($entry->getSupplement('baz'));
$this->assertTrue($entry->hasSupplement('foo'));
$this->assertFalse($entry->hasSupplement('bar'));
$this->assertTrue($entry->hasSupplement('baz'));
$this->assertEquals(['foo' => 'bar', 'baz' => null], $entry->supplements()->all());
}
#[Test]
public function it_compiles_augmented_array_data()
{
// The values of the augmented array are tested in AugmentedEntryTest
$entry = (new Entry)
->collection(Collection::make('blog')->routes('blog/{slug}')->save());
$this->assertInstanceOf(Augmentable::class, $entry);
$this->assertInstanceOf(AugmentedEntry::class, $entry->newAugmentedInstance());
$this->assertInstanceOf(AugmentedCollection::class, $entry->toAugmentedCollection());
}
#[Test]
public function setting_queried_keys_will_filter_the_arrayable_array()
{
$entry = (new Entry)
->id('test-id')
->locale('en')
->slug('test')
->set('foo', 'bar')
->collection(Collection::make('blog')->save());
$arr = $entry->toAugmentedArray();
$this->assertTrue(count($arr) > 3);
$this->assertArraySubset([
'id' => 'test-id',
'foo' => 'bar',
'published' => true,
], $arr);
$return = $entry->selectedQueryColumns(['id', 'foo']);
$this->assertEquals($entry, $return);
$this->assertEquals(['id', 'foo'], $entry->selectedQueryColumns());
$this->assertEquals([
'id' => 'test-id',
'foo' => 'bar',
], $entry->toAugmentedArray());
}
#[Test]
public function it_converts_to_an_array()
{
$fieldtype = new class extends Fieldtype
{
protected static $handle = 'test';
public function augment($value)
{
return [
new Value('alfa'),
new Value([
new Value('bravo'),
new Value('charlie'),
'delta',
]),
];
}
};
$fieldtype::register();
$blueprint = Blueprint::makeFromFields([
'baz' => [
'type' => 'test',
],
]);
BlueprintRepository::shouldReceive('in')->with('collections/blog')->andReturn(collect([
'post' => $blueprint->setHandle('post'),
]));
$entry = (new Entry)
->id('test-id')
->locale('en')
->slug('test')
->set('foo', 'bar')
->set('baz', 'qux')
->collection(Collection::make('blog')->save());
$this->assertInstanceOf(Arrayable::class, $entry);
$array = $entry->toArray();
$this->assertEquals($entry->augmented()->keys(), array_keys($array));
$this->assertEquals([
'alfa',
[
'bravo',
'charlie',
'delta',
],
], $array['baz'], 'Value objects are not resolved recursively');
$array = $entry
->selectedQueryColumns($keys = ['id', 'foo', 'baz'])
->toArray();
$this->assertEquals($keys, array_keys($array), 'toArray keys differ from selectedQueryColumns');
}
#[Test]
public function only_requested_relationship_fields_are_included_in_to_array()
{
$regularFieldtype = new class extends Fieldtype
{
protected static $handle = 'regular';
public function augment($value)
{
return 'augmented '.$value;
}
};
$regularFieldtype::register();
$relationshipFieldtype = new class extends Fieldtype
{
protected static $handle = 'relationship';
protected $relationship = true;
public function augment($values)
{
return collect($values)->map(fn ($value) => 'augmented '.$value)->all();
}
};
$relationshipFieldtype::register();
$blueprint = Blueprint::makeFromFields([
'alfa' => ['type' => 'regular'],
'bravo' => ['type' => 'relationship'],
'charlie' => ['type' => 'relationship'],
]);
BlueprintRepository::shouldReceive('in')->with('collections/blog')->andReturn(collect([
'post' => $blueprint->setHandle('post'),
]));
$entry = (new Entry)
->id('test-id')
->locale('en')
->slug('test')
->set('alfa', 'one')
->set('bravo', ['a', 'b'])
->set('charlie', ['c', 'd'])
->collection(Collection::make('blog')->save());
$this->assertEquals([
'alfa' => 'augmented one',
'bravo' => ['a', 'b'],
'charlie' => ['augmented c', 'augmented d'],
], Arr::only($entry->selectedQueryRelations(['charlie'])->toArray(), ['alfa', 'bravo', 'charlie']));
}
#[Test]
public function it_gets_and_sets_initial_path()
{
$entry = new Entry;
$this->assertNull($entry->initialPath());
$return = $entry->initialPath('123');
$this->assertEquals($entry, $return);
$this->assertEquals('123', $entry->initialPath());
}
#[Test]
public function it_gets_the_path_and_excludes_locale_when_theres_a_single_site()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US'],
]);
$collection = tap(Facades\Collection::make('blog')->dated(true))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('post');
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/post.md', $entry->path());
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/2018-01-02.post.md', $entry->date('2018-01-02')->path());
}
#[Test]
public function it_gets_the_path_and_includes_locale_when_theres_multiple_sites()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US'],
'fr' => ['url' => '/', 'locale' => 'fr_FR'],
]);
$collection = tap(Facades\Collection::make('blog')->dated(true))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('post');
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/en/post.md', $entry->path());
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/en/2018-01-02.post.md', $entry->date('2018-01-02')->path());
}
#[Test]
public function the_path_uses_the_slug_if_set_even_if_slugs_arent_required()
{
$collection = tap(Facades\Collection::make('blog')->requiresSlugs(false))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug('post')->id('123');
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/post.md', $entry->path());
}
#[Test]
public function the_path_uses_the_id_if_slug_is_not_set()
{
$collection = tap(Facades\Collection::make('blog')->requiresSlugs(false))->save();
$entry = (new Entry)->collection($collection)->locale('en')->slug(null)->id('123');
$this->assertEquals($this->fakeStacheDirectory.'/content/collections/blog/123.md', $entry->path());
}
#[Test]
#[DataProvider('dateCollectionEntriesProvider')]
public function it_gets_dates_for_dated_collection_entries(
$setDate,
$enableTimeInBlueprint,
$enableSecondsInBlueprint,
$expectedDate,
$expectedToHaveTime,
$expectedToHaveSeconds,
$expectedPath,
) {
Carbon::setTestNow(Carbon::parse('2015-09-24 13:45:23'));
$fields = [];
if ($enableTimeInBlueprint) {
$fields['date'] = ['type' => 'date', 'time_enabled' => $enableTimeInBlueprint, 'time_seconds_enabled' => $enableSecondsInBlueprint];
}
$blueprint = Blueprint::makeFromFields($fields);
BlueprintRepository::shouldReceive('in')->with('collections/test')->andReturn(collect([
'test' => $blueprint->setHandle('test'),
]));
$collection = tap(Facades\Collection::make('test')->dated(true))->save();
$entry = (new Entry)->collection($collection)->slug('foo');
if ($setDate) {
$entry->date($setDate);
}
$this->assertTrue($entry->hasDate());
$this->assertEquals($expectedDate, $entry->date()->format('Y-m-d H:i:s'));
$this->assertEquals($expectedToHaveTime, $entry->hasTime());
$this->assertEquals($expectedToHaveSeconds, $entry->hasSeconds());
$this->assertEquals($expectedPath, pathinfo($entry->path(), PATHINFO_FILENAME));
}
public static function dateCollectionEntriesProvider()
{
return [
'no date explicitly set, time not explicitly enabled' => [null, null, null, '2015-09-24 00:00:00', false, false, 'foo'], // By default, the date field added to dated collection blueprints does not have time enabled.
'no date explicitly set, time enabled, seconds enabled' => [null, true, true, '2015-09-24 13:45:23', true, true, 'foo'],
'no date explicitly set, time enabled, seconds disabled' => [null, true, false, '2015-09-24 13:45:00', true, false, 'foo'], // Seconds are disabled, so they should be zeroed out.
'date set, time not explicitly enabled' => ['2023-04-19', null, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'],
'date set, time enabled, seconds enabled' => ['2023-04-19', true, true, '2023-04-19 00:00:00', true, true, '2023-04-19-000000.foo'],
'date set, time enabled, seconds disabled' => ['2023-04-19', true, false, '2023-04-19 00:00:00', true, false, '2023-04-19-0000.foo'],
'datetime set, time not explicitly enabled' => ['2023-04-19-1425', null, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'], // Time is not enabled, so it should be zeroed out.
'datetime set, time enabled, seconds enabled' => ['2023-04-19-1425', true, true, '2023-04-19 14:25:00', true, true, '2023-04-19-142500.foo'],
'datetime set, time enabled, seconds disabled' => ['2023-04-19-1425', true, false, '2023-04-19 14:25:00', true, false, '2023-04-19-1425.foo'],
'datetime with seconds set, time not explicitly enabled' => ['2023-04-19-142512', null, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'], // Time is not enabled, so it should be zeroed out.
'datetime with seconds set, time enabled, seconds enabled' => ['2023-04-19-142512', true, true, '2023-04-19 14:25:12', true, true, '2023-04-19-142512.foo'],
'datetime with seconds set, time enabled, seconds disabled' => ['2023-04-19-142512', true, false, '2023-04-19 14:25:00', true, false, '2023-04-19-1425.foo'], // Seconds are disabled, so they should be zeroed out.
'date set, time disabled' => ['2023-04-19', false, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'],
'date set, time disabled, seconds enabled' => ['2023-04-19', false, true, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'], // Time is disabled, so seconds should be disabled too.
'datetime set, time disabled' => ['2023-04-19-1425', false, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'],
'datetime set, time disabled, seconds enabled' => ['2023-04-19-1425', false, true, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'], // Time is disabled, so seconds should be disabled too.
'datetime with seconds set, time disabled' => ['2023-04-19-142512', false, null, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'],
'datetime with seconds set, time disabled, seconds enabled' => ['2023-04-19-142512', false, true, '2023-04-19 00:00:00', false, false, '2023-04-19.foo'], // Time is disabled, so seconds should be disabled too.
'date explicitly set in another timezone' => [Carbon::parse('2025-03-07 22:00', 'America/New_York'), false, false, '2025-03-08 03:00:00', true, false, '2025-03-08-0300.foo'], // Passing in a carbon instance will adjust from its timezone