1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=1);
24
25namespace Ampache\Module\Application\Preferences;
26
27use Ampache\MockeryTestCase;
28use Ampache\Module\Application\Exception\AccessDeniedException;
29use Ampache\Module\Authorization\AccessLevelEnum;
30use Ampache\Module\Authorization\GuiGatekeeperInterface;
31use Ampache\Module\Util\UiInterface;
32use Ampache\Repository\Model\User;
33use Mockery\MockInterface;
34use Psr\Http\Message\ServerRequestInterface;
35
36class AdminActionTest extends MockeryTestCase
37{
38    /** @var MockInterface|UiInterface */
39    private MockInterface $ui;
40
41    private AdminAction $subject;
42
43    public function setUp(): void
44    {
45        $this->ui = $this->mock(UiInterface::class);
46
47        $this->subject = new AdminAction(
48            $this->ui
49        );
50    }
51
52    public function testRunThrowsExceptionIfAccessIsDenied(): void
53    {
54        $request    = $this->mock(ServerRequestInterface::class);
55        $gatekeeper = $this->mock(GuiGatekeeperInterface::class);
56
57        $this->expectException(AccessDeniedException::class);
58
59        $gatekeeper->shouldReceive('mayAccess')
60            ->with(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN)
61            ->once()
62            ->andReturnFalse();
63
64        $this->subject->run($request, $gatekeeper);
65    }
66
67    public function testRunShowOptions(): void
68    {
69        $request    = $this->mock(ServerRequestInterface::class);
70        $gatekeeper = $this->mock(GuiGatekeeperInterface::class);
71        $user       = $this->mock(User::class);
72
73        $preferences = ['some' => 'preference'];
74        $tab         = 'some-tab';
75
76        $gatekeeper->shouldReceive('mayAccess')
77            ->with(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN)
78            ->once()
79            ->andReturnTrue();
80        $gatekeeper->shouldReceive('getUser')
81            ->withNoArgs()
82            ->once()
83            ->andReturn($user);
84
85        $request->shouldReceive('getQueryParams')
86            ->withNoArgs()
87            ->once()
88            ->andReturn(['tab' => $tab]);
89
90        $user->shouldReceive('get_preferences')
91            ->with($tab, true)
92            ->once()
93            ->andReturn($preferences);
94
95        $this->ui->shouldReceive('showHeader')
96            ->withNoArgs()
97            ->once();
98        $this->ui->shouldReceive('show')
99            ->with(
100                'show_preferences.inc.php',
101                [
102                    'fullname' => 'Server',
103                    'preferences' => $preferences,
104                    'ui' => $this->ui,
105                ]
106            )
107            ->once();
108        $this->ui->shouldReceive('showQueryStats')
109            ->withNoArgs()
110            ->once();
111        $this->ui->shouldReceive('showFooter')
112            ->withNoArgs()
113            ->once();
114
115        $this->assertNull(
116            $this->subject->run($request, $gatekeeper)
117        );
118    }
119}
120