|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Tests\Startup; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Startup\ConfirmationTokenChain; |
|
6
|
|
|
use SilverStripe\Core\Startup\ParameterConfirmationToken; |
|
7
|
|
|
use SilverStripe\Core\Startup\URLConfirmationToken; |
|
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
9
|
|
|
|
|
10
|
|
|
class ConfirmationTokenChainTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
protected function getTokenRequiringReload($requiresReload = true, $extraMethods = []) |
|
13
|
|
|
{ |
|
14
|
|
|
$methods = array_merge(['reloadRequired'], $extraMethods); |
|
15
|
|
|
$mock = $this->createPartialMock(ParameterConfirmationToken::class, $methods); |
|
16
|
|
|
$mock->expects($this->any()) |
|
17
|
|
|
->method('reloadRequired') |
|
18
|
|
|
->will($this->returnValue($requiresReload)); |
|
19
|
|
|
return $mock; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function getTokenRequiringReloadIfError($requiresReload = true, $extraMethods = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$methods = array_merge(['reloadRequired', 'reloadRequiredIfError'], $extraMethods); |
|
25
|
|
|
$mock = $this->createPartialMock(ParameterConfirmationToken::class, $methods); |
|
26
|
|
|
$mock->expects($this->any()) |
|
27
|
|
|
->method('reloadRequired') |
|
28
|
|
|
->will($this->returnValue(false)); |
|
29
|
|
|
$mock->expects($this->any()) |
|
30
|
|
|
->method('reloadRequiredIfError') |
|
31
|
|
|
->will($this->returnValue($requiresReload)); |
|
32
|
|
|
return $mock; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testFilteredTokens() |
|
36
|
|
|
{ |
|
37
|
|
|
$chain = new ConfirmationTokenChain(); |
|
38
|
|
|
$chain->pushToken($tokenRequiringReload = $this->getTokenRequiringReload()); |
|
39
|
|
|
$chain->pushToken($tokenNotRequiringReload = $this->getTokenRequiringReload(false)); |
|
40
|
|
|
$chain->pushToken($tokenRequiringReloadIfError = $this->getTokenRequiringReloadIfError()); |
|
41
|
|
|
$chain->pushToken($tokenNotRequiringReloadIfError = $this->getTokenRequiringReloadIfError(false)); |
|
42
|
|
|
|
|
43
|
|
|
$reflectionMethod = new \ReflectionMethod(ConfirmationTokenChain::class, 'filteredTokens'); |
|
44
|
|
|
$reflectionMethod->setAccessible(true); |
|
45
|
|
|
$tokens = iterator_to_array($reflectionMethod->invoke($chain)); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertContains($tokenRequiringReload, $tokens, 'Token requiring a reload was not returned'); |
|
48
|
|
|
$this->assertNotContains($tokenNotRequiringReload, $tokens, 'Token not requiring a reload was returned'); |
|
49
|
|
|
$this->assertContains($tokenRequiringReloadIfError, $tokens, 'Token requiring a reload on error was not returned'); |
|
50
|
|
|
$this->assertNotContains($tokenNotRequiringReloadIfError, $tokens, 'Token not requiring a reload on error was returned'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testSuppressionRequired() |
|
54
|
|
|
{ |
|
55
|
|
|
$chain = new ConfirmationTokenChain(); |
|
56
|
|
|
$chain->pushToken($this->getTokenRequiringReload(false)); |
|
57
|
|
|
$this->assertFalse($chain->suppressionRequired(), 'Suppression incorrectly marked as required'); |
|
58
|
|
|
|
|
59
|
|
|
$chain = new ConfirmationTokenChain(); |
|
60
|
|
|
$chain->pushToken($this->getTokenRequiringReloadIfError(false)); |
|
61
|
|
|
$this->assertFalse($chain->suppressionRequired(), 'Suppression incorrectly marked as required'); |
|
62
|
|
|
|
|
63
|
|
|
$chain = new ConfirmationTokenChain(); |
|
64
|
|
|
$chain->pushToken($this->getTokenRequiringReload()); |
|
65
|
|
|
$this->assertTrue($chain->suppressionRequired(), 'Suppression not marked as required'); |
|
66
|
|
|
|
|
67
|
|
|
$chain = new ConfirmationTokenChain(); |
|
68
|
|
|
$chain->pushToken($this->getTokenRequiringReloadIfError()); |
|
69
|
|
|
$this->assertFalse($chain->suppressionRequired(), 'Suppression incorrectly marked as required'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testSuppressTokens() |
|
73
|
|
|
{ |
|
74
|
|
|
$mockToken = $this->getTokenRequiringReload(true, ['suppress']); |
|
75
|
|
|
$mockToken->expects($this->once()) |
|
76
|
|
|
->method('suppress'); |
|
77
|
|
|
$secondMockToken = $this->getTokenRequiringReloadIfError(true, ['suppress']); |
|
78
|
|
|
$secondMockToken->expects($this->once()) |
|
79
|
|
|
->method('suppress'); |
|
80
|
|
|
|
|
81
|
|
|
$chain = new ConfirmationTokenChain(); |
|
82
|
|
|
$chain->pushToken($mockToken); |
|
83
|
|
|
$chain->pushToken($secondMockToken); |
|
84
|
|
|
$chain->suppressTokens(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testReloadRequired() |
|
88
|
|
|
{ |
|
89
|
|
|
$mockToken = $this->getTokenRequiringReload(true); |
|
90
|
|
|
$secondMockToken = $this->getTokenRequiringReload(false); |
|
91
|
|
|
|
|
92
|
|
|
$chain = new ConfirmationTokenChain(); |
|
93
|
|
|
$chain->pushToken($mockToken); |
|
94
|
|
|
$chain->pushToken($secondMockToken); |
|
95
|
|
|
$this->assertTrue($chain->reloadRequired()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testReloadRequiredIfError() |
|
99
|
|
|
{ |
|
100
|
|
|
$mockToken = $this->getTokenRequiringReloadIfError(true); |
|
101
|
|
|
$secondMockToken = $this->getTokenRequiringReloadIfError(false); |
|
102
|
|
|
|
|
103
|
|
|
$chain = new ConfirmationTokenChain(); |
|
104
|
|
|
$chain->pushToken($mockToken); |
|
105
|
|
|
$chain->pushToken($secondMockToken); |
|
106
|
|
|
$this->assertTrue($chain->reloadRequiredIfError()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testParams() |
|
110
|
|
|
{ |
|
111
|
|
|
$mockToken = $this->getTokenRequiringReload(true, ['params']); |
|
112
|
|
|
$mockToken->expects($this->once()) |
|
113
|
|
|
->method('params') |
|
114
|
|
|
->with($this->isTrue()) |
|
115
|
|
|
->will($this->returnValue(['mockTokenParam' => '1'])); |
|
116
|
|
|
$secondMockToken = $this->getTokenRequiringReload(true, ['params']); |
|
117
|
|
|
$secondMockToken->expects($this->once()) |
|
118
|
|
|
->method('params') |
|
119
|
|
|
->with($this->isTrue()) |
|
120
|
|
|
->will($this->returnValue(['secondMockTokenParam' => '2'])); |
|
121
|
|
|
|
|
122
|
|
|
$chain = new ConfirmationTokenChain(); |
|
123
|
|
|
$chain->pushToken($mockToken); |
|
124
|
|
|
$chain->pushToken($secondMockToken); |
|
125
|
|
|
$this->assertEquals(['mockTokenParam' => '1', 'secondMockTokenParam' => '2'], $chain->params(true)); |
|
126
|
|
|
|
|
127
|
|
|
$mockToken = $this->getTokenRequiringReload(true, ['params']); |
|
128
|
|
|
$mockToken->expects($this->once()) |
|
129
|
|
|
->method('params') |
|
130
|
|
|
->with($this->isFalse()) |
|
131
|
|
|
->will($this->returnValue(['mockTokenParam' => '1'])); |
|
132
|
|
|
|
|
133
|
|
|
$chain = new ConfirmationTokenChain(); |
|
134
|
|
|
$chain->pushToken($mockToken); |
|
135
|
|
|
$this->assertEquals(['mockTokenParam' => '1'], $chain->params(false)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testGetRedirectUrlBase() |
|
139
|
|
|
{ |
|
140
|
|
|
$mockUrlToken = $this->createPartialMock(URLConfirmationToken::class, ['reloadRequired', 'getRedirectUrlBase']); |
|
141
|
|
|
$mockUrlToken->expects($this->any()) |
|
142
|
|
|
->method('reloadRequired') |
|
143
|
|
|
->will($this->returnValue(true)); |
|
144
|
|
|
$mockUrlToken->expects($this->any()) |
|
145
|
|
|
->method('getRedirectUrlBase') |
|
146
|
|
|
->will($this->returnValue('url-base')); |
|
147
|
|
|
|
|
148
|
|
|
$mockParameterToken = $this->createPartialMock(ParameterConfirmationToken::class, ['reloadRequired', 'getRedirectUrlBase']); |
|
149
|
|
|
$mockParameterToken->expects($this->any()) |
|
150
|
|
|
->method('reloadRequired') |
|
151
|
|
|
->will($this->returnValue(true)); |
|
152
|
|
|
$mockParameterToken->expects($this->any()) |
|
153
|
|
|
->method('getRedirectUrlBase') |
|
154
|
|
|
->will($this->returnValue('parameter-base')); |
|
155
|
|
|
|
|
156
|
|
|
$chain = new ConfirmationTokenChain(); |
|
157
|
|
|
$chain->pushToken($mockParameterToken); |
|
158
|
|
|
$chain->pushToken($mockUrlToken); |
|
159
|
|
|
$this->assertEquals('url-base', $chain->getRedirectUrlBase(), 'URLConfirmationToken url base should take priority'); |
|
160
|
|
|
|
|
161
|
|
|
// Push them in reverse order to check priority still correct |
|
162
|
|
|
$chain = new ConfirmationTokenChain(); |
|
163
|
|
|
$chain->pushToken($mockUrlToken); |
|
164
|
|
|
$chain->pushToken($mockParameterToken); |
|
165
|
|
|
$this->assertEquals('url-base', $chain->getRedirectUrlBase(), 'URLConfirmationToken url base should take priority'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testGetRedirectUrlParams() |
|
169
|
|
|
{ |
|
170
|
|
|
$mockToken = $this->getTokenRequiringReload(true, ['getRedirectUrlParams']); |
|
171
|
|
|
$mockToken->expects($this->once()) |
|
172
|
|
|
->method('getRedirectUrlParams') |
|
173
|
|
|
->will($this->returnValue(['mockTokenParam' => '1'])); |
|
174
|
|
|
|
|
175
|
|
|
$secondMockToken = $this->getTokenRequiringReload(true, ['getRedirectUrlParams']); |
|
176
|
|
|
$secondMockToken->expects($this->once()) |
|
177
|
|
|
->method('getRedirectUrlParams') |
|
178
|
|
|
->will($this->returnValue(['secondMockTokenParam' => '2'])); |
|
179
|
|
|
|
|
180
|
|
|
$chain = new ConfirmationTokenChain(); |
|
181
|
|
|
$chain->pushToken($mockToken); |
|
182
|
|
|
$chain->pushToken($secondMockToken); |
|
183
|
|
|
$this->assertEquals(['mockTokenParam' => '1', 'secondMockTokenParam' => '2'], $chain->getRedirectUrlParams()); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|