diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-22 13:20:31 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 2899ebb5b5e82890c877151f5c02045266ac9973 (patch) | |
tree | 0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /tests/front/controller/visitor/LoginControllerTest.php | |
parent | af290059d10319e76d1e7d78b592cab99c26d91a (diff) | |
download | Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip |
Initialize admin Slim controllers
- Reorganize visitor controllers
- Fix redirection with Slim's requests base path
- Fix daily links
Diffstat (limited to 'tests/front/controller/visitor/LoginControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/LoginControllerTest.php | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/LoginControllerTest.php b/tests/front/controller/visitor/LoginControllerTest.php new file mode 100644 index 00000000..9d223316 --- /dev/null +++ b/tests/front/controller/visitor/LoginControllerTest.php | |||
@@ -0,0 +1,144 @@ | |||
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 | $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('> 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>', $assignedVariables['username']); | ||
86 | static::assertSame('> 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 | } | ||