3 namespace Friendica\Test\src\Console;
5 use Friendica\Console\ServerBlock;
6 use Friendica\Core\Config\IConfiguration;
8 class ServerBlockConsoleTest extends ConsoleTest
10 protected $defaultBlockList = [
12 'domain' => 'social.nobodyhasthe.biz',
13 'reason' => 'Illegal content',
16 'domain' => 'pod.ordoevangelistarum.com',
17 'reason' => 'Illegal content',
21 protected function setUp()
25 $this->configMock = \Mockery::mock(IConfiguration::class);
29 * Test to list the default blocked servers
31 public function testBlockedServersList()
34 ->shouldReceive('get')
35 ->with('system', 'blocklist', [])
36 ->andReturn($this->defaultBlockList)
39 $console = new ServerBlock($this->configMock, $this->consoleArgv);
40 $txt = $this->dumpExecute($console);
43 +----------------------------+-----------------+
45 +----------------------------+-----------------+
46 | social.nobodyhasthe.biz | Illegal content |
47 | pod.ordoevangelistarum.com | Illegal content |
48 +----------------------------+-----------------+
53 $this->assertEquals($output, $txt);
57 * Test blockedservers add command
59 public function testAddBlockedServer()
62 ->shouldReceive('get')
63 ->with('system', 'blocklist', [])
64 ->andReturn($this->defaultBlockList)
67 $newBlockList = $this->defaultBlockList;
69 'domain' => 'testme.now',
70 'reason' => 'I like it!',
74 ->shouldReceive('set')
75 ->with('system', 'blocklist', $newBlockList)
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);
85 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
89 * Test blockedservers add command with the default reason
91 public function testAddBlockedServerWithDefaultReason()
94 ->shouldReceive('get')
95 ->with('system', 'blocklist', [])
96 ->andReturn($this->defaultBlockList)
99 $newBlockList = $this->defaultBlockList;
101 'domain' => 'testme.now',
102 'reason' => ServerBlock::DEFAULT_REASON,
106 ->shouldReceive('set')
107 ->with('system', 'blocklist', $newBlockList)
111 $console = new ServerBlock($this->configMock, $this->consoleArgv);
112 $console->setArgument(0, 'add');
113 $console->setArgument(1, 'testme.now');
114 $txt = $this->dumpExecute($console);
116 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
120 * Test blockedservers add command on existed domain
122 public function testUpdateBlockedServer()
125 ->shouldReceive('get')
126 ->with('system', 'blocklist', [])
127 ->andReturn($this->defaultBlockList)
132 'domain' => 'social.nobodyhasthe.biz',
133 'reason' => 'Illegal content',
136 'domain' => 'pod.ordoevangelistarum.com',
137 'reason' => 'Other reason',
142 ->shouldReceive('set')
143 ->with('system', 'blocklist', $newBlockList)
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);
153 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
157 * Test blockedservers remove command
159 public function testRemoveBlockedServer()
162 ->shouldReceive('get')
163 ->with('system', 'blocklist', [])
164 ->andReturn($this->defaultBlockList)
169 'domain' => 'social.nobodyhasthe.biz',
170 'reason' => 'Illegal content',
175 ->shouldReceive('set')
176 ->with('system', 'blocklist', $newBlockList)
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);
185 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
189 * Test blockedservers with a wrong command
191 public function testBlockedServersWrongCommand()
193 $console = new ServerBlock($this->configMock, $this->consoleArgv);
194 $console->setArgument(0, 'wrongcommand');
195 $txt = $this->dumpExecute($console);
197 $this->assertStringStartsWith('[Warning] Unknown command', $txt);
201 * Test blockedservers remove with not existing domain
203 public function testRemoveBlockedServerNotExist()
206 ->shouldReceive('get')
207 ->with('system', 'blocklist', [])
208 ->andReturn($this->defaultBlockList)
211 $console = new ServerBlock($this->configMock, $this->consoleArgv);
212 $console->setArgument(0, 'remove');
213 $console->setArgument(1, 'not.exiting');
214 $txt = $this->dumpExecute($console);
216 $this->assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
220 * Test blockedservers add command without argument
222 public function testAddBlockedServerMissingArgument()
224 $console = new ServerBlock($this->configMock, $this->consoleArgv);
225 $console->setArgument(0, 'add');
226 $txt = $this->dumpExecute($console);
228 $this->assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
232 * Test blockedservers add command without save
234 public function testAddBlockedServerNoSave()
237 ->shouldReceive('get')
238 ->with('system', 'blocklist', [])
239 ->andReturn($this->defaultBlockList)
242 $newBlockList = $this->defaultBlockList;
244 'domain' => 'testme.now',
245 'reason' => ServerBlock::DEFAULT_REASON,
249 ->shouldReceive('set')
250 ->with('system', 'blocklist', $newBlockList)
254 $console = new ServerBlock($this->configMock, $this->consoleArgv);
255 $console->setArgument(0, 'add');
256 $console->setArgument(1, 'testme.now');
257 $txt = $this->dumpExecute($console);
259 $this->assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
263 * Test blockedservers remove command without save
265 public function testRemoveBlockedServerNoSave()
268 ->shouldReceive('get')
269 ->with('system', 'blocklist', [])
270 ->andReturn($this->defaultBlockList)
275 'domain' => 'social.nobodyhasthe.biz',
276 'reason' => 'Illegal content',
281 ->shouldReceive('set')
282 ->with('system', 'blocklist', $newBlockList)
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);
291 $this->assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
295 * Test blockedservers remove command without argument
297 public function testRemoveBlockedServerMissingArgument()
299 $console = new ServerBlock($this->configMock, $this->consoleArgv);
300 $console->setArgument(0, 'remove');
301 $txt = $this->dumpExecute($console);
303 $this->assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
307 * Test the blockedservers help
309 public function testBlockedServersHelp()
311 $console = new ServerBlock($this->configMock, $this->consoleArgv);
312 $console->setOption('help', true);
313 $txt = $this->dumpExecute($console);
316 console serverblock - Manage blocked server domain patterns
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]
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.
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...
332 -h|--help|-? Show help information
333 -v Show more debug information.
337 $this->assertEquals($help, $txt);