1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class URLConfirmationTokenTest_StubToken extends URLConfirmationToken implements TestOnly { |
4
|
|
|
public function urlMatches() { |
5
|
|
|
return parent::urlMatches(); |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
public function currentURL() { |
9
|
|
|
return parent::currentURL(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function redirectURL() { |
13
|
|
|
return parent::redirectURL(); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
class URLConfirmationTokenTest_StubValidToken extends URLConfirmationTokenTest_StubToken { |
18
|
|
|
protected function checkToken($token) { |
19
|
|
|
return true; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
class URLConfirmationTokenTest extends SapphireTest { |
24
|
|
|
|
25
|
|
|
protected $originalURL; |
26
|
|
|
|
27
|
|
|
protected $originalGetVars; |
28
|
|
|
|
29
|
|
|
public function setUp() { |
|
|
|
|
30
|
|
|
parent::setUp(); |
31
|
|
|
global $url; |
|
|
|
|
32
|
|
|
$this->originalURL = $url; |
33
|
|
|
$this->originalGetVars = $_GET; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function tearDown() { |
|
|
|
|
37
|
|
|
parent::tearDown(); |
38
|
|
|
global $url; |
|
|
|
|
39
|
|
|
$url = $this->originalURL; |
40
|
|
|
$_GET = $this->originalGetVars; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testValidToken() { |
|
|
|
|
44
|
|
|
global $url; |
|
|
|
|
45
|
|
|
$url = Controller::join_links(BASE_URL, '/', 'token/test/url'); |
46
|
|
|
$_GET = array('tokentesturltoken' => 'value', 'url' => $url); |
47
|
|
|
|
48
|
|
|
$validToken = new URLConfirmationTokenTest_StubValidToken('token/test/url'); |
49
|
|
|
$this->assertTrue($validToken->urlMatches()); |
50
|
|
|
$this->assertTrue($validToken->tokenProvided()); // Actually forced to true for this test |
51
|
|
|
$this->assertFalse($validToken->reloadRequired()); |
52
|
|
|
$this->assertStringStartsWith(Controller::join_links(BASE_URL, '/', 'token/test/url'), $validToken->redirectURL()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testTokenWithTrailingSlashInUrl() { |
|
|
|
|
56
|
|
|
global $url; |
|
|
|
|
57
|
|
|
$url = Controller::join_links(BASE_URL, '/', 'trailing/slash/url/'); |
58
|
|
|
$_GET = array('url' => $url); |
59
|
|
|
|
60
|
|
|
$trailingSlash = new URLConfirmationTokenTest_StubToken('trailing/slash/url'); |
61
|
|
|
$this->assertTrue($trailingSlash->urlMatches()); |
62
|
|
|
$this->assertFalse($trailingSlash->tokenProvided()); |
63
|
|
|
$this->assertTrue($trailingSlash->reloadRequired()); |
64
|
|
|
$this->assertContains('trailing/slash/url', $trailingSlash->redirectURL()); |
65
|
|
|
$this->assertContains('trailingslashurltoken', $trailingSlash->redirectURL()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testUrlSuppressionWhenTokenMissing() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
global $url; |
|
|
|
|
71
|
|
|
$url = Controller::join_links(BASE_URL, '/', 'test/url/'); |
72
|
|
|
$_GET = array('url' => $url); |
73
|
|
|
|
74
|
|
|
$token = new URLConfirmationTokenTest_StubToken('test/url'); |
75
|
|
|
$token->suppress(); |
76
|
|
|
$this->assertEquals('/', $_GET['url']); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: