aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ShaarliAdminControllerTest.php
blob: 7c5f50a67f7575ba462bea3482c9140e699ce9b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use PHPUnit\Framework\TestCase;
use Shaarli\Front\Exception\UnauthorizedException;
use Shaarli\Front\Exception\WrongTokenException;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;

/**
 * Class ShaarliControllerTest
 *
 * This class is used to test default behavior of ShaarliAdminController abstract class.
 * It uses a dummy non abstract controller.
 */
class ShaarliAdminControllerTest extends TestCase
{
    use FrontAdminControllerMockHelper;

    /** @var ShaarliAdminController */
    protected $controller;

    public function setUp(): void
    {
        $this->createContainer();

        $this->controller = new class($this->container) extends ShaarliAdminController
        {
            public function checkToken(Request $request): bool
            {
                return parent::checkToken($request);
            }

            public function saveSuccessMessage(string $message): void
            {
                parent::saveSuccessMessage($message);
            }

            public function saveWarningMessage(string $message): void
            {
                parent::saveWarningMessage($message);
            }

            public function saveErrorMessage(string $message): void
            {
                parent::saveErrorMessage($message);
            }
        };
    }

    /**
     * Creating an instance of an admin controller while logged out should raise an exception.
     */
    public function testInstantiateWhileLoggedOut(): void
    {
        $this->expectException(UnauthorizedException::class);

        $this->container->loginManager = $this->createMock(LoginManager::class);
        $this->container->loginManager->method('isLoggedIn')->willReturn(false);

        $this->controller = new class($this->container) extends ShaarliAdminController {};
    }

    /**
     * Trigger controller's checkToken with a valid token.
     */
    public function testCheckTokenWithValidToken(): void
    {
        $request = $this->createMock(Request::class);
        $request->method('getParam')->with('token')->willReturn($token = '12345');

        $this->container->sessionManager = $this->createMock(SessionManager::class);
        $this->container->sessionManager->method('checkToken')->with($token)->willReturn(true);

        static::assertTrue($this->controller->checkToken($request));
    }

    /**
     * Trigger controller's checkToken with na valid token should raise an exception.
     */
    public function testCheckTokenWithNotValidToken(): void
    {
        $request = $this->createMock(Request::class);
        $request->method('getParam')->with('token')->willReturn($token = '12345');

        $this->container->sessionManager = $this->createMock(SessionManager::class);
        $this->container->sessionManager->method('checkToken')->with($token)->willReturn(false);

        $this->expectException(WrongTokenException::class);

        $this->controller->checkToken($request);
    }

    /**
     * Test saveSuccessMessage() with a first message.
     */
    public function testSaveSuccessMessage(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_SUCCESS_MESSAGES, [$message = 'bravo!'])
        ;

        $this->controller->saveSuccessMessage($message);
    }

    /**
     * Test saveSuccessMessage() with existing messages.
     */
    public function testSaveSuccessMessageWithExistingMessages(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('getSessionParameter')
            ->with(SessionManager::KEY_SUCCESS_MESSAGES)
            ->willReturn(['success1', 'success2'])
        ;
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_SUCCESS_MESSAGES, ['success1', 'success2', $message = 'bravo!'])
        ;

        $this->controller->saveSuccessMessage($message);
    }

    /**
     * Test saveWarningMessage() with a first message.
     */
    public function testSaveWarningMessage(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_WARNING_MESSAGES, [$message = 'warning!'])
        ;

        $this->controller->saveWarningMessage($message);
    }

    /**
     * Test saveWarningMessage() with existing messages.
     */
    public function testSaveWarningMessageWithExistingMessages(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('getSessionParameter')
            ->with(SessionManager::KEY_WARNING_MESSAGES)
            ->willReturn(['warning1', 'warning2'])
        ;
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_WARNING_MESSAGES, ['warning1', 'warning2', $message = 'warning!'])
        ;

        $this->controller->saveWarningMessage($message);
    }

    /**
     * Test saveErrorMessage() with a first message.
     */
    public function testSaveErrorMessage(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, [$message = 'error!'])
        ;

        $this->controller->saveErrorMessage($message);
    }

    /**
     * Test saveErrorMessage() with existing messages.
     */
    public function testSaveErrorMessageWithExistingMessages(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('getSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES)
            ->willReturn(['error1', 'error2'])
        ;
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, ['error1', 'error2', $message = 'error!'])
        ;

        $this->controller->saveErrorMessage($message);
    }
}