xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision d501c45d339d4a2d06248f9197d7875a4df14e48)
1a0801ffbSGreg Roach<?php
23976b470SGreg Roach
3a0801ffbSGreg Roach/**
4a0801ffbSGreg Roach * webtrees: online genealogy
5a0801ffbSGreg Roach * Copyright (C) 2019 webtrees development team
6a0801ffbSGreg Roach * This program is free software: you can redistribute it and/or modify
7a0801ffbSGreg Roach * it under the terms of the GNU General Public License as published by
8a0801ffbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a0801ffbSGreg Roach * (at your option) any later version.
10a0801ffbSGreg Roach * This program is distributed in the hope that it will be useful,
11a0801ffbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a0801ffbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a0801ffbSGreg Roach * GNU General Public License for more details.
14a0801ffbSGreg Roach * You should have received a copy of the GNU General Public License
15a0801ffbSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a0801ffbSGreg Roach */
17fcfa147eSGreg Roach
18a0801ffbSGreg Roachdeclare(strict_types=1);
19a0801ffbSGreg Roach
2074d6dc0eSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2174d6dc0eSGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23*d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
24*d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
25a0801ffbSGreg Roachuse Fisharebest\Webtrees\GuestUser;
2671378461SGreg Roachuse Fisharebest\Webtrees\Module\AbstractModule;
2771378461SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
28a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
29a0801ffbSGreg Roachuse Fisharebest\Webtrees\TestCase;
3071378461SGreg Roachuse Psr\Http\Message\ResponseInterface;
3157ab2231SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32a0801ffbSGreg Roach
3371378461SGreg Roachuse function response;
3471378461SGreg Roach
35a0801ffbSGreg Roach/**
36a0801ffbSGreg Roach * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
37a0801ffbSGreg Roach */
38a0801ffbSGreg Roachclass ModuleActionTest extends TestCase
39a0801ffbSGreg Roach{
40a0801ffbSGreg Roach    /**
41a0801ffbSGreg Roach     * @return void
42a0801ffbSGreg Roach     */
43a0801ffbSGreg Roach    public function testModuleAction(): void
44a0801ffbSGreg Roach    {
4571378461SGreg Roach        $module_service = $this->createMock(ModuleService::class);
4671378461SGreg Roach        $module_service
4771378461SGreg Roach            ->expects($this->once())
4871378461SGreg Roach            ->method('findByName')
4971378461SGreg Roach            ->with('test')
5071378461SGreg Roach            ->willReturn($this->dummyModule());
5171378461SGreg Roach
52a0801ffbSGreg Roach        $user     = new GuestUser();
5371378461SGreg Roach        $request  = self::createRequest()
5471378461SGreg Roach            ->withAttribute('module', 'test')
55f4917837SGreg Roach            ->withAttribute('action', 'Test')
56f4917837SGreg Roach            ->withAttribute('user', $user);
57f4917837SGreg Roach        $handler  = new ModuleAction($module_service);
58a0801ffbSGreg Roach        $response = $handler->handle($request);
59a0801ffbSGreg Roach
6071378461SGreg Roach        $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
6171378461SGreg Roach        $this->assertSame('It works!', (string) $response->getBody());
62a0801ffbSGreg Roach    }
63a0801ffbSGreg Roach
64a0801ffbSGreg Roach    /**
65a0801ffbSGreg Roach     * @return void
66a0801ffbSGreg Roach     */
67a0801ffbSGreg Roach    public function testNonExistingAction(): void
68a0801ffbSGreg Roach    {
69*d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
703cfcc809SGreg Roach        $this->expectExceptionMessage('Method getTestingAction() not found in test');
713cfcc809SGreg Roach
7271378461SGreg Roach        $module_service = $this->createMock(ModuleService::class);
7371378461SGreg Roach        $module_service
7471378461SGreg Roach            ->expects($this->once())
7571378461SGreg Roach            ->method('findByName')
7671378461SGreg Roach            ->with('test')
7771378461SGreg Roach            ->willReturn($this->dummyModule());
7871378461SGreg Roach
79a0801ffbSGreg Roach        $user    = new GuestUser();
8071378461SGreg Roach        $request = self::createRequest()
8171378461SGreg Roach            ->withAttribute('module', 'test')
82f4917837SGreg Roach            ->withAttribute('action', 'Testing')
83f4917837SGreg Roach            ->withAttribute('user', $user);
84f4917837SGreg Roach        $handler = new ModuleAction($module_service);
85a0801ffbSGreg Roach        $handler->handle($request);
86a0801ffbSGreg Roach    }
87a0801ffbSGreg Roach
88a0801ffbSGreg Roach    /**
89a0801ffbSGreg Roach     * @return void
90a0801ffbSGreg Roach     */
91a0801ffbSGreg Roach    public function testNonExistingModule(): void
92a0801ffbSGreg Roach    {
93*d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
943cfcc809SGreg Roach        $this->expectExceptionMessage('Module test does not exist');
953cfcc809SGreg Roach
9671378461SGreg Roach        $module_service = $this->createMock(ModuleService::class);
9771378461SGreg Roach        $module_service
9871378461SGreg Roach            ->expects($this->once())
9971378461SGreg Roach            ->method('findByName')
10071378461SGreg Roach            ->with('test')
10171378461SGreg Roach            ->willReturn(null);
102a0801ffbSGreg Roach
10371378461SGreg Roach        $user    = new GuestUser();
10471378461SGreg Roach        $request = self::createRequest()
10571378461SGreg Roach            ->withAttribute('module', 'test')
106f4917837SGreg Roach            ->withAttribute('action', 'Test')
107f4917837SGreg Roach            ->withAttribute('user', $user);
108f4917837SGreg Roach        $handler = new ModuleAction($module_service);
10971378461SGreg Roach        $handler->handle($request);
110a0801ffbSGreg Roach    }
111a0801ffbSGreg Roach
112a0801ffbSGreg Roach    /**
113a0801ffbSGreg Roach     * @return void
114a0801ffbSGreg Roach     */
115a0801ffbSGreg Roach    public function testAdminAction(): void
116a0801ffbSGreg Roach    {
117*d501c45dSGreg Roach        $this->expectException(HttpAccessDeniedException::class);
1183cfcc809SGreg Roach        $this->expectExceptionMessage('Admin only action');
1193cfcc809SGreg Roach
12071378461SGreg Roach        $module_service = $this->createMock(ModuleService::class);
12171378461SGreg Roach        $module_service
12271378461SGreg Roach            ->expects($this->once())
12371378461SGreg Roach            ->method('findByName')
12471378461SGreg Roach            ->with('test')
12571378461SGreg Roach            ->willReturn($this->dummyModule());
12671378461SGreg Roach
127a0801ffbSGreg Roach        $user    = new GuestUser();
12871378461SGreg Roach        $request = self::createRequest()
12971378461SGreg Roach            ->withAttribute('module', 'test')
130f4917837SGreg Roach            ->withAttribute('action', 'Admin')
131f4917837SGreg Roach            ->withAttribute('user', $user);
132f4917837SGreg Roach        $handler = new ModuleAction($module_service);
133a0801ffbSGreg Roach        $handler->handle($request);
134a0801ffbSGreg Roach    }
13571378461SGreg Roach
13671378461SGreg Roach    /**
13771378461SGreg Roach     * @return ModuleInterface
13871378461SGreg Roach     */
13971378461SGreg Roach    private function dummyModule(): ModuleInterface
14071378461SGreg Roach    {
141c81b7bf1SGreg Roach        return new class extends AbstractModule {
14271378461SGreg Roach            public function getTestAction(ServerRequestInterface $request): ResponseInterface
14371378461SGreg Roach            {
14471378461SGreg Roach                return response('It works!');
14571378461SGreg Roach            }
14671378461SGreg Roach        };
14771378461SGreg Roach    }
148a0801ffbSGreg Roach}
149