diff --git a/src/Middleware/InitStateMiddleware.php b/src/Middleware/InitStateMiddleware.php index d173754..f2c7199 100644 --- a/src/Middleware/InitStateMiddleware.php +++ b/src/Middleware/InitStateMiddleware.php @@ -94,7 +94,7 @@ class InitStateMiddleware implements HTTPMiddleware return (int) $request->getSession()->get('SubsiteID'); } - $subsiteIdFromDomain = Subsite::getSubsiteIDForDomain(); + $subsiteIdFromDomain = Subsite::getSubsiteIDForDomain($request->getHost()); if ($subsiteIdFromDomain !== null) { return (int) $subsiteIdFromDomain; } diff --git a/tests/php/InitStateMiddlewareTest.php b/tests/php/InitStateMiddlewareTest.php new file mode 100644 index 0000000..5476f72 --- /dev/null +++ b/tests/php/InitStateMiddlewareTest.php @@ -0,0 +1,80 @@ +origServer = $_SERVER; + } + + protected function tearDown() + { + $_SERVER = $this->origServer; + + parent::tearDown(); + } + + public function testDomainDetectionViaServerHeaders() + { + $_SERVER['HTTP_HOST'] = 'one.example.org'; + + $this->getMiddleware()->process($this->getRequest(), $this->getCallback()); + + $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest1'); + $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId()); + } + + public function testDomainDetectionViaRequestOverridesServerHeaders() + { + $_SERVER['HTTP_HOST'] = 'one.example.org'; + + $this->getMiddleware()->process($this->getRequest('two.mysite.com'), $this->getCallback()); + + $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest2'); + $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId()); + } + + protected function getMiddleware() + { + return new InitStateMiddleware(); + } + + protected function getRequest($domain = null) + { + $request = new HTTPRequest('GET', '/test/url'); + if ($domain) { + $request->addHeader('host', $domain); + } + return $request; + } + + protected function getCallback() + { + return function () { + }; + } + + protected function getState() + { + return Injector::inst()->get(SubsiteState::class); + } +}