xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision 165434418b8f3c779dafa6b89dedb34ec0ed2ac3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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;
31use Psr\Http\Message\ServerRequestInterface;
32
33use function response;
34
35/**
36 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
37 */
38class ModuleActionTest extends TestCase
39{
40    /**
41     * @return void
42     */
43    public function testModuleAction(): void
44    {
45        $module_service = $this->createMock(ModuleService::class);
46        $module_service
47            ->expects($this->once())
48            ->method('findByName')
49            ->with('test')
50            ->willReturn($this->fooModule());
51
52        $user     = new GuestUser();
53        $request  = self::createRequest()
54            ->withAttribute('module', 'test')
55            ->withAttribute('action', 'Test')
56            ->withAttribute('user', $user);
57        $handler  = new ModuleAction($module_service);
58        $response = $handler->handle($request);
59
60        $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
61        $this->assertSame('It works!', (string) $response->getBody());
62    }
63
64    /**
65     * @return void
66     */
67    public function testNonExistingAction(): void
68    {
69        $this->expectException(HttpNotFoundException::class);
70        $this->expectExceptionMessage('Method getTestingAction() not found in test');
71
72        $module_service = $this->createMock(ModuleService::class);
73        $module_service
74            ->expects($this->once())
75            ->method('findByName')
76            ->with('test')
77            ->willReturn($this->fooModule());
78
79        $user    = new GuestUser();
80        $request = self::createRequest()
81            ->withAttribute('module', 'test')
82            ->withAttribute('action', 'Testing')
83            ->withAttribute('user', $user);
84        $handler = new ModuleAction($module_service);
85        $handler->handle($request);
86    }
87
88    /**
89     * @return void
90     */
91    public function testNonExistingModule(): void
92    {
93        $this->expectException(HttpNotFoundException::class);
94        $this->expectExceptionMessage('Module test does not exist');
95
96        $module_service = $this->createMock(ModuleService::class);
97        $module_service
98            ->expects($this->once())
99            ->method('findByName')
100            ->with('test')
101            ->willReturn(null);
102
103        $user    = new GuestUser();
104        $request = self::createRequest()
105            ->withAttribute('module', 'test')
106            ->withAttribute('action', 'Test')
107            ->withAttribute('user', $user);
108        $handler = new ModuleAction($module_service);
109        $handler->handle($request);
110    }
111
112    /**
113     * @return void
114     */
115    public function testAdminAction(): void
116    {
117        $this->expectException(HttpAccessDeniedException::class);
118        $this->expectExceptionMessage('Admin only action');
119
120        $module_service = $this->createMock(ModuleService::class);
121        $module_service
122            ->expects($this->once())
123            ->method('findByName')
124            ->with('test')
125            ->willReturn($this->fooModule());
126
127        $user    = new GuestUser();
128        $request = self::createRequest()
129            ->withAttribute('module', 'test')
130            ->withAttribute('action', 'Admin')
131            ->withAttribute('user', $user);
132        $handler = new ModuleAction($module_service);
133        $handler->handle($request);
134    }
135
136    /**
137     * @return ModuleInterface
138     */
139    private function fooModule(): ModuleInterface
140    {
141        return new class extends AbstractModule {
142            public function getTestAction(ServerRequestInterface $request): ResponseInterface
143            {
144                return response('It works!');
145            }
146        };
147    }
148}
149