-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect domains correctly in Director sub-calls
Previously it relied on the PHP-level $_SERVER variable; now it will use the HTTPRequest so it works correctly in more situations.
- Loading branch information
1 parent
ee96159
commit 46a8635
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Subsites\Tests; | ||
|
||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Subsites\Middleware\InitStateMiddleware; | ||
use SilverStripe\Subsites\Model\Subsite; | ||
use SilverStripe\Subsites\State\SubsiteState; | ||
|
||
class InitStateMiddlewareTest extends BaseSubsiteTest | ||
{ | ||
protected static $fixture_file = 'SubsiteTest.yml'; | ||
|
||
/** | ||
* Original value of $_REQUEST | ||
* | ||
* @var array | ||
*/ | ||
protected $origServer = []; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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); | ||
} | ||
} |