Passed
Push — master ( 0208b2...1155ca )
by Robbie
09:03
created

CanonicalURLMiddlewareTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent() 0 12 1
A testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled() 0 14 1
A testGetForceBasicAuthToSSL() 0 16 1
A testHttpsIsForcedForBasicAuth() 0 16 1
A setUp() 0 12 1
1
<?php
2
3
namespace SilverStripe\Control\Tests\Middleware;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Control\Middleware\CanonicalURLMiddleware;
8
use SilverStripe\Dev\SapphireTest;
9
10
class CanonicalURLMiddlewareTest extends SapphireTest
11
{
12
    /**
13
     * Stub middleware class, partially mocked
14
     *
15
     * @var CanonicalURLMiddleware
16
     */
17
    protected $middleware;
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
23
        /** @var CanonicalURLMiddleware $middleware */
24
        $this->middleware = $this->getMockBuilder(CanonicalURLMiddleware::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Si...tRedirect'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type SilverStripe\Control\Mid...\CanonicalURLMiddleware of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
            ->setMethods(['getRedirect'])
26
            ->getMock();
27
28
        $this->middleware->expects($this->any())->method('getRedirect')->willReturn(false);
29
30
        $this->middleware->setForceBasicAuthToSSL(true);
0 ignored issues
show
Bug introduced by
The method setForceBasicAuthToSSL() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $this->middleware->/** @scrutinizer ignore-call */ 
31
                           setForceBasicAuthToSSL(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
    }
32
33
    public function testHttpsIsForcedForBasicAuth()
34
    {
35
        $this->middleware->expects($this->once())->method('getRedirect');
0 ignored issues
show
Bug introduced by
The method expects() does not exist on SilverStripe\Control\Mid...\CanonicalURLMiddleware. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $this->middleware->/** @scrutinizer ignore-call */ 
36
                           expects($this->once())->method('getRedirect');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
        $request = new HTTPRequest('GET', '/');
38
        $mockResponse = (new HTTPResponse)
39
            ->addHeader('WWW-Authenticate', 'basic')
40
            ->setStatusCode(401);
41
42
        $result = $this->middleware->process($request, function () use ($mockResponse) {
43
            return $mockResponse;
44
        });
45
46
        $this->assertNotSame($mockResponse, $result, 'New response is created and returned');
47
        $this->assertEquals(301, $result->getStatusCode(), 'Basic auth responses are redirected');
48
        $this->assertContains('https://', $result->getHeader('Location'), 'HTTPS is in the redirect location');
49
    }
50
51
    public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled()
52
    {
53
        $this->middleware->expects($this->once())->method('getRedirect');
54
        $this->middleware->setForceBasicAuthToSSL(false);
55
56
        $request = new HTTPRequest('GET', '/');
57
        $mockResponse = (new HTTPResponse)
58
            ->addHeader('WWW-Authenticate', 'basic')
59
            ->setStatusCode(401);
60
61
        $result = $this->middleware->process($request, function () use ($mockResponse) {
62
            return $mockResponse;
63
        });
64
        $this->assertSame($mockResponse, $result, 'Response returned verbatim with auto redirect disabled');
65
    }
66
67
    public function testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent()
68
    {
69
        $this->middleware->expects($this->once())->method('getRedirect');
70
71
        $request = new HTTPRequest('GET', '/');
72
        $mockResponse = (new HTTPResponse)->addHeader('Foo', 'bar');
73
74
        $result = $this->middleware->process($request, function () use ($mockResponse) {
75
            return $mockResponse;
76
        });
77
78
        $this->assertSame($mockResponse, $result, 'Non basic-auth responses are returned verbatim');
79
    }
80
81
    public function testGetForceBasicAuthToSSL()
82
    {
83
        $this->middleware->setForceBasicAuthToSSL(null);
84
85
        $this->middleware->setForceSSL(true);
86
        $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
87
88
        $this->middleware->setForceSSL(false);
89
        $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
90
91
        $this->middleware->setForceBasicAuthToSSL(true);
92
        $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
93
94
        $this->middleware->setForceBasicAuthToSSL(false);
95
        $this->middleware->setForceSSL(true);
96
        $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
97
    }
98
}
99