er/MemcookieTest.php$0   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\memcookie\Controller;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Module\memcookie\Controller;
12
use SimpleSAML\Session;
13
use SimpleSAML\Utils;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Set of tests for the controllers in the "memcookie" module.
18
 *
19
 * @package SimpleSAML\Test
20
 */
21
final class MemcookieTest extends TestCase
22
{
23
    /** @var \SimpleSAML\Configuration */
24
    protected Configuration $authsources;
25
26
    /** @var \SimpleSAML\Configuration */
27
    protected Configuration $config;
28
29
    /** @var \SimpleSAML\Utils\HTTP */
30
    protected Utils\Http $http_utils;
31
32
    /** @var \SimpleSAML\Configuration */
33
    protected Configuration $module_config;
34
35
    /** @var \SimpleSAML\Session */
36
    protected Session $session;
37
38
39
    /**
40
     * Set up for each test.
41
     * @return void
42
     */
43
    protected function setUp(): void
44
    {
45
        parent::setUp();
46
47
        $this->config = Configuration::loadFromArray(
48
            [
49
                'module.enable' => ['memcookie' => true],
50
            ],
51
            '[ARRAY]',
52
            'simplesaml',
53
        );
54
55
        $session = $this->createMock(Session::class);
56
        $session->method('getData')->willReturn(['default-sp' => []]);
57
        /** @var \SimpleSAML\Session $session */
58
        $this->session = $session;
59
60
61
        $this->authsources = Configuration::loadFromArray(
62
            [
63
                'default-sp' => ['saml:SP'],
64
            ],
65
            '[ARRAY]',
66
            'simplesaml',
67
        );
68
        Configuration::setPreLoadedConfig($this->authsources, 'authsources.php', 'simplesaml');
69
70
        $this->http_utils = new class () extends Utils\HTTP {
71
            public function setCookie(string $name, ?string $value, ?array $params = null, bool $throw = true): void
72
            {
73
                // stub
74
            }
75
76
            public function redirectTrustedURL(string $url, array $parameters = []): void
77
            {
78
                // stub
79
            }
80
        };
81
82
        $this->module_config = Configuration::loadFromArray(
83
            [
84
                'authsource' => 'default-sp',
85
                'cookiename' => 'AuthMemCookie',
86
                'username' => 'uid',
87
                'groups' => null,
88
                'memcache.host' => '127.0.0.1',
89
                'memcache.port' => 11211,
90
            ],
91
            '[ARRAY]',
92
            'simplesaml',
93
        );
94
        Configuration::setPreLoadedConfig($this->module_config, 'module_authmemcookie.php', 'simplesaml');
95
    }
96
97
98
    /**
99
     * Test that a valid requests results in a RunnableResponse
100
     * @return void
101
     */
102
    public function testMemcookie(): void
103
    {
104
        $sysUtils = new Utils\System();
105
        if ($sysUtils->getOS() === $sysUtils::WINDOWS) {
106
            $this->markTestSkipped(
107
                'This test can only run on Linux because of the availability of the memcached-extension.',
108
            );
109
        }
110
111
        $_SERVER['REQUEST_METHOD'] = 'GET';
112
        $_SERVER['REQUEST_URI'] = '/module.php/memcookie/';
113
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
114
115
        $request = Request::create(
116
            '/module.php/memcookie/',
117
            'GET',
118
            [],
119
        );
120
121
        $c = new Controller\Memcookie($this->config, $this->session);
122
        $c->setHttpUtils($this->http_utils);
123
        $c->setAuthSimple(new class ('admin') extends Auth\Simple {
124
            public function requireAuth(array $params = []): void
125
            {
126
                // stub
127
            }
128
129
            public function getAttributes(): array
130
            {
131
                return ['uid' => ['dduck']];
132
            }
133
        });
134
135
        $response = $c->main($request);
136
137
        $this->assertInstanceOf(RunnableResponse::class, $response);
138
        $this->assertTrue($response->isSuccessful());
139
    }
140
}
141