]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ServerBlockConsoleTest.php
Merge pull request #12920 from annando/issue-12701
[friendica.git] / tests / src / Console / ServerBlockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Console;
23
24 use Friendica\Console\ServerBlock;
25 use Friendica\Moderation\DomainPatternBlocklist;
26 use Friendica\Test\FixtureTestTrait;
27 use Mockery;
28
29 class ServerBlockConsoleTest extends ConsoleTest
30 {
31         use FixtureTestTrait;
32
33         protected $defaultBlockList = [
34                 [
35                         'domain' => 'social.nobodyhasthe.biz',
36                         'reason' => 'Illegal content',
37                 ],
38                 [
39                         'domain' => 'pod.ordoevangelistarum.com',
40                         'reason' => 'Illegal content',
41                 ]
42         ];
43         /**
44          * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
45          */
46         private $blocklistMock;
47
48         protected function setUp() : void
49         {
50                 parent::setUp();
51
52                 $this->setUpFixtures();
53
54                 $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
55         }
56
57         protected function tearDown(): void
58         {
59                 $this->tearDownFixtures();
60
61                 parent::tearDown();
62         }
63
64         /**
65          * Test to list the default blocked servers
66          */
67         public function testBlockedServersList()
68         {
69                 $this->blocklistMock
70                         ->shouldReceive('get')
71                         ->andReturn($this->defaultBlockList)
72                         ->once();
73
74                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
75                 $txt = $this->dumpExecute($console);
76
77                 $php_eol = PHP_EOL;
78
79                 $output = <<<CONS
80 +----------------------------+-----------------+$php_eol| Pattern                    | Reason          |$php_eol+----------------------------+-----------------+$php_eol| social.nobodyhasthe.biz    | Illegal content |$php_eol| pod.ordoevangelistarum.com | Illegal content |$php_eol+----------------------------+-----------------+$php_eol
81
82 CONS;
83
84                 self::assertEquals($output, $txt);
85         }
86
87         /**
88          * Test blockedservers add command
89          */
90         public function testAddBlockedServer()
91         {
92                 $this->blocklistMock
93                         ->shouldReceive('addPattern')
94                         ->with('testme.now', 'I like it!')
95                         ->andReturn(1)
96                         ->once();
97
98                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
99                 $console->setArgument(0, 'add');
100                 $console->setArgument(1, 'testme.now');
101                 $console->setArgument(2, 'I like it!');
102                 $txt = $this->dumpExecute($console);
103
104                 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
105         }
106
107         /**
108          * Test blockedservers add command on existed domain
109          */
110         public function testUpdateBlockedServer()
111         {
112                 $this->blocklistMock
113                         ->shouldReceive('addPattern')
114                         ->with('pod.ordoevangelistarum.com', 'Other reason')
115                         ->andReturn(2)
116                         ->once();
117
118                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
119                 $console->setArgument(0, 'add');
120                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
121                 $console->setArgument(2, 'Other reason');
122                 $txt = $this->dumpExecute($console);
123
124                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
125         }
126
127         /**
128          * Test blockedservers remove command
129          */
130         public function testRemoveBlockedServer()
131         {
132                 $this->blocklistMock
133                         ->shouldReceive('removePattern')
134                         ->with('pod.ordoevangelistarum.com')
135                         ->andReturn(2)
136                         ->once();
137
138                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
139                 $console->setArgument(0, 'remove');
140                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
141                 $txt = $this->dumpExecute($console);
142
143                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
144         }
145
146         /**
147          * Test blockedservers with a wrong command
148          */
149         public function testBlockedServersWrongCommand()
150         {
151                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
152                 $console->setArgument(0, 'wrongcommand');
153                 $txt = $this->dumpExecute($console);
154
155                 self::assertStringStartsWith('[Warning] Unknown command', $txt);
156         }
157
158         /**
159          * Test blockedservers remove with not existing domain
160          */
161         public function testRemoveBlockedServerNotExist()
162         {
163                 $this->blocklistMock
164                         ->shouldReceive('removePattern')
165                         ->with('not.existing')
166                         ->andReturn(1)
167                         ->once();
168
169                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
170                 $console->setArgument(0, 'remove');
171                 $console->setArgument(1, 'not.existing');
172                 $txt = $this->dumpExecute($console);
173
174                 self::assertEquals('The domain pattern \'not.existing\' wasn\'t blocked.' . "\n", $txt);
175         }
176
177         /**
178          * Test blockedservers add command without argument
179          */
180         public function testAddBlockedServerMissingArgument()
181         {
182                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
183                 $console->setArgument(0, 'add');
184                 $txt = $this->dumpExecute($console);
185
186                 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
187
188                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
189                 $console->setArgument(0, 'add');
190                 $console->setArgument(1, 'testme.now');
191                 $txt = $this->dumpExecute($console);
192
193                 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
194         }
195
196         /**
197          * Test blockedservers add command without save
198          */
199         public function testAddBlockedServerNoSave()
200         {
201                 $this->blocklistMock
202                         ->shouldReceive('addPattern')
203                         ->with('testme.now', 'I like it!')
204                         ->andReturn(0)
205                         ->once();
206
207                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
208                 $console->setArgument(0, 'add');
209                 $console->setArgument(1, 'testme.now');
210                 $console->setArgument(2, 'I like it!');
211                 $txt = $this->dumpExecute($console);
212
213                 self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
214         }
215
216         /**
217          * Test blockedservers remove command without save
218          */
219         public function testRemoveBlockedServerNoSave()
220         {
221                 $this->blocklistMock
222                         ->shouldReceive('removePattern')
223                         ->with('pod.ordoevangelistarum.com')
224                         ->andReturn(0)
225                         ->once();
226
227                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
228                 $console->setArgument(0, 'remove');
229                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
230                 $txt = $this->dumpExecute($console);
231
232                 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
233         }
234
235         /**
236          * Test blockedservers remove command without argument
237          */
238         public function testRemoveBlockedServerMissingArgument()
239         {
240                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
241                 $console->setArgument(0, 'remove');
242                 $txt = $this->dumpExecute($console);
243
244                 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
245         }
246
247         /**
248          * Test the blockedservers help
249          */
250         public function testBlockedServersHelp()
251         {
252                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
253                 $console->setOption('help', true);
254                 $txt = $this->dumpExecute($console);
255
256                 $help = <<<HELP
257 console serverblock - Manage blocked server domain patterns
258 Usage
259     bin/console serverblock [-h|--help|-?] [-v]
260     bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
261     bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
262     bin/console serverblock export <filename>
263     bin/console serverblock import <filename>
264
265 Description
266     With this tool, you can list the current blocked server domain patterns
267     or you can add / remove a blocked server domain pattern from the list.
268     Using the export and import options you can share your server blocklist
269     with other node admins by CSV files.
270
271     Patterns are case-insensitive shell wildcard comprising the following special characters:
272     - * : Any number of characters
273     - ? : Any single character
274     - [<char1><char2>...] : char1 or char2 or...
275
276 Options
277     -h|--help|-? Show help information
278     -v           Show more debug information.
279
280 HELP;
281
282                 self::assertEquals($help, $txt);
283         }
284 }