3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
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 $request = $this->createMock(Request
::class);
30 $request->expects(static::once())->method('getServerParam')->willReturn('> referer');
31 $response = new Response();
33 $assignedVariables = [];
34 $this->container
->pageBuilder
36 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
37 $assignedVariables[$key] = $value;
43 $this->container
->loginManager
->method('canLogin')->willReturn(true);
45 $result = $this->controller
->index($request, $response);
47 static::assertInstanceOf(Response
::class, $result);
48 static::assertSame(200, $result->getStatusCode());
49 static::assertSame('loginform', (string) $result->getBody());
51 static::assertSame('> referer', $assignedVariables['returnurl']);
52 static::assertSame(true, $assignedVariables['remember_user_default']);
53 static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
56 public function testValidControllerInvokeWithUserName(): void
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();
63 $assignedVariables = [];
64 $this->container
->pageBuilder
66 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
67 $assignedVariables[$key] = $value;
73 $this->container
->loginManager
->expects(static::once())->method('canLogin')->willReturn(true);
75 $result = $this->controller
->index($request, $response);
77 static::assertInstanceOf(Response
::class, $result);
78 static::assertSame(200, $result->getStatusCode());
79 static::assertSame('loginform', (string) $result->getBody());
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']);
87 public function testLoginControllerWhileLoggedIn(): void
89 $request = $this->createMock(Request
::class);
90 $response = new Response();
92 $this->container
->loginManager
->expects(static::once())->method('isLoggedIn')->willReturn(true);
94 $result = $this->controller
->index($request, $response);
96 static::assertInstanceOf(Response
::class, $result);
97 static::assertSame(302, $result->getStatusCode());
98 static::assertSame(['./'], $result->getHeader('Location'));
101 public function testLoginControllerOpenShaarli(): void
103 $request = $this->createMock(Request
::class);
104 $response = new Response();
106 $conf = $this->createMock(ConfigManager
::class);
107 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
108 if ($parameter === 'security.open_shaarli') {
113 $this->container
->conf
= $conf;
115 $result = $this->controller
->index($request, $response);
117 static::assertInstanceOf(Response
::class, $result);
118 static::assertSame(302, $result->getStatusCode());
119 static::assertSame(['./'], $result->getHeader('Location'));
122 public function testLoginControllerWhileBanned(): void
124 $request = $this->createMock(Request
::class);
125 $response = new Response();
127 $this->container
->loginManager
->method('isLoggedIn')->willReturn(false);
128 $this->container
->loginManager
->method('canLogin')->willReturn(false);
130 $this->expectException(LoginBannedException
::class);
132 $this->controller
->index($request, $response);