-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
IntegrationTestCaseTest.php
1140 lines (1012 loc) · 32.7 KB
/
IntegrationTestCaseTest.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
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
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\TestSuite;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\Routing\Route\InflectedRoute;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Test\Fixture\AssertIntegrationTestCase;
use Cake\Utility\Security;
/**
* Self test of the IntegrationTestCase
*/
class IntegrationTestCaseTest extends IntegrationTestCase
{
/**
* Setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
static::setAppNamespace();
Router::reload();
Router::scope('/', function ($routes) {
$routes->setRouteClass(InflectedRoute::class);
$routes->get('/get/:controller/:action', []);
$routes->head('/head/:controller/:action', []);
$routes->options('/options/:controller/:action', []);
$routes->connect('/:controller/:action/*', []);
});
Router::$initialized = true;
$this->useHttpServer(true);
DispatcherFactory::clear();
}
/**
* Helper for enabling the legacy stack for backwards compatibility testing.
*
* @return void
*/
protected function useLegacyDispatcher()
{
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
$this->useHttpServer(false);
}
/**
* Test building a request.
*
* @return void
*/
public function testRequestBuilding()
{
$this->configRequest([
'headers' => [
'X-CSRF-Token' => 'abc123',
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
'base' => '',
'webroot' => '/',
'environment' => [
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar'
]
]);
$this->cookie('split_token', 'def345');
$this->session(['User' => ['id' => 1, 'username' => 'mark']]);
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
$this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
$this->assertEquals('/tasks/add', $request['url']);
$this->assertArrayHasKey('split_token', $request['cookies']);
$this->assertEquals('def345', $request['cookies']['split_token']);
$this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
$this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
$this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
}
/**
* Test request building adds csrf tokens
*
* @return void
*/
public function testRequestBuildingCsrfTokens()
{
$this->enableCsrfToken();
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertArrayHasKey('csrfToken', $request['cookies']);
$this->assertArrayHasKey('_csrfToken', $request['post']);
$this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
$this->cookie('csrfToken', '');
$request = $this->_buildRequest('/tasks/add', 'POST', [
'_csrfToken' => 'fale',
'title' => 'First post'
]);
$this->assertSame('', $request['cookies']['csrfToken']);
$this->assertSame('fale', $request['post']['_csrfToken']);
}
/**
* Test multiple actions using CSRF tokens don't fail
*
* @return void
*/
public function testEnableCsrfMultipleRequests()
{
$this->enableCsrfToken();
$first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
$this->assertSame(
$first['cookies']['csrfToken'],
$second['post']['_csrfToken'],
'Csrf token should match cookie'
);
$this->assertSame(
$first['post']['_csrfToken'],
$second['post']['_csrfToken'],
'Tokens should be consistent per test method'
);
}
/**
* Test pre-determined CSRF tokens.
*
* @return void
*/
public function testEnableCsrfPredeterminedCookie()
{
$this->enableCsrfToken();
$value = 'I am a teapot';
$this->cookie('csrfToken', $value);
$request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
$this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
$this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
}
/**
* Test building a request, with query parameters
*
* @return void
*/
public function testRequestBuildingQueryParameters()
{
$request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
$this->assertSame('/tasks/view', $request['url']);
$this->assertSame('archived=yes', $request['environment']['QUERY_STRING']);
$this->assertSame('/tasks/view', $request['environment']['REQUEST_URI']);
}
/**
* Test cookie encrypted
*
* @see CookieComponentControllerTest
*/
public function testCookieEncrypted()
{
Security::setSalt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
$this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
$request = $this->_buildRequest('/tasks/view', 'GET', []);
$this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
}
/**
* Test sending get requests.
*
* @group deprecated
* @return void
*/
public function testGetLegacy()
{
$this->useLegacyDispatcher();
$this->deprecated(function () {
$this->assertNull($this->_response);
$this->get('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Http\Response', $this->_response);
$this->assertEquals('This is a test', $this->_response->getBody());
$this->_response = null;
$this->get('/get/request_action/test_request_action');
$this->assertEquals('This is a test', $this->_response->getBody());
});
}
/**
* Test sending get request and using default `test_app/config/routes.php`.
*
* @return void
*/
public function testGetUsingApplicationWithDefaultRoutes()
{
// first clean routes to have Router::$initailized === false
Router::reload();
$this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);
$this->get('/some_alias');
$this->assertResponseOk();
$this->assertEquals('5', $this->_getBodyAsString());
}
/**
* Test sending head requests.
*
* @return void
*/
public function testHead()
{
$this->assertNull($this->_response);
$this->head('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Http\Response', $this->_response);
$this->assertResponseSuccess();
$this->_response = null;
$this->head('/head/request_action/test_request_action');
$this->assertResponseSuccess();
}
/**
* Test sending options requests.
*
* @return void
*/
public function testOptions()
{
$this->assertNull($this->_response);
$this->options('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Http\Response', $this->_response);
$this->assertResponseSuccess();
$this->_response = null;
$this->options('/options/request_action/test_request_action');
$this->assertResponseSuccess();
}
/**
* Test sending get requests sets the request method
*
* @return void
*/
public function testGetSpecificRouteLegacy()
{
$this->useLegacyDispatcher();
$this->deprecated(function () {
$this->get('/get/request_action/test_request_action');
$this->assertResponseOk();
$this->assertEquals('This is a test', $this->_response->getBody());
});
}
/**
* Test sending get requests sets the request method
*
* @return void
*/
public function testGetSpecificRouteHttpServer()
{
$this->get('/get/request_action/test_request_action');
$this->assertResponseOk();
$this->assertEquals('This is a test', $this->_response->getBody());
}
/**
* Test customizing the app class.
*
* @return void
*/
public function testConfigApplication()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot load "TestApp\MissingApp" for use in integration');
$this->configApplication('TestApp\MissingApp', []);
$this->get('/request_action/test_request_action');
}
/**
* Test sending get requests with Http\Server
*
* @return void
*/
public function testGetHttpServer()
{
$this->assertNull($this->_response);
$this->get('/request_action/test_request_action');
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Http\Response', $this->_response);
$this->assertEquals('This is a test', $this->_response->getBody());
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get query string data
*
* @return void
*/
public function testGetQueryStringHttpServer()
{
$this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
$this->get('/request_action/params_pass?q=query');
$this->assertResponseOk();
$this->assertResponseContains('"q":"query"');
$this->assertResponseContains('"contentType":"text\/plain"');
$this->assertHeader('X-Middleware', 'true');
$request = $this->_controller->request;
$this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
}
/**
* Test that the PSR7 requests get query string data
*
* @group deprecated
* @return void
*/
public function testGetQueryStringSetsHere()
{
$this->deprecated(function () {
$this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
$this->get('/request_action/params_pass?q=query');
$this->assertResponseOk();
$this->assertResponseContains('"q":"query"');
$this->assertResponseContains('"contentType":"text\/plain"');
$this->assertHeader('X-Middleware', 'true');
$request = $this->_controller->request;
$this->assertContains('/request_action/params_pass?q=query', $request->here());
$this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
});
}
/**
* Test that the PSR7 requests get cookies
*
* @return void
*/
public function testGetCookiesHttpServer()
{
$this->configRequest(['cookies' => ['split_test' => 'abc']]);
$this->get('/request_action/cookie_pass');
$this->assertResponseOk();
$this->assertResponseContains('"split_test":"abc"');
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests receive post data
*
* @return void
*/
public function testPostDataLegacyDispatcher()
{
$this->useLegacyDispatcher();
$this->deprecated(function () {
$this->post('/request_action/post_pass', ['title' => 'value']);
$data = json_decode($this->_response->getBody());
$this->assertEquals('value', $data->title);
});
}
/**
* Test that the PSR7 requests receive post data
*
* @return void
*/
public function testPostDataHttpServer()
{
$this->post('/request_action/post_pass', ['title' => 'value']);
$data = json_decode($this->_response->getBody());
$this->assertEquals('value', $data->title);
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests receive encoded data.
*
* @return void
*/
public function testInputDataHttpServer()
{
$this->post('/request_action/input_test', '{"hello":"world"}');
if ($this->_response->getBody()->isSeekable()) {
$this->_response->getBody()->rewind();
}
$this->assertSame('world', $this->_response->getBody()->getContents());
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests receive encoded data.
*
* @return void
*/
public function testInputDataSecurityToken()
{
$this->enableSecurityToken();
$this->post('/request_action/input_test', '{"hello":"world"}');
$this->assertSame('world', '' . $this->_response->getBody());
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test that the PSR7 requests get cookies
*
* @return void
*/
public function testSessionHttpServer()
{
$this->session(['foo' => 'session data']);
$this->get('/request_action/session_test');
$this->assertResponseOk();
$this->assertResponseContains('session data');
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test sending requests stores references to controller/view/layout.
*
* @return void
*/
public function testRequestSetsProperties()
{
$this->post('/posts/index');
$this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
$this->assertNotEmpty($this->_viewName, 'View name not set');
$this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
$this->assertNotEmpty($this->_layoutName, 'Layout name not set');
$this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
$this->assertTemplate('index');
$this->assertLayout('default');
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test PSR7 requests store references to controller/view/layout
*
* @return void
*/
public function testRequestSetsPropertiesHttpServer()
{
$this->post('/posts/index');
$this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
$this->assertNotEmpty($this->_viewName, 'View name not set');
$this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
$this->assertNotEmpty($this->_layoutName, 'Layout name not set');
$this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
$this->assertTemplate('index');
$this->assertLayout('default');
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Assert that the stored template doesn't change when cells are rendered.
*
* @return void
*/
public function testAssertTemplateAfterCellRender()
{
$this->get('/posts/get');
$this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
$this->assertTemplate('get');
$this->assertResponseContains('cellcontent');
}
/**
* Test array URLs
*
* @return void
*/
public function testArrayUrls()
{
$this->post(['controller' => 'Posts', 'action' => 'index']);
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test array URLs with an empty router.
*
* @return void
*/
public function testArrayUrlsEmptyRouter()
{
Router::reload();
$this->assertFalse(Router::$initialized);
$this->post(['controller' => 'Posts', 'action' => 'index']);
$this->assertEquals('value', $this->viewVariable('test'));
}
/**
* Test flash and cookie assertions
*
* @return void
*/
public function testFlashSessionAndCookieAsserts()
{
$this->post('/posts/index');
$this->assertSession('An error message', 'Flash.flash.0.message');
$this->assertCookie(1, 'remember_me');
$this->assertCookieNotSet('user_id');
}
/**
* Test flash and cookie assertions
*
* @return void
*/
public function testFlashSessionAndCookieAssertsHttpServer()
{
$this->post('/posts/index');
$this->assertSession('An error message', 'Flash.flash.0.message');
$this->assertCookieNotSet('user_id');
$this->assertCookie(1, 'remember_me');
}
/**
* Test flash assertions stored with enableRememberFlashMessages() after they
* are rendered
*
* @return void
*/
public function testFlashAssertionsAfterRender()
{
$this->enableRetainFlashMessages();
$this->get('/posts/index/with_flash');
$this->assertSession('An error message', 'Flash.flash.0.message');
}
/**
* Test flash assertions stored with enableRememberFlashMessages() even if
* no view is rendered
*
* @return void
*/
public function testFlashAssertionsWithNoRender()
{
$this->enableRetainFlashMessages();
$this->get('/posts/flashNoRender');
$this->assertRedirect();
$this->assertSession('An error message', 'Flash.flash.0.message');
}
/**
* Tests the failure message for assertCookieNotSet
*
* @return void
*/
public function testCookieNotSetFailure()
{
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
$this->expectExceptionMessage('Cookie \'remember_me\' has been set');
$this->post('/posts/index');
$this->assertCookieNotSet('remember_me');
}
/**
* Tests the failure message for assertCookieNotSet when no
* response whas generated
*
* @return void
*/
public function testCookieNotSetFailureNoResponse()
{
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
$this->expectExceptionMessage('No response set, cannot assert cookies.');
$this->assertCookieNotSet('remember_me');
}
/**
* Test error handling and error page rendering.
*
* @return void
*/
public function testPostAndErrorHandling()
{
$this->post('/request_action/error_method');
$this->assertResponseNotEmpty();
$this->assertResponseContains('Not there or here');
$this->assertResponseContains('<!DOCTYPE html>');
}
/**
* Test posting to a secured form action.
*
* @return void
*/
public function testPostSecuredForm()
{
$this->enableSecurityToken();
$data = [
'title' => 'Some title',
'body' => 'Some text'
];
$this->post('/posts/securePost', $data);
$this->assertResponseOk();
$this->assertResponseContains('Request was accepted');
}
/**
* Test posting to a secured form action with nested data.
*
* @return void
*/
public function testPostSecuredFormNestedData()
{
$this->enableSecurityToken();
$data = [
'title' => 'New post',
'comments' => [
['comment' => 'A new comment']
],
'tags' => ['_ids' => [1, 2, 3, 4]]
];
$this->post('/posts/securePost', $data);
$this->assertResponseOk();
$this->assertResponseContains('Request was accepted');
}
/**
* Test posting to a secured form action.
*
* @return void
*/
public function testPostSecuredFormWithQuery()
{
$this->enableSecurityToken();
$data = [
'title' => 'Some title',
'body' => 'Some text'
];
$this->post('/posts/securePost?foo=bar', $data);
$this->assertResponseOk();
$this->assertResponseContains('Request was accepted');
}
/**
* Test posting to a secured form action with a query that has a part that
* will be encoded by the security component
*
* @return void
*/
public function testPostSecuredFormWithUnencodedQuery()
{
$this->enableSecurityToken();
$data = [
'title' => 'Some title',
'body' => 'Some text'
];
$this->post('/posts/securePost?foo=/', $data);
$this->assertResponseOk();
$this->assertResponseContains('Request was accepted');
}
/**
* Test posting to a secured form action action.
*
* @return void
*/
public function testPostSecuredFormFailure()
{
$data = [
'title' => 'Some title',
'body' => 'Some text'
];
$this->post('/posts/securePost', $data);
$this->assertResponseError();
}
/**
* Test that exceptions being thrown are handled correctly.
*
* @return void
*/
public function testWithExpectedException()
{
$this->get('/tests_apps/throw_exception');
$this->assertResponseCode(500);
}
/**
* Test that exceptions being thrown are handled correctly by the psr7 stack.
*
* @return void
*/
public function testWithExpectedExceptionHttpServer()
{
DispatcherFactory::clear();
$this->get('/tests_apps/throw_exception');
$this->assertResponseCode(500);
}
/**
* Test that exceptions being thrown are handled correctly.
*
* @return void
*/
public function testWithUnexpectedException()
{
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
$this->get('/tests_apps/throw_exception');
$this->assertResponseCode(501);
}
/**
* Test redirecting and integration tests.
*
* @return void
*/
public function testRedirect()
{
$this->post('/tests_apps/redirect_to');
$this->assertResponseSuccess();
$this->assertResponseCode(302);
}
/**
* Test redirecting and psr7 stack
*
* @return void
*/
public function testRedirectHttpServer()
{
DispatcherFactory::clear();
$this->post('/tests_apps/redirect_to');
$this->assertResponseCode(302);
$this->assertHeader('X-Middleware', 'true');
}
/**
* Test redirecting and integration tests.
*
* @return void
*/
public function testRedirectPermanent()
{
$this->post('/tests_apps/redirect_to_permanent');
$this->assertResponseSuccess();
$this->assertResponseCode(301);
}
/**
* Test the responseOk status assertion
*
* @return void
*/
public function testAssertResponseStatusCodes()
{
$this->_response = new Response();
$this->_response = $this->_response->withStatus(200);
$this->assertResponseOk();
$this->_response = $this->_response->withStatus(201);
$this->assertResponseOk();
$this->_response = $this->_response->withStatus(204);
$this->assertResponseOk();
$this->_response = $this->_response->withStatus(202);
$this->assertResponseSuccess();
$this->_response = $this->_response->withStatus(302);
$this->assertResponseSuccess();
$this->_response = $this->_response->withStatus(400);
$this->assertResponseError();
$this->_response = $this->_response->withStatus(417);
$this->assertResponseError();
$this->_response = $this->_response->withStatus(500);
$this->assertResponseFailure();
$this->_response = $this->_response->withStatus(505);
$this->assertResponseFailure();
$this->_response = $this->_response->withStatus(301);
$this->assertResponseCode(301);
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertRedirect()
{
$this->_response = new Response();
$this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
$this->assertRedirect();
$this->assertRedirect('/tasks/index');
$this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
$this->assertResponseEmpty();
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertNoRedirect()
{
$this->_response = new Response();
$this->assertNoRedirect();
}
/**
* Test the location header assertion.
*
* @return void
*/
public function testAssertNoRedirectFail()
{
$test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
$result = $test->run();
$this->assertFalse($result->wasSuccessful());
$this->assertEquals(1, $result->failureCount());
}
/**
* Test the location header assertion string contains
*
* @return void
*/
public function testAssertRedirectContains()
{
$this->_response = new Response();
$this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
$this->assertRedirectContains('/tasks/index');
}
/**
* Test the header assertion.
*
* @return void
*/
public function testAssertHeader()
{
$this->_response = new Response();
$this->_response = $this->_response->withHeader('Etag', 'abc123');
$this->assertHeader('Etag', 'abc123');
}
/**
* Test the header contains assertion.
*
* @return void
*/
public function testAssertHeaderContains()
{
$this->_response = new Response();
$this->_response = $this->_response->withHeader('Etag', 'abc123');
$this->assertHeaderContains('Etag', 'abc');
}
/**
* Test the content type assertion.
*
* @return void
*/
public function testAssertContentType()
{
$this->_response = new Response();
$this->_response = $this->_response->withType('json');
$this->assertContentType('json');
$this->assertContentType('application/json');
}
/**
* Test that type() in an action sets the content-type header.
*
* @return void
*/
public function testContentTypeInAction()
{
$this->get('/tests_apps/set_type');
$this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
$this->assertContentType('json');
$this->assertContentType('application/json');
}
/**
* Test the content assertion.
*
* @return void
*/
public function testAssertResponseContains()
{
$this->_response = new Response();
$this->_response = $this->_response->withStringBody('Some content');
$this->assertResponseContains('content');
}
/**
* Test the content assertion with no case sensitivity.
*
* @return void
*/
public function testAssertResponseContainsWithIgnoreCaseFlag()
{
$this->_response = new Response();
$this->_response = $this->_response->withStringBody('Some content');
$this->assertResponseContains('some', 'Failed asserting that the body contains given content', true);
}
/**
* Test the negated content assertion.
*
* @return void
*/
public function testAssertResponseNotContains()
{
$this->_response = new Response();
$this->_response = $this->_response->withStringBody('Some content');
$this->assertResponseNotContains('contents');
}
/**
* Test the content regexp assertion.
*
* @return void
*/
public function testAssertResponseRegExp()
{
$this->_response = new Response();
$this->_response = $this->_response->withStringBody('Some content');
$this->assertResponseRegExp('/cont/');
}
/**
* Test the content regexp assertion failing
*
* @return void
*/
public function testAssertResponseRegExpNoResponse()
{
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
$this->expectExceptionMessage('No response set');
$this->assertResponseRegExp('/cont/');
}
/**
* Test the negated content regexp assertion.
*