|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\BrowserKit\Cookie as RequestCookie; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Cookie as ResponseCookie; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\Presentation\SkinSettings; |
|
11
|
|
|
|
|
12
|
|
|
class SkinTest extends WebRouteTestCase { |
|
13
|
|
|
|
|
14
|
|
|
private const SKIN_1 = '10h16'; |
|
15
|
|
|
private const SKIN_2 = 'cat17'; |
|
16
|
|
|
private const DEFAULT_SKIN = self::SKIN_1; |
|
17
|
|
|
|
|
18
|
|
|
public function testDefaultSkinGetsUsed(): void { |
|
19
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
20
|
|
|
$client->request( 'GET', '/' ); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() ); |
|
23
|
|
|
$this->assertNoSkinResponseCookie( $client->getResponse() ); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testDefaultSkinNotSavedInCookie(): void { |
|
27
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
28
|
|
|
$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::DEFAULT_SKIN ] ); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() ); |
|
31
|
|
|
$this->assertNoSkinResponseCookie( $client->getResponse() ); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testDefaultSkinUsedAndCookieRemovedWhenRequestedViaQueryWithOpposingCookie(): void { |
|
35
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
36
|
|
|
$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_2 ) ); |
|
37
|
|
|
$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::DEFAULT_SKIN ] ); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() ); |
|
40
|
|
|
$this->assertSkinResponseCookie( '', false, $client->getResponse() ); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testSkinChoosableViaCookie(): void { |
|
44
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
45
|
|
|
$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_2 ) ); |
|
46
|
|
|
$client->request( 'GET', '/' ); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() ); |
|
49
|
|
|
$this->assertNoSkinResponseCookie( $client->getResponse() ); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSkinChoosableViaQuery(): void { |
|
53
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
54
|
|
|
$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::SKIN_2 ] ); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() ); |
|
57
|
|
|
$this->assertSkinResponseCookie( self::SKIN_2, true, $client->getResponse() ); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testSkinViaQuerySupersedesCookie(): void { |
|
61
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
62
|
|
|
$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_1 ) ); |
|
63
|
|
|
$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::SKIN_2 ] ); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() ); |
|
66
|
|
|
$this->assertSkinResponseCookie( self::SKIN_2, true, $client->getResponse() ); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testInvalidQueryIgnored(): void { |
|
70
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
71
|
|
|
$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => 'fff' ] ); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() ); |
|
74
|
|
|
$this->assertNoSkinResponseCookie( $client->getResponse() ); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testInvalidCookieIgnored(): void { |
|
78
|
|
|
$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG ); |
|
79
|
|
|
$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, 'ggg' ) ); |
|
80
|
|
|
$client->request( 'GET', '/' ); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() ); |
|
83
|
|
|
$this->assertNoSkinResponseCookie( $client->getResponse() ); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* While this can return skin config as it pleases, the skin files are not faked but needed to generate response content |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getDummyConfig(): array { |
|
90
|
|
|
return [ |
|
91
|
|
|
'skin' => [ |
|
92
|
|
|
'options' => [ self::SKIN_1, self::SKIN_2 ], |
|
93
|
|
|
'default' => self::DEFAULT_SKIN, |
|
94
|
|
|
'cookie-lifetime' => 5 |
|
95
|
|
|
] |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function assertSkinResponseCookie( string $expectedValue, bool $positiveExpiresTime, Response $response ): void { |
|
100
|
|
|
$cookies = $response->headers->getCookies(); |
|
101
|
|
|
foreach ( $cookies as $cookie ) { |
|
102
|
|
|
/** |
|
103
|
|
|
* @var ResponseCookie $cookie |
|
104
|
|
|
*/ |
|
105
|
|
|
if ( $cookie->getName() === SkinSettings::COOKIE_NAME ) { |
|
106
|
|
|
$this->assertSame( $expectedValue, $cookie->getValue() ); |
|
107
|
|
|
if ( $positiveExpiresTime ) { |
|
108
|
|
|
$this->assertGreaterThan( time(), $cookie->getExpiresTime() ); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->assertLessThan( time(), $cookie->getExpiresTime() ); |
|
111
|
|
|
} |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
$this->fail( 'Could not find the "skin" response cookie.' ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function assertNoSkinResponseCookie( Response $response ): void { |
|
119
|
|
|
$cookies = $response->headers->getCookies(); |
|
120
|
|
|
foreach ( $cookies as $cookie ) { |
|
121
|
|
|
/** |
|
122
|
|
|
* @var ResponseCookie $cookie |
|
123
|
|
|
*/ |
|
124
|
|
|
if ( $cookie->getName() === SkinSettings::COOKIE_NAME ) { |
|
125
|
|
|
$this->fail( 'Found an unexpected "skin" response cookie.' ); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: