3 namespace Friendica\Test\src\Console;
5 use Friendica\Console\ServerBlock;
8 * @runTestsInSeparateProcesses
9 * @preserveGlobalState disabled
11 class ServerBlockConsoleTest extends ConsoleTest
13 protected $defaultBlockList = [
15 'domain' => 'social.nobodyhasthe.biz',
16 'reason' => 'Illegal content',
19 'domain' => 'pod.ordoevangelistarum.com',
20 'reason' => 'Illegal content',
24 protected function setUp()
28 $this->mockApp($this->root);
32 * Test to list the default blocked servers
34 public function testBlockedServersList()
37 ->shouldReceive('get')
38 ->with('system', 'blocklist')
39 ->andReturn($this->defaultBlockList)
42 $console = new ServerBlock($this->consoleArgv);
43 $txt = $this->dumpExecute($console);
46 +----------------------------+-----------------+
48 +----------------------------+-----------------+
49 | social.nobodyhasthe.biz | Illegal content |
50 | pod.ordoevangelistarum.com | Illegal content |
51 +----------------------------+-----------------+
56 $this->assertEquals($output, $txt);
60 * Test blockedservers add command
62 public function testAddBlockedServer()
65 ->shouldReceive('get')
66 ->with('system', 'blocklist')
67 ->andReturn($this->defaultBlockList)
70 $newBlockList = $this->defaultBlockList;
72 'domain' => 'testme.now',
73 'reason' => 'I like it!',
77 ->shouldReceive('set')
78 ->with('system', 'blocklist', $newBlockList)
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);
88 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
92 * Test blockedservers add command with the default reason
94 public function testAddBlockedServerWithDefaultReason()
97 ->shouldReceive('get')
98 ->with('system', 'blocklist')
99 ->andReturn($this->defaultBlockList)
102 $newBlockList = $this->defaultBlockList;
104 'domain' => 'testme.now',
105 'reason' => ServerBlock::DEFAULT_REASON,
109 ->shouldReceive('set')
110 ->with('system', 'blocklist', $newBlockList)
114 $console = new ServerBlock($this->consoleArgv);
115 $console->setArgument(0, 'add');
116 $console->setArgument(1, 'testme.now');
117 $txt = $this->dumpExecute($console);
119 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
123 * Test blockedservers add command on existed domain
125 public function testUpdateBlockedServer()
128 ->shouldReceive('get')
129 ->with('system', 'blocklist')
130 ->andReturn($this->defaultBlockList)
135 'domain' => 'social.nobodyhasthe.biz',
136 'reason' => 'Illegal content',
139 'domain' => 'pod.ordoevangelistarum.com',
140 'reason' => 'Other reason',
145 ->shouldReceive('set')
146 ->with('system', 'blocklist', $newBlockList)
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);
156 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
160 * Test blockedservers remove command
162 public function testRemoveBlockedServer()
165 ->shouldReceive('get')
166 ->with('system', 'blocklist')
167 ->andReturn($this->defaultBlockList)
172 'domain' => 'social.nobodyhasthe.biz',
173 'reason' => 'Illegal content',
178 ->shouldReceive('set')
179 ->with('system', 'blocklist', $newBlockList)
183 $console = new ServerBlock($this->consoleArgv);
184 $console->setArgument(0, 'remove');
185 $console->setArgument(1, 'pod.ordoevangelistarum.com');
186 $txt = $this->dumpExecute($console);
188 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
192 * Test blockedservers with a wrong command
194 public function testBlockedServersWrongCommand()
196 $console = new ServerBlock($this->consoleArgv);
197 $console->setArgument(0, 'wrongcommand');
198 $txt = $this->dumpExecute($console);
200 $this->assertStringStartsWith('[Warning] Unknown command', $txt);
204 * Test blockedservers remove with not existing domain
206 public function testRemoveBlockedServerNotExist()
209 ->shouldReceive('get')
210 ->with('system', 'blocklist')
211 ->andReturn($this->defaultBlockList)
214 $console = new ServerBlock($this->consoleArgv);
215 $console->setArgument(0, 'remove');
216 $console->setArgument(1, 'not.exiting');
217 $txt = $this->dumpExecute($console);
219 $this->assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
223 * Test blockedservers add command without argument
225 public function testAddBlockedServerMissingArgument()
227 $console = new ServerBlock($this->consoleArgv);
228 $console->setArgument(0, 'add');
229 $txt = $this->dumpExecute($console);
231 $this->assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
235 * Test blockedservers add command without save
237 public function testAddBlockedServerNoSave()
240 ->shouldReceive('get')
241 ->with('system', 'blocklist')
242 ->andReturn($this->defaultBlockList)
245 $newBlockList = $this->defaultBlockList;
247 'domain' => 'testme.now',
248 'reason' => ServerBlock::DEFAULT_REASON,
252 ->shouldReceive('set')
253 ->with('system', 'blocklist', $newBlockList)
257 $console = new ServerBlock($this->consoleArgv);
258 $console->setArgument(0, 'add');
259 $console->setArgument(1, 'testme.now');
260 $txt = $this->dumpExecute($console);
262 $this->assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
266 * Test blockedservers remove command without save
268 public function testRemoveBlockedServerNoSave()
271 ->shouldReceive('get')
272 ->with('system', 'blocklist')
273 ->andReturn($this->defaultBlockList)
278 'domain' => 'social.nobodyhasthe.biz',
279 'reason' => 'Illegal content',
284 ->shouldReceive('set')
285 ->with('system', 'blocklist', $newBlockList)
289 $console = new ServerBlock($this->consoleArgv);
290 $console->setArgument(0, 'remove');
291 $console->setArgument(1, 'pod.ordoevangelistarum.com');
292 $txt = $this->dumpExecute($console);
294 $this->assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
298 * Test blockedservers remove command without argument
300 public function testRemoveBlockedServerMissingArgument()
302 $console = new ServerBlock($this->consoleArgv);
303 $console->setArgument(0, 'remove');
304 $txt = $this->dumpExecute($console);
306 $this->assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
310 * Test the blockedservers help
312 public function testBlockedServersHelp()
314 $console = new ServerBlock($this->consoleArgv);
315 $console->setOption('help', true);
316 $txt = $this->dumpExecute($console);
319 console serverblock - Manage blocked servers
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]
326 With this tool, you can list the current blocked servers
327 or you can add / remove a blocked server from the list
330 -h|--help|-? Show help information
331 -v Show more debug information.
335 $this->assertEquals($help, $txt);