@@ -40,19 +40,19 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | protected $forcedPaths; |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * The request URI |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $requestUri; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * The request HTTP HOST |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $httpHost; |
|
| 43 | + /** |
|
| 44 | + * The request URI |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $requestUri; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * The request HTTP HOST |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $httpHost; |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * Constructor. Takes a key for the domain and its array of settings from the config |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | return $url; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - $domainUrl = str_replace('/', '\/', $this->getURL()); |
|
| 154 | + $domainUrl = str_replace('/', '\/', $this->getURL()); |
|
| 155 | 155 | return preg_replace('/^\/?' . $domainUrl . '\//', '', $url); |
| 156 | 156 | } |
| 157 | 157 | |
@@ -170,59 +170,59 @@ discard block |
||
| 170 | 170 | return false; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Returns the key/identifier for this domain |
|
| 175 | - * |
|
| 176 | - * @return string |
|
| 177 | - */ |
|
| 178 | - public function getKey() |
|
| 179 | - { |
|
| 180 | - return $this->key; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Set the request URI |
|
| 185 | - * |
|
| 186 | - * @param string $requestUri |
|
| 187 | - * @return $this |
|
| 188 | - */ |
|
| 189 | - public function setRequestUri($requestUri) |
|
| 190 | - { |
|
| 191 | - $this->requestUri = (string) $requestUri; |
|
| 192 | - return $this; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal |
|
| 197 | - * |
|
| 198 | - * @return string |
|
| 199 | - */ |
|
| 200 | - public function getRequestUri() |
|
| 201 | - { |
|
| 202 | - return $this->requestUri ?: $_SERVER['REQUEST_URI']; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Set the HTTP host in the request |
|
| 207 | - * |
|
| 208 | - * @param string $httpHost |
|
| 209 | - * @return $this |
|
| 210 | - */ |
|
| 211 | - public function setHttpHost($httpHost) |
|
| 212 | - { |
|
| 213 | - $this->httpHost = (string) $httpHost; |
|
| 214 | - return $this; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal |
|
| 219 | - * |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - public function getHttpHost() |
|
| 223 | - { |
|
| 224 | - return $this->httpHost ?: $_SERVER['HTTP_HOST']; |
|
| 225 | - } |
|
| 173 | + /** |
|
| 174 | + * Returns the key/identifier for this domain |
|
| 175 | + * |
|
| 176 | + * @return string |
|
| 177 | + */ |
|
| 178 | + public function getKey() |
|
| 179 | + { |
|
| 180 | + return $this->key; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Set the request URI |
|
| 185 | + * |
|
| 186 | + * @param string $requestUri |
|
| 187 | + * @return $this |
|
| 188 | + */ |
|
| 189 | + public function setRequestUri($requestUri) |
|
| 190 | + { |
|
| 191 | + $this->requestUri = (string) $requestUri; |
|
| 192 | + return $this; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Return the current request URI, defaulting to retrieving it from the $_SERVER superglobal |
|
| 197 | + * |
|
| 198 | + * @return string |
|
| 199 | + */ |
|
| 200 | + public function getRequestUri() |
|
| 201 | + { |
|
| 202 | + return $this->requestUri ?: $_SERVER['REQUEST_URI']; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Set the HTTP host in the request |
|
| 207 | + * |
|
| 208 | + * @param string $httpHost |
|
| 209 | + * @return $this |
|
| 210 | + */ |
|
| 211 | + public function setHttpHost($httpHost) |
|
| 212 | + { |
|
| 213 | + $this->httpHost = (string) $httpHost; |
|
| 214 | + return $this; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Return the current HTTP host, defaulting to retrieving it from the $_SERVER superglobal |
|
| 219 | + * |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + public function getHttpHost() |
|
| 223 | + { |
|
| 224 | + return $this->httpHost ?: $_SERVER['HTTP_HOST']; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | 227 | /** |
| 228 | 228 | * Checks a given list of wildcard patterns to see if a path is allowed |
@@ -64,10 +64,10 @@ discard block |
||
| 64 | 64 | $this->hostname = $config['hostname']; |
| 65 | 65 | $this->url = isset($config['resolves_to']) ? $config['resolves_to'] : null; |
| 66 | 66 | |
| 67 | - $globalAllowed = (array) Config::inst()->get('MultiDomain','allow'); |
|
| 68 | - $globalForced = (array) Config::inst()->get('MultiDomain','force'); |
|
| 69 | - $myAllowed = isset($config['allow']) ? $config['allow'] : array (); |
|
| 70 | - $myForced = isset($config['force']) ? $config['force'] : array (); |
|
| 67 | + $globalAllowed = (array) Config::inst()->get('MultiDomain', 'allow'); |
|
| 68 | + $globalForced = (array) Config::inst()->get('MultiDomain', 'force'); |
|
| 69 | + $myAllowed = isset($config['allow']) ? $config['allow'] : array(); |
|
| 70 | + $myForced = isset($config['force']) ? $config['force'] : array(); |
|
| 71 | 71 | $this->allowedPaths = array_merge($globalAllowed, $myAllowed); |
| 72 | 72 | $this->forcedPaths = array_merge($globalForced, $myForced); |
| 73 | 73 | |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @return boolean |
| 96 | 96 | */ |
| 97 | 97 | public function isActive() { |
| 98 | - if($this->isAllowedPath($this->getRequestUri())) { |
|
| 98 | + if ($this->isAllowedPath($this->getRequestUri())) { |
|
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | |
@@ -108,8 +108,7 @@ discard block |
||
| 108 | 108 | $hostname = $this->getHostname(); |
| 109 | 109 | |
| 110 | 110 | return $allow_subdomains ? |
| 111 | - (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) : |
|
| 112 | - ($currentHost == $hostname); |
|
| 111 | + (bool) preg_match('/(\.|^)'.$hostname.'$/', $currentHost) : ($currentHost == $hostname); |
|
| 113 | 112 | } |
| 114 | 113 | |
| 115 | 114 | /** |
@@ -128,11 +127,11 @@ discard block |
||
| 128 | 127 | * @return string |
| 129 | 128 | */ |
| 130 | 129 | public function getNativeURL($url) { |
| 131 | - if($this->isPrimary()) { |
|
| 130 | + if ($this->isPrimary()) { |
|
| 132 | 131 | throw new Exception("Cannot convert a native URL on the primary domain"); |
| 133 | 132 | } |
| 134 | 133 | |
| 135 | - if($this->isAllowedPath($url) || $this->isForcedPath($url)) { |
|
| 134 | + if ($this->isAllowedPath($url) || $this->isForcedPath($url)) { |
|
| 136 | 135 | return $url; |
| 137 | 136 | } |
| 138 | 137 | |
@@ -147,12 +146,12 @@ discard block |
||
| 147 | 146 | * @return string |
| 148 | 147 | */ |
| 149 | 148 | public function getVanityURL($url) { |
| 150 | - if($this->isPrimary() || $this->isAllowedPath($url)) { |
|
| 149 | + if ($this->isPrimary() || $this->isAllowedPath($url)) { |
|
| 151 | 150 | return $url; |
| 152 | 151 | } |
| 153 | 152 | |
| 154 | 153 | $domainUrl = str_replace('/', '\/', $this->getURL()); |
| 155 | - return preg_replace('/^\/?' . $domainUrl . '\//', '', $url); |
|
| 154 | + return preg_replace('/^\/?'.$domainUrl.'\//', '', $url); |
|
| 156 | 155 | } |
| 157 | 156 | |
| 158 | 157 | /** |
@@ -161,9 +160,9 @@ discard block |
||
| 161 | 160 | * @return boolean |
| 162 | 161 | */ |
| 163 | 162 | public function hasURL($url) { |
| 164 | - if($this->isForcedPath($url)) return true; |
|
| 165 | - $domainBaseURL = trim($this->getURL(),'/'); |
|
| 166 | - if(preg_match('/^'.$domainBaseURL.'/', $url)) { |
|
| 163 | + if ($this->isForcedPath($url)) return true; |
|
| 164 | + $domainBaseURL = trim($this->getURL(), '/'); |
|
| 165 | + if (preg_match('/^'.$domainBaseURL.'/', $url)) { |
|
| 167 | 166 | return true; |
| 168 | 167 | } |
| 169 | 168 | |
@@ -249,13 +248,13 @@ discard block |
||
| 249 | 248 | * @return boolean |
| 250 | 249 | */ |
| 251 | 250 | protected static function match_url($url, $patterns) { |
| 252 | - if(!is_array($patterns)) return false; |
|
| 251 | + if (!is_array($patterns)) return false; |
|
| 253 | 252 | |
| 254 | 253 | $url = ltrim($url, '/'); |
| 255 | - if(substr($url, -1) !== '/') $url .= '/'; |
|
| 254 | + if (substr($url, -1) !== '/') $url .= '/'; |
|
| 256 | 255 | |
| 257 | - foreach($patterns as $pattern) { |
|
| 258 | - if(fnmatch($pattern, $url)) return true; |
|
| 256 | + foreach ($patterns as $pattern) { |
|
| 257 | + if (fnmatch($pattern, $url)) return true; |
|
| 259 | 258 | } |
| 260 | 259 | |
| 261 | 260 | return false; |
@@ -161,7 +161,9 @@ discard block |
||
| 161 | 161 | * @return boolean |
| 162 | 162 | */ |
| 163 | 163 | public function hasURL($url) { |
| 164 | - if($this->isForcedPath($url)) return true; |
|
| 164 | + if($this->isForcedPath($url)) { |
|
| 165 | + return true; |
|
| 166 | + } |
|
| 165 | 167 | $domainBaseURL = trim($this->getURL(),'/'); |
| 166 | 168 | if(preg_match('/^'.$domainBaseURL.'/', $url)) { |
| 167 | 169 | return true; |
@@ -249,13 +251,19 @@ discard block |
||
| 249 | 251 | * @return boolean |
| 250 | 252 | */ |
| 251 | 253 | protected static function match_url($url, $patterns) { |
| 252 | - if(!is_array($patterns)) return false; |
|
| 254 | + if(!is_array($patterns)) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 253 | 257 | |
| 254 | 258 | $url = ltrim($url, '/'); |
| 255 | - if(substr($url, -1) !== '/') $url .= '/'; |
|
| 259 | + if(substr($url, -1) !== '/') { |
|
| 260 | + $url .= '/'; |
|
| 261 | + } |
|
| 256 | 262 | |
| 257 | 263 | foreach($patterns as $pattern) { |
| 258 | - if(fnmatch($pattern, $url)) return true; |
|
| 264 | + if(fnmatch($pattern, $url)) { |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 259 | 267 | } |
| 260 | 268 | |
| 261 | 269 | return false; |
@@ -27,8 +27,8 @@ discard block |
||
| 27 | 27 | public static function domain_for_url($url) { |
| 28 | 28 | $url = trim($url, '/'); |
| 29 | 29 | |
| 30 | - foreach(self::get_all_domains() as $domain) { |
|
| 31 | - if($domain->hasURL($url)) { |
|
| 30 | + foreach (self::get_all_domains() as $domain) { |
|
| 31 | + if ($domain->hasURL($url)) { |
|
| 32 | 32 | return $domain; |
| 33 | 33 | } |
| 34 | 34 | } |
@@ -43,10 +43,10 @@ discard block |
||
| 43 | 43 | * @return array |
| 44 | 44 | */ |
| 45 | 45 | public static function get_all_domains($includePrimary = false) { |
| 46 | - $domains = array (); |
|
| 46 | + $domains = array(); |
|
| 47 | 47 | |
| 48 | - foreach(self::config()->domains as $key => $config) { |
|
| 49 | - if(!$includePrimary && $key === self::KEY_PRIMARY) continue; |
|
| 48 | + foreach (self::config()->domains as $key => $config) { |
|
| 49 | + if (!$includePrimary && $key === self::KEY_PRIMARY) continue; |
|
| 50 | 50 | $domains[] = MultiDomainDomain::create($key, $config); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * @return MultiDomainDomain |
| 68 | 68 | */ |
| 69 | 69 | public static function get_domain($domain) { |
| 70 | - if(isset(self::config()->domains[$domain])) { |
|
| 70 | + if (isset(self::config()->domains[$domain])) { |
|
| 71 | 71 | return MultiDomainDomain::create( |
| 72 | 72 | $domain, |
| 73 | 73 | self::config()->domains[$domain] |
@@ -2,168 +2,168 @@ |
||
| 2 | 2 | |
| 3 | 3 | class MultiDomainDomainTest extends SapphireTest |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Set up some test domain data for testing |
|
| 7 | - * |
|
| 8 | - * {@inheritDoc} |
|
| 9 | - */ |
|
| 10 | - public function setUp() |
|
| 11 | - { |
|
| 12 | - parent::setUp(); |
|
| 13 | - Config::nest(); |
|
| 14 | - |
|
| 15 | - Config::inst()->remove('MultiDomain', 'domains'); |
|
| 16 | - Config::inst()->update('MultiDomain', 'domains', array( |
|
| 17 | - 'primary' => array( |
|
| 18 | - 'hostname' => 'example.com' |
|
| 19 | - ), |
|
| 20 | - 'store' => array( |
|
| 21 | - 'hostname' => 'example-store.com', |
|
| 22 | - 'resolves_to' => 'shop/store', |
|
| 23 | - 'allow' => array( |
|
| 24 | - 'admin/*', |
|
| 25 | - 'Security/*', |
|
| 26 | - 'my-custom-webhook/' |
|
| 27 | - ) |
|
| 28 | - ), |
|
| 29 | - 'configurable' => array( |
|
| 30 | - 'hostname' => 'MY_CONSTANT_HOSTNAME' |
|
| 31 | - ), |
|
| 32 | - 'forceful' => array( |
|
| 33 | - 'hostname' => 'forced.com', |
|
| 34 | - 'force' => array( |
|
| 35 | - 'buy-now/*' |
|
| 36 | - ) |
|
| 37 | - ) |
|
| 38 | - )); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Test that a hostname defined in a constant will override the default configuration, otherwise the default |
|
| 43 | - * configuration for the domain is returned |
|
| 44 | - */ |
|
| 45 | - public function testGetHostname() |
|
| 46 | - { |
|
| 47 | - $configurableDomain = MultiDomain::get_domain('configurable'); |
|
| 48 | - define('MY_CONSTANT_HOSTNAME', 'I am a constant'); |
|
| 49 | - $this->assertSame('I am a constant', $configurableDomain->getHostname()); |
|
| 50 | - |
|
| 51 | - $storeDomain = MultiDomain::get_domain('store'); |
|
| 52 | - $this->assertSame('example-store.com', $storeDomain->getHostname()); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null |
|
| 57 | - */ |
|
| 58 | - public function testGetUrl() |
|
| 59 | - { |
|
| 60 | - $primaryDomain = MultiDomain::get_domain('primary'); |
|
| 61 | - $this->assertNull($primaryDomain->getURL()); |
|
| 62 | - |
|
| 63 | - $storeDomain = MultiDomain::get_domain('store'); |
|
| 64 | - $this->assertSame('shop/store', $storeDomain->getURL()); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Test that a domain can be identified as the primary domain or otherwise |
|
| 69 | - */ |
|
| 70 | - public function testIsPrimary() |
|
| 71 | - { |
|
| 72 | - $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); |
|
| 73 | - $this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * When the request URI matches one of the allowed rules for a domain, the isActive method should return false |
|
| 78 | - */ |
|
| 79 | - public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() |
|
| 80 | - { |
|
| 81 | - $domain = MultiDomain::get_domain('store'); |
|
| 82 | - $domain->setRequestUri('/Security/login'); |
|
| 83 | - $this->assertFalse($domain->isActive()); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well |
|
| 88 | - * as the primary domain |
|
| 89 | - */ |
|
| 90 | - public function testSubdomainsAllowedInIsActiveWhenConfigured() |
|
| 91 | - { |
|
| 92 | - Config::inst()->update('MultiDomain', 'allow_subdomains', true); |
|
| 93 | - |
|
| 94 | - $domain = MultiDomain::get_domain('store') |
|
| 95 | - ->setRequestUri('/some/page') |
|
| 96 | - ->setHttpHost('api.example-store.com'); |
|
| 97 | - |
|
| 98 | - $this->assertTrue($domain->isActive()); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * The default behaviour would be that if the current host from the request matchese that of the domain model |
|
| 103 | - * then isActive should be true |
|
| 104 | - */ |
|
| 105 | - public function testReturnActiveIfCurrentHostMatchesDomainsHostname() |
|
| 106 | - { |
|
| 107 | - $domain = MultiDomain::get_domain('primary') |
|
| 108 | - ->setRequestUri('/another/page') |
|
| 109 | - ->setHttpHost('example.com'); |
|
| 110 | - |
|
| 111 | - $this->assertTrue($domain->isActive()); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * getNativeUrl should not be used on the primary domain |
|
| 116 | - * |
|
| 117 | - * @expectedException Exception |
|
| 118 | - * @expectedExceptionMessage Cannot convert a native URL on the primary domain |
|
| 119 | - */ |
|
| 120 | - public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() |
|
| 121 | - { |
|
| 122 | - MultiDomain::get_primary_domain()->getNativeUrl('foo'); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Test that a URL segment can be added to the domain's URL and returned as a "native URL" |
|
| 127 | - */ |
|
| 128 | - public function testGetNativeUrl() |
|
| 129 | - { |
|
| 130 | - $domain = MultiDomain::get_domain('store'); |
|
| 131 | - $this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is |
|
| 136 | - */ |
|
| 137 | - public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() |
|
| 138 | - { |
|
| 139 | - $domain = MultiDomain::get_domain('store'); |
|
| 140 | - $this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); |
|
| 141 | - |
|
| 142 | - $domain = MultiDomain::get_domain('forceful'); |
|
| 143 | - $this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * The primary domain and "allowed" route matches should be returned as it |
|
| 148 | - */ |
|
| 149 | - public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() |
|
| 150 | - { |
|
| 151 | - $this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); |
|
| 152 | - $this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Non-primary domains and un-allowed route matches should be returned without their URL for vanity |
|
| 157 | - */ |
|
| 158 | - public function testGetVanityUrl() |
|
| 159 | - { |
|
| 160 | - $this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); |
|
| 161 | - $this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function tearDown() |
|
| 165 | - { |
|
| 166 | - Config::unnest(); |
|
| 167 | - parent::tearDown(); |
|
| 168 | - } |
|
| 5 | + /** |
|
| 6 | + * Set up some test domain data for testing |
|
| 7 | + * |
|
| 8 | + * {@inheritDoc} |
|
| 9 | + */ |
|
| 10 | + public function setUp() |
|
| 11 | + { |
|
| 12 | + parent::setUp(); |
|
| 13 | + Config::nest(); |
|
| 14 | + |
|
| 15 | + Config::inst()->remove('MultiDomain', 'domains'); |
|
| 16 | + Config::inst()->update('MultiDomain', 'domains', array( |
|
| 17 | + 'primary' => array( |
|
| 18 | + 'hostname' => 'example.com' |
|
| 19 | + ), |
|
| 20 | + 'store' => array( |
|
| 21 | + 'hostname' => 'example-store.com', |
|
| 22 | + 'resolves_to' => 'shop/store', |
|
| 23 | + 'allow' => array( |
|
| 24 | + 'admin/*', |
|
| 25 | + 'Security/*', |
|
| 26 | + 'my-custom-webhook/' |
|
| 27 | + ) |
|
| 28 | + ), |
|
| 29 | + 'configurable' => array( |
|
| 30 | + 'hostname' => 'MY_CONSTANT_HOSTNAME' |
|
| 31 | + ), |
|
| 32 | + 'forceful' => array( |
|
| 33 | + 'hostname' => 'forced.com', |
|
| 34 | + 'force' => array( |
|
| 35 | + 'buy-now/*' |
|
| 36 | + ) |
|
| 37 | + ) |
|
| 38 | + )); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Test that a hostname defined in a constant will override the default configuration, otherwise the default |
|
| 43 | + * configuration for the domain is returned |
|
| 44 | + */ |
|
| 45 | + public function testGetHostname() |
|
| 46 | + { |
|
| 47 | + $configurableDomain = MultiDomain::get_domain('configurable'); |
|
| 48 | + define('MY_CONSTANT_HOSTNAME', 'I am a constant'); |
|
| 49 | + $this->assertSame('I am a constant', $configurableDomain->getHostname()); |
|
| 50 | + |
|
| 51 | + $storeDomain = MultiDomain::get_domain('store'); |
|
| 52 | + $this->assertSame('example-store.com', $storeDomain->getHostname()); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null |
|
| 57 | + */ |
|
| 58 | + public function testGetUrl() |
|
| 59 | + { |
|
| 60 | + $primaryDomain = MultiDomain::get_domain('primary'); |
|
| 61 | + $this->assertNull($primaryDomain->getURL()); |
|
| 62 | + |
|
| 63 | + $storeDomain = MultiDomain::get_domain('store'); |
|
| 64 | + $this->assertSame('shop/store', $storeDomain->getURL()); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Test that a domain can be identified as the primary domain or otherwise |
|
| 69 | + */ |
|
| 70 | + public function testIsPrimary() |
|
| 71 | + { |
|
| 72 | + $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); |
|
| 73 | + $this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * When the request URI matches one of the allowed rules for a domain, the isActive method should return false |
|
| 78 | + */ |
|
| 79 | + public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() |
|
| 80 | + { |
|
| 81 | + $domain = MultiDomain::get_domain('store'); |
|
| 82 | + $domain->setRequestUri('/Security/login'); |
|
| 83 | + $this->assertFalse($domain->isActive()); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well |
|
| 88 | + * as the primary domain |
|
| 89 | + */ |
|
| 90 | + public function testSubdomainsAllowedInIsActiveWhenConfigured() |
|
| 91 | + { |
|
| 92 | + Config::inst()->update('MultiDomain', 'allow_subdomains', true); |
|
| 93 | + |
|
| 94 | + $domain = MultiDomain::get_domain('store') |
|
| 95 | + ->setRequestUri('/some/page') |
|
| 96 | + ->setHttpHost('api.example-store.com'); |
|
| 97 | + |
|
| 98 | + $this->assertTrue($domain->isActive()); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * The default behaviour would be that if the current host from the request matchese that of the domain model |
|
| 103 | + * then isActive should be true |
|
| 104 | + */ |
|
| 105 | + public function testReturnActiveIfCurrentHostMatchesDomainsHostname() |
|
| 106 | + { |
|
| 107 | + $domain = MultiDomain::get_domain('primary') |
|
| 108 | + ->setRequestUri('/another/page') |
|
| 109 | + ->setHttpHost('example.com'); |
|
| 110 | + |
|
| 111 | + $this->assertTrue($domain->isActive()); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * getNativeUrl should not be used on the primary domain |
|
| 116 | + * |
|
| 117 | + * @expectedException Exception |
|
| 118 | + * @expectedExceptionMessage Cannot convert a native URL on the primary domain |
|
| 119 | + */ |
|
| 120 | + public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() |
|
| 121 | + { |
|
| 122 | + MultiDomain::get_primary_domain()->getNativeUrl('foo'); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Test that a URL segment can be added to the domain's URL and returned as a "native URL" |
|
| 127 | + */ |
|
| 128 | + public function testGetNativeUrl() |
|
| 129 | + { |
|
| 130 | + $domain = MultiDomain::get_domain('store'); |
|
| 131 | + $this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is |
|
| 136 | + */ |
|
| 137 | + public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() |
|
| 138 | + { |
|
| 139 | + $domain = MultiDomain::get_domain('store'); |
|
| 140 | + $this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); |
|
| 141 | + |
|
| 142 | + $domain = MultiDomain::get_domain('forceful'); |
|
| 143 | + $this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * The primary domain and "allowed" route matches should be returned as it |
|
| 148 | + */ |
|
| 149 | + public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() |
|
| 150 | + { |
|
| 151 | + $this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); |
|
| 152 | + $this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Non-primary domains and un-allowed route matches should be returned without their URL for vanity |
|
| 157 | + */ |
|
| 158 | + public function testGetVanityUrl() |
|
| 159 | + { |
|
| 160 | + $this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); |
|
| 161 | + $this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function tearDown() |
|
| 165 | + { |
|
| 166 | + Config::unnest(); |
|
| 167 | + parent::tearDown(); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -2,96 +2,96 @@ |
||
| 2 | 2 | |
| 3 | 3 | class MultiDomainTest extends SapphireTest |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Set up some test domain configuration |
|
| 7 | - * |
|
| 8 | - * {@inheritDoc} |
|
| 9 | - */ |
|
| 10 | - public function setUp() |
|
| 11 | - { |
|
| 12 | - parent::setUp(); |
|
| 13 | - Config::nest(); |
|
| 5 | + /** |
|
| 6 | + * Set up some test domain configuration |
|
| 7 | + * |
|
| 8 | + * {@inheritDoc} |
|
| 9 | + */ |
|
| 10 | + public function setUp() |
|
| 11 | + { |
|
| 12 | + parent::setUp(); |
|
| 13 | + Config::nest(); |
|
| 14 | 14 | |
| 15 | - Config::inst()->remove('MultiDomain', 'domains'); |
|
| 16 | - Config::inst()->update('MultiDomain', 'domains', array( |
|
| 17 | - 'primary' => array( |
|
| 18 | - 'hostname' => 'foo.bar', |
|
| 19 | - 'resolves_to' => 'bar.baz' |
|
| 20 | - ), |
|
| 21 | - 'secondary' => array( |
|
| 22 | - 'hostname' => 'localhost', |
|
| 23 | - 'resolves_to' => 'local.dev' |
|
| 24 | - ) |
|
| 25 | - )); |
|
| 26 | - } |
|
| 15 | + Config::inst()->remove('MultiDomain', 'domains'); |
|
| 16 | + Config::inst()->update('MultiDomain', 'domains', array( |
|
| 17 | + 'primary' => array( |
|
| 18 | + 'hostname' => 'foo.bar', |
|
| 19 | + 'resolves_to' => 'bar.baz' |
|
| 20 | + ), |
|
| 21 | + 'secondary' => array( |
|
| 22 | + 'hostname' => 'localhost', |
|
| 23 | + 'resolves_to' => 'local.dev' |
|
| 24 | + ) |
|
| 25 | + )); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Test that a MultiDomainDomain can be returned from the configured domains |
|
| 30 | - */ |
|
| 31 | - public function testGetDomain() |
|
| 32 | - { |
|
| 33 | - $this->assertNull(MultiDomain::get_domain('does-not-exist')); |
|
| 34 | - $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * Test that a MultiDomainDomain can be returned from the configured domains |
|
| 30 | + */ |
|
| 31 | + public function testGetDomain() |
|
| 32 | + { |
|
| 33 | + $this->assertNull(MultiDomain::get_domain('does-not-exist')); |
|
| 34 | + $this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Test that all domains can be returned, with or without the primary domain |
|
| 39 | - * |
|
| 40 | - * @dataProvider getAllDomainsProvider |
|
| 41 | - * @param bool $withPrimary |
|
| 42 | - */ |
|
| 43 | - public function testGetAllDomains($withPrimary) |
|
| 44 | - { |
|
| 45 | - $result = MultiDomain::get_all_domains($withPrimary); |
|
| 46 | - $this->assertInternalType('array', $result); |
|
| 47 | - $this->assertNotEmpty($result); |
|
| 37 | + /** |
|
| 38 | + * Test that all domains can be returned, with or without the primary domain |
|
| 39 | + * |
|
| 40 | + * @dataProvider getAllDomainsProvider |
|
| 41 | + * @param bool $withPrimary |
|
| 42 | + */ |
|
| 43 | + public function testGetAllDomains($withPrimary) |
|
| 44 | + { |
|
| 45 | + $result = MultiDomain::get_all_domains($withPrimary); |
|
| 46 | + $this->assertInternalType('array', $result); |
|
| 47 | + $this->assertNotEmpty($result); |
|
| 48 | 48 | |
| 49 | - $expectedCount = $withPrimary ? 2 : 1; |
|
| 50 | - $this->assertCount($expectedCount, $result); |
|
| 51 | - } |
|
| 49 | + $expectedCount = $withPrimary ? 2 : 1; |
|
| 50 | + $this->assertCount($expectedCount, $result); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return array[] |
|
| 55 | - */ |
|
| 56 | - public function getAllDomainsProvider() |
|
| 57 | - { |
|
| 58 | - return array( |
|
| 59 | - array(true), |
|
| 60 | - array(false) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 53 | + /** |
|
| 54 | + * @return array[] |
|
| 55 | + */ |
|
| 56 | + public function getAllDomainsProvider() |
|
| 57 | + { |
|
| 58 | + return array( |
|
| 59 | + array(true), |
|
| 60 | + array(false) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Test that the primary domain can be returned |
|
| 66 | - */ |
|
| 67 | - public function testGetPrimaryDomain() |
|
| 68 | - { |
|
| 69 | - $result = MultiDomain::get_primary_domain(); |
|
| 70 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
| 71 | - $this->assertTrue($result->isPrimary()); |
|
| 72 | - } |
|
| 64 | + /** |
|
| 65 | + * Test that the primary domain can be returned |
|
| 66 | + */ |
|
| 67 | + public function testGetPrimaryDomain() |
|
| 68 | + { |
|
| 69 | + $result = MultiDomain::get_primary_domain(); |
|
| 70 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
| 71 | + $this->assertTrue($result->isPrimary()); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Test that the correct domain can be returned by a provided URL |
|
| 76 | - */ |
|
| 77 | - public function testDomainForUrl() |
|
| 78 | - { |
|
| 79 | - $result = MultiDomain::domain_for_url('foo.bar/my-page'); |
|
| 80 | - $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
| 81 | - $this->assertSame('primary', $result->getKey()); |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * Test that the correct domain can be returned by a provided URL |
|
| 76 | + */ |
|
| 77 | + public function testDomainForUrl() |
|
| 78 | + { |
|
| 79 | + $result = MultiDomain::domain_for_url('foo.bar/my-page'); |
|
| 80 | + $this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
| 81 | + $this->assertSame('primary', $result->getKey()); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Test that if a URL doesn't match any domain then the primary domain is returned |
|
| 86 | - */ |
|
| 87 | - public function testDomainForUrlDefaultsToPrimaryDomain() |
|
| 88 | - { |
|
| 89 | - $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
|
| 90 | - } |
|
| 84 | + /** |
|
| 85 | + * Test that if a URL doesn't match any domain then the primary domain is returned |
|
| 86 | + */ |
|
| 87 | + public function testDomainForUrlDefaultsToPrimaryDomain() |
|
| 88 | + { |
|
| 89 | + $this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - public function tearDown() |
|
| 93 | - { |
|
| 94 | - Config::unnest(); |
|
| 95 | - parent::tearDown(); |
|
| 96 | - } |
|
| 92 | + public function tearDown() |
|
| 93 | + { |
|
| 94 | + Config::unnest(); |
|
| 95 | + parent::tearDown(); |
|
| 96 | + } |
|
| 97 | 97 | } |