@@ -8,168 +8,168 @@ |
||
8 | 8 | |
9 | 9 | class MultiDomainDomainTest extends SapphireTest |
10 | 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 | - } |
|
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 | 175 | } |
@@ -9,96 +9,96 @@ |
||
9 | 9 | |
10 | 10 | class MultiDomainTest extends SapphireTest |
11 | 11 | { |
12 | - /** |
|
13 | - * Set up some test domain configuration |
|
14 | - * |
|
15 | - * {@inheritDoc} |
|
16 | - */ |
|
17 | - public function setUp() |
|
18 | - { |
|
19 | - parent::setUp(); |
|
20 | - Config::nest(); |
|
12 | + /** |
|
13 | + * Set up some test domain configuration |
|
14 | + * |
|
15 | + * {@inheritDoc} |
|
16 | + */ |
|
17 | + public function setUp() |
|
18 | + { |
|
19 | + parent::setUp(); |
|
20 | + Config::nest(); |
|
21 | 21 | |
22 | - Config::inst()->remove(MultiDomain::class, 'domains'); |
|
23 | - Config::inst()->update(MultiDomain::class, 'domains', array( |
|
24 | - 'primary' => array( |
|
25 | - 'hostname' => 'foo.bar', |
|
26 | - 'resolves_to' => 'bar.baz' |
|
27 | - ), |
|
28 | - 'secondary' => array( |
|
29 | - 'hostname' => 'localhost', |
|
30 | - 'resolves_to' => 'local.dev' |
|
31 | - ) |
|
32 | - )); |
|
33 | - } |
|
22 | + Config::inst()->remove(MultiDomain::class, 'domains'); |
|
23 | + Config::inst()->update(MultiDomain::class, 'domains', array( |
|
24 | + 'primary' => array( |
|
25 | + 'hostname' => 'foo.bar', |
|
26 | + 'resolves_to' => 'bar.baz' |
|
27 | + ), |
|
28 | + 'secondary' => array( |
|
29 | + 'hostname' => 'localhost', |
|
30 | + 'resolves_to' => 'local.dev' |
|
31 | + ) |
|
32 | + )); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Test that a MultiDomainDomain can be returned from the configured domains |
|
37 | - */ |
|
38 | - public function testGetDomain() |
|
39 | - { |
|
40 | - $this->assertNull(MultiDomain::get_domain('does-not-exist')); |
|
41 | - $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
|
42 | - } |
|
35 | + /** |
|
36 | + * Test that a MultiDomainDomain can be returned from the configured domains |
|
37 | + */ |
|
38 | + public function testGetDomain() |
|
39 | + { |
|
40 | + $this->assertNull(MultiDomain::get_domain('does-not-exist')); |
|
41 | + $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Test that all domains can be returned, with or without the primary domain |
|
46 | - * |
|
47 | - * @dataProvider getAllDomainsProvider |
|
48 | - * @param bool $withPrimary |
|
49 | - */ |
|
50 | - public function testGetAllDomains($withPrimary) |
|
51 | - { |
|
52 | - $result = MultiDomain::get_all_domains($withPrimary); |
|
53 | - $this->assertInternalType('array', $result); |
|
54 | - $this->assertNotEmpty($result); |
|
44 | + /** |
|
45 | + * Test that all domains can be returned, with or without the primary domain |
|
46 | + * |
|
47 | + * @dataProvider getAllDomainsProvider |
|
48 | + * @param bool $withPrimary |
|
49 | + */ |
|
50 | + public function testGetAllDomains($withPrimary) |
|
51 | + { |
|
52 | + $result = MultiDomain::get_all_domains($withPrimary); |
|
53 | + $this->assertInternalType('array', $result); |
|
54 | + $this->assertNotEmpty($result); |
|
55 | 55 | |
56 | - $expectedCount = $withPrimary ? 2 : 1; |
|
57 | - $this->assertCount($expectedCount, $result); |
|
58 | - } |
|
56 | + $expectedCount = $withPrimary ? 2 : 1; |
|
57 | + $this->assertCount($expectedCount, $result); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return array[] |
|
62 | - */ |
|
63 | - public function getAllDomainsProvider() |
|
64 | - { |
|
65 | - return array( |
|
66 | - array(true), |
|
67 | - array(false) |
|
68 | - ); |
|
69 | - } |
|
60 | + /** |
|
61 | + * @return array[] |
|
62 | + */ |
|
63 | + public function getAllDomainsProvider() |
|
64 | + { |
|
65 | + return array( |
|
66 | + array(true), |
|
67 | + array(false) |
|
68 | + ); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Test that the primary domain can be returned |
|
73 | - */ |
|
74 | - public function testGetPrimaryDomain() |
|
75 | - { |
|
76 | - $result = MultiDomain::get_primary_domain(); |
|
77 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
78 | - $this->assertTrue($result->isPrimary()); |
|
79 | - } |
|
71 | + /** |
|
72 | + * Test that the primary domain can be returned |
|
73 | + */ |
|
74 | + public function testGetPrimaryDomain() |
|
75 | + { |
|
76 | + $result = MultiDomain::get_primary_domain(); |
|
77 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
78 | + $this->assertTrue($result->isPrimary()); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Test that the correct domain can be returned by a provided URL |
|
83 | - */ |
|
84 | - public function testDomainForUrl() |
|
85 | - { |
|
86 | - $result = MultiDomain::domain_for_url('foo.bar/my-page'); |
|
87 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
88 | - $this->assertSame('primary', $result->getKey()); |
|
89 | - } |
|
81 | + /** |
|
82 | + * Test that the correct domain can be returned by a provided URL |
|
83 | + */ |
|
84 | + public function testDomainForUrl() |
|
85 | + { |
|
86 | + $result = MultiDomain::domain_for_url('foo.bar/my-page'); |
|
87 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
88 | + $this->assertSame('primary', $result->getKey()); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Test that if a URL doesn't match any domain then the primary domain is returned |
|
93 | - */ |
|
94 | - public function testDomainForUrlDefaultsToPrimaryDomain() |
|
95 | - { |
|
96 | - $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
|
97 | - } |
|
91 | + /** |
|
92 | + * Test that if a URL doesn't match any domain then the primary domain is returned |
|
93 | + */ |
|
94 | + public function testDomainForUrlDefaultsToPrimaryDomain() |
|
95 | + { |
|
96 | + $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
|
97 | + } |
|
98 | 98 | |
99 | - public function tearDown() |
|
100 | - { |
|
101 | - Config::unnest(); |
|
102 | - parent::tearDown(); |
|
103 | - } |
|
99 | + public function tearDown() |
|
100 | + { |
|
101 | + Config::unnest(); |
|
102 | + parent::tearDown(); |
|
103 | + } |
|
104 | 104 | } |