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