|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Tests\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
6
|
|
|
use SilverStripe\CMS\Model\RedirectorPage; |
|
7
|
|
|
use SilverStripe\CMS\Model\RedirectorPageController; |
|
8
|
|
|
use SilverStripe\Control\Director; |
|
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
|
10
|
|
|
|
|
11
|
|
|
class RedirectorPageTest extends FunctionalTest |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $fixture_file = 'RedirectorPageTest.yml'; |
|
14
|
|
|
|
|
15
|
|
|
protected $autoFollowRedirection = false; |
|
16
|
|
|
|
|
17
|
|
|
protected function setUp() : void |
|
18
|
|
|
{ |
|
19
|
|
|
parent::setUp(); |
|
20
|
|
|
Director::config()->update('alternate_base_url', 'http://www.mysite.com/'); |
|
21
|
|
|
|
|
22
|
|
|
// Ensure all pages are published |
|
23
|
|
|
/** @var SiteTree $page */ |
|
24
|
|
|
foreach (SiteTree::get() as $page) { |
|
25
|
|
|
$page->publishSingle(); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testGoodRedirectors() |
|
30
|
|
|
{ |
|
31
|
|
|
// For good redirectors, the final destination URL will be returned |
|
32
|
|
|
$this->assertEquals( |
|
33
|
|
|
"http://www.google.com", |
|
34
|
|
|
$this->objFromFixture(RedirectorPage::class, 'goodexternal')->Link() |
|
35
|
|
|
); |
|
36
|
|
|
$this->assertEquals( |
|
37
|
|
|
"/redirection-dest/", |
|
38
|
|
|
$this->objFromFixture(RedirectorPage::class, 'goodinternal')->redirectionLink() |
|
39
|
|
|
); |
|
40
|
|
|
$this->assertEquals( |
|
41
|
|
|
"/redirection-dest/", |
|
42
|
|
|
$this->objFromFixture(RedirectorPage::class, 'goodinternal')->Link() |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testEmptyRedirectors() |
|
47
|
|
|
{ |
|
48
|
|
|
// If a redirector page is misconfigured, then its link method will just return the usual |
|
49
|
|
|
// URLSegment-generated value |
|
50
|
|
|
$page1 = $this->objFromFixture(RedirectorPage::class, 'badexternal'); |
|
51
|
|
|
$this->assertEquals('/bad-external/', $page1->Link()); |
|
52
|
|
|
|
|
53
|
|
|
// An error message will be shown if you visit it |
|
54
|
|
|
$content = $this->get(Director::makeRelative($page1->Link()))->getBody(); |
|
55
|
|
|
$this->assertStringContainsString('message-setupWithoutRedirect', $content); |
|
56
|
|
|
|
|
57
|
|
|
// This also applies for internal links |
|
58
|
|
|
$page2 = $this->objFromFixture(RedirectorPage::class, 'badinternal'); |
|
59
|
|
|
$this->assertEquals('/bad-internal/', $page2->Link()); |
|
60
|
|
|
$content = $this->get(Director::makeRelative($page2->Link()))->getBody(); |
|
61
|
|
|
$this->assertStringContainsString('message-setupWithoutRedirect', $content); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testReflexiveAndTransitiveInternalRedirectors() |
|
65
|
|
|
{ |
|
66
|
|
|
// Reflexive redirectors are those that point to themselves. |
|
67
|
|
|
// They should behave the same as an empty redirector |
|
68
|
|
|
$page = $this->objFromFixture(RedirectorPage::class, 'reflexive'); |
|
69
|
|
|
$this->assertEquals('/reflexive/', $page->Link()); |
|
70
|
|
|
$content = $this->get(Director::makeRelative($page->Link()))->getBody(); |
|
71
|
|
|
$this->assertStringContainsString('message-setupWithoutRedirect', $content); |
|
72
|
|
|
|
|
73
|
|
|
// Transitive redirectors are those that point to another redirector page. |
|
74
|
|
|
// They should send people to the URLSegment of the destination page - the middle-stop, so to speak. |
|
75
|
|
|
// That should redirect to the final destination |
|
76
|
|
|
$page = $this->objFromFixture(RedirectorPage::class, 'transitive'); |
|
77
|
|
|
$this->assertEquals('/good-internal/', $page->Link()); |
|
78
|
|
|
|
|
79
|
|
|
$this->autoFollowRedirection = false; |
|
80
|
|
|
$response = $this->get(Director::makeRelative($page->Link())); |
|
81
|
|
|
$this->assertEquals(Director::absoluteURL('/redirection-dest/'), $response->getHeader("Location")); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testExternalURLGetsPrefixIfNotSet() |
|
85
|
|
|
{ |
|
86
|
|
|
$page = $this->objFromFixture(RedirectorPage::class, 'externalnoprefix'); |
|
87
|
|
|
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite has prefixed with http'); |
|
88
|
|
|
$page->write(); |
|
89
|
|
|
$this->assertEquals( |
|
90
|
|
|
$page->ExternalURL, |
|
91
|
|
|
'http://google.com', |
|
92
|
|
|
'onBeforeWrite will not double prefix if written again!' |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testAllowsProtocolRelative() |
|
97
|
|
|
{ |
|
98
|
|
|
$noProtocol = new RedirectorPage(['ExternalURL' => 'mydomain.com']); |
|
99
|
|
|
$noProtocol->write(); |
|
100
|
|
|
$this->assertEquals('http://mydomain.com', $noProtocol->ExternalURL); |
|
101
|
|
|
|
|
102
|
|
|
$protocolAbsolute = new RedirectorPage(['ExternalURL' => 'http://mydomain.com']); |
|
103
|
|
|
$protocolAbsolute->write(); |
|
104
|
|
|
$this->assertEquals('http://mydomain.com', $protocolAbsolute->ExternalURL); |
|
105
|
|
|
|
|
106
|
|
|
$protocolRelative = new RedirectorPage(['ExternalURL' => '//mydomain.com']); |
|
107
|
|
|
$protocolRelative->write(); |
|
108
|
|
|
$this->assertEquals('//mydomain.com', $protocolRelative->ExternalURL); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Test that we can trigger a redirection before RedirectorPageController::init() is called |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testRedirectRespectsFinishedResponse() |
|
115
|
|
|
{ |
|
116
|
|
|
$page = $this->objFromFixture(RedirectorPage::class, 'goodinternal'); |
|
117
|
|
|
RedirectorPageController::add_extension(RedirectorPageTest_RedirectExtension::class); |
|
118
|
|
|
|
|
119
|
|
|
$response = $this->get($page->regularLink()); |
|
120
|
|
|
$this->assertEquals(302, $response->getStatusCode()); |
|
121
|
|
|
$this->assertEquals('http://www.mysite.com/foo', $response->getHeader('Location')); |
|
122
|
|
|
|
|
123
|
|
|
RedirectorPageController::remove_extension(RedirectorPageTest_RedirectExtension::class); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testNoJSLinksAllowed() |
|
127
|
|
|
{ |
|
128
|
|
|
$page = new RedirectorPage(); |
|
129
|
|
|
$js = 'javascript:alert("hello world")'; |
|
130
|
|
|
$page->ExternalURL = $js; |
|
131
|
|
|
$this->assertEquals($js, $page->ExternalURL); |
|
132
|
|
|
|
|
133
|
|
|
$page->write(); |
|
134
|
|
|
$this->assertEmpty($page->ExternalURL); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|