]>
Commit | Line | Data |
---|---|---|
6c50a6cc A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Config\ConfigManager; | |
6c50a6cc | 9 | use Shaarli\Front\Exception\LoginBannedException; |
6c50a6cc A |
10 | use Slim\Http\Request; |
11 | use Slim\Http\Response; | |
12 | ||
13 | class LoginControllerTest extends TestCase | |
14 | { | |
dd09ec52 | 15 | use FrontControllerMockHelper; |
6c50a6cc A |
16 | |
17 | /** @var LoginController */ | |
18 | protected $controller; | |
19 | ||
20 | public function setUp(): void | |
21 | { | |
dd09ec52 A |
22 | $this->createContainer(); |
23 | ||
6c50a6cc A |
24 | $this->controller = new LoginController($this->container); |
25 | } | |
26 | ||
27 | public function testValidControllerInvoke(): void | |
28 | { | |
29 | $this->createValidContainerMockSet(); | |
30 | ||
31 | $request = $this->createMock(Request::class); | |
32 | $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); | |
33 | $response = new Response(); | |
34 | ||
35 | $assignedVariables = []; | |
36 | $this->container->pageBuilder | |
6c50a6cc A |
37 | ->method('assign') |
38 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
39 | $assignedVariables[$key] = $value; | |
40 | ||
41 | return $this; | |
42 | }) | |
43 | ; | |
44 | ||
dd09ec52 A |
45 | $this->container->loginManager->method('canLogin')->willReturn(true); |
46 | ||
6c50a6cc A |
47 | $result = $this->controller->index($request, $response); |
48 | ||
49 | static::assertInstanceOf(Response::class, $result); | |
50 | static::assertSame(200, $result->getStatusCode()); | |
51 | static::assertSame('loginform', (string) $result->getBody()); | |
52 | ||
53 | static::assertSame('> referer', $assignedVariables['returnurl']); | |
54 | static::assertSame(true, $assignedVariables['remember_user_default']); | |
55 | static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); | |
56 | } | |
57 | ||
58 | public function testValidControllerInvokeWithUserName(): void | |
59 | { | |
60 | $this->createValidContainerMockSet(); | |
61 | ||
62 | $request = $this->createMock(Request::class); | |
63 | $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); | |
64 | $request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>'); | |
65 | $response = new Response(); | |
66 | ||
67 | $assignedVariables = []; | |
68 | $this->container->pageBuilder | |
6c50a6cc A |
69 | ->method('assign') |
70 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
71 | $assignedVariables[$key] = $value; | |
72 | ||
73 | return $this; | |
74 | }) | |
75 | ; | |
76 | ||
dd09ec52 A |
77 | $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true); |
78 | ||
6c50a6cc A |
79 | $result = $this->controller->index($request, $response); |
80 | ||
81 | static::assertInstanceOf(Response::class, $result); | |
82 | static::assertSame(200, $result->getStatusCode()); | |
83 | static::assertSame('loginform', (string) $result->getBody()); | |
84 | ||
85 | static::assertSame('myUser>', $assignedVariables['username']); | |
86 | static::assertSame('> referer', $assignedVariables['returnurl']); | |
87 | static::assertSame(true, $assignedVariables['remember_user_default']); | |
88 | static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); | |
89 | } | |
90 | ||
91 | public function testLoginControllerWhileLoggedIn(): void | |
92 | { | |
dd09ec52 A |
93 | $this->createValidContainerMockSet(); |
94 | ||
6c50a6cc A |
95 | $request = $this->createMock(Request::class); |
96 | $response = new Response(); | |
97 | ||
dd09ec52 | 98 | $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true); |
6c50a6cc A |
99 | |
100 | $result = $this->controller->index($request, $response); | |
101 | ||
102 | static::assertInstanceOf(Response::class, $result); | |
103 | static::assertSame(302, $result->getStatusCode()); | |
104 | static::assertSame(['./'], $result->getHeader('Location')); | |
105 | } | |
106 | ||
107 | public function testLoginControllerOpenShaarli(): void | |
108 | { | |
109 | $this->createValidContainerMockSet(); | |
110 | ||
111 | $request = $this->createMock(Request::class); | |
112 | $response = new Response(); | |
113 | ||
114 | $conf = $this->createMock(ConfigManager::class); | |
115 | $conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
116 | if ($parameter === 'security.open_shaarli') { | |
117 | return true; | |
118 | } | |
119 | return $default; | |
120 | }); | |
121 | $this->container->conf = $conf; | |
122 | ||
123 | $result = $this->controller->index($request, $response); | |
124 | ||
125 | static::assertInstanceOf(Response::class, $result); | |
126 | static::assertSame(302, $result->getStatusCode()); | |
127 | static::assertSame(['./'], $result->getHeader('Location')); | |
128 | } | |
129 | ||
130 | public function testLoginControllerWhileBanned(): void | |
131 | { | |
132 | $this->createValidContainerMockSet(); | |
133 | ||
134 | $request = $this->createMock(Request::class); | |
135 | $response = new Response(); | |
136 | ||
dd09ec52 A |
137 | $this->container->loginManager->method('isLoggedIn')->willReturn(false); |
138 | $this->container->loginManager->method('canLogin')->willReturn(false); | |
6c50a6cc A |
139 | |
140 | $this->expectException(LoginBannedException::class); | |
141 | ||
142 | $this->controller->index($request, $response); | |
143 | } | |
6c50a6cc | 144 | } |