]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Visitor; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Config\ConfigManager; | |
9 | use Shaarli\Front\Exception\LoginBannedException; | |
10 | use Slim\Http\Request; | |
11 | use Slim\Http\Response; | |
12 | ||
13 | class LoginControllerTest extends TestCase | |
14 | { | |
15 | use FrontControllerMockHelper; | |
16 | ||
17 | /** @var LoginController */ | |
18 | protected $controller; | |
19 | ||
20 | public function setUp(): void | |
21 | { | |
22 | $this->createContainer(); | |
23 | ||
24 | $this->controller = new LoginController($this->container); | |
25 | } | |
26 | ||
27 | public function testValidControllerInvoke(): void | |
28 | { | |
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 | |
35 | ->method('assign') | |
36 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
37 | $assignedVariables[$key] = $value; | |
38 | ||
39 | return $this; | |
40 | }) | |
41 | ; | |
42 | ||
43 | $this->container->loginManager->method('canLogin')->willReturn(true); | |
44 | ||
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 | { | |
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 | |
65 | ->method('assign') | |
66 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | |
67 | $assignedVariables[$key] = $value; | |
68 | ||
69 | return $this; | |
70 | }) | |
71 | ; | |
72 | ||
73 | $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true); | |
74 | ||
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 | ||
92 | $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true); | |
93 | ||
94 | $result = $this->controller->index($request, $response); | |
95 | ||
96 | static::assertInstanceOf(Response::class, $result); | |
97 | static::assertSame(302, $result->getStatusCode()); | |
98 | static::assertSame(['/subfolder/'], $result->getHeader('Location')); | |
99 | } | |
100 | ||
101 | public function testLoginControllerOpenShaarli(): void | |
102 | { | |
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(['/subfolder/'], $result->getHeader('Location')); | |
120 | } | |
121 | ||
122 | public function testLoginControllerWhileBanned(): void | |
123 | { | |
124 | $request = $this->createMock(Request::class); | |
125 | $response = new Response(); | |
126 | ||
127 | $this->container->loginManager->method('isLoggedIn')->willReturn(false); | |
128 | $this->container->loginManager->method('canLogin')->willReturn(false); | |
129 | ||
130 | $this->expectException(LoginBannedException::class); | |
131 | ||
132 | $this->controller->index($request, $response); | |
133 | } | |
134 | } |