]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ServerBlockConsoleTest.php
cdd7efef0e1386cd9de88e9980aa71feb8d9336b
[friendica.git] / tests / src / Console / ServerBlockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Console;
23
24 use Friendica\Console\ServerBlock;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Moderation\DomainPatternBlocklist;
27 use Mockery;
28
29 class ServerBlockConsoleTest extends ConsoleTest
30 {
31         protected $defaultBlockList = [
32                 [
33                         'domain' => 'social.nobodyhasthe.biz',
34                         'reason' => 'Illegal content',
35                 ],
36                 [
37                         'domain' => 'pod.ordoevangelistarum.com',
38                         'reason' => 'Illegal content',
39                 ]
40         ];
41         /**
42          * @var DomainPatternBlocklist|Mockery\LegacyMockInterface|Mockery\MockInterface
43          */
44         private $blocklistMock;
45
46         protected function setUp() : void
47         {
48                 parent::setUp();
49
50                 $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
51         }
52
53         /**
54          * Test to list the default blocked servers
55          */
56         public function testBlockedServersList()
57         {
58                 $this->blocklistMock
59                         ->shouldReceive('get')
60                         ->andReturn($this->defaultBlockList)
61                         ->once();
62
63                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
64                 $txt = $this->dumpExecute($console);
65
66                 $php_eol = PHP_EOL;
67
68                 $output = <<<CONS
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
70
71 CONS;
72
73                 self::assertEquals($output, $txt);
74         }
75
76         /**
77          * Test blockedservers add command
78          */
79         public function testAddBlockedServer()
80         {
81                 $this->blocklistMock
82                         ->shouldReceive('addPattern')
83                         ->with('testme.now', 'I like it!')
84                         ->andReturn(1)
85                         ->once();
86
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);
92
93                 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'I like it!\')' . "\n", $txt);
94         }
95
96         /**
97          * Test blockedservers add command with the default reason
98          */
99         public function testAddBlockedServerWithDefaultReason()
100         {
101                 $this->blocklistMock
102                         ->shouldReceive('addPattern')
103                         ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
104                         ->andReturn(1)
105                         ->once();
106
107                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
108                 $console->setArgument(0, 'add');
109                 $console->setArgument(1, 'testme.now');
110                 $txt = $this->dumpExecute($console);
111
112                 self::assertEquals('The domain pattern \'testme.now\' is now blocked. (Reason: \'' . DomainPatternBlocklist::DEFAULT_REASON . '\')' . "\n", $txt);
113         }
114
115         /**
116          * Test blockedservers add command on existed domain
117          */
118         public function testUpdateBlockedServer()
119         {
120                 $this->blocklistMock
121                         ->shouldReceive('addPattern')
122                         ->with('pod.ordoevangelistarum.com', 'Other reason')
123                         ->andReturn(2)
124                         ->once();
125
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);
131
132                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . "\n", $txt);
133         }
134
135         /**
136          * Test blockedservers remove command
137          */
138         public function testRemoveBlockedServer()
139         {
140                 $this->blocklistMock
141                         ->shouldReceive('removePattern')
142                         ->with('pod.ordoevangelistarum.com')
143                         ->andReturn(2)
144                         ->once();
145
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);
150
151                 self::assertEquals('The domain pattern \'pod.ordoevangelistarum.com\' isn\'t blocked anymore' . "\n", $txt);
152         }
153
154         /**
155          * Test blockedservers with a wrong command
156          */
157         public function testBlockedServersWrongCommand()
158         {
159                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
160                 $console->setArgument(0, 'wrongcommand');
161                 $txt = $this->dumpExecute($console);
162
163                 self::assertStringStartsWith('[Warning] Unknown command', $txt);
164         }
165
166         /**
167          * Test blockedservers remove with not existing domain
168          */
169         public function testRemoveBlockedServerNotExist()
170         {
171                 $this->blocklistMock
172                         ->shouldReceive('removePattern')
173                         ->with('not.exiting')
174                         ->andReturn(1)
175                         ->once();
176
177                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
178                 $console->setArgument(0, 'remove');
179                 $console->setArgument(1, 'not.exiting');
180                 $txt = $this->dumpExecute($console);
181
182                 self::assertEquals('The domain pattern \'not.exiting\' wasn\'t blocked.' . "\n", $txt);
183         }
184
185         /**
186          * Test blockedservers add command without argument
187          */
188         public function testAddBlockedServerMissingArgument()
189         {
190                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
191                 $console->setArgument(0, 'add');
192                 $txt = $this->dumpExecute($console);
193
194                 self::assertStringStartsWith('[Warning] Add needs a domain pattern and optionally a reason.', $txt);
195         }
196
197         /**
198          * Test blockedservers add command without save
199          */
200         public function testAddBlockedServerNoSave()
201         {
202                 $this->blocklistMock
203                         ->shouldReceive('addPattern')
204                         ->with('testme.now', DomainPatternBlocklist::DEFAULT_REASON)
205                         ->andReturn(0)
206                         ->once();
207
208                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
209                 $console->setArgument(0, 'add');
210                 $console->setArgument(1, 'testme.now');
211                 $txt = $this->dumpExecute($console);
212
213                 self::assertEquals('Couldn\'t save \'testme.now\' as blocked domain pattern' . "\n", $txt);
214         }
215
216         /**
217          * Test blockedservers remove command without save
218          */
219         public function testRemoveBlockedServerNoSave()
220         {
221                 $this->blocklistMock
222                         ->shouldReceive('removePattern')
223                         ->with('pod.ordoevangelistarum.com')
224                         ->andReturn(0)
225                         ->once();
226
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);
231
232                 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked domain patterns' . "\n", $txt);
233         }
234
235         /**
236          * Test blockedservers remove command without argument
237          */
238         public function testRemoveBlockedServerMissingArgument()
239         {
240                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
241                 $console->setArgument(0, 'remove');
242                 $txt = $this->dumpExecute($console);
243
244                 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
245         }
246
247         /**
248          * Test the blockedservers help
249          */
250         public function testBlockedServersHelp()
251         {
252                 $console = new ServerBlock($this->blocklistMock, $this->consoleArgv);
253                 $console->setOption('help', true);
254                 $txt = $this->dumpExecute($console);
255
256                 $help = <<<HELP
257 console serverblock - Manage blocked server domain patterns
258 Usage
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>
264
265 Description
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.
270
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...
275
276 Options
277     -h|--help|-? Show help information
278     -v           Show more debug information.
279
280 HELP;
281
282                 self::assertEquals($help, $txt);
283         }
284 }