]>
Commit | Line | Data |
---|---|---|
6c50a6cc A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
6c50a6cc A |
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 | { | |
6c50a6cc A |
29 | $request = $this->createMock(Request::class); |
30 | $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); | |
31 | $response = new Response(); | |
32 | ||
33 | $assignedVariables = []; | |
34 | $this->container->pageBuilder | |
6c50a6cc A |
35 | ->method('assign') |
36 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
37 | $assignedVariables[$key] = $value; | |
38 | ||
39 | return $this; | |
40 | }) | |
41 | ; | |
42 | ||
dd09ec52 A |
43 | $this->container->loginManager->method('canLogin')->willReturn(true); |
44 | ||
6c50a6cc A |
45 | $result = $this->controller->index($request, $response); |
46 | ||
47 | static::assertInstanceOf(Response::class, $result); | |
48 | static::assertSame(200, $result->getStatusCode()); | |
49 | static::assertSame('loginform', (string) $result->getBody()); | |
50 | ||
51 | static::assertSame('> referer', $assignedVariables['returnurl']); | |
52 | static::assertSame(true, $assignedVariables['remember_user_default']); | |
53 | static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); | |
54 | } | |
55 | ||
56 | public function testValidControllerInvokeWithUserName(): void | |
57 | { | |
6c50a6cc A |
58 | $request = $this->createMock(Request::class); |
59 | $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); | |
60 | $request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>'); | |
61 | $response = new Response(); | |
62 | ||
63 | $assignedVariables = []; | |
64 | $this->container->pageBuilder | |
6c50a6cc A |
65 | ->method('assign') |
66 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
67 | $assignedVariables[$key] = $value; | |
68 | ||
69 | return $this; | |
70 | }) | |
71 | ; | |
72 | ||
dd09ec52 A |
73 | $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true); |
74 | ||
6c50a6cc A |
75 | $result = $this->controller->index($request, $response); |
76 | ||
77 | static::assertInstanceOf(Response::class, $result); | |
78 | static::assertSame(200, $result->getStatusCode()); | |
79 | static::assertSame('loginform', (string) $result->getBody()); | |
80 | ||
81 | static::assertSame('myUser>', $assignedVariables['username']); | |
82 | static::assertSame('> referer', $assignedVariables['returnurl']); | |
83 | static::assertSame(true, $assignedVariables['remember_user_default']); | |
84 | static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); | |
85 | } | |
86 | ||
87 | public function testLoginControllerWhileLoggedIn(): void | |
88 | { | |
89 | $request = $this->createMock(Request::class); | |
90 | $response = new Response(); | |
91 | ||
dd09ec52 | 92 | $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true); |
6c50a6cc A |
93 | |
94 | $result = $this->controller->index($request, $response); | |
95 | ||
96 | static::assertInstanceOf(Response::class, $result); | |
97 | static::assertSame(302, $result->getStatusCode()); | |
98 | static::assertSame(['./'], $result->getHeader('Location')); | |
99 | } | |
100 | ||
101 | public function testLoginControllerOpenShaarli(): void | |
102 | { | |
6c50a6cc A |
103 | $request = $this->createMock(Request::class); |
104 | $response = new Response(); | |
105 | ||
106 | $conf = $this->createMock(ConfigManager::class); | |
107 | $conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
108 | if ($parameter === 'security.open_shaarli') { | |
109 | return true; | |
110 | } | |
111 | return $default; | |
112 | }); | |
113 | $this->container->conf = $conf; | |
114 | ||
115 | $result = $this->controller->index($request, $response); | |
116 | ||
117 | static::assertInstanceOf(Response::class, $result); | |
118 | static::assertSame(302, $result->getStatusCode()); | |
119 | static::assertSame(['./'], $result->getHeader('Location')); | |
120 | } | |
121 | ||
122 | public function testLoginControllerWhileBanned(): void | |
123 | { | |
6c50a6cc A |
124 | $request = $this->createMock(Request::class); |
125 | $response = new Response(); | |
126 | ||
dd09ec52 A |
127 | $this->container->loginManager->method('isLoggedIn')->willReturn(false); |
128 | $this->container->loginManager->method('canLogin')->willReturn(false); | |
6c50a6cc A |
129 | |
130 | $this->expectException(LoginBannedException::class); | |
131 | ||
132 | $this->controller->index($request, $response); | |
133 | } | |
6c50a6cc | 134 | } |