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 with the default reason
99 public function testAddBlockedServerWithDefaultReason()
102 ->shouldReceive('addPattern')
103 ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
107 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
108 $console->setArgument(0, 'add');
109 $console->setArgument(1, 'testme.now');
110 $txt = $this->dumpExecute($console);
112 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'' . DomainPatternBlocklist::DEFAULT_REASON . '\')' . "\n", $txt);
116 * Test blockedservers add command on existed domain
118 public function testUpdateBlockedServer()
121 ->shouldReceive('addPattern')
122 ->with('pod.ordoevangelistarum.com', 'Other reason')
126 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
127 $console->setArgument(0, 'add');
128 $console->setArgument(1, 'pod.ordoevangelistarum.com');
129 $console->setArgument(2, 'Other reason');
130 $txt = $this->dumpExecute($console);
132 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
136 * Test blockedservers remove command
138 public function testRemoveBlockedServer()
141 ->shouldReceive('removePattern')
142 ->with('pod.ordoevangelistarum.com')
146 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
147 $console->setArgument(0, 'remove');
148 $console->setArgument(1, 'pod.ordoevangelistarum.com');
149 $txt = $this->dumpExecute($console);
151 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
155 * Test blockedservers with a wrong command
157 public function testBlockedServersWrongCommand()
159 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
160 $console->setArgument(0, 'wrongcommand');
161 $txt = $this->dumpExecute($console);
163 self::assertStringStartsWith('[Warning] Unknown command', $txt);
167 * Test blockedservers remove with not existing domain
169 public function testRemoveBlockedServerNotExist()
172 ->shouldReceive('removePattern')
173 ->with('not.exiting')
177 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
178 $console->setArgument(0, 'remove');
179 $console->setArgument(1, 'not.exiting');
180 $txt = $this->dumpExecute($console);
182 self::assertEquals('The domain pattern \'not.exiting\' wasn\'t blocked.' . "\n", $txt);
186 * Test blockedservers add command without argument
188 public function testAddBlockedServerMissingArgument()
190 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
191 $console->setArgument(0, 'add');
192 $txt = $this->dumpExecute($console);
194 self::assertStringStartsWith('[Warning] Add needs a domain pattern and optionally a reason.', $txt);
198 * Test blockedservers add command without save
200 public function testAddBlockedServerNoSave()
203 ->shouldReceive('addPattern')
204 ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
208 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
209 $console->setArgument(0, 'add');
210 $console->setArgument(1, 'testme.now');
211 $txt = $this->dumpExecute($console);
213 self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
217 * Test blockedservers remove command without save
219 public function testRemoveBlockedServerNoSave()
222 ->shouldReceive('removePattern')
223 ->with('pod.ordoevangelistarum.com')
227 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
228 $console->setArgument(0, 'remove');
229 $console->setArgument(1, 'pod.ordoevangelistarum.com');
230 $txt = $this->dumpExecute($console);
232 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
236 * Test blockedservers remove command without argument
238 public function testRemoveBlockedServerMissingArgument()
240 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
241 $console->setArgument(0, 'remove');
242 $txt = $this->dumpExecute($console);
244 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
248 * Test the blockedservers help
250 public function testBlockedServersHelp()
252 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
253 $console->setOption('help', true);
254 $txt = $this->dumpExecute($console);
257 console serverblock - Manage blocked server domain patterns
259 bin/console serverblock [-h|--help|-?] [-v]
260 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
261 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
262 bin/console serverblock export <filename>
263 bin/console serverblock import <filename>
266 With this tool, you can list the current blocked server domain patterns
267 or you can add / remove a blocked server domain pattern from the list.
268 Using the export and import options you can share your server blocklist
269 with other node admins by CSV files.
271 Patterns are case-insensitive shell wildcard comprising the following special characters:
272 - * : Any number of characters
273 - ? : Any single character
274 - [<char1><char2>...] : char1 or char2 or...
277 -h|--help|-? Show help information
278 -v Show more debug information.
282 self::assertEquals($help, $txt);