Completed
Push — master ( d8776d...afe412 )
by Robbie
46s queued 25s
created

testHttpsIsForcedForBasicAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
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() : void
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...
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

24
        $this->middleware = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(CanonicalURLMiddleware::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
25
            ->setMethods(['getRedirect'])
26
            ->getMock();
27
28
        $this->middleware->expects($this->any())->method('getRedirect')->willReturn(false);
29
30
        $this->middleware->setForceBasicAuthToSSL(true);
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->assertStringContainsString(
49
            'https://',
50
            $result->getHeader('Location'),
51
            'HTTPS is in the redirect location'
52
        );
53
    }
54
55
    public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled()
56
    {
57
        $this->middleware->expects($this->once())->method('getRedirect');
58
        $this->middleware->setForceBasicAuthToSSL(false);
59
60
        $request = new HTTPRequest('GET', '/');
61
        $mockResponse = (new HTTPResponse)
62
            ->addHeader('WWW-Authenticate', 'basic')
63
            ->setStatusCode(401);
64
65
        $result = $this->middleware->process($request, function () use ($mockResponse) {
66
            return $mockResponse;
67
        });
68
        $this->assertSame($mockResponse, $result, 'Response returned verbatim with auto redirect disabled');
69
    }
70
71
    public function testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent()
72
    {
73
        $this->middleware->expects($this->once())->method('getRedirect');
74
75
        $request = new HTTPRequest('GET', '/');
76
        $mockResponse = (new HTTPResponse)->addHeader('Foo', 'bar');
77
78
        $result = $this->middleware->process($request, function () use ($mockResponse) {
79
            return $mockResponse;
80
        });
81
82
        $this->assertSame($mockResponse, $result, 'Non basic-auth responses are returned verbatim');
83
    }
84
85
    public function testGetForceBasicAuthToSSL()
86
    {
87
        $this->middleware->setForceBasicAuthToSSL(null);
88
89
        $this->middleware->setForceSSL(true);
90
        $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
91
92
        $this->middleware->setForceSSL(false);
93
        $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
94
95
        $this->middleware->setForceBasicAuthToSSL(true);
96
        $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
97
98
        $this->middleware->setForceBasicAuthToSSL(false);
99
        $this->middleware->setForceSSL(true);
100
        $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
101
    }
102
}
103