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
22declare(strict_types=1);
23
24namespace Ampache\Module\Application\Admin\Shout;
25
26use Ampache\Config\ConfigContainerInterface;
27use Ampache\MockeryTestCase;
28use Ampache\Repository\Model\ModelFactoryInterface;
29use Ampache\Repository\Model\Shoutbox;
30use Ampache\Module\Application\Exception\AccessDeniedException;
31use Ampache\Module\Authorization\AccessLevelEnum;
32use Ampache\Module\Authorization\GuiGatekeeperInterface;
33use Ampache\Module\Util\UiInterface;
34use Ampache\Repository\ShoutRepositoryInterface;
35use Mockery\MockInterface;
36use Psr\Http\Message\ServerRequestInterface;
37
38class DeleteActionTest extends MockeryTestCase
39{
40    /** @var MockInterface|UiInterface|null */
41    private MockInterface $ui;
42
43    /** @var MockInterface|ConfigContainerInterface|null */
44    private MockInterface $configContainer;
45
46    /** @var MockInterface|ModelFactoryInterface|null */
47    private MockInterface $modelFactory;
48
49    /** @var MockInterface|ShoutRepositoryInterface|null */
50    private MockInterface $shoutRepository;
51
52    private ?DeleteAction $subject;
53
54    public function setUp(): void
55    {
56        $this->ui              = $this->mock(UiInterface::class);
57        $this->configContainer = $this->mock(ConfigContainerInterface::class);
58        $this->modelFactory    = $this->mock(ModelFactoryInterface::class);
59        $this->shoutRepository = $this->mock(ShoutRepositoryInterface::class);
60
61        $this->subject = new DeleteAction(
62            $this->ui,
63            $this->configContainer,
64            $this->modelFactory,
65            $this->shoutRepository
66        );
67    }
68
69    public function testRunThrowsExceptionIfAccessIsDenied(): void
70    {
71        $this->expectException(AccessDeniedException::class);
72
73        $request    = $this->mock(ServerRequestInterface::class);
74        $gatekeeper = $this->mock(GuiGatekeeperInterface::class);
75
76        $gatekeeper->shouldReceive('mayAccess')
77            ->with(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN)
78            ->once()
79            ->andReturnFalse();
80
81        $this->subject->run(
82            $request,
83            $gatekeeper
84        );
85    }
86
87    public function testRunDeletesShout(): void
88    {
89        $request    = $this->mock(ServerRequestInterface::class);
90        $gatekeeper = $this->mock(GuiGatekeeperInterface::class);
91        $shout      = $this->mock(Shoutbox::class);
92
93        $shoutId = 666;
94        $webPath = 'some-path';
95
96        $gatekeeper->shouldReceive('mayAccess')
97            ->with(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN)
98            ->once()
99            ->andReturnTrue();
100
101        $this->modelFactory->shouldReceive('createShoutbox')
102            ->with($shoutId)
103            ->once()
104            ->andReturn($shout);
105
106        $request->shouldReceive('getQueryParams')
107            ->withNoArgs()
108            ->once()
109            ->andReturn(['shout_id' => (string) $shoutId]);
110
111        $shout->shouldReceive('getId')
112            ->withNoArgs()
113            ->once()
114            ->andReturn($shoutId);
115
116        $this->shoutRepository->shouldReceive('delete')
117            ->with($shoutId)
118            ->once();
119
120        $this->ui->shouldReceive('showHeader')
121            ->withNoArgs()
122            ->once();
123        $this->ui->shouldReceive('showConfirmation')
124            ->with(
125                'No Problem',
126                'Shoutbox post has been deleted',
127                sprintf('%s/admin/shout.php', $webPath)
128            )
129            ->once();
130        $this->ui->shouldReceive('showQueryStats')
131            ->withNoArgs()
132            ->once();
133        $this->ui->shouldReceive('showFooter')
134            ->withNoArgs()
135            ->once();
136
137        $this->configContainer->shouldReceive('getWebPath')
138            ->withNoArgs()
139            ->once()
140            ->andReturn($webPath);
141
142        $this->assertNull(
143            $this->subject->run(
144                $request,
145                $gatekeeper
146            )
147        );
148    }
149}
150