Completed
Pull Request — master (#428)
by
unknown
03:25
created

testDomainDetectionViaServerHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Subsites\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Subsites\Middleware\InitStateMiddleware;
8
use SilverStripe\Subsites\Model\Subsite;
9
use SilverStripe\Subsites\State\SubsiteState;
10
11
class InitStateMiddlewareTest extends BaseSubsiteTest
12
{
13
    protected static $fixture_file = 'SubsiteTest.yml';
14
15
    /**
16
     * Original value of $_REQUEST
17
     *
18
     * @var array
19
     */
20
    protected $origServer = [];
21
22
    protected function setUp() {
23
        parent::setUp();
24
25
        $this->origServer = $_SERVER;
26
    }
27
28
    protected function tearDown() {
29
        $_SERVER = $this->origServer;
30
31
        parent::tearDown();
32
    }
33
34
    public function testDomainDetectionViaServerHeaders() {
35
        $_SERVER['HTTP_HOST'] = 'one.example.org';
36
37
        $this->getMiddleware()->process($this->getRequest(), $this->getCallback());
38
39
        $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest1');
40
        $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId());
41
    }
42
43
    public function testDomainDetectionViaRequestOverridesServerHeaders() {
44
        $_SERVER['HTTP_HOST'] = 'one.example.org';
45
46
        $this->getMiddleware()->process($this->getRequest('two.mysite.com'), $this->getCallback());
47
48
        $expectedSubsite = $this->objFromFixture(Subsite::class, 'domaintest2');
49
        $this->assertEquals($expectedSubsite->ID, $this->getState()->getSubsiteId());
50
    }
51
52
    protected function getMiddleware() {
53
        return new InitStateMiddleware();
54
    }
55
56
    protected function getRequest($domain = null) {
57
        $request = new HTTPRequest('GET', "/test/url");
58
        if ($domain) {
59
            $request->addHeader('host', $domain);
60
        }
61
        return $request;
62
    }
63
64
    protected function getCallback() {
65
        return function () {};
66
    }
67
68
    protected function getState() {
69
        return Injector::inst()->get(SubsiteState::class);
70
    }
71
}
72