3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
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
;
13 class LoginControllerTest
extends TestCase
15 use FrontControllerMockHelper
;
17 /** @var LoginController */
18 protected $controller;
20 public function setUp(): void
22 $this->createContainer();
24 $this->controller
= new LoginController($this->container
);
27 public function testValidControllerInvoke(): void
29 $this->createValidContainerMockSet();
31 $request = $this->createMock(Request
::class);
32 $request->expects(static::once())->method('getServerParam')->willReturn('> referer');
33 $response = new Response();
35 $assignedVariables = [];
36 $this->container
->pageBuilder
38 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
39 $assignedVariables[$key] = $value;
45 $this->container
->loginManager
->method('canLogin')->willReturn(true);
47 $result = $this->controller
->index($request, $response);
49 static::assertInstanceOf(Response
::class, $result);
50 static::assertSame(200, $result->getStatusCode());
51 static::assertSame('loginform', (string) $result->getBody());
53 static::assertSame('> referer', $assignedVariables['returnurl']);
54 static::assertSame(true, $assignedVariables['remember_user_default']);
55 static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
58 public function testValidControllerInvokeWithUserName(): void
60 $this->createValidContainerMockSet();
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();
67 $assignedVariables = [];
68 $this->container
->pageBuilder
70 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
71 $assignedVariables[$key] = $value;
77 $this->container
->loginManager
->expects(static::once())->method('canLogin')->willReturn(true);
79 $result = $this->controller
->index($request, $response);
81 static::assertInstanceOf(Response
::class, $result);
82 static::assertSame(200, $result->getStatusCode());
83 static::assertSame('loginform', (string) $result->getBody());
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']);
91 public function testLoginControllerWhileLoggedIn(): void
93 $this->createValidContainerMockSet();
95 $request = $this->createMock(Request
::class);
96 $response = new Response();
98 $this->container
->loginManager
->expects(static::once())->method('isLoggedIn')->willReturn(true);
100 $result = $this->controller
->index($request, $response);
102 static::assertInstanceOf(Response
::class, $result);
103 static::assertSame(302, $result->getStatusCode());
104 static::assertSame(['./'], $result->getHeader('Location'));
107 public function testLoginControllerOpenShaarli(): void
109 $this->createValidContainerMockSet();
111 $request = $this->createMock(Request
::class);
112 $response = new Response();
114 $conf = $this->createMock(ConfigManager
::class);
115 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
116 if ($parameter === 'security.open_shaarli') {
121 $this->container
->conf
= $conf;
123 $result = $this->controller
->index($request, $response);
125 static::assertInstanceOf(Response
::class, $result);
126 static::assertSame(302, $result->getStatusCode());
127 static::assertSame(['./'], $result->getHeader('Location'));
130 public function testLoginControllerWhileBanned(): void
132 $this->createValidContainerMockSet();
134 $request = $this->createMock(Request
::class);
135 $response = new Response();
137 $this->container
->loginManager
->method('isLoggedIn')->willReturn(false);
138 $this->container
->loginManager
->method('canLogin')->willReturn(false);
140 $this->expectException(LoginBannedException
::class);
142 $this->controller
->index($request, $response);