3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Console;
24 use Friendica\Console\ServerBlock;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Moderation\DomainPatternBlocklist;
29 class ServerBlockConsoleTest extends ConsoleTest
31 protected $defaultBlockList = [
33 'domain' => 'social.nobodyhasthe.biz',
34 'reason' => 'Illegal content',
37 'domain' => 'pod.ordoevangelistarum.com',
38 'reason' => 'Illegal content',
42 * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
44 private $blocklistMock;
46 protected function setUp() : void
50 $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
54 * Test to list the default blocked servers
56 public function testBlockedServersList()
59 ->shouldReceive('get')
60 ->andReturn($this->defaultBlockList)
63 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
64 $txt = $this->dumpExecute($console);
69 +----------------------------+-----------------+$php_eol| Pattern | Reason |$php_eol+----------------------------+-----------------+$php_eol| social.nobodyhasthe.biz | Illegal content |$php_eol| pod.ordoevangelistarum.com | Illegal content |$php_eol+----------------------------+-----------------+$php_eol
73 self::assertEquals($output, $txt);
77 * Test blockedservers add command
79 public function testAddBlockedServer()
82 ->shouldReceive('addPattern')
83 ->with('testme.now', 'I like it!')
87 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
88 $console->setArgument(0, 'add');
89 $console->setArgument(1, 'testme.now');
90 $console->setArgument(2, 'I like it!');
91 $txt = $this->dumpExecute($console);
93 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
97 * Test blockedservers add command on existed domain
99 public function testUpdateBlockedServer()
102 ->shouldReceive('addPattern')
103 ->with('pod.ordoevangelistarum.com', 'Other reason')
107 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
108 $console->setArgument(0, 'add');
109 $console->setArgument(1, 'pod.ordoevangelistarum.com');
110 $console->setArgument(2, 'Other reason');
111 $txt = $this->dumpExecute($console);
113 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
117 * Test blockedservers remove command
119 public function testRemoveBlockedServer()
122 ->shouldReceive('removePattern')
123 ->with('pod.ordoevangelistarum.com')
127 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
128 $console->setArgument(0, 'remove');
129 $console->setArgument(1, 'pod.ordoevangelistarum.com');
130 $txt = $this->dumpExecute($console);
132 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
136 * Test blockedservers with a wrong command
138 public function testBlockedServersWrongCommand()
140 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
141 $console->setArgument(0, 'wrongcommand');
142 $txt = $this->dumpExecute($console);
144 self::assertStringStartsWith('[Warning] Unknown command', $txt);
148 * Test blockedservers remove with not existing domain
150 public function testRemoveBlockedServerNotExist()
153 ->shouldReceive('removePattern')
154 ->with('not.existing')
158 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
159 $console->setArgument(0, 'remove');
160 $console->setArgument(1, 'not.existing');
161 $txt = $this->dumpExecute($console);
163 self::assertEquals('The domain pattern \'not.existing\' wasn\'t blocked.' . "\n", $txt);
167 * Test blockedservers add command without argument
169 public function testAddBlockedServerMissingArgument()
171 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
172 $console->setArgument(0, 'add');
173 $txt = $this->dumpExecute($console);
175 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
177 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
178 $console->setArgument(0, 'add');
179 $console->setArgument(1, 'testme.now');
180 $txt = $this->dumpExecute($console);
182 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
186 * Test blockedservers add command without save
188 public function testAddBlockedServerNoSave()
191 ->shouldReceive('addPattern')
192 ->with('testme.now', 'I like it!')
196 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
197 $console->setArgument(0, 'add');
198 $console->setArgument(1, 'testme.now');
199 $console->setArgument(2, 'I like it!');
200 $txt = $this->dumpExecute($console);
202 self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
206 * Test blockedservers remove command without save
208 public function testRemoveBlockedServerNoSave()
211 ->shouldReceive('removePattern')
212 ->with('pod.ordoevangelistarum.com')
216 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
217 $console->setArgument(0, 'remove');
218 $console->setArgument(1, 'pod.ordoevangelistarum.com');
219 $txt = $this->dumpExecute($console);
221 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
225 * Test blockedservers remove command without argument
227 public function testRemoveBlockedServerMissingArgument()
229 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
230 $console->setArgument(0, 'remove');
231 $txt = $this->dumpExecute($console);
233 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
237 * Test the blockedservers help
239 public function testBlockedServersHelp()
241 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
242 $console->setOption('help', true);
243 $txt = $this->dumpExecute($console);
246 console serverblock - Manage blocked server domain patterns
248 bin/console serverblock [-h|--help|-?] [-v]
249 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
250 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
251 bin/console serverblock export <filename>
252 bin/console serverblock import <filename>
255 With this tool, you can list the current blocked server domain patterns
256 or you can add / remove a blocked server domain pattern from the list.
257 Using the export and import options you can share your server blocklist
258 with other node admins by CSV files.
260 Patterns are case-insensitive shell wildcard comprising the following special characters:
261 - * : Any number of characters
262 - ? : Any single character
263 - [<char1><char2>...] : char1 or char2 or...
266 -h|--help|-? Show help information
267 -v Show more debug information.
271 self::assertEquals($help, $txt);