]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/LoginControllerTest.php
Refactor front controller tests to create container mock using a trait
[github/shaarli/Shaarli.git] / tests / front / controller / LoginControllerTest.php
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;
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 $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
37 ->method('assign')
38 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
39 $assignedVariables[$key] = $value;
40
41 return $this;
42 })
43 ;
44
45 $this->container->loginManager->method('canLogin')->willReturn(true);
46
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('&gt; 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
69 ->method('assign')
70 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
71 $assignedVariables[$key] = $value;
72
73 return $this;
74 })
75 ;
76
77 $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true);
78
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&gt;', $assignedVariables['username']);
86 static::assertSame('&gt; 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 {
93 $this->createValidContainerMockSet();
94
95 $request = $this->createMock(Request::class);
96 $response = new Response();
97
98 $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
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
137 $this->container->loginManager->method('isLoggedIn')->willReturn(false);
138 $this->container->loginManager->method('canLogin')->willReturn(false);
139
140 $this->expectException(LoginBannedException::class);
141
142 $this->controller->index($request, $response);
143 }
144 }