forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeIgniterTest.php
963 lines (753 loc) · 30.7 KB
/
CodeIgniterTest.php
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\Mock\MockCodeIgniter;
use Config\App;
use Config\Cache;
use Config\Filters as FiltersConfig;
use Config\Modules;
use Config\Routing;
use Tests\Support\Filters\Customfilter;
use Tests\Support\Filters\RedirectFilter;
/**
* @runTestsInSeparateProcesses
*
* @backupGlobals enabled
*
* @internal
*
* @group Others
*/
final class CodeIgniterTest extends CIUnitTestCase
{
private CodeIgniter $codeigniter;
protected $routes;
protected function setUp(): void
{
parent::setUp();
$this->resetServices();
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$this->codeigniter = new MockCodeIgniter(new App());
$response = Services::response();
$response->pretend();
}
protected function tearDown(): void
{
parent::tearDown();
$this->resetServices();
}
public function testRunEmptyDefaultRoute(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
public function testOutputBufferingControl()
{
ob_start();
$this->codeigniter->run();
ob_get_clean();
// 1 phpunit output buffering level
$this->assertSame(1, ob_get_level());
}
public function testRunEmptyDefaultRouteReturnResponse(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$response = $this->codeigniter->run(null, true);
$this->assertStringContainsString('Welcome to CodeIgniter', $response->getBody());
}
public function testRunClosureRoute(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('pages/(:segment)', static function ($segment): void {
echo 'You want to see "' . esc($segment) . '" page.';
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('You want to see "about" page.', $output);
}
public function testRun404Override(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Errors::show404');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run($routes);
$output = ob_get_clean();
$this->assertStringContainsString("Can't find a route for 'GET: pages/about'.", $output);
}
public function testRun404OverrideControllerReturnsResponse(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Controllers\Popcorn::pop');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
$response = $this->codeigniter->run($routes, true);
$this->assertStringContainsString('Oops', $response->getBody());
$this->assertSame(567, $response->getStatusCode());
}
public function testRun404OverrideReturnResponse(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Controllers\Popcorn::pop');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
$response = $this->codeigniter->run($routes, true);
$this->assertStringContainsString('Oops', $response->getBody());
}
public function testRun404OverrideByClosure(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$routes = new RouteCollection(Services::locator(), new Modules(), new Routing());
$routes->setAutoRoute(false);
$routes->set404Override(static function (): void {
echo '404 Override by Closure.';
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run($routes);
$output = ob_get_clean();
$this->assertStringContainsString('404 Override by Closure.', $output);
}
public function testControllersCanReturnString(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add(
'pages/(:segment)',
static fn ($segment) => 'You want to see "' . esc($segment) . '" page.'
);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('You want to see "about" page.', $output);
}
public function testControllersCanReturnResponseObject(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('pages/(:segment)', static function ($segment) {
$response = Services::response();
$string = "You want to see 'about' page.";
return $response->setBody($string);
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString("You want to see 'about' page.", $output);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6358
*/
public function testControllersCanReturnDownloadResponseObject(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('pages/(:segment)', static function ($segment) {
$response = Services::response();
return $response->download('some.txt', 'some text', true);
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertSame('some text', $output);
}
public function testRunExecuteFilterByClassName(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add(
'pages/about',
static fn () => Services::incomingrequest()->getBody(),
['filter' => Customfilter::class]
);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('http://hellowworld.com', $output);
$this->resetServices();
}
public function testRegisterSameFilterTwiceWithDifferentArgument()
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('"test-customfilter" already has arguments: null');
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$routes = Services::routes();
$routes->add(
'pages/about',
static fn () => Services::incomingrequest()->getBody(),
// Set filter with no argument.
['filter' => 'test-customfilter']
);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
/** @var FiltersConfig $filterConfig */
$filterConfig = config('Filters');
$filterConfig->filters = [
// Set filter with argument.
'test-customfilter:arg1' => [
'before' => ['pages/*'],
],
];
Services::filters($filterConfig);
$this->codeigniter->run();
$this->resetServices();
}
public function testDisableControllerFilters(): void
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add(
'pages/about',
static fn () => Services::incomingrequest()->getBody(),
['filter' => Customfilter::class]
);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->disableFilters();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('', $output);
$this->resetServices();
}
public function testResponseConfigEmpty(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$response = Services::response(null, false);
$this->assertInstanceOf(Response::class, $response);
}
public function testRoutesIsEmpty(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$router = Services::router(null, Services::incomingrequest(), false);
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
public function testTransfersCorrectHTTPVersion(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/2.0';
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('2.0', $response->getProtocolVersion());
}
public function testSupportsHttp3(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/3.0';
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('3.0', $response->getProtocolVersion());
}
public function testIgnoringErrorSuppressedByAt(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
ob_start();
@unlink('inexistent-file');
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
public function testRunForceSecure(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$filterConfig = config(FiltersConfig::class);
$filterConfig->required['before'][] = 'forcehttps';
$config = config(App::class);
$config->forceGlobalSecureRequests = true;
$codeigniter = new MockCodeIgniter($config);
$codeigniter->setContext('web');
$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
$response = $this->getPrivateProperty($codeigniter, 'response');
$this->assertNull($response->header('Location'));
$response = $codeigniter->run(null, true);
$this->assertSame('https://example.com/index.php/', $response->header('Location')->getValue());
}
public function testRunRedirectionWithNamed(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('pages/named', static function (): void {
}, ['as' => 'name']);
$routes->addRedirect('example', 'name');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('http://example.com/pages/named', $response->header('Location')->getValue());
}
public function testRunRedirectionWithURI(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('pages/uri', static function (): void {
});
$routes->addRedirect('example', 'pages/uri');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('http://example.com/pages/uri', $response->header('Location')->getValue());
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3041
*/
public function testRunRedirectionWithGET(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
// Inject mock router.
$routes = Services::routes();
// addRedirect() sets status code 302 by default.
$routes->addRedirect('example', 'pages/notset');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame('http://example.com/pages/notset', $response->header('Location')->getValue());
$this->assertSame(302, $response->getStatusCode());
}
public function testRunRedirectionWithGETAndHTTPCode301(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
// Inject mock router.
$routes = Services::routes();
$routes->addRedirect('example', 'pages/notset', 301);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame(301, $response->getStatusCode());
}
public function testRunRedirectionWithPOSTAndHTTPCode301(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
// Inject mock router.
$routes = Services::routes();
$routes->addRedirect('example', 'pages/notset', 301);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame(301, $response->getStatusCode());
}
/**
* test for deprecated \CodeIgniter\Router\Exceptions\RedirectException for backward compatibility
*/
public function testRedirectExceptionDeprecated(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$routes = Services::routes();
$routes->get('/', static function () {
throw new RedirectException('redirect-exception', 503);
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
$response = $this->codeigniter->run($routes, true);
$this->assertSame(503, $response->getStatusCode());
$this->assertSame('http://example.com/redirect-exception', $response->getHeaderLine('Location'));
}
public function testStoresPreviousURL(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
// Inject mock router.
$router = Services::router(null, Services::incomingrequest(), false);
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('http://example.com/index.php/', $_SESSION['_ci_previous_url']);
}
public function testNotStoresPreviousURL(): void
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/example';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
// Inject mock router.
$routes = Services::routes();
$routes->addRedirect('example', 'pages/notset', 301);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
}
public function testNotStoresPreviousURLByCheckingContentType(): void
{
$_SERVER['argv'] = ['index.php', 'image'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/image';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->add('image', static function () {
$response = Services::response();
return $response->setContentType('image/jpeg', '');
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
}
/**
* The method after all test, reset Servces:: config
* Can't use static::tearDownAfterClass. This will cause a buffer exception
* need improve
*/
public function testRunDefaultRoute(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
}
public function testRunCLIRoute(): void
{
$_SERVER['argv'] = ['index.php', 'cli'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_URI'] = '/cli';
$_SERVER['SCRIPT_NAME'] = 'public/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'CLI';
$routes = Services::routes();
$routes->cli('cli', '\Tests\Support\Controllers\Popcorn::index');
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('Method Not Allowed', $output);
}
public function testSpoofRequestMethodCanUsePUT(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['_method'] = Method::PUT;
$routes = \Config\Services::routes();
$routes->setDefaultNamespace('App\Controllers');
$routes->resetRoutes();
$routes->post('/', 'Home::index');
$routes->put('/', 'Home::index');
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertSame(Method::PUT, Services::incomingrequest()->getMethod());
}
public function testSpoofRequestMethodCannotUseGET(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['_method'] = 'GET';
$routes = \Config\Services::routes();
$routes->setDefaultNamespace('App\Controllers');
$routes->resetRoutes();
$routes->post('/', 'Home::index');
$routes->get('/', 'Home::index');
ob_start();
$this->codeigniter->run();
ob_get_clean();
$this->assertSame('POST', Services::incomingrequest()->getMethod());
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6281
*/
public function testPageCacheSendSecureHeaders(): void
{
// Suppress command() output
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();
CITestStreamFilter::addOutputFilter();
// Clear Page cache
command('cache:clear');
$_SERVER['REQUEST_URI'] = '/test';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$routes = Services::routes();
$routes->add('test', static function () {
CodeIgniter::cache(3600);
$response = Services::response();
$string = 'This is a test page. Elapsed time: {elapsed_time}';
return $response->setBody($string);
});
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);
/** @var FiltersConfig $filterConfig */
$filterConfig = config('Filters');
$filterConfig->globals['after'] = ['secureheaders'];
Services::filters($filterConfig);
// The first response to be cached.
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('This is a test page', $output);
$response = Services::response();
$headers = $response->headers();
$this->assertArrayHasKey('X-Frame-Options', $headers);
// The second response from the Page cache.
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertStringContainsString('This is a test page', $output);
$response = Services::response();
$headers = $response->headers();
$this->assertArrayHasKey('X-Frame-Options', $headers);
// Clear Page cache
command('cache:clear');
// Remove stream filters
CITestStreamFilter::removeErrorFilter();
CITestStreamFilter::removeOutputFilter();
}
/**
* @param array|bool $cacheQueryStringValue
*
* @dataProvider providePageCacheWithCacheQueryString
*
* @see https://github.com/codeigniter4/CodeIgniter4/pull/6410
*/
public function testPageCacheWithCacheQueryString(
$cacheQueryStringValue,
int $expectedPagesInCache,
array $testingUrls
): void {
// Suppress command() output
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
CITestStreamFilter::addErrorFilter();
// Create cache config with cacheQueryString value from the dataProvider
$cacheConfig = config(Cache::class);
$cacheConfig->cacheQueryString = $cacheQueryStringValue;
// Clear cache before starting the test
command('cache:clear');
// Calculate amount of items in the cache before the test
$cache = \Config\Services::cache();
$cacheStartCounter = count($cache->getCacheInfo());
// Generate request to each URL from the testing array
foreach ($testingUrls as $testingUrl) {
$this->resetServices();
$_SERVER['REQUEST_URI'] = '/' . $testingUrl;
$_SERVER['SCRIPT_NAME'] = '/index.php';
$this->codeigniter = new MockCodeIgniter(new App());
$routes = Services::routes(true);
$routePath = explode('?', $testingUrl)[0];
$string = 'This is a test page, to check cache configuration';
$routes->add($routePath, static function () use ($string) {
Services::responsecache()->setTtl(60);
$response = Services::response();
return $response->setBody($string);
});
// Inject router
$router = Services::router($routes, Services::incomingrequest(null, false));
Services::injectMock('router', $router);
// Cache the page output using default caching function and $cacheConfig
// with value from the data provider
ob_start();
$this->codeigniter->run();
$output = ob_get_clean();
$this->assertSame($string, $output);
}
// Calculate how much cached items exist in the cache after the test requests
$cacheEndCounter = count($cache->getCacheInfo());
$newPagesCached = $cacheEndCounter - $cacheStartCounter;
// Clear cache after the test
command('cache:clear');
// Check that amount of new items created in the cache matching expected value from the data provider
$this->assertSame($expectedPagesInCache, $newPagesCached);
// Remove stream filters
CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}
public static function providePageCacheWithCacheQueryString(): iterable
{
$testingUrls = [
// URL #1
'test',
// URL #2
'test?important_parameter=1',
// URL #3
'test?important_parameter=2',
// URL #4
'test?important_parameter=1¬_important_parameter=2',
// URL #5
'test?important_parameter=1¬_important_parameter=2&another_not_important_parameter=3',
];
return [
// We expect only 1 page in the cache, because when cacheQueryString
// is set to false, all GET parameter should be ignored, and page URI
// will be absolutely same "/test" string for all 5 requests
'$cacheQueryString=false' => [false, 1, $testingUrls],
// We expect all 5 pages in the cache, because when cacheQueryString
// is set to true, all GET parameter should be processed as unique requests
'$cacheQueryString=true' => [true, 5, $testingUrls],
// We expect only 3 pages in the cache, because when cacheQueryString
// is set to array with important parameters, we should ignore all
// parameters thats not in the array. Only URL #1, URL #2 and URL #3
// should be cached. URL #4 and URL #5 is duplication of URL #2
// (with value ?important_parameter=1), so they should not be processed
// as new unique requests and application should return already cached
// page for URL #2
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
];
}
/**
* See https://github.com/codeigniter4/CodeIgniter4/issues/7205
*/
public function testRunControllerNotFoundBeforeFilter(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;
$_SERVER['REQUEST_URI'] = '/cannotFound';
$_SERVER['SCRIPT_NAME'] = '/index.php';
// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(true);
// Inject the before filter.
$filterConfig = config('Filters');
$filterConfig->aliases['redirectFilter'] = RedirectFilter::class;
$filterConfig->globals['before'] = ['redirectFilter'];
Services::filters($filterConfig);
$this->expectException(PageNotFoundException::class);
$this->codeigniter->run($routes);
}
}