3 * @copyright Copyright (C) 2010-2023, 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\Moderation\DomainPatternBlocklist;
26 use Friendica\Test\FixtureTestTrait;
29 class ServerBlockConsoleTest extends ConsoleTest
33 protected $defaultBlockList = [
35 'domain' => 'social.nobodyhasthe.biz',
36 'reason' => 'Illegal content',
39 'domain' => 'pod.ordoevangelistarum.com',
40 'reason' => 'Illegal content',
44 * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
46 private $blocklistMock;
48 protected function setUp() : void
52 $this->setUpFixtures();
54 $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
57 protected function tearDown(): void
59 $this->tearDownFixtures();
65 * Test to list the default blocked servers
67 public function testBlockedServersList()
70 ->shouldReceive('get')
71 ->andReturn($this->defaultBlockList)
74 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
75 $txt = $this->dumpExecute($console);
80 +----------------------------+-----------------+$php_eol| Pattern | Reason |$php_eol+----------------------------+-----------------+$php_eol| social.nobodyhasthe.biz | Illegal content |$php_eol| pod.ordoevangelistarum.com | Illegal content |$php_eol+----------------------------+-----------------+$php_eol
84 self::assertEquals($output, $txt);
88 * Test blockedservers add command
90 public function testAddBlockedServer()
93 ->shouldReceive('addPattern')
94 ->with('testme.now', 'I like it!')
98 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
99 $console->setArgument(0, 'add');
100 $console->setArgument(1, 'testme.now');
101 $console->setArgument(2, 'I like it!');
102 $txt = $this->dumpExecute($console);
104 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
108 * Test blockedservers add command on existed domain
110 public function testUpdateBlockedServer()
113 ->shouldReceive('addPattern')
114 ->with('pod.ordoevangelistarum.com', 'Other reason')
118 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
119 $console->setArgument(0, 'add');
120 $console->setArgument(1, 'pod.ordoevangelistarum.com');
121 $console->setArgument(2, 'Other reason');
122 $txt = $this->dumpExecute($console);
124 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
128 * Test blockedservers remove command
130 public function testRemoveBlockedServer()
133 ->shouldReceive('removePattern')
134 ->with('pod.ordoevangelistarum.com')
138 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
139 $console->setArgument(0, 'remove');
140 $console->setArgument(1, 'pod.ordoevangelistarum.com');
141 $txt = $this->dumpExecute($console);
143 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
147 * Test blockedservers with a wrong command
149 public function testBlockedServersWrongCommand()
151 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
152 $console->setArgument(0, 'wrongcommand');
153 $txt = $this->dumpExecute($console);
155 self::assertStringStartsWith('[Warning] Unknown command', $txt);
159 * Test blockedservers remove with not existing domain
161 public function testRemoveBlockedServerNotExist()
164 ->shouldReceive('removePattern')
165 ->with('not.existing')
169 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
170 $console->setArgument(0, 'remove');
171 $console->setArgument(1, 'not.existing');
172 $txt = $this->dumpExecute($console);
174 self::assertEquals('The domain pattern \'not.existing\' wasn\'t blocked.' . "\n", $txt);
178 * Test blockedservers add command without argument
180 public function testAddBlockedServerMissingArgument()
182 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
183 $console->setArgument(0, 'add');
184 $txt = $this->dumpExecute($console);
186 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
188 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
189 $console->setArgument(0, 'add');
190 $console->setArgument(1, 'testme.now');
191 $txt = $this->dumpExecute($console);
193 self::assertStringStartsWith('[Warning] Add needs a domain pattern and a reason.', $txt);
197 * Test blockedservers add command without save
199 public function testAddBlockedServerNoSave()
202 ->shouldReceive('addPattern')
203 ->with('testme.now', 'I like it!')
207 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
208 $console->setArgument(0, 'add');
209 $console->setArgument(1, 'testme.now');
210 $console->setArgument(2, 'I like it!');
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);