-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathFrontendTest.php
More file actions
1080 lines (874 loc) · 35.9 KB
/
FrontendTest.php
File metadata and controls
1080 lines (874 loc) · 35.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
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;
use Facades\Statamic\CP\LivePreview;
use Facades\Statamic\Routing\ResolveRedirect;
use Facades\Tests\Factories\EntryFactory;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Auth\Protect\ProtectorManager;
use Statamic\Auth\Protect\Protectors\Protector;
use Statamic\Events\ResponseCreated;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Cascade;
use Statamic\Facades\Collection;
use Statamic\Facades\User;
use Statamic\Tags\Tags;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
class FrontendTest extends TestCase
{
use FakesContent;
use FakesRoles;
use FakesViews;
use PreventSavingStacheItemsToDisk;
public function setUp(): void
{
parent::setUp();
$this->withStandardFakeViews();
}
private function withStandardBlueprints()
{
Blueprint::shouldReceive('in')->withAnyArgs()->andReturn(collect([new \Statamic\Fields\Blueprint]));
}
#[Test]
public function page_is_displayed()
{
$this->withStandardBlueprints();
$this->withoutExceptionHandling();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');
$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);
$response = $this->get('/about')
->assertStatus(200)
->assertHeaderMissing('X-Statamic-Draft');
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}
#[Test]
public function page_is_displayed_with_query_string()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');
$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);
$response = $this->get('/about?some=querystring')->assertStatus(200);
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}
#[Test]
public function page_is_displayed_with_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');
$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);
$response = $this->get('/about/')->assertStatus(200);
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}
#[Test]
public function page_is_displayed_with_query_string_and_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');
$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);
$response = $this->get('/about/?some=querystring')->assertStatus(200);
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}
#[Test]
public function page_with_no_explicit_layout_will_not_use_a_layout()
{
$this->withStandardBlueprints();
$this->withoutExceptionHandling();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', 'Layout {{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');
$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
'layout' => false,
],
]);
$response = $this->get('/about')->assertStatus(200);
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
$response->assertDontSee('Layout');
}
#[Test]
public function home_page_on_second_subdirectory_based_site_is_displayed()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]);
$this->createHomePagesForTwoSites();
$response = $this->get('/fr')->assertStatus(200);
$this->assertEquals('French Home', trim($response->content()));
}
#[Test]
public function home_page_on_second_subdirectory_based_site_is_displayed_with_ending_slash()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]);
$this->createHomePagesForTwoSites();
$response = $this->get('/fr/')->assertStatus(200);
$this->assertEquals('French Home', trim($response->content()));
}
#[Test]
public function home_page_on_second_domain_site_is_displayed()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]);
$this->createHomePagesForTwoSites();
$response = $this->get('http://anotherhost.com')->assertStatus(200);
$this->assertEquals('French Home', trim($response->content()));
}
#[Test]
public function home_page_on_second_domain_site_is_displayed_with_ending_slash()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]);
$this->createHomePagesForTwoSites();
$response = $this->get('http://anotherhost.com/')->assertStatus(200);
$this->assertEquals('French Home', trim($response->content()));
}
private function createHomePagesForTwoSites()
{
$this->withStandardBlueprints();
$this->withoutExceptionHandling();
$this->withStandardFakeViews();
$c = tap(Collection::make('pages')->sites(['english', 'french'])->routes('{slug}')->structureContents(['root' => true]))->save();
EntryFactory::id('1')->locale('english')->slug('home')->collection('pages')->data(['content' => 'Home'])->create();
EntryFactory::id('2')->locale('french')->slug('french-home')->collection('pages')->data(['content' => 'French Home'])->create();
$c->structure()->in('english')->tree([['entry' => '1']])->save();
$c->structure()->in('french')->tree([['entry' => '2']])->save();
}
#[Test]
public function drafts_are_not_visible()
{
$this->withStandardFakeErrorViews();
$this->createPage('about')->published(false)->save();
$this->get('/about')->assertStatus(404);
}
#[Test]
public function drafts_are_visible_if_using_live_preview()
{
$this->withStandardBlueprints();
$page = tap($this->createPage('about')->published(false)->set('content', 'Testing 123'))->save();
LivePreview::tokenize('test-token', $page);
$response = $this
->get('/about?token=test-token')
->assertStatus(200)
->assertHeader('X-Statamic-Draft', true);
$this->assertEquals('Testing 123', $response->content());
}
#[Test]
public function drafts_are_not_visible_if_using_live_preview_token_for_different_entry()
{
$this->withStandardFakeErrorViews();
$page = tap($this->createPage('about')->published(false)->set('content', 'Testing 123'))->save();
$other = $this->createPage('other');
LivePreview::tokenize('test-token', $other);
$this
->get('/about?token=test-token')
->assertStatus(404);
}
#[Test]
public function drafts_dont_get_statically_cached()
{
$this->markTestIncomplete();
}
#[Test]
public function future_private_entries_are_not_viewable()
{
Carbon::setTestNow(Carbon::parse('2019-01-01'));
$this->withStandardFakeErrorViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRendered('default', 'The template contents');
$collection = tap($this->makeCollection()->dated(true))->save();
tap($this->makePage('about')->date('2019-01-02'))->save();
$this
->get('/about')
->assertStatus(200)
->assertSee('The template contents');
tap($collection->futureDateBehavior('private'))->save();
$this
->get('/about')
->assertStatus(404);
}
#[Test]
public function future_private_entries_viewable_in_live_preview()
{
Carbon::setTestNow(Carbon::parse('2019-01-01'));
$this->withStandardFakeErrorViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRendered('default', 'The template contents');
tap($this->makeCollection()->dated(true)->futureDateBehavior('private'))->save();
$page = tap($this->makePage('about')->date('2019-01-02'))->save();
LivePreview::tokenize('test-token', $page);
$this
->get('/about?token=test-token')
->assertStatus(200)
->assertHeader('X-Statamic-Private', true);
}
#[Test]
public function future_private_entries_dont_get_statically_cached()
{
$this->markTestIncomplete();
}
#[Test]
public function past_private_entries_are_not_viewable()
{
Carbon::setTestNow(Carbon::parse('2019-01-01'));
$this->withStandardFakeErrorViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRendered('default', 'The template contents');
$collection = tap($this->makeCollection()->dated(true))->save();
tap($this->makePage('about')->date('2018-01-01'))->save();
$this
->get('/about')
->assertStatus(200)
->assertSee('The template contents');
tap($collection->pastDateBehavior('private'))->save();
$this
->get('/about')
->assertStatus(404);
}
#[Test]
public function past_private_entries_are_viewable_in_live_preview()
{
Carbon::setTestNow(Carbon::parse('2019-01-01'));
$this->withStandardFakeErrorViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRendered('default', 'The template contents');
tap($this->makeCollection()->dated(true)->pastDateBehavior('private'))->save();
$page = tap($this->makePage('about')->date('2018-01-01'))->save();
LivePreview::tokenize('test-token', $page);
$this
->get('/about?token=test-token')
->assertStatus(200)
->assertHeader('X-Statamic-Private', true);
}
#[Test]
public function past_private_entries_dont_get_statically_cached()
{
$this->markTestIncomplete();
}
#[Test]
public function header_is_added_to_protected_responses()
{
$page = $this->createPage('about');
$this
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');
$page->set('protect', 'logged_in')->save();
$this
->actingAs(User::make())
->get('/about')
->assertOk()
->assertHeader('X-Statamic-Protected', true);
}
#[Test]
public function header_is_not_added_to_cacheable_protected_responses()
{
// config(['statamic.protect.default' => 'test']);
config(['statamic.protect.schemes.test' => [
'driver' => 'test',
]]);
app(ProtectorManager::class)->extend('test', function ($app) {
return new class() extends Protector
{
public function protect()
{
//
}
public function cacheable()
{
return true;
}
};
});
$page = $this->createPage('about');
$this
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');
$page->set('protect', 'test')->save();
$this
->actingAs(User::make())
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');
}
#[Test]
public function key_variables_key_added()
{
$page = $this->createPage('about');
$response = $this->get('about')->assertStatus(200);
$keys = [
'site', 'homepage', 'current_url', 'current_uri', 'current_date', 'now', 'today',
'get', 'post', 'get_post', 'old', 'response_code',
'logged_in', 'logged_out', 'current_user', 'environment', 'xml_header', 'csrf_token', 'csrf_field', 'config',
];
$cascade = $this->app['Statamic\View\Cascade']->toArray();
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $cascade);
}
}
#[Test]
public function fields_gets_augmented()
{
$this->withoutExceptionHandling();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('default', '{{ augment_me }}{{ dont_augment_me }}');
$blueprint = Blueprint::makeFromFields([
'augment_me' => ['type' => 'markdown'],
])->setHandle('test');
Blueprint::shouldReceive('in')->with('collections/pages')->once()->andReturn(collect([$blueprint]));
$this->createPage('about', [
'path' => 'about.md',
'with' => [
'blueprint' => 'test',
'augment_me' => '# Foo *Bar*',
'dont_augment_me' => '# Foo *Bar*',
],
]);
$response = $this->get('about');
$this->assertEquals("<h1>Foo <em>Bar</em></h1>\n# Foo *Bar*", StringUtilities::normalizeLineEndings(trim($response->content())));
}
#[Test]
public function changes_content_type_to_xml()
{
$this->createPage('about', ['with' => ['content_type' => 'xml']]);
// Symfony adds utf-8 if the content-type starts with text/
$this->get('about')->assertContentType('text/xml; charset=utf-8');
}
#[Test]
public function changes_content_type_to_atom()
{
$this->createPage('about', ['with' => ['content_type' => 'atom']]);
// Symfony adds utf-8 if the content-type starts with text/
$this->get('about')->assertContentType('application/atom+xml; charset=utf-8');
}
#[Test]
public function changes_content_type_to_json()
{
$this->createPage('about', ['with' => ['content_type' => 'json']]);
$this->get('about')->assertContentType('application/json');
}
#[Test]
public function changes_content_type_to_text()
{
$this->createPage('about', ['with' => ['content_type' => 'text']]);
// Symfony adds utf-8 if the content-type starts with text/
$this->get('about')->assertContentType('text/plain; charset=utf-8');
}
#[Test]
public function xml_antlers_template_with_xml_layout_will_use_both_and_change_the_content_type()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<?xml ?>{{ template_content }}', 'antlers.xml');
$this->viewShouldReturnRaw('feed', '<foo></foo>', 'antlers.xml');
$this->createPage('about', ['with' => ['template' => 'feed']]);
$response = $this
->get('about')
->assertContentType('text/xml; charset=utf-8');
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
}
#[Test]
public function xml_antlers_template_with_non_xml_layout_will_change_content_type_but_avoid_using_the_layout()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<html>{{ template_content }}</html>', 'antlers.html');
$this->viewShouldReturnRaw('feed', '<foo></foo>', 'antlers.xml');
$this->createPage('about', ['with' => ['template' => 'feed']]);
$response = $this
->get('about')
->assertContentType('text/xml; charset=utf-8');
$this->assertEquals('<foo></foo>', $response->getContent());
}
#[Test]
public function xml_antlers_layout_will_change_the_content_type()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<?xml ?>{{ template_content }}', 'antlers.xml');
$this->viewShouldReturnRaw('feed', '<foo></foo>', 'antlers.html');
$this->createPage('about', ['with' => ['template' => 'feed']]);
$response = $this
->get('about')
->assertContentType('text/xml; charset=utf-8');
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
}
#[Test]
public function xml_blade_template_will_not_change_content_type()
{
// Blade doesnt support xml files, but even if it did,
// we only want it to happen when using Antlers.
$this->withFakeViews();
$this->viewShouldReturnRaw('feed', '<foo></foo>', 'blade.xml');
$this->createPage('about', ['with' => ['template' => 'feed']]);
$response = $this
->get('about')
->assertContentType('text/html; charset=utf-8');
$this->assertEquals('<foo></foo>', $response->getContent());
}
#[Test]
public function xml_template_with_custom_content_type_does_not_change_to_xml()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<?xml ?>{{ template_content }}', 'antlers.xml');
$this->viewShouldReturnRaw('feed', '<foo></foo>', 'antlers.xml');
$this->createPage('about', ['with' => ['template' => 'feed', 'content_type' => 'json']]);
$this
->get('about')
->assertContentType('application/json');
}
#[Test]
public function sends_powered_by_header_if_enabled()
{
config(['statamic.system.send_powered_by_header' => true]);
$this->createPage('about');
$this->get('about')->assertHeader('X-Powered-By', 'Statamic');
}
#[Test]
public function doesnt_send_powered_by_header_if_disabled()
{
config(['statamic.system.send_powered_by_header' => false]);
$this->createPage('about');
$this->get('about')->assertHeaderMissing('X-Powered-By', 'Statamic');
}
#[Test]
public function disables_floc_through_header_by_default()
{
$this->createPage('about');
$this->get('about')->assertHeader('Permissions-Policy', 'interest-cohort=()');
}
#[Test]
public function doesnt_disable_floc_through_header_if_disabled()
{
config(['statamic.system.disable_floc' => false]);
$this->createPage('about');
$this->get('about')->assertHeaderMissing('Permissions-Policy', 'interest-cohort=()');
}
#[Test]
public function headers_can_be_set_in_content()
{
$page = $this->createPage('about', ['with' => [
'headers' => [
'X-Some-Header' => 'Foo',
'X-Another-Header' => 'Bar',
],
]]);
$this->get('about')
->assertHeader('X-Some-Header', 'Foo')
->assertHeader('X-Another-Header', 'Bar');
}
#[Test]
public function event_is_emitted_when_response_is_created()
{
Event::fake([ResponseCreated::class]);
$this->createPage('about')->set('headers', ['X-Foo' => 'Bar'])->save();
$this->get('about')->assertStatus(200);
Event::assertDispatched(ResponseCreated::class, function ($event) {
return $event->response instanceof Response
&& $event->response->headers->has('X-Foo');
});
}
#[Test]
public function amp_requests_load_their_amp_directory_counterparts()
{
$this->markTestIncomplete();
}
#[Test]
public function amp_requests_without_an_amp_template_result_in_a_404()
{
$this->markTestIncomplete();
}
#[Test]
public function routes_pointing_to_controllers_should_render()
{
$this->markTestIncomplete();
}
#[Test]
public function routes_pointing_to_invalid_controller_should_render_404()
{
$this->markTestIncomplete();
}
#[Test]
public function a_redirect_key_in_the_page_data_should_redirect()
{
$this->markTestIncomplete();
}
#[Test]
public function a_redirect_key_with_a_404_value_should_404()
{
$this->markTestIncomplete();
}
#[Test]
public function a_redirect_key_with_an_entry_should_redirect_to_the_entry()
{
$this->markTestIncomplete();
}
#[Test]
public function a_redirect_key_with_an_unknown_entry_should_404()
{
$this->markTestIncomplete();
}
#[Test]
public function debug_bar_shows_cascade_variables_if_enabled()
{
$this->markTestIncomplete();
}
#[Test]
public function the_404_page_is_treated_like_a_template()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('errors.404', 'Not found {{ response_code }} {{ site:handle }}');
$this->get('unknown')->assertNotFound()->assertSee('Not found 404 en');
$this->assertEquals(404, Cascade::get('response_code'));
// todo: test cascade vars are in the debugbar
}
#[Test]
public function it_sets_the_translation_locale_based_on_site()
{
app('translator')->addNamespace('test', __DIR__.'/__fixtures__/lang');
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]);
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<p>{{ trans key="test::messages.hello" }}</p>');
$this->makeCollection()->sites(['english', 'french'])->save();
tap($this->makePage('about', ['with' => ['template' => 'some_template']])->locale('english'))->save();
tap($this->makePage('le-about', ['with' => ['template' => 'some_template']])->locale('french'))->save();
$this->get('/about')->assertSee('Hello');
$this->get('/fr/le-about')->assertSee('Bonjour');
}
#[Test]
public function it_sets_the_carbon_to_string_format()
{
config(['statamic.system.date_format' => 'd/m/Y']);
Date::setTestNow('October 21st, 2022');
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<p>{{ now }}</p>');
$this->makeCollection()->save();
tap($this->makePage('about', ['with' => ['template' => 'some_template']]))->save();
$this->assertDefaultCarbonFormat();
$this->get('/about')->assertSee('21/10/2022');
$this->assertDefaultCarbonFormat();
}
#[Test]
public function it_sets_the_locale()
{
// You can only set the locale to one that is actually installed on the server.
// The names are a little different across jobs in the GitHub actions matrix.
// We'll test against whichever was successfully applied. Finally, we will
// reset the locale back to the original state to start the test clean.
$locales = ['fr_FR', 'fr_FR.utf-8', 'fr_FR.UTF-8', 'french'];
$originalLocale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, $locales);
$frLocale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, $originalLocale);
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en', 'lang' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => $frLocale, 'lang' => 'fr'],
]);
(new class extends Tags
{
public static $handle = 'php_locale';
public function index()
{
return setlocale(LC_TIME, 0);
}
})->register();
(new class extends Tags
{
public static $handle = 'laravel_locale';
public function index()
{
return app()->getLocale();
}
})->register();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', 'PHP Locale: {{ php_locale }} App Locale: {{ laravel_locale }}');
$this->makeCollection()->sites(['english', 'french'])->save();
tap($this->makePage('about', ['with' => ['template' => 'some_template']])->locale('english'))->save();
tap($this->makePage('le-about', ['with' => ['template' => 'some_template']])->locale('french'))->save();
$this->assertEquals('en', app()->getLocale());
$this->assertEquals($originalLocale, setlocale(LC_TIME, 0));
$this->get('/fr/le-about')->assertSeeInOrder([
'PHP Locale: '.$frLocale,
'App Locale: fr',
]);
$this->assertEquals('en', app()->getLocale());
$this->assertEquals($originalLocale, setlocale(LC_TIME, 0));
}
private function assertDefaultCarbonFormat()
{
$this->assertEquals(
Date::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT),
(string) Date::now(),
'Carbon was not formatted using the default format.'
);
}
/**
* @see https://github.com/statamic/cms/issues/1537
**/
#[Test]
public function home_page_is_not_overridden_by_entries_in_another_structured_collection_with_no_url()
{
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('default', '<h1>{{ title }}</h1>');
// The bug would happen if the non-routable collection happened to be created first. It's not
// really specific to the naming. However when reading from files, it goes in alphabetical
// order which makes it seem like it could be an alphabetical problem.
$c = tap(Collection::make('services')->structureContents(['root' => true]))->save();
$c->structure()->in('en')->tree([['entry' => '2']])->save();
$c = tap(Collection::make('pages')->routes('{slug}')->structureContents(['root' => true]))->save();
$c->structure()->in('en')->tree([['entry' => '1']])->save();
EntryFactory::id('1')->slug('service')->collection('services')->data(['title' => 'Service'])->create();
EntryFactory::id('2')->slug('home')->collection('pages')->data(['title' => 'Home'])->create();
// Before the fix, you'd see "Service" instead of "Home", because the URI would also be /
$this->get('/')->assertSee('Home');
}
#[Test]
#[DataProvider('redirectProvider')]
public function redirect_is_followed($dataValue, $augmentedValue, $expectedStatus, $expectedLocation)
{
// Making a fake fieldtype to test that the augmented value is used for the redirect.
// The actual redirect resolving logic is already completely under test, and happens
// in the "link" fieldtype's augment method.
app()->bind('test-augmented-value', fn () => $augmentedValue);
(new class($augmentedValue) extends \Statamic\Fields\Fieldtype
{
protected static $handle = 'fake_link';
public function augment($value)
{
return app('test-augmented-value');
}
})->register();
$blueprint = Blueprint::makeFromFields(['redirect' => ['type' => 'fake_link']]);
Blueprint::shouldReceive('in')->with('collections/pages')->andReturn(collect([$blueprint]));
$this->createPage('about', [
'with' => [
'title' => 'About',
'redirect' => $dataValue, // this should not be used - the augmented value should.
],
])->save();
$response = $this->get('/about');
if ($expectedStatus === 302) {
$response->assertRedirect($expectedLocation);
} elseif ($expectedStatus === 200) {
$response->assertOk();
} elseif ($expectedStatus === 404) {
$response->assertNotFound();
} else {
throw new \Exception('Test not set up to handle status code: '.$expectedStatus);
}
}
public static function redirectProvider()
{
return [
'valid redirect' => [
'/shouldnt-be-used', // its got a value
'/target', // the fieldtype will augment to this
302, // its a redirect
'/target', // to here
],
'invalid redirect' => [
'something', // its got a value
null, // the fieldtype will augment to this because its an invalid reference
404, // so it should 404
null, // and not redirect
],
'missing redirect' => [
null, // its got no value
null, // the fieldtype will augment to this (although it wouldn't even be called)
200, // since there's no redirect, its a successful response
null, // and not a redirect
],
];
}
#[Test]
#[DataProvider('redirectProviderNoBlueprintProvider')]
public function redirect_is_followed_when_no_field_is_present_in_blueprint(
$dataValue,
$shouldResolve,
$resolvedValue,
$expectedStatus,
$expectedLocation
) {
$entry = tap($this->createPage('about', [
'with' => [
'title' => 'About',
'redirect' => $dataValue,
],
]))->save();
$mock = ResolveRedirect::shouldReceive('resolve');
if ($shouldResolve) {
$mock->with($dataValue, $entry)->andReturn($resolvedValue)->once();
} else {
$mock->never();
}
$response = $this->get('/about');
if ($expectedStatus === 302) {
$response->assertRedirect($expectedLocation);
} elseif ($expectedStatus === 200) {
$response->assertOk();
} elseif ($expectedStatus === 404) {
$response->assertNotFound();
} else {
throw new \Exception('Test not set up to handle status code: '.$expectedStatus);
}
}
public static function redirectProviderNoBlueprintProvider()
{
return [
'valid redirect' => [
// A valid redirect could be a literal URL, "@child", "entry::id", etc.
// It's irrelevant for this test since we're mocking the resolver.
'something', // the value
true, // the resolver will run, getting the above value
'/target', // and return this.
302, // its a redirect
'/target', // to here.
],
'missing redirect' => [
null, // its got no value
false, // so the resolver will not be called at all.
null, // irrelevant since it won't be called.
200, // since there's no redirect, its a successful response
null, // and not a redirect
],
'intentional 404' => [
'404', // its got a value
true, // the resolver will run, getting the above value,
404, // and return this
404, // so it should 404
null, // and not redirect
],
];
}
#[Test]
public function redirect_is_followed_when_value_is_inherited_from_origin()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en'],
'fr' => ['url' => '/fr/', 'locale' => 'fr'],
]);
$blueprint = Blueprint::makeFromFields([
'redirect' => [
'type' => 'group',
'fields' => [
['handle' => 'url', 'field' => ['type' => 'link']],
['handle' => 'status', 'field' => ['type' => 'radio', 'options' => [301, 302]]],
],
]]);
Blueprint::shouldReceive('in')->with('collections/pages')->andReturn(collect([$blueprint]));
Collection::make('pages')->sites(['en', 'fr'])->routes(['en' => '{slug}', 'fr' => '{slug}'])->save();