xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision ee4364da76be9b2651fd11a0b3dae7961849d2f3)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\RequestHandlers;
20
21use Fisharebest\Webtrees\GuestUser;
22use Fisharebest\Webtrees\Services\ModuleService;
23use Fisharebest\Webtrees\TestCase;
24use Fisharebest\Webtrees\Tree;
25use Psr\Http\Message\ServerRequestInterface;
26
27/**
28 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
29 */
30class ModuleActionTest extends TestCase
31{
32    protected static $uses_database = true;
33
34    /**
35     * @return void
36     */
37    public function testModuleAction(): void
38    {
39        $tree = Tree::create('tree', 'tree');
40        app()->instance(Tree::class, $tree);
41        $user           = new GuestUser();
42        $module_service = new ModuleService();
43        $handler        = new ModuleAction($module_service, $user);
44        $request        = self::createRequest(self::METHOD_GET, ['route' => 'module', 'ged' => $tree->name()])
45            ->withAttribute('module', 'faq')
46            ->withAttribute('action', 'Show')
47            ->withAttribute('tree', $tree);
48
49        app()->instance(ServerRequestInterface::class, $request);
50
51        $response = $handler->handle($request);
52
53        $this->assertSame(self::STATUS_OK, $response->getStatusCode());
54    }
55
56    /**
57     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
58     * @expectedExceptionMessage Method getFishAction() not found in faq
59     * @return void
60     */
61    public function testNonExistingAction(): void
62    {
63        $user           = new GuestUser();
64        $module_service = new ModuleService();
65        $handler        = new ModuleAction($module_service, $user);
66        $request        = self::createRequest(self::METHOD_GET, ['route' => 'module'])
67            ->withAttribute('module', 'faq')
68            ->withAttribute('action', 'Fish')
69        ;
70        $handler->handle($request);
71    }
72
73    /**
74     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
75     * @expectedExceptionMessage Module fish does not exist
76     * @return void
77     */
78    public function testNonExistingModule(): void
79    {
80        $user           = new GuestUser();
81        $module_service = new ModuleService();
82        $handler        = new ModuleAction($module_service, $user);
83        $request        = self::createRequest(self::METHOD_GET, ['route' => 'module'])
84            ->withAttribute('module', 'fish')
85            ->withAttribute('action', 'Show')
86        ;
87        $response       = $handler->handle($request);
88
89        $this->assertSame(self::STATUS_OK, $response->getStatusCode());
90    }
91
92    /**
93     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
94     * @expectedExceptionMessage Admin only action
95     * @return void
96     */
97    public function testAdminAction(): void
98    {
99        $tree = Tree::create('tree', 'tree');
100        app()->instance(Tree::class, $tree);
101        $user           = new GuestUser();
102        $module_service = new ModuleService();
103        $handler        = new ModuleAction($module_service, $user);
104        $request        = self::createRequest(self::METHOD_GET, ['route' => 'module', 'ged' => $tree->name()])
105            ->withAttribute('module', 'faq')
106            ->withAttribute('action', 'Admin')
107        ;
108        $handler->handle($request);
109    }
110}
111