xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision b47837c47d7b6e97a8ae8b39adde4ac935004240)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
24use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
25use Fisharebest\Webtrees\GuestUser;
26use Fisharebest\Webtrees\Module\AbstractModule;
27use Fisharebest\Webtrees\Module\ModuleInterface;
28use Fisharebest\Webtrees\Services\ModuleService;
29use Fisharebest\Webtrees\TestCase;
30use Psr\Http\Message\ResponseInterface;
31
32use function response;
33
34/**
35 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
36 */
37class ModuleActionTest extends TestCase
38{
39    /**
40     * @return void
41     */
42    public function testModuleAction(): void
43    {
44        $module_service = self::createMock(ModuleService::class);
45        $module_service
46            ->expects(self::once())
47            ->method('findByName')
48            ->with('test')
49            ->willReturn($this->fooModule());
50
51        $user     = new GuestUser();
52        $request  = self::createRequest()
53            ->withAttribute('module', 'test')
54            ->withAttribute('action', 'Test')
55            ->withAttribute('user', $user);
56        $handler  = new ModuleAction($module_service);
57        $response = $handler->handle($request);
58
59        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
60        self::assertSame('It works!', (string) $response->getBody());
61    }
62
63    /**
64     * @return void
65     */
66    public function testNonExistingAction(): void
67    {
68        $this->expectException(HttpNotFoundException::class);
69        $this->expectExceptionMessage('Method getTestingAction() not found in test');
70
71        $module_service = self::createMock(ModuleService::class);
72        $module_service
73            ->expects(self::once())
74            ->method('findByName')
75            ->with('test')
76            ->willReturn($this->fooModule());
77
78        $user    = new GuestUser();
79        $request = self::createRequest()
80            ->withAttribute('module', 'test')
81            ->withAttribute('action', 'Testing')
82            ->withAttribute('user', $user);
83        $handler = new ModuleAction($module_service);
84        $handler->handle($request);
85    }
86
87    /**
88     * @return void
89     */
90    public function testNonExistingModule(): void
91    {
92        $this->expectException(HttpNotFoundException::class);
93        $this->expectExceptionMessage('Module test does not exist');
94
95        $module_service = self::createMock(ModuleService::class);
96        $module_service
97            ->expects(self::once())
98            ->method('findByName')
99            ->with('test')
100            ->willReturn(null);
101
102        $user    = new GuestUser();
103        $request = self::createRequest()
104            ->withAttribute('module', 'test')
105            ->withAttribute('action', 'Test')
106            ->withAttribute('user', $user);
107        $handler = new ModuleAction($module_service);
108        $handler->handle($request);
109    }
110
111    /**
112     * @return void
113     */
114    public function testAdminAction(): void
115    {
116        $this->expectException(HttpAccessDeniedException::class);
117        $this->expectExceptionMessage('Admin only action');
118
119        $module_service = self::createMock(ModuleService::class);
120        $module_service
121            ->expects(self::once())
122            ->method('findByName')
123            ->with('test')
124            ->willReturn($this->fooModule());
125
126        $user    = new GuestUser();
127        $request = self::createRequest()
128            ->withAttribute('module', 'test')
129            ->withAttribute('action', 'Admin')
130            ->withAttribute('user', $user);
131        $handler = new ModuleAction($module_service);
132        $handler->handle($request);
133    }
134
135    /**
136     * @return ModuleInterface
137     */
138    private function fooModule(): ModuleInterface
139    {
140        return new class extends AbstractModule {
141            public function getTestAction(): ResponseInterface
142            {
143                return response('It works!');
144            }
145        };
146    }
147}
148