1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MultiDomain\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\MultiDomain\MultiDomain; |
8
|
|
|
|
9
|
|
|
class MultiDomainDomainTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Set up some test domain data for testing |
13
|
|
|
* |
14
|
|
|
* {@inheritDoc} |
15
|
|
|
*/ |
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
Config::nest(); |
20
|
|
|
|
21
|
|
|
Config::inst()->remove(MultiDomain::class, 'domains'); |
22
|
|
|
Config::inst()->update(MultiDomain::class, 'domains', array( |
23
|
|
|
'primary' => array( |
24
|
|
|
'hostname' => 'example.com' |
25
|
|
|
), |
26
|
|
|
'store' => array( |
27
|
|
|
'hostname' => 'example-store.com', |
28
|
|
|
'resolves_to' => 'shop/store', |
29
|
|
|
'allow' => array( |
30
|
|
|
'admin/*', |
31
|
|
|
'Security/*', |
32
|
|
|
'my-custom-webhook/' |
33
|
|
|
) |
34
|
|
|
), |
35
|
|
|
'configurable' => array( |
36
|
|
|
'hostname' => 'MY_CONSTANT_HOSTNAME' |
37
|
|
|
), |
38
|
|
|
'forceful' => array( |
39
|
|
|
'hostname' => 'forced.com', |
40
|
|
|
'force' => array( |
41
|
|
|
'buy-now/*' |
42
|
|
|
) |
43
|
|
|
) |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test that a hostname defined in a constant will override the default configuration, otherwise the default |
49
|
|
|
* configuration for the domain is returned |
50
|
|
|
*/ |
51
|
|
|
public function testGetHostname() |
52
|
|
|
{ |
53
|
|
|
$configurableDomain = MultiDomain::get_domain('configurable'); |
54
|
|
|
define('MY_CONSTANT_HOSTNAME', 'I am a constant'); |
55
|
|
|
$this->assertSame('I am a constant', $configurableDomain->getHostname()); |
56
|
|
|
|
57
|
|
|
$storeDomain = MultiDomain::get_domain('store'); |
58
|
|
|
$this->assertSame('example-store.com', $storeDomain->getHostname()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null |
63
|
|
|
*/ |
64
|
|
|
public function testGetUrl() |
65
|
|
|
{ |
66
|
|
|
$primaryDomain = MultiDomain::get_domain('primary'); |
67
|
|
|
$this->assertNull($primaryDomain->getURL()); |
68
|
|
|
|
69
|
|
|
$storeDomain = MultiDomain::get_domain('store'); |
70
|
|
|
$this->assertSame('shop/store', $storeDomain->getURL()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Test that a domain can be identified as the primary domain or otherwise |
75
|
|
|
*/ |
76
|
|
|
public function testIsPrimary() |
77
|
|
|
{ |
78
|
|
|
$this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); |
79
|
|
|
$this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* When the request URI matches one of the allowed rules for a domain, the isActive method should return false |
84
|
|
|
*/ |
85
|
|
|
public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() |
86
|
|
|
{ |
87
|
|
|
$domain = MultiDomain::get_domain('store'); |
88
|
|
|
$domain->setRequestUri('/Security/login'); |
89
|
|
|
$this->assertFalse($domain->isActive()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well |
94
|
|
|
* as the primary domain |
95
|
|
|
*/ |
96
|
|
|
public function testSubdomainsAllowedInIsActiveWhenConfigured() |
97
|
|
|
{ |
98
|
|
|
Config::inst()->update(MultiDomain::class, 'allow_subdomains', true); |
99
|
|
|
|
100
|
|
|
$domain = MultiDomain::get_domain('store') |
101
|
|
|
->setRequestUri('/some/page') |
102
|
|
|
->setHttpHost('api.example-store.com'); |
103
|
|
|
|
104
|
|
|
$this->assertTrue($domain->isActive()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The default behaviour would be that if the current host from the request matchese that of the domain model |
109
|
|
|
* then isActive should be true |
110
|
|
|
*/ |
111
|
|
|
public function testReturnActiveIfCurrentHostMatchesDomainsHostname() |
112
|
|
|
{ |
113
|
|
|
$domain = MultiDomain::get_domain('primary') |
114
|
|
|
->setRequestUri('/another/page') |
115
|
|
|
->setHttpHost('example.com'); |
116
|
|
|
|
117
|
|
|
$this->assertTrue($domain->isActive()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* getNativeUrl should not be used on the primary domain |
122
|
|
|
* |
123
|
|
|
* @expectedException Exception |
124
|
|
|
* @expectedExceptionMessage Cannot convert a native URL on the primary domain |
125
|
|
|
*/ |
126
|
|
|
public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() |
127
|
|
|
{ |
128
|
|
|
MultiDomain::get_primary_domain()->getNativeUrl('foo'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Test that a URL segment can be added to the domain's URL and returned as a "native URL" |
133
|
|
|
*/ |
134
|
|
|
public function testGetNativeUrl() |
135
|
|
|
{ |
136
|
|
|
$domain = MultiDomain::get_domain('store'); |
137
|
|
|
$this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* "Allowed" and "forced" URLs should just be returned from getNativeUrl as is |
142
|
|
|
*/ |
143
|
|
|
public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() |
144
|
|
|
{ |
145
|
|
|
$domain = MultiDomain::get_domain('store'); |
146
|
|
|
$this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); |
147
|
|
|
|
148
|
|
|
$domain = MultiDomain::get_domain('forceful'); |
149
|
|
|
$this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The primary domain and "allowed" route matches should be returned as it |
154
|
|
|
*/ |
155
|
|
|
public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() |
156
|
|
|
{ |
157
|
|
|
$this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); |
158
|
|
|
$this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Non-primary domains and un-allowed route matches should be returned without their URL for vanity |
163
|
|
|
*/ |
164
|
|
|
public function testGetVanityUrl() |
165
|
|
|
{ |
166
|
|
|
$this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); |
167
|
|
|
$this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function tearDown() |
171
|
|
|
{ |
172
|
|
|
Config::unnest(); |
173
|
|
|
parent::tearDown(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|