]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ServerBlockConsoleTest.php
Merge branch 'bug/phpinfo-accessible-hotfix' into 2020.09-rc
[friendica.git] / tests / src / Console / ServerBlockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Core\Config\IConfig;
26
27 class ServerBlockConsoleTest extends ConsoleTest
28 {
29         protected $defaultBlockList = [
30                 [
31                         'domain' => 'social.nobodyhasthe.biz',
32                         'reason' => 'Illegal content',
33                 ],
34                 [
35                         'domain' => 'pod.ordoevangelistarum.com',
36                         'reason' => 'Illegal content',
37                 ]
38         ];
39
40         protected function setUp()
41         {
42                 parent::setUp();
43
44                 $this->configMock = \Mockery::mock(IConfig::class);
45         }
46
47         /**
48          * Test to list the default blocked servers
49          */
50         public function testBlockedServersList()
51         {
52                 $this->configMock
53                         ->shouldReceive('get')
54                         ->with('system', 'blocklist', [])
55                         ->andReturn($this->defaultBlockList)
56                         ->once();
57
58                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
59                 $txt = $this->dumpExecute($console);
60
61                 $output = <<<CONS
62 +----------------------------+-----------------+
63 | Domain                     | Reason          |
64 +----------------------------+-----------------+
65 | social.nobodyhasthe.biz    | Illegal content |
66 | pod.ordoevangelistarum.com | Illegal content |
67 +----------------------------+-----------------+
68
69
70 CONS;
71
72                 $this->assertEquals($output, $txt);
73         }
74
75         /**
76          * Test blockedservers add command
77          */
78         public function testAddBlockedServer()
79         {
80                 $this->configMock
81                         ->shouldReceive('get')
82                         ->with('system', 'blocklist', [])
83                         ->andReturn($this->defaultBlockList)
84                         ->once();
85
86                 $newBlockList = $this->defaultBlockList;
87                 $newBlockList[] = [
88                         'domain' => 'testme.now',
89                         'reason' => 'I like it!',
90                 ];
91
92                 $this->configMock
93                         ->shouldReceive('set')
94                         ->with('system', 'blocklist', $newBlockList)
95                         ->andReturn(true)
96                         ->once();
97
98                 $console = new ServerBlock($this->configMock, $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                 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
105         }
106
107         /**
108          * Test blockedservers add command with the default reason
109          */
110         public function testAddBlockedServerWithDefaultReason()
111         {
112                 $this->configMock
113                         ->shouldReceive('get')
114                         ->with('system', 'blocklist', [])
115                         ->andReturn($this->defaultBlockList)
116                         ->once();
117
118                 $newBlockList = $this->defaultBlockList;
119                 $newBlockList[] = [
120                         'domain' => 'testme.now',
121                         'reason' => ServerBlock::DEFAULT_REASON,
122                 ];
123
124                 $this->configMock
125                         ->shouldReceive('set')
126                         ->with('system', 'blocklist', $newBlockList)
127                         ->andReturn(true)
128                         ->once();
129
130                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
131                 $console->setArgument(0, 'add');
132                 $console->setArgument(1, 'testme.now');
133                 $txt = $this->dumpExecute($console);
134
135                 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
136         }
137
138         /**
139          * Test blockedservers add command on existed domain
140          */
141         public function testUpdateBlockedServer()
142         {
143                 $this->configMock
144                         ->shouldReceive('get')
145                         ->with('system', 'blocklist', [])
146                         ->andReturn($this->defaultBlockList)
147                         ->once();
148
149                 $newBlockList = [
150                         [
151                                 'domain' => 'social.nobodyhasthe.biz',
152                                 'reason' => 'Illegal content',
153                         ],
154                         [
155                                 'domain' => 'pod.ordoevangelistarum.com',
156                                 'reason' => 'Other reason',
157                         ]
158                 ];
159
160                 $this->configMock
161                         ->shouldReceive('set')
162                         ->with('system', 'blocklist', $newBlockList)
163                         ->andReturn(true)
164                         ->once();
165
166                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
167                 $console->setArgument(0, 'add');
168                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
169                 $console->setArgument(2, 'Other reason');
170                 $txt = $this->dumpExecute($console);
171
172                 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
173         }
174
175         /**
176          * Test blockedservers remove command
177          */
178         public function testRemoveBlockedServer()
179         {
180                 $this->configMock
181                         ->shouldReceive('get')
182                         ->with('system', 'blocklist', [])
183                         ->andReturn($this->defaultBlockList)
184                         ->once();
185
186                 $newBlockList = [
187                         [
188                                 'domain' => 'social.nobodyhasthe.biz',
189                                 'reason' => 'Illegal content',
190                         ],
191                 ];
192
193                 $this->configMock
194                         ->shouldReceive('set')
195                         ->with('system', 'blocklist', $newBlockList)
196                         ->andReturn(true)
197                         ->once();
198
199                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
200                 $console->setArgument(0, 'remove');
201                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
202                 $txt = $this->dumpExecute($console);
203
204                 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
205         }
206
207         /**
208          * Test blockedservers with a wrong command
209          */
210         public function testBlockedServersWrongCommand()
211         {
212                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
213                 $console->setArgument(0, 'wrongcommand');
214                 $txt = $this->dumpExecute($console);
215
216                 $this->assertStringStartsWith('[Warning] Unknown command', $txt);
217         }
218
219         /**
220          * Test blockedservers remove with not existing domain
221          */
222         public function testRemoveBlockedServerNotExist()
223         {
224                 $this->configMock
225                         ->shouldReceive('get')
226                         ->with('system', 'blocklist', [])
227                         ->andReturn($this->defaultBlockList)
228                         ->once();
229
230                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
231                 $console->setArgument(0, 'remove');
232                 $console->setArgument(1, 'not.exiting');
233                 $txt = $this->dumpExecute($console);
234
235                 $this->assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
236         }
237
238         /**
239          * Test blockedservers add command without argument
240          */
241         public function testAddBlockedServerMissingArgument()
242         {
243                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
244                 $console->setArgument(0, 'add');
245                 $txt = $this->dumpExecute($console);
246
247                 $this->assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
248         }
249
250         /**
251          * Test blockedservers add command without save
252          */
253         public function testAddBlockedServerNoSave()
254         {
255                 $this->configMock
256                         ->shouldReceive('get')
257                         ->with('system', 'blocklist', [])
258                         ->andReturn($this->defaultBlockList)
259                         ->once();
260
261                 $newBlockList = $this->defaultBlockList;
262                 $newBlockList[] = [
263                         'domain' => 'testme.now',
264                         'reason' => ServerBlock::DEFAULT_REASON,
265                 ];
266
267                 $this->configMock
268                         ->shouldReceive('set')
269                         ->with('system', 'blocklist', $newBlockList)
270                         ->andReturn(false)
271                         ->once();
272
273                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
274                 $console->setArgument(0, 'add');
275                 $console->setArgument(1, 'testme.now');
276                 $txt = $this->dumpExecute($console);
277
278                 $this->assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
279         }
280
281         /**
282          * Test blockedservers remove command without save
283          */
284         public function testRemoveBlockedServerNoSave()
285         {
286                 $this->configMock
287                         ->shouldReceive('get')
288                         ->with('system', 'blocklist', [])
289                         ->andReturn($this->defaultBlockList)
290                         ->once();
291
292                 $newBlockList = [
293                         [
294                                 'domain' => 'social.nobodyhasthe.biz',
295                                 'reason' => 'Illegal content',
296                         ],
297                 ];
298
299                 $this->configMock
300                         ->shouldReceive('set')
301                         ->with('system', 'blocklist', $newBlockList)
302                         ->andReturn(false)
303                         ->once();
304
305                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
306                 $console->setArgument(0, 'remove');
307                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
308                 $txt = $this->dumpExecute($console);
309
310                 $this->assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
311         }
312
313         /**
314          * Test blockedservers remove command without argument
315          */
316         public function testRemoveBlockedServerMissingArgument()
317         {
318                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
319                 $console->setArgument(0, 'remove');
320                 $txt = $this->dumpExecute($console);
321
322                 $this->assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
323         }
324
325         /**
326          * Test the blockedservers help
327          */
328         public function testBlockedServersHelp()
329         {
330                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
331                 $console->setOption('help', true);
332                 $txt = $this->dumpExecute($console);
333
334                 $help = <<<HELP
335 console serverblock - Manage blocked server domain patterns
336 Usage
337     bin/console serverblock [-h|--help|-?] [-v]
338     bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
339     bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
340     bin/console serverblock export <filename>
341     bin/console serverblock import <filename>
342
343 Description
344     With this tool, you can list the current blocked server domain patterns
345     or you can add / remove a blocked server domain pattern from the list.
346     Using the export and import options you can share your server blocklist
347     with other node admins by CSV files.
348
349     Patterns are case-insensitive shell wildcard comprising the following special characters:
350     - * : Any number of characters
351     - ? : Any single character
352     - [<char1><char2>...] : char1 or char2 or...
353
354 Options
355     -h|--help|-? Show help information
356     -v           Show more debug information.
357
358 HELP;
359
360                 $this->assertEquals($help, $txt);
361         }
362 }