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